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 static org.apache.commons.vfs2.VfsTestUtils.getTestDirectory;
20  
21  import java.io.File;
22  import java.nio.file.Paths;
23  
24  import org.apache.commons.vfs2.Capability;
25  import org.apache.commons.vfs2.FileObject;
26  import org.apache.commons.vfs2.Selectors;
27  import org.apache.commons.vfs2.VFS;
28  import org.apache.sshd.server.channel.ChannelSession;
29  import org.junit.Test;
30  import org.junit.jupiter.api.Assertions;
31  
32  /**
33   * Test SftpFileObject.doGetOutputStream return the channel to pool, even throw a sftp write permission exception.
34   */
35  public class SftpPermissionExceptionTestCase extends AbstractSftpProviderTestCase {
36  
37      /**
38       * Creates the test suite for the sftp file system.
39       */
40      public static junit.framework.Test suite() throws Exception {
41          return new SftpProviderTestSuite(new SftpPermissionExceptionTestCase()){
42              @Override
43              protected void addBaseTests() throws Exception {
44                  // Just tries to read
45                  addTests(SftpPermissionExceptionTestCase.class);
46              }
47          };
48      }
49  
50      /**
51       * Sets up a scratch folder for the test to use.
52       */
53      protected FileObject createScratchFolder() throws Exception {
54          final FileObject scratchFolder = getWriteFolder();
55  
56          // Make sure the test folder is empty
57          scratchFolder.delete(Selectors.EXCLUDE_SELF);
58          scratchFolder.createFolder();
59          scratchFolder.setWritable(false, false);
60          return scratchFolder;
61      }
62  
63      /**
64       * Returns the capabilities required by the tests of this test case.
65       */
66      @Override
67      protected Capability[] getRequiredCapabilities() {
68          return new Capability[] {Capability.CREATE, Capability.DELETE, Capability.GET_TYPE, Capability.LIST_CHILDREN,
69              Capability.READ_CONTENT, Capability.WRITE_CONTENT};
70      }
71  
72      @Override
73      protected boolean isExecChannelClosed() {
74          return false;
75      }
76  
77      /**
78       * Test SftpFileObject.doGetOutputStream return the channel to pool, when there is an exception in channel.put.
79       */
80      @Test
81      public void testGetOutputStreamException() throws Exception {
82          final File localFile = new File("src/test/resources/test-data/test.zip");
83  
84          final FileObject localFileObject = VFS.getManager().toFileObject(localFile);
85  
86          final FileObject scratchFolder = createScratchFolder();
87  
88          // try to create local file
89          final String fileName = "filecopy.txt";
90          FileObject fileObjectCopy = scratchFolder.resolveFile(fileName);
91          fileObjectCopy.setWritable(false, false);
92          fileObjectCopy.copyFrom(localFileObject, Selectors.SELECT_SELF);
93  
94          // try to set the local file to readonly
95          Paths.get(getTestDirectory(), scratchFolder.getName().getBaseName(), fileName).toFile().setWritable(false);
96          for (int i = 0; i < 30; i++) {
97              try{
98                  fileObjectCopy = scratchFolder.resolveFile(fileName);
99                  Assertions.assertFalse(fileObjectCopy.isWriteable());
100                 fileObjectCopy.copyFrom(localFileObject, Selectors.SELECT_SELF);
101                 Assertions.fail("permission fail");
102             } catch (final Exception ex) {
103                 // ignore no permission
104             }
105         }
106 
107         // try to get created channel number.
108         final int channelId = server.getActiveSessions().get(0).registerChannel(new ChannelSession());
109         Assertions.assertTrue(channelId < 30, "create too many sftp channel more");
110 
111         // try to set the local file to writable
112         Paths.get(getTestDirectory(), scratchFolder.getName().getBaseName(), fileName).toFile().setWritable(true);
113 
114         fileObjectCopy = scratchFolder.resolveFile(fileName);
115         Assertions.assertTrue(fileObjectCopy.isWriteable());
116         fileObjectCopy.copyFrom(localFileObject, Selectors.SELECT_SELF);
117     }
118 
119 }