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 org.apache.commons.vfs2.FileObject;
22  import org.apache.commons.vfs2.FileSystemManager;
23  import org.apache.commons.vfs2.FileSystemOptions;
24  import org.apache.commons.vfs2.PermissionsTests;
25  import org.apache.commons.vfs2.ProviderReadTests;
26  
27  import com.jcraft.jsch.TestIdentityRepositoryFactory;
28  
29  import junit.framework.Test;
30  
31  public class SftpProviderStreamProxyModeTestCase extends AbstractSftpProviderTestCase {
32      // --- VFS-440: stream proxy test suite
33      // We override the addBaseTests method so that only
34      // one test is run (we just test that the input/output are correctly forwarded, and
35      // hence if the reading test succeeds/fails the other will also succeed/fail)
36      public static Test suite() throws Exception {
37          final SftpProviderTestSuite suite = new SftpProviderTestSuite(new SftpProviderStreamProxyModeTestCase()) {
38              @Override
39              protected void addBaseTests() throws Exception {
40                  // Just tries to read
41                  addTests(ProviderReadTests.class);
42                  // VFS-405: set/get permissions
43                  addTests(PermissionsTests.class);
44              }
45          };
46          return suite;
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  
91          final FileObject fileObject = manager.resolveFile(uri, fileSystemOptions);
92          this.fileSystem = (SftpFileSystem) fileObject.getFileSystem();
93          return fileObject;
94      }
95  
96      @Override
97      protected boolean isExecChannelClosed() {
98          return false;
99      }
100 }