1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.sftp;
18
19 import java.io.IOException;
20
21 import junit.framework.Test;
22
23 import org.apache.commons.io.input.NullInputStream;
24 import org.apache.commons.vfs2.FileName;
25 import org.apache.commons.vfs2.FileObject;
26 import org.apache.commons.vfs2.FileSystem;
27 import org.apache.commons.vfs2.FileSystemOptions;
28 import org.apache.commons.vfs2.IPv6LocalConnectionTests;
29 import org.apache.commons.vfs2.provider.GenericFileName;
30 import org.mockito.Mockito;
31
32 import com.jcraft.jsch.ChannelExec;
33 import com.jcraft.jsch.JSchException;
34 import com.jcraft.jsch.Session;
35
36 public class SftpProviderIPv6TestCase extends AbstractSftpProviderTestCase {
37
38 private static class MockedClientSftpFileProvider extends SftpFileProvider {
39 @Override
40 protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions) {
41 final GenericFileName rootName = (GenericFileName) name;
42
43 final Session sessionMock = Mockito.mock(Session.class);
44 final ChannelExec channelExecMock = Mockito.mock(ChannelExec.class);
45
46 Mockito.when(sessionMock.isConnected()).thenReturn(true);
47
48 try {
49 Mockito.when(sessionMock.openChannel(Mockito.anyString())).thenReturn(channelExecMock);
50 } catch (final JSchException e) {
51 throw new AssertionError("Should never happen", e);
52 }
53
54 Mockito.when(channelExecMock.isClosed()).thenReturn(true);
55
56 try {
57 Mockito.when(channelExecMock.getInputStream()).thenReturn(new NullInputStream());
58 } catch (final IOException e) {
59 throw new AssertionError("Should never happen", e);
60 }
61
62 return new SftpFileSystem(rootName, sessionMock, fileSystemOptions);
63 }
64 }
65
66 public static Test suite() throws Exception {
67 return new SftpProviderTestSuite(new SftpProviderIPv6TestCase()) {
68 @Override
69 protected void addBaseTests() throws Exception {
70 addTests(SftpProviderIPv6TestCase.class);
71
72 if (getSystemTestUriOverride() == null) {
73 addTests(IPv6LocalConnectionTests.class);
74 }
75 }
76 };
77 }
78
79 @Override
80 protected boolean isExecChannelClosed() {
81 return false;
82 }
83
84 @org.junit.Test
85 public void testResolveIPv6Url() throws Exception {
86 try {
87
88 getManager().removeProvider("sftp");
89 getManager().addProvider("sftp", new MockedClientSftpFileProvider());
90
91 final String ipv6Url = "sftp://user:pass@[fe80::1c42:dae:8370:aea6%en1]/file.txt";
92
93 final FileObject fileObject = getManager().resolveFile(ipv6Url, new FileSystemOptions());
94
95 assertEquals("sftp://user:pass@[fe80::1c42:dae:8370:aea6%en1]/", fileObject.getFileSystem().getRootURI());
96 assertEquals("sftp://user:pass@[fe80::1c42:dae:8370:aea6%en1]/file.txt", fileObject.getName().getURI());
97 } finally {
98 getManager().removeProvider("sftp");
99 getManager().addProvider("sftp", new SftpFileProvider());
100 }
101 }
102 }