View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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              // We only want to use mocked client for this test, not for the test class initialization
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 }