+ public void testReceiveUntilIosXr() throws TimedOutException, IOException, JSchException {
+ Session mockSession = Mockito.mock(Session.class);
+ Whitebox.setInternalState(wrapper, "session", mockSession);
+ BufferedReader mockReader = Mockito.mock(BufferedReader.class);
+ Mockito.doReturn(3).when(mockReader).read(Mockito.anyObject(), Mockito.anyInt(),
+ Mockito.anyInt());
+ Whitebox.setInternalState(wrapper, "reader", mockReader);
+ Mockito.doReturn(false).when(wrapper).jcraftReadSwConfigFileFromDisk();
+ Mockito.doReturn("\nXML>").when(wrapper).getLastFewLinesOfFile(Mockito.anyObject(),
+ Mockito.anyInt());
+
+ assertNull(wrapper.receiveUntil("]]>]]>", 3000, "IOS_XR_uploadedSwConfigCmd\nOTHER\nXML>"));
+ }
+
+ @Test
+ public void testReceiveStringDelimiters() {
+ assertEquals(false, wrapper.checkIfReceivedStringMatchesDelimeter("#$", "", ""));
+ }
+
+ @Test
+ public void testReceiveStringDelimitersShowConfig() {
+ assertEquals(true, wrapper.checkIfReceivedStringMatchesDelimeter("]]>]]>", "]]>]]>\n #",
+ "show config"));
+ }
+
+ @Test
+ public void testReceiveStringDelimitersTwoArg() throws IOException {
+ SshJcraftWrapper localWrapper = Mockito.spy(new SshJcraftWrapper());
+ Mockito.doReturn(true).when(localWrapper).jcraftReadSwConfigFileFromDisk();
+ Mockito.doThrow(new IOException()).when(localWrapper)
+ .getLastFewLinesOfFile(Mockito.anyObject(), Mockito.anyInt());
+ Whitebox.setInternalState(localWrapper, "routerFileName", "DUMMY_FILE_NAME");
+ assertEquals(false, localWrapper.checkIfReceivedStringMatchesDelimeter(3, "]]>]]>\n #"));
+ }
+
+ @Test
+ public void testCloseConnection() {
+ Session mockSession = Mockito.mock(Session.class);
+ Whitebox.setInternalState(wrapper, "session", mockSession);
+ wrapper.closeConnection();
+ Mockito.verify(mockSession, Mockito.times(1)).disconnect();
+ }
+
+ @Test
+ public void testSend() throws IOException {
+ ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
+ OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
+ Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
+ DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
+ PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
+ Whitebox.setInternalState(wrapper, "channel", mockChannel);
+ wrapper.send("TEST COMMAND\n");
+ Mockito.verify(wrapper, Mockito.times(2)).appendToFile(Mockito.anyString(),
+ Mockito.anyString());
+ }
+
+ @Test
+ public void testSendExceptionFlow() throws IOException {
+ ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
+ OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
+ Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
+ DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
+ PowerMockito.doThrow(new IOException()).when(mockDos).flush();
+ PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
+ Whitebox.setInternalState(wrapper, "channel", mockChannel);
+ expectedEx.expect(IOException.class);
+ expectedEx.expectMessage("java.io.IOException");
+ wrapper.send("TEST COMMAND");
+ }
+
+ @Test
+ public void testSendChar() throws IOException {
+ ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
+ OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
+ Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
+ DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
+ PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
+ Whitebox.setInternalState(wrapper, "channel", mockChannel);
+ wrapper.sendChar(74);
+ Mockito.verify(mockDos).flush();
+ }
+
+ @Test
+ public void testSendCharExceptionFlow() throws IOException {
+ ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
+ OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
+ Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
+ DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
+ PowerMockito.doThrow(new IOException()).when(mockDos).flush();
+ PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
+ Whitebox.setInternalState(wrapper, "channel", mockChannel);
+ expectedEx.expect(IOException.class);
+ expectedEx.expectMessage("java.io.IOException");
+ wrapper.sendChar(65);
+ }
+
+ @Test
+ public void testSendByteArrayExceptionFlow() throws IOException {
+ ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
+ OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
+ Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
+ DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
+ PowerMockito.doThrow(new IOException()).when(mockDos).flush();
+ PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
+ Whitebox.setInternalState(wrapper, "channel", mockChannel);
+ expectedEx.expect(IOException.class);
+ expectedEx.expectMessage("java.io.IOException");
+ byte[] byteArray = new byte[] {65, 74};
+ wrapper.send(byteArray, 0, 2);
+ }
+
+ @Test
+ public void testGetLastFewLinesOfFile() throws FileNotFoundException, IOException {
+ File file = new File("src/test/resources/TEST_FILE.txt");
+ BufferedWriter writer = new BufferedWriter(new FileWriter(file.getPath()));
+ writer.write("line1\nline2");
+ writer.flush();
+ writer.close();
+ assertEquals("\nline2", wrapper.getLastFewLinesOfFile(file, 2));
+ }
+
+ @Test
+ public void testReceiveUntilBufferFlush() throws TimedOutException, IOException {
+ Session mockSession = Mockito.mock(Session.class);
+ Whitebox.setInternalState(wrapper, "session", mockSession);
+ BufferedReader mockReader = Mockito.mock(BufferedReader.class);
+ Mockito.doReturn(12).when(mockReader).read(Mockito.anyObject(), Mockito.anyInt(),
+ Mockito.anyInt());
+ Whitebox.setInternalState(wrapper, "reader", mockReader);
+
+ wrapper.receiveUntilBufferFlush(12, 100, "TEST_MESSAGE");
+ Mockito.verify(wrapper, Mockito.times(2)).logMemoryUsage();
+ }
+
+ @Test
+ public void testReceiveUntilBufferFlushTimeout()
+ throws TimedOutException, IOException, JSchException {
+ Session mockSession = Mockito.mock(Session.class);
+ // The sleep is required to make sure that the system clock has incremented by
+ // (at least) 1 millisecond between passing in our timeout value and setting a deadline
+ // and checking to see if the deadline has been missed
+ Mockito.doAnswer(new Answer<String>() {
+ @Override
+ public String answer(InvocationOnMock invocation) throws InterruptedException {
+ Thread.sleep(1);
+ return null;
+ }
+ }).when(mockSession).setTimeout(0);
+ Whitebox.setInternalState(wrapper, "session", mockSession);
+ expectedEx.expect(IOException.class);
+ expectedEx.expectMessage("Timeout: time in routine has exceed our deadline");
+ wrapper.receiveUntilBufferFlush(10, 0, "TEST_MESSAGE");
+ }
+
+ @Test
+ public void testReceiveUntilBufferFlushJSchException()
+ throws TimedOutException, IOException, JSchException {
+ Session mockSession = Mockito.mock(Session.class);
+ Mockito.doThrow(new JSchException()).when(mockSession).setTimeout(0);
+ Whitebox.setInternalState(wrapper, "session", mockSession);
+ expectedEx.expect(TimedOutException.class);
+ expectedEx.expectMessage("com.jcraft.jsch.JSchException");
+ wrapper.receiveUntilBufferFlush(10, 0, "TEST_MESSAGE");
+ }
+
+ @Test
+ public void testSftpPutSourceToDest() throws JSchException, IOException {
+ Whitebox.setInternalState(wrapper, "hostName", "testHost");
+ Whitebox.setInternalState(wrapper, "userName", "testUser");
+ Whitebox.setInternalState(wrapper, "passWord", "testPwd");
+ ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
+ JSch mockJsch = Mockito.mock(JSch.class);
+ Session mockSession = Mockito.mock(Session.class);
+ Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
+ Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
+ Mockito.doNothing().when(mockSession).connect();
+ Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
+ Whitebox.setInternalState(wrapper, "jsch", mockJsch);
+ wrapper.sftpPut("DUMMY_SRC_PATH", "DUMMY_DEST_DIRECTORY");
+ Mockito.verify(mockSession).disconnect();
+ }
+
+ @Test
+ public void testSftpPutSourceToDestExceptionFlow() throws JSchException, IOException {
+ JSch mockJsch = Mockito.mock(JSch.class);
+ Mockito.doThrow(new JSchException()).when(mockJsch).getSession(null, null, 22);
+ Whitebox.setInternalState(wrapper, "jsch", mockJsch);
+ expectedEx.expect(IOException.class);
+ expectedEx.expectMessage("com.jcraft.jsch.JSchException");
+ wrapper.sftpPut("DUMMY_SRC_PATH", "DUMMY_DEST_DIRECTORY");
+ }
+
+ @Test
+ public void testSftpPutStringToDest() throws JSchException, IOException {
+ Whitebox.setInternalState(wrapper, "hostName", "testHost");
+ Whitebox.setInternalState(wrapper, "userName", "testUser");
+ Whitebox.setInternalState(wrapper, "passWord", "testPwd");
+ ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
+ JSch mockJsch = Mockito.mock(JSch.class);
+ Session mockSession = Mockito.mock(Session.class);
+ Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
+ Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
+ Mockito.doNothing().when(mockSession).connect();
+ Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
+ Whitebox.setInternalState(wrapper, "jsch", mockJsch);
+ wrapper.SftpPut("DUMMY_STRING", "DUMMY_DEST_DIRECTORY");
+ Mockito.verify(mockSession).disconnect();
+ }
+
+ @Test
+ public void testSftpPutStringToDestExceptionFlow() throws JSchException, IOException {
+ JSch mockJsch = Mockito.mock(JSch.class);
+ Mockito.doThrow(new JSchException()).when(mockJsch).getSession(null, null, 22);
+ Whitebox.setInternalState(wrapper, "jsch", mockJsch);
+ expectedEx.expect(IOException.class);
+ expectedEx.expectMessage("com.jcraft.jsch.JSchException");
+ wrapper.SftpPut("DUMMY_STRING", "DUMMY_DEST_DIRECTORY");
+ }
+
+ @Test
+ public void testSftpGet() throws JSchException, IOException, SftpException {
+ File file = new File("src/test/resources/TEST_FILE.txt");
+ BufferedWriter writer = new BufferedWriter(new FileWriter(file.getPath()));
+ writer.write("line1\nline2");
+ writer.flush();
+ Whitebox.setInternalState(wrapper, "hostName", "testHost");
+ Whitebox.setInternalState(wrapper, "userName", "testUser");
+ Whitebox.setInternalState(wrapper, "passWord", "testPwd");
+ ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
+ JSch mockJsch = Mockito.mock(JSch.class);
+ Session mockSession = Mockito.mock(Session.class);
+ Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
+ Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
+ Mockito.doNothing().when(mockSession).connect();
+ Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
+ Mockito.doReturn(new FileInputStream(file)).when(mockChannel)
+ .get("src/test/resources/TEST_FILE.txt");
+ Whitebox.setInternalState(wrapper, "jsch", mockJsch);;
+ assertEquals("line1\nline2", wrapper.sftpGet("src/test/resources/TEST_FILE.txt"));
+ }
+
+ @Test
+ public void testSftpGetExceptionFlow() throws JSchException, IOException {
+ JSch mockJsch = Mockito.mock(JSch.class);
+ Mockito.doThrow(new JSchException()).when(mockJsch).getSession(null, null, 22);
+ Whitebox.setInternalState(wrapper, "jsch", mockJsch);
+ expectedEx.expect(IOException.class);
+ expectedEx.expectMessage("com.jcraft.jsch.JSchException");
+ wrapper.sftpGet("DUMMY_FILE_PATH");
+ }
+
+ @Test
+ public void testConnectWithSubsystem() throws IOException, JSchException, InterruptedException {
+ JSch mockJSch = Mockito.mock(JSch.class);
+ Session mockSession = Mockito.mock(Session.class);
+ ChannelSubsystem mockChannel = Mockito.mock(ChannelSubsystem.class);
+ InputStream stubInputStream = IOUtils.toInputStream("hello\n:~#", Charsets.UTF_8);
+ Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream();
+ Mockito.doReturn(mockChannel).when(mockSession).openChannel("subsystem");
+ Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22);
+ Mockito.doReturn(mockJSch).when(wrapper).getJSch();
+ wrapper.connect("testHost", "testUser", "testPswd", ":~#", 1000, 22, "testSubsystem");
+ Mockito.verify(mockChannel).connect();
+ }
+
+ @Test
+ public void testConnectWithSubsystemExceptionFlow() throws IOException, JSchException {
+ JSch mockJSch = Mockito.mock(JSch.class);
+ Mockito.doThrow(new JSchException()).when(mockJSch).getSession("testUser", "testHost", 22);
+ Mockito.doReturn(mockJSch).when(wrapper).getJSch();
+ expectedEx.expect(IOException.class);
+ expectedEx.expectMessage("com.jcraft.jsch.JSchException");
+ wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000, 22, "testSubsystem");
+ }
+
+ @Test
+ public void testConnectShellFourParameters() throws IOException, JSchException {
+ JSch mockJSch = Mockito.mock(JSch.class);
+ Session mockSession = Mockito.mock(Session.class);
+ ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
+ InputStream stubInputStream = IOUtils.toInputStream("hello\n]]>]]>", Charsets.UTF_8);
+ Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream();
+ Mockito.doReturn(mockChannel).when(mockSession).openChannel("shell");
+ Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22);
+ Mockito.doReturn(null).when(wrapper).receiveUntil(":~#", 9000,
+ "No cmd was sent, just waiting, but we can stop on a '~#'");
+ Mockito.doReturn(mockJSch).when(wrapper).getJSch();
+ wrapper.connect("testHost", "testUser", "testPswd", 22);
+ Mockito.verify(mockChannel).connect();
+ }
+
+ @Test
+ public void testConnectShellFourParametersExceptionFlow() throws IOException, JSchException {
+ JSch mockJSch = Mockito.mock(JSch.class);
+ Mockito.doThrow(new JSchException()).when(mockJSch).getSession("testUser", "testHost", 22);
+ Mockito.doReturn(mockJSch).when(wrapper).getJSch();
+ expectedEx.expect(IOException.class);
+ expectedEx.expectMessage("com.jcraft.jsch.JSchException");
+ wrapper.connect("testHost", "testUser", "testPswd", 22);
+ }
+
+ @Test
+ public void testPutInputStreamToDest() throws JSchException, IOException {
+ ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
+ JSch mockJsch = Mockito.mock(JSch.class);
+ Session mockSession = Mockito.mock(Session.class);
+ Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
+ Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
+ Mockito.doNothing().when(mockSession).connect(30 * 1000);
+ Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
+ Mockito.doReturn(mockJsch).when(wrapper).getJSch();
+ InputStream inputStream = Mockito.mock(InputStream.class);
+ wrapper.put(inputStream, "DUMMY_DEST_PATH/", "testHost", "testUser", "testPswd");
+ Mockito.verify(mockSession).disconnect();
+ }
+
+ @Test
+ public void testPutInputStreamToDestExceptionFlow()
+ throws JSchException, IOException, SftpException {
+ ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
+ Mockito.doThrow(new SftpException(0, null)).when(mockChannel).rm("DUMMY_DEST_PATH/*");
+ JSch mockJsch = Mockito.mock(JSch.class);
+ Session mockSession = Mockito.mock(Session.class);
+ Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
+ Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
+ Mockito.doNothing().when(mockSession).connect(30 * 1000);
+ Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
+ Mockito.doReturn(mockJsch).when(wrapper).getJSch();
+ InputStream inputStream = Mockito.mock(InputStream.class);
+ expectedEx.expect(IOException.class);
+ expectedEx.expectMessage("0: null");
+ wrapper.put(inputStream, "DUMMY_DEST_PATH/", "testHost", "testUser", "testPswd");
+ }
+
+ @Test
+ public void testPutInputStreamToDestExceptionFlow2()
+ throws JSchException, IOException, SftpException {
+ ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
+ Mockito.doThrow(new SftpException(0, "No such file")).when(mockChannel)
+ .rm("DUMMY_DEST_PATH/*");
+ JSch mockJsch = Mockito.mock(JSch.class);
+ Session mockSession = Mockito.mock(Session.class);
+ Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
+ Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
+ Mockito.doNothing().when(mockSession).connect(30 * 1000);
+ Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
+ Mockito.doReturn(mockJsch).when(wrapper).getJSch();
+ InputStream inputStream = Mockito.mock(InputStream.class);
+ wrapper.put(inputStream, "DUMMY_DEST_PATH/", "testHost", "testUser", "testPswd");
+ Mockito.verify(mockSession).disconnect();
+ }
+
+ @Test
+ public void testGet() throws JSchException, IOException, SftpException {
+ File file = new File("src/test/resources/TEST_FILE.txt");
+ BufferedWriter writer = new BufferedWriter(new FileWriter(file.getPath()));
+ writer.write("line1\nline2");
+ writer.flush();
+ Whitebox.setInternalState(wrapper, "hostName", "testHost");
+ Whitebox.setInternalState(wrapper, "userName", "testUser");
+ Whitebox.setInternalState(wrapper, "passWord", "testPwd");
+ ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
+ JSch mockJsch = Mockito.mock(JSch.class);
+ Session mockSession = Mockito.mock(Session.class);
+ Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
+ Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
+ Mockito.doNothing().when(mockSession).connect();
+ Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
+ Mockito.doReturn(new FileInputStream(file)).when(mockChannel)
+ .get("src/test/resources/TEST_FILE.txt");
+ Mockito.doReturn(mockJsch).when(wrapper).getJSch();
+ assertEquals("line1\nline2",
+ wrapper.get("src/test/resources/TEST_FILE.txt", "testHost", "testUser", "testPwd"));
+ }
+
+ @Test
+ public void testGetExceptionFlow() throws JSchException, IOException {
+ JSch mockJsch = Mockito.mock(JSch.class);
+ Mockito.doThrow(new JSchException()).when(mockJsch).getSession("testUser", "testHost", 22);
+ Mockito.doReturn(mockJsch).when(wrapper).getJSch();
+ expectedEx.expect(IOException.class);
+ expectedEx.expectMessage("com.jcraft.jsch.JSchException");
+ wrapper.get("src/test/resources/TEST_FILE.txt", "testHost", "testUser", "testPwd");
+ }
+
+ @Test
+ public void testSendWithDelimiter() throws IOException {
+ ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
+ OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
+ Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
+ DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
+ PowerMockito.doReturn("TEST RESPONSE").when(wrapper).receiveUntil("#$", 300000,
+ "TEST COMMAND\n");
+ PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
+ Whitebox.setInternalState(wrapper, "channel", mockChannel);
+ assertEquals("TEST RESPONSE", wrapper.send("TEST COMMAND\n", "#$"));
+ }
+
+ @Test
+ public void testSendWithDelimiterExceptionFlow() throws IOException {
+ ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
+ OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
+ Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
+ DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
+ PowerMockito.doThrow(new IOException()).when(mockDos).flush();
+ PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
+ Whitebox.setInternalState(wrapper, "channel", mockChannel);
+ expectedEx.expect(IOException.class);
+ expectedEx.expectMessage("java.io.IOException");
+ wrapper.send("TEST COMMAND", "]]>]]>");
+ }
+
+ @Test
+ public void testValues() throws IOException {