2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modifications Copyright (C) 2018 IBM.
10 * ================================================================================
11 * Modifications Copyright (C) 2018 Ericsson
12 * =============================================================================
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
17 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
25 * ============LICENSE_END=========================================================
28 package org.onap.appc.ccadaptor;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertNull;
32 import static org.junit.Assert.assertTrue;
33 import static org.junit.Assert.assertFalse;
34 import com.google.common.base.Charsets;
35 import com.jcraft.jsch.ChannelSftp;
36 import com.jcraft.jsch.ChannelShell;
37 import com.jcraft.jsch.ChannelSubsystem;
38 import com.jcraft.jsch.JSch;
39 import com.jcraft.jsch.JSchException;
40 import com.jcraft.jsch.Session;
41 import com.jcraft.jsch.SftpException;
42 import java.io.BufferedOutputStream;
43 import java.io.BufferedReader;
44 import java.io.BufferedWriter;
45 import java.io.DataOutputStream;
47 import java.io.FileInputStream;
48 import java.io.FileNotFoundException;
49 import java.io.FileWriter;
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.io.OutputStream;
53 import org.apache.commons.io.IOUtils;
54 import org.junit.Assert;
55 import org.junit.Before;
56 import org.junit.Rule;
57 import org.junit.Test;
58 import org.junit.rules.ExpectedException;
59 import org.junit.runner.RunWith;
60 import org.mockito.Mockito;
61 import org.mockito.internal.util.reflection.Whitebox;
62 import org.mockito.invocation.InvocationOnMock;
63 import org.mockito.stubbing.Answer;
64 import org.onap.appc.ccadaptor.SshJcraftWrapper.MyUserInfo;
65 import org.powermock.api.mockito.PowerMockito;
66 import org.powermock.core.classloader.annotations.PrepareForTest;
67 import org.powermock.modules.junit4.PowerMockRunner;
69 @RunWith(PowerMockRunner.class)
70 @PrepareForTest(DataOutputStream.class)
71 public class SshJcraftWrapperTest {
73 private SshJcraftWrapper wrapper;
74 private File debugFile;
77 public ExpectedException expectedEx = ExpectedException.none();
80 public void setupForTests() throws IOException, InterruptedException {
81 wrapper = Mockito.spy(new SshJcraftWrapper());
82 Mockito.doNothing().when(wrapper).delay(Mockito.anyInt());
83 debugFile = new File("src/test/resources/sshJcraftWrapperDebug");
84 File debugFile2 = new File("src/test/resources/sshJcraftWrapperDEBUG");
85 File configFile = new File("src/test/resources/jcraftReadSwConfigFileFromDisk");
86 File proxyRouterLogFile = new File("src/test/resources/proxyRouterLogFile");
87 debugFile.getParentFile().mkdirs();
88 debugFile.createNewFile();
89 debugFile2.createNewFile();
90 configFile.createNewFile();
91 proxyRouterLogFile.createNewFile();
92 Whitebox.setInternalState(wrapper, "debugLogFileName",
93 "src/test/resources/sshJcraftWrapperDebug");
94 Whitebox.setInternalState(wrapper, "extraDebugFile", debugFile2);
95 Whitebox.setInternalState(wrapper, "jcraftReadSwConfigFileFromDisk", configFile);
99 public void testConnect() throws IOException, JSchException, InterruptedException {
100 JSch mockJSch = Mockito.mock(JSch.class);
101 Session mockSession = Mockito.mock(Session.class);
102 ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
103 InputStream stubInputStream = IOUtils.toInputStream("hello\n]]>]]>", Charsets.UTF_8);
104 Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream();
105 Mockito.doReturn(mockChannel).when(mockSession).openChannel("shell");
106 Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22);
107 Mockito.doReturn(mockJSch).when(wrapper).getJSch();
108 wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000);
109 Mockito.verify(wrapper, Mockito.times(9)).appendToFile(Mockito.anyString(),
110 Mockito.anyString());
114 public void testConnectExceptionFlow() throws IOException, JSchException {
115 JSch mockJSch = Mockito.mock(JSch.class);
116 Mockito.doThrow(new JSchException()).when(mockJSch).getSession("testUser", "testHost", 22);
117 Mockito.doReturn(mockJSch).when(wrapper).getJSch();
118 expectedEx.expect(IOException.class);
119 expectedEx.expectMessage("com.jcraft.jsch.JSchException");
120 wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000);
124 public void testConnectExceptionFlow2() throws IOException, JSchException {
125 JSch mockJSch = Mockito.mock(JSch.class);
126 Session mockSession = Mockito.mock(Session.class);
127 ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
128 Mockito.doReturn(mockChannel).when(mockSession).openChannel("shell");
129 Mockito.doThrow(new IOException()).when(wrapper).receiveUntil("]]>]]>", 3000,
130 "No cmd was sent, just waiting");
131 Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22);
132 Mockito.doReturn(mockJSch).when(wrapper).getJSch();
133 wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000);
134 Mockito.verify(wrapper, Mockito.times(1)).receiveUntil("]]>]]>", 3000,
135 "No cmd was sent, just waiting");
139 public void testConnectWithPortNumber() throws IOException, JSchException {
140 JSch mockJSch = Mockito.mock(JSch.class);
141 Session mockSession = Mockito.mock(Session.class);
142 ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
143 InputStream stubInputStream = IOUtils.toInputStream("hello\n:~#", Charsets.UTF_8);
144 Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream();
145 Mockito.doReturn(mockChannel).when(mockSession).openChannel("shell");
146 Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22);
147 Mockito.doReturn(mockJSch).when(wrapper).getJSch();
148 wrapper.connect("testHost", "testUser", "testPswd", ":~#", 1000, 22);
149 Mockito.verify(wrapper, Mockito.times(9)).appendToFile(Mockito.anyString(),
150 Mockito.anyString());
154 public void testConnectWithPortNumberSuccessFlow2()
155 throws IOException, JSchException, InterruptedException {
156 JSch mockJSch = Mockito.mock(JSch.class);
157 Session mockSession = Mockito.mock(Session.class);
158 ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
159 InputStream stubInputStream = IOUtils.toInputStream("hello\n]]>]]>", Charsets.UTF_8);
160 Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream();
161 Mockito.doReturn(mockChannel).when(mockSession).openChannel("shell");
162 Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22);
163 Mockito.doReturn(mockJSch).when(wrapper).getJSch();
164 wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000, 22);
165 Mockito.verify(wrapper, Mockito.times(9)).appendToFile(Mockito.anyString(),
166 Mockito.anyString());
170 public void testConnectWithPortNumberExceptionFlow() throws IOException, JSchException {
171 JSch mockJSch = Mockito.mock(JSch.class);
172 Mockito.doThrow(new JSchException()).when(mockJSch).getSession("testUser", "testHost", 22);
173 Mockito.doReturn(mockJSch).when(wrapper).getJSch();
174 expectedEx.expect(IOException.class);
175 expectedEx.expectMessage("com.jcraft.jsch.JSchException");
176 wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000, 22);
180 public void testConnectWithPortNumberExceptionFlow2() throws IOException, JSchException {
181 JSch mockJSch = Mockito.mock(JSch.class);
182 Session mockSession = Mockito.mock(Session.class);
183 ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
184 InputStream stubInputStream = IOUtils.toInputStream("", Charsets.UTF_8);
185 Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream();
186 Mockito.doReturn(mockChannel).when(mockSession).openChannel("shell");
187 Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22);
188 Mockito.doReturn(mockJSch).when(wrapper).getJSch();
189 wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000, 22);
190 Mockito.verify(wrapper, Mockito.times(1)).receiveUntil("]]>]]>", 10000,
191 "No cmd was sent, just waiting");
195 public void testReceiveUntilTimeout() throws TimedOutException, IOException, JSchException {
196 Session mockSession = Mockito.mock(Session.class);
197 Whitebox.setInternalState(wrapper, "session", mockSession);
198 // The sleep is required to make sure that the system clock has incremented by
199 // (at least) 1 millisecond between passing in our timeout value and setting a deadline
200 // and checking to see if the deadline has been missed
201 Mockito.doAnswer(new Answer<String>() {
203 public String answer(InvocationOnMock invocation) throws InterruptedException {
207 }).when(mockSession).setTimeout(0);
208 expectedEx.expect(IOException.class);
209 expectedEx.expectMessage("Timeout: time in routine has exceed our deadline");
210 wrapper.receiveUntil("", 0, "");
214 public void testReceiveUntilReaderTimeout()
215 throws TimedOutException, IOException, JSchException {
216 Session mockSession = Mockito.mock(Session.class);
217 Whitebox.setInternalState(wrapper, "session", mockSession);
218 BufferedReader mockReader = Mockito.mock(BufferedReader.class);
219 Whitebox.setInternalState(wrapper, "reader", mockReader);
220 expectedEx.expect(IOException.class);
221 expectedEx.expectMessage("Received a SocketTimeoutException router=");
222 wrapper.receiveUntil("", 3000, "");
226 public void testReceiveUntilIosXr() throws TimedOutException, IOException, JSchException {
227 Session mockSession = Mockito.mock(Session.class);
228 Whitebox.setInternalState(wrapper, "session", mockSession);
229 BufferedReader mockReader = Mockito.mock(BufferedReader.class);
230 Mockito.doReturn(3).when(mockReader).read(Mockito.anyObject(), Mockito.anyInt(),
232 Whitebox.setInternalState(wrapper, "reader", mockReader);
233 Mockito.doReturn(false).when(wrapper).jcraftReadSwConfigFileFromDisk();
234 Mockito.doReturn("\nXML>").when(wrapper).getLastFewLinesOfFile(Mockito.anyObject(),
237 assertNull(wrapper.receiveUntil("]]>]]>", 3000, "IOS_XR_uploadedSwConfigCmd\nOTHER\nXML>"));
241 public void testReceiveStringDelimiters() {
242 assertEquals(false, wrapper.checkIfReceivedStringMatchesDelimeter("#$", "", ""));
246 public void testReceiveStringDelimitersShowConfig() {
247 assertEquals(true, wrapper.checkIfReceivedStringMatchesDelimeter("]]>]]>", "]]>]]>\n #",
252 public void testReceiveStringDelimitersTwoArg() throws IOException {
253 SshJcraftWrapper localWrapper = Mockito.spy(new SshJcraftWrapper());
254 Mockito.doReturn(true).when(localWrapper).jcraftReadSwConfigFileFromDisk();
255 Mockito.doThrow(new IOException()).when(localWrapper)
256 .getLastFewLinesOfFile(Mockito.anyObject(), Mockito.anyInt());
257 Whitebox.setInternalState(localWrapper, "routerFileName", "DUMMY_FILE_NAME");
258 assertEquals(false, localWrapper.checkIfReceivedStringMatchesDelimeter(3, "]]>]]>\n #"));
262 public void testCloseConnection() {
263 Session mockSession = Mockito.mock(Session.class);
264 Whitebox.setInternalState(wrapper, "session", mockSession);
265 wrapper.closeConnection();
266 Mockito.verify(mockSession, Mockito.times(1)).disconnect();
270 public void testSend() throws IOException {
271 ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
272 OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
273 Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
274 DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
275 PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
276 Whitebox.setInternalState(wrapper, "channel", mockChannel);
277 wrapper.send("TEST COMMAND\n");
278 Mockito.verify(wrapper, Mockito.times(2)).appendToFile(Mockito.anyString(),
279 Mockito.anyString());
283 public void testSendExceptionFlow() throws IOException {
284 ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
285 OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
286 Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
287 DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
288 PowerMockito.doThrow(new IOException()).when(mockDos).flush();
289 PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
290 Whitebox.setInternalState(wrapper, "channel", mockChannel);
291 expectedEx.expect(IOException.class);
292 expectedEx.expectMessage("java.io.IOException");
293 wrapper.send("TEST COMMAND");
297 public void testSendChar() throws IOException {
298 ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
299 OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
300 Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
301 DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
302 PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
303 Whitebox.setInternalState(wrapper, "channel", mockChannel);
304 wrapper.sendChar(74);
305 Mockito.verify(mockDos).flush();
309 public void testSendCharExceptionFlow() throws IOException {
310 ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
311 OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
312 Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
313 DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
314 PowerMockito.doThrow(new IOException()).when(mockDos).flush();
315 PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
316 Whitebox.setInternalState(wrapper, "channel", mockChannel);
317 expectedEx.expect(IOException.class);
318 expectedEx.expectMessage("java.io.IOException");
319 wrapper.sendChar(65);
323 public void testSendByteArrayExceptionFlow() throws IOException {
324 ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
325 OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
326 Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
327 DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
328 PowerMockito.doThrow(new IOException()).when(mockDos).flush();
329 PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
330 Whitebox.setInternalState(wrapper, "channel", mockChannel);
331 expectedEx.expect(IOException.class);
332 expectedEx.expectMessage("java.io.IOException");
333 byte[] byteArray = new byte[] {65, 74};
334 wrapper.send(byteArray, 0, 2);
338 public void testGetLastFewLinesOfFile() throws FileNotFoundException, IOException {
339 File file = new File("src/test/resources/TEST_FILE.txt");
340 BufferedWriter writer = new BufferedWriter(new FileWriter(file.getPath()));
341 writer.write("line1\nline2");
344 assertEquals("\nline2", wrapper.getLastFewLinesOfFile(file, 2));
348 public void testReceiveUntilBufferFlush() throws TimedOutException, IOException {
349 Session mockSession = Mockito.mock(Session.class);
350 Whitebox.setInternalState(wrapper, "session", mockSession);
351 BufferedReader mockReader = Mockito.mock(BufferedReader.class);
352 Mockito.doReturn(12).when(mockReader).read(Mockito.anyObject(), Mockito.anyInt(),
354 Whitebox.setInternalState(wrapper, "reader", mockReader);
356 wrapper.receiveUntilBufferFlush(12, 100, "TEST_MESSAGE");
357 Mockito.verify(wrapper, Mockito.times(2)).logMemoryUsage();
361 public void testReceiveUntilBufferFlushTimeout()
362 throws TimedOutException, IOException, JSchException {
363 Session mockSession = Mockito.mock(Session.class);
364 // The sleep is required to make sure that the system clock has incremented by
365 // (at least) 1 millisecond between passing in our timeout value and setting a deadline
366 // and checking to see if the deadline has been missed
367 Mockito.doAnswer(new Answer<String>() {
369 public String answer(InvocationOnMock invocation) throws InterruptedException {
373 }).when(mockSession).setTimeout(0);
374 Whitebox.setInternalState(wrapper, "session", mockSession);
375 expectedEx.expect(IOException.class);
376 expectedEx.expectMessage("Timeout: time in routine has exceed our deadline");
377 wrapper.receiveUntilBufferFlush(10, 0, "TEST_MESSAGE");
381 public void testReceiveUntilBufferFlushJSchException()
382 throws TimedOutException, IOException, JSchException {
383 Session mockSession = Mockito.mock(Session.class);
384 Mockito.doThrow(new JSchException()).when(mockSession).setTimeout(0);
385 Whitebox.setInternalState(wrapper, "session", mockSession);
386 expectedEx.expect(TimedOutException.class);
387 expectedEx.expectMessage("com.jcraft.jsch.JSchException");
388 wrapper.receiveUntilBufferFlush(10, 0, "TEST_MESSAGE");
392 public void testSftpPutSourceToDest() throws JSchException, IOException {
393 Whitebox.setInternalState(wrapper, "hostName", "testHost");
394 Whitebox.setInternalState(wrapper, "userName", "testUser");
395 Whitebox.setInternalState(wrapper, "passWord", "testPwd");
396 ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
397 JSch mockJsch = Mockito.mock(JSch.class);
398 Session mockSession = Mockito.mock(Session.class);
399 Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
400 Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
401 Mockito.doNothing().when(mockSession).connect();
402 Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
403 Whitebox.setInternalState(wrapper, "jsch", mockJsch);
404 wrapper.sftpPut("DUMMY_SRC_PATH", "DUMMY_DEST_DIRECTORY");
405 Mockito.verify(mockSession).disconnect();
409 public void testSftpPutSourceToDestExceptionFlow() throws JSchException, IOException {
410 JSch mockJsch = Mockito.mock(JSch.class);
411 Mockito.doThrow(new JSchException()).when(mockJsch).getSession(null, null, 22);
412 Whitebox.setInternalState(wrapper, "jsch", mockJsch);
413 expectedEx.expect(IOException.class);
414 expectedEx.expectMessage("com.jcraft.jsch.JSchException");
415 wrapper.sftpPut("DUMMY_SRC_PATH", "DUMMY_DEST_DIRECTORY");
419 public void testSftpPutStringToDest() throws JSchException, IOException {
420 Whitebox.setInternalState(wrapper, "hostName", "testHost");
421 Whitebox.setInternalState(wrapper, "userName", "testUser");
422 Whitebox.setInternalState(wrapper, "passWord", "testPwd");
423 ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
424 JSch mockJsch = Mockito.mock(JSch.class);
425 Session mockSession = Mockito.mock(Session.class);
426 Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
427 Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
428 Mockito.doNothing().when(mockSession).connect();
429 Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
430 Whitebox.setInternalState(wrapper, "jsch", mockJsch);
431 wrapper.SftpPut("DUMMY_STRING", "DUMMY_DEST_DIRECTORY");
432 Mockito.verify(mockSession).disconnect();
436 public void testSftpPutStringToDestExceptionFlow() throws JSchException, IOException {
437 JSch mockJsch = Mockito.mock(JSch.class);
438 Mockito.doThrow(new JSchException()).when(mockJsch).getSession(null, null, 22);
439 Whitebox.setInternalState(wrapper, "jsch", mockJsch);
440 expectedEx.expect(IOException.class);
441 expectedEx.expectMessage("com.jcraft.jsch.JSchException");
442 wrapper.SftpPut("DUMMY_STRING", "DUMMY_DEST_DIRECTORY");
446 public void testSftpGet() throws JSchException, IOException, SftpException {
447 File file = new File("src/test/resources/TEST_FILE.txt");
448 BufferedWriter writer = new BufferedWriter(new FileWriter(file.getPath()));
449 writer.write("line1\nline2");
451 Whitebox.setInternalState(wrapper, "hostName", "testHost");
452 Whitebox.setInternalState(wrapper, "userName", "testUser");
453 Whitebox.setInternalState(wrapper, "passWord", "testPwd");
454 ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
455 JSch mockJsch = Mockito.mock(JSch.class);
456 Session mockSession = Mockito.mock(Session.class);
457 Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
458 Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
459 Mockito.doNothing().when(mockSession).connect();
460 Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
461 Mockito.doReturn(new FileInputStream(file)).when(mockChannel)
462 .get("src/test/resources/TEST_FILE.txt");
463 Whitebox.setInternalState(wrapper, "jsch", mockJsch);;
464 assertEquals("line1\nline2", wrapper.sftpGet("src/test/resources/TEST_FILE.txt"));
468 public void testSftpGetExceptionFlow() throws JSchException, IOException {
469 JSch mockJsch = Mockito.mock(JSch.class);
470 Mockito.doThrow(new JSchException()).when(mockJsch).getSession(null, null, 22);
471 Whitebox.setInternalState(wrapper, "jsch", mockJsch);
472 expectedEx.expect(IOException.class);
473 expectedEx.expectMessage("com.jcraft.jsch.JSchException");
474 wrapper.sftpGet("DUMMY_FILE_PATH");
478 public void testConnectWithSubsystem() throws IOException, JSchException, InterruptedException {
479 JSch mockJSch = Mockito.mock(JSch.class);
480 Session mockSession = Mockito.mock(Session.class);
481 ChannelSubsystem mockChannel = Mockito.mock(ChannelSubsystem.class);
482 InputStream stubInputStream = IOUtils.toInputStream("hello\n:~#", Charsets.UTF_8);
483 Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream();
484 Mockito.doReturn(mockChannel).when(mockSession).openChannel("subsystem");
485 Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22);
486 Mockito.doReturn(mockJSch).when(wrapper).getJSch();
487 wrapper.connect("testHost", "testUser", "testPswd", ":~#", 1000, 22, "testSubsystem");
488 Mockito.verify(mockChannel).connect();
492 public void testConnectWithSubsystemExceptionFlow() throws IOException, JSchException {
493 JSch mockJSch = Mockito.mock(JSch.class);
494 Mockito.doThrow(new JSchException()).when(mockJSch).getSession("testUser", "testHost", 22);
495 Mockito.doReturn(mockJSch).when(wrapper).getJSch();
496 expectedEx.expect(IOException.class);
497 expectedEx.expectMessage("com.jcraft.jsch.JSchException");
498 wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000, 22, "testSubsystem");
502 public void testConnectShellFourParameters() throws IOException, JSchException {
503 JSch mockJSch = Mockito.mock(JSch.class);
504 Session mockSession = Mockito.mock(Session.class);
505 ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
506 InputStream stubInputStream = IOUtils.toInputStream("hello\n]]>]]>", Charsets.UTF_8);
507 Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream();
508 Mockito.doReturn(mockChannel).when(mockSession).openChannel("shell");
509 Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22);
510 Mockito.doReturn(null).when(wrapper).receiveUntil(":~#", 9000,
511 "No cmd was sent, just waiting, but we can stop on a '~#'");
512 Mockito.doReturn(mockJSch).when(wrapper).getJSch();
513 wrapper.connect("testHost", "testUser", "testPswd", 22);
514 Mockito.verify(mockChannel).connect();
518 public void testConnectShellFourParametersExceptionFlow() throws IOException, JSchException {
519 JSch mockJSch = Mockito.mock(JSch.class);
520 Mockito.doThrow(new JSchException()).when(mockJSch).getSession("testUser", "testHost", 22);
521 Mockito.doReturn(mockJSch).when(wrapper).getJSch();
522 expectedEx.expect(IOException.class);
523 expectedEx.expectMessage("com.jcraft.jsch.JSchException");
524 wrapper.connect("testHost", "testUser", "testPswd", 22);
528 public void testPutInputStreamToDest() throws JSchException, IOException {
529 ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
530 JSch mockJsch = Mockito.mock(JSch.class);
531 Session mockSession = Mockito.mock(Session.class);
532 Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
533 Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
534 Mockito.doNothing().when(mockSession).connect(30 * 1000);
535 Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
536 Mockito.doReturn(mockJsch).when(wrapper).getJSch();
537 InputStream inputStream = Mockito.mock(InputStream.class);
538 wrapper.put(inputStream, "DUMMY_DEST_PATH/", "testHost", "testUser", "testPswd");
539 Mockito.verify(mockSession).disconnect();
543 public void testPutInputStreamToDestExceptionFlow()
544 throws JSchException, IOException, SftpException {
545 ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
546 Mockito.doThrow(new SftpException(0, null)).when(mockChannel).rm("DUMMY_DEST_PATH/*");
547 JSch mockJsch = Mockito.mock(JSch.class);
548 Session mockSession = Mockito.mock(Session.class);
549 Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
550 Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
551 Mockito.doNothing().when(mockSession).connect(30 * 1000);
552 Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
553 Mockito.doReturn(mockJsch).when(wrapper).getJSch();
554 InputStream inputStream = Mockito.mock(InputStream.class);
555 expectedEx.expect(IOException.class);
556 expectedEx.expectMessage("0: null");
557 wrapper.put(inputStream, "DUMMY_DEST_PATH/", "testHost", "testUser", "testPswd");
561 public void testPutInputStreamToDestExceptionFlow2()
562 throws JSchException, IOException, SftpException {
563 ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
564 Mockito.doThrow(new SftpException(0, "No such file")).when(mockChannel)
565 .rm("DUMMY_DEST_PATH/*");
566 JSch mockJsch = Mockito.mock(JSch.class);
567 Session mockSession = Mockito.mock(Session.class);
568 Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
569 Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
570 Mockito.doNothing().when(mockSession).connect(30 * 1000);
571 Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
572 Mockito.doReturn(mockJsch).when(wrapper).getJSch();
573 InputStream inputStream = Mockito.mock(InputStream.class);
574 wrapper.put(inputStream, "DUMMY_DEST_PATH/", "testHost", "testUser", "testPswd");
575 Mockito.verify(mockSession).disconnect();
579 public void testGet() throws JSchException, IOException, SftpException {
580 File file = new File("src/test/resources/TEST_FILE.txt");
581 BufferedWriter writer = new BufferedWriter(new FileWriter(file.getPath()));
582 writer.write("line1\nline2");
584 Whitebox.setInternalState(wrapper, "hostName", "testHost");
585 Whitebox.setInternalState(wrapper, "userName", "testUser");
586 Whitebox.setInternalState(wrapper, "passWord", "testPwd");
587 ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class);
588 JSch mockJsch = Mockito.mock(JSch.class);
589 Session mockSession = Mockito.mock(Session.class);
590 Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22);
591 Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString());
592 Mockito.doNothing().when(mockSession).connect();
593 Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp");
594 Mockito.doReturn(new FileInputStream(file)).when(mockChannel)
595 .get("src/test/resources/TEST_FILE.txt");
596 Mockito.doReturn(mockJsch).when(wrapper).getJSch();
597 assertEquals("line1\nline2",
598 wrapper.get("src/test/resources/TEST_FILE.txt", "testHost", "testUser", "testPwd"));
602 public void testGetExceptionFlow() throws JSchException, IOException {
603 JSch mockJsch = Mockito.mock(JSch.class);
604 Mockito.doThrow(new JSchException()).when(mockJsch).getSession("testUser", "testHost", 22);
605 Mockito.doReturn(mockJsch).when(wrapper).getJSch();
606 expectedEx.expect(IOException.class);
607 expectedEx.expectMessage("com.jcraft.jsch.JSchException");
608 wrapper.get("src/test/resources/TEST_FILE.txt", "testHost", "testUser", "testPwd");
612 public void testSendWithDelimiter() throws IOException {
613 ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
614 OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
615 Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
616 DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
617 PowerMockito.doReturn("TEST RESPONSE").when(wrapper).receiveUntil("#$", 300000,
619 PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
620 Whitebox.setInternalState(wrapper, "channel", mockChannel);
621 assertEquals("TEST RESPONSE", wrapper.send("TEST COMMAND\n", "#$"));
625 public void testSendWithDelimiterExceptionFlow() throws IOException {
626 ChannelShell mockChannel = Mockito.mock(ChannelShell.class);
627 OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class);
628 Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
629 DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream));
630 PowerMockito.doThrow(new IOException()).when(mockDos).flush();
631 PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject());
632 Whitebox.setInternalState(wrapper, "channel", mockChannel);
633 expectedEx.expect(IOException.class);
634 expectedEx.expectMessage("java.io.IOException");
635 wrapper.send("TEST COMMAND", "]]>]]>");
639 public void testValues() throws IOException {
640 SshJcraftWrapper wrapper = new SshJcraftWrapper();
641 wrapper.setEquipNameCode("testcode");
642 wrapper.setRouterCommandType("testcommand");
643 String equipName = wrapper.getEquipNameCode();
644 assertNull(wrapper.getHostName());
645 assertNull(wrapper.getPassWord());
646 assertNull(wrapper.getRouterName());
647 assertNull(wrapper.getUserName());
649 wrapper.getTheDate().indexOf('/') > -1 && wrapper.getTheDate().indexOf(':') > -1);
650 Assert.assertEquals("testcode", equipName);
653 @Test(expected = Exception.class)
654 public void testSetRouterCommandType2() throws IOException {
655 SshJcraftWrapper wrapper = new SshJcraftWrapper();
656 wrapper.appendToRouterFile("test", 2);
657 StringBuffer buffer = new StringBuffer();
658 buffer.append("test");
659 wrapper.appendToRouterFile("Test.txt", buffer);
660 wrapper.receiveUntilBufferFlush(3, 4, "test");
663 @Test(expected = Exception.class)
664 public void testSetRouterCommandType3() throws IOException {
665 SshJcraftWrapper wrapper = new SshJcraftWrapper();
666 wrapper.checkIfReceivedStringMatchesDelimeter(3, "test");
670 public void testMyUserInfoGetPassword() {
671 MyUserInfo myUserInfo = new MyUserInfo();
672 assertNull(myUserInfo.getPassword());
676 public void testMyUserInfoPromptYesNo() {
677 MyUserInfo myUserInfo = new MyUserInfo();
678 assertFalse(myUserInfo.promptYesNo(""));