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.net.URI;
20  
21  import junit.framework.Test;
22  
23  import org.apache.commons.vfs2.FileObject;
24  import org.apache.commons.vfs2.FileSystemManager;
25  import org.apache.commons.vfs2.FileSystemOptions;
26  import org.apache.commons.vfs2.PermissionsTests;
27  import org.apache.commons.vfs2.ProviderReadTests;
28  
29  import com.jcraft.jsch.TestIdentityRepositoryFactory;
30  
31  public class SftpProviderStreamProxyModeTestCase extends AbstractSftpProviderTestCase {
32  
33      // VFS-440: stream proxy test suite
34      // We override the addBaseTests method so that only
35      // one test is run (we just test that the input/output are correctly forwarded, and
36      // hence if the reading test succeeds/fails the other will also succeed/fail)
37      public static Test suite() throws Exception {
38          return new SftpProviderTestSuite(new SftpProviderStreamProxyModeTestCase()) {
39              @Override
40              protected void addBaseTests() throws Exception {
41                  // Just tries to read
42                  addTests(ProviderReadTests.class);
43                  // VFS-405: set/get permissions
44                  addTests(PermissionsTests.class);
45              }
46          };
47      }
48  
49      @Override
50      public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
51          String uri = getSystemTestUriOverride();
52          if (uri == null) {
53              uri = connectionUri;
54          }
55  
56          final FileSystemOptions fileSystemOptions = new FileSystemOptions();
57          final SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
58          builder.setStrictHostKeyChecking(fileSystemOptions, "no");
59          builder.setUserInfo(fileSystemOptions, new TrustEveryoneUserInfo());
60          builder.setIdentityRepositoryFactory(fileSystemOptions, new TestIdentityRepositoryFactory());
61  
62          final FileSystemOptions proxyOptions = (FileSystemOptions) fileSystemOptions.clone();
63  
64          final URI parsedURI = new URI(uri);
65          final String userInfo = parsedURI.getUserInfo();
66          final String[] userFields = userInfo == null ? null : userInfo.split(":", 2);
67  
68          builder.setProxyType(fileSystemOptions, SftpFileSystemConfigBuilder.PROXY_STREAM);
69          if (userFields != null) {
70              if (userFields.length > 0) {
71                  builder.setProxyUser(fileSystemOptions, userFields[0]);
72              }
73              if (userFields.length > 1) {
74                  builder.setProxyPassword(fileSystemOptions, userFields[1]);
75              }
76          }
77          builder.setProxyHost(fileSystemOptions, parsedURI.getHost());
78          builder.setProxyPort(fileSystemOptions, parsedURI.getPort());
79          builder.setProxyCommand(fileSystemOptions, SftpStreamProxy.NETCAT_COMMAND);
80          builder.setProxyOptions(fileSystemOptions, proxyOptions);
81          builder.setProxyPassword(fileSystemOptions, parsedURI.getAuthority());
82  
83          // Set up the new URI
84          if (userInfo == null) {
85              uri = String.format("sftp://localhost:%d", parsedURI.getPort());
86          } else {
87              uri = String.format("sftp://%s@localhost:%d", userInfo, parsedURI.getPort());
88          }
89  
90          final FileObject fileObject = manager.resolveFile(uri, fileSystemOptions);
91          fileSystem = (SftpFileSystem) fileObject.getFileSystem();
92          return fileObject;
93      }
94  
95      @Override
96      protected boolean isExecChannelClosed() {
97          return false;
98      }
99  
100 }