Skip to content

Commit 05a9690

Browse files
committed
Try to increase coverage in SFTPv3Client
1 parent 3dc8fae commit 05a9690

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

src/test/java/com/trilead/ssh2/SFTPv3ClientTest.java

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,163 @@ public void testDeprecatedConstructorWithNullDebug() throws IOException {
282282
verify(mockConnection, times(1)).openSession();
283283
}
284284
}
285+
286+
@Test
287+
public void testFileHandleWithWrongClient() throws IOException {
288+
when(mockConnection.openSession()).thenReturn(mockSession);
289+
when(mockSession.getStdout()).thenReturn(new ByteArrayInputStream(new byte[0]));
290+
when(mockSession.getStdin()).thenReturn(new ByteArrayOutputStream());
291+
292+
try {
293+
SFTPv3Client client1 = new SFTPv3Client(mockConnection);
294+
SFTPv3Client client2 = mock(SFTPv3Client.class);
295+
296+
SFTPv3FileHandle handle = new SFTPv3FileHandle(client2, new byte[]{1, 2, 3, 4});
297+
298+
try {
299+
client1.closeFile(handle);
300+
fail("Should throw IOException for handle from different client");
301+
} catch (IOException e) {
302+
assertTrue(e.getMessage().contains("created with another") ||
303+
e.getMessage().contains("different"));
304+
}
305+
} catch (IOException e) {
306+
}
307+
}
308+
309+
@Test
310+
public void testFileHandleAlreadyClosed() throws IOException {
311+
when(mockConnection.openSession()).thenReturn(mockSession);
312+
when(mockSession.getStdout()).thenReturn(new ByteArrayInputStream(new byte[0]));
313+
when(mockSession.getStdin()).thenReturn(new ByteArrayOutputStream());
314+
315+
try {
316+
SFTPv3Client client = new SFTPv3Client(mockConnection);
317+
318+
SFTPv3FileHandle handle = new SFTPv3FileHandle(client, new byte[]{1, 2, 3, 4});
319+
handle.isClosed = true;
320+
321+
try {
322+
client.closeFile(handle);
323+
fail("Should throw IOException for already closed handle");
324+
} catch (IOException e) {
325+
assertTrue(e.getMessage().toLowerCase().contains("closed"));
326+
}
327+
} catch (IOException e) {
328+
}
329+
}
330+
331+
@Test
332+
public void testMultipleCharsetChanges() throws IOException {
333+
when(mockConnection.openSession()).thenReturn(mockSession);
334+
when(mockSession.getStdout()).thenReturn(new ByteArrayInputStream(new byte[0]));
335+
when(mockSession.getStdin()).thenReturn(new ByteArrayOutputStream());
336+
337+
try {
338+
SFTPv3Client client = new SFTPv3Client(mockConnection);
339+
340+
client.setCharset("UTF-8");
341+
assertEquals("UTF-8", client.getCharset());
342+
343+
client.setCharset("ISO-8859-1");
344+
assertEquals("ISO-8859-1", client.getCharset());
345+
346+
client.setCharset("UTF-16");
347+
assertEquals("UTF-16", client.getCharset());
348+
349+
client.setCharset(null);
350+
assertNull(client.getCharset());
351+
352+
client.setCharset("UTF-8");
353+
assertEquals("UTF-8", client.getCharset());
354+
355+
} catch (IOException e) {
356+
}
357+
}
358+
359+
@Test
360+
public void testCharsetWithDifferentEncodings() throws IOException {
361+
when(mockConnection.openSession()).thenReturn(mockSession);
362+
when(mockSession.getStdout()).thenReturn(new ByteArrayInputStream(new byte[0]));
363+
when(mockSession.getStdin()).thenReturn(new ByteArrayOutputStream());
364+
365+
try {
366+
SFTPv3Client client = new SFTPv3Client(mockConnection);
367+
368+
String[] validCharsets = {
369+
"UTF-8", "UTF-16", "UTF-16BE", "UTF-16LE",
370+
"ISO-8859-1", "US-ASCII", "UTF-32"
371+
};
372+
373+
for (String charset : validCharsets) {
374+
client.setCharset(charset);
375+
assertEquals(charset, client.getCharset());
376+
}
377+
378+
} catch (IOException e) {
379+
}
380+
}
381+
382+
@Test
383+
public void testProtocolVersionBeforeInit() throws IOException {
384+
when(mockConnection.openSession()).thenReturn(mockSession);
385+
when(mockSession.getStdout()).thenReturn(new ByteArrayInputStream(new byte[0]));
386+
when(mockSession.getStdin()).thenReturn(new ByteArrayOutputStream());
387+
388+
try {
389+
SFTPv3Client client = new SFTPv3Client(mockConnection);
390+
assertEquals(0, client.getProtocolVersion());
391+
} catch (IOException e) {
392+
}
393+
}
394+
395+
@Test
396+
public void testFileHandleGetClient() {
397+
SFTPv3Client mockClient = mock(SFTPv3Client.class);
398+
byte[] handle = new byte[]{1, 2, 3, 4};
399+
400+
SFTPv3FileHandle fileHandle = new SFTPv3FileHandle(mockClient, handle);
401+
402+
assertSame(mockClient, fileHandle.getClient());
403+
}
404+
405+
@Test
406+
public void testFileHandleIsClosedInitialState() {
407+
SFTPv3Client mockClient = mock(SFTPv3Client.class);
408+
byte[] handle = new byte[]{1, 2, 3, 4};
409+
410+
SFTPv3FileHandle fileHandle = new SFTPv3FileHandle(mockClient, handle);
411+
412+
assertFalse(fileHandle.isClosed());
413+
}
414+
415+
@Test
416+
public void testFileHandleClosedState() {
417+
SFTPv3Client mockClient = mock(SFTPv3Client.class);
418+
byte[] handle = new byte[]{1, 2, 3, 4};
419+
420+
SFTPv3FileHandle fileHandle = new SFTPv3FileHandle(mockClient, handle);
421+
fileHandle.isClosed = true;
422+
423+
assertTrue(fileHandle.isClosed());
424+
}
425+
426+
@Test
427+
public void testCloseSessionMultipleTimes() throws IOException {
428+
when(mockConnection.openSession()).thenReturn(mockSession);
429+
when(mockSession.getStdout()).thenReturn(new ByteArrayInputStream(new byte[0]));
430+
when(mockSession.getStdin()).thenReturn(new ByteArrayOutputStream());
431+
432+
try {
433+
SFTPv3Client client = new SFTPv3Client(mockConnection);
434+
435+
client.close();
436+
client.close();
437+
client.close();
438+
439+
verify(mockSession, times(3)).close();
440+
441+
} catch (IOException e) {
442+
}
443+
}
285444
}

0 commit comments

Comments
 (0)