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;
18  
19  import java.io.OutputStream;
20  import java.nio.charset.StandardCharsets;
21  
22  import org.apache.commons.lang3.SystemUtils;
23  import org.apache.commons.vfs2.provider.local.LocalFileSystem;
24  import org.junit.Test;
25  import org.junit.jupiter.api.Assertions;
26  
27  /**
28   * Additional file permission tests.
29   *
30   * Used by Local and SFTP File System.
31   */
32  public class PermissionsTests extends AbstractProviderTestCase {
33  
34      public static final String FILENAME = "permission.txt";
35  
36      private FileObject createTestFile() throws Exception {
37          // Get the scratch folder
38          final FileObject scratchFolder = getWriteFolder();
39          assertNotNull(scratchFolder);
40  
41          // Make sure the test folder is empty
42          scratchFolder.delete(Selectors.EXCLUDE_SELF);
43          scratchFolder.createFolder();
44  
45          // Create direct child of the test folder
46          final FileObject file = scratchFolder.resolveFile(FILENAME);
47          assertFalse(file.exists());
48  
49          // Create the source file
50          final String content = "Here is some sample content for the file.  Blah Blah Blah.";
51  
52          try (OutputStream os = file.getContent().getOutputStream()) {
53              os.write(content.getBytes(StandardCharsets.UTF_8));
54          }
55          return file;
56      }
57  
58      /**
59       * Returns the capabilities required by the tests of this test case.
60       */
61      @Override
62      protected Capability[] getRequiredCapabilities() {
63          return new Capability[] { Capability.CREATE, Capability.DELETE, Capability.READ_CONTENT,
64                  Capability.WRITE_CONTENT, };
65      }
66  
67      /**
68       * Returns true if the file system is a LocalFileSystem on Windows
69       */
70      private boolean isWindows() {
71          return SystemUtils.IS_OS_WINDOWS && getFileSystem() instanceof LocalFileSystem;
72      }
73  
74      /**
75       * Clean up the permission-modified file to not affect other tests.
76       */
77      @Override
78      protected void tearDown() throws Exception {
79          final FileObject scratchFolder = getWriteFolder();
80          final FileObject file = scratchFolder.resolveFile(FILENAME);
81          file.setWritable(true, true);
82          file.delete();
83  
84          super.tearDown();
85      }
86  
87      /**
88       * Tests for the execution permission.
89       */
90      @Test
91      public void testExecutable() throws Exception {
92          final FileObject file = createTestFile();
93  
94          // On Windows, all files are executable
95          if (isWindows()) {
96              Assertions.assertTrue(file.isExecutable(), "File expected to be executable: " + file);
97  
98          } else {
99              // Set the executable flag for owner
100             Assertions.assertTrue(file.setExecutable(true, true), "Setting executable permission failed: " + file);
101             Assertions.assertTrue(file.isExecutable(), "File expected to be executable: " + file);
102 
103             // Set the executable flag for all
104             Assertions.assertTrue(file.setExecutable(true, false), "Setting executable permission failed: " + file);
105             Assertions.assertTrue(file.isExecutable(), "File expected to be executable: " + file);
106 
107             // Clear the executable flag
108             Assertions.assertTrue(file.setExecutable(false, true), "Setting executable permission failed: " + file);
109             Assertions.assertFalse(file.isExecutable(), "File expected to be not executable: " + file);
110         }
111     }
112 
113     /**
114      * Tests for the readable permission.
115      */
116     @Test
117     public void testReadable() throws Exception {
118         final FileObject file = createTestFile();
119 
120         if (isWindows()) {
121             // On Windows, all owned files are readable
122             Assertions.assertTrue(file.isReadable(), "File expected to be readable: " + file);
123         } else {
124             // Set the readable permission for owner
125             Assertions.assertTrue(file.setReadable(true, true), "Setting read permission failed: " + file);
126             Assertions.assertTrue(file.isReadable(), "File expected to be readable: " + file);
127 
128             // Set the readable permission for all
129             Assertions.assertTrue(file.setReadable(true, false), "Setting read permission failed: " + file);
130             Assertions.assertTrue(file.isReadable(), "File expected to be readable: " + file);
131 
132             // Clear the readable permission
133             Assertions.assertTrue(file.setReadable(false, true), "Setting read permission failed: " + file);
134             Assertions.assertFalse(file.isReadable(), "File expected to be not readable: " + file);
135         }
136     }
137 
138     /**
139      * Tests for the writable permission.
140      */
141     @Test
142     public void testWriteable() throws Exception {
143         final FileObject file = createTestFile();
144 
145         // Set the write permission for owner
146         Assertions.assertTrue(file.setWritable(true, true), "Setting write permission failed: " + file);
147         Assertions.assertTrue(file.isWriteable(), "File expected to be writable: " + file);
148 
149         // Set the write permission for all
150         Assertions.assertTrue(file.setWritable(true, false), "Setting write permission failed: " + file);
151         Assertions.assertTrue(file.isWriteable(), "File expected to be writable: " + file);
152 
153         // Clear the write permission
154         Assertions.assertTrue(file.setWritable(false, true), "Setting write permission failed: " + file);
155         Assertions.assertFalse(file.isWriteable(), "File expected to be not writable: " + file);
156     }
157 
158 }