• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 package org.apache.harmony.luni.tests.java.io;
19 
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 
27 import tests.support.resource.Support_Resources;
28 
29 import junit.framework.TestCase;
30 
31 import libcore.io.Libcore;
32 import android.system.StructStatVfs;
33 
34 
35 /**
36  * Please note that this case can only be passed on Linux due to some file
37  * system dependent reason.
38  */
39 public class UnixFileTest extends TestCase {
40     private boolean root = false;
41 
42     private File testFile;
43 
44     private File testDir;
45 
46     private static final int TOTAL_SPACE_NUM = 0;
47 
48     private static final int FREE_SPACE_NUM = 1;
49 
50     private static final int USABLE_SPACE_NUM = 2;
51 
52     private static class ConsoleResulter extends Thread {
53         Process proc;
54 
55         InputStream is;
56 
57         String resStr;
58 
ConsoleResulter(Process p, InputStream in)59         ConsoleResulter(Process p, InputStream in) {
60             proc = p;
61             is = in;
62         }
63 
64         @Override
run()65         public void run() {
66             StringBuffer result = new StringBuffer();
67             synchronized (result) {
68                 try {
69                     BufferedReader br = new BufferedReader(
70                             new InputStreamReader(is));
71                     String line;
72                     while ((line = br.readLine()) != null) {
73                         result.append(line);
74                     }
75                     if (result.length() != 0) {
76                         resStr = result.toString();
77                     }
78 
79                     br.close();
80                 } catch (IOException ioe) {
81                     result = null;
82                 }
83                 synchronized (proc) {
84                     proc.notifyAll();
85                 }
86             }
87         }
88     }
89 
getLinuxSpace(int index, File file)90     private static long getLinuxSpace(int index, File file) throws Exception {
91         StructStatVfs stat = Libcore.os.statvfs(file.getAbsolutePath());
92         switch (index) {
93             case TOTAL_SPACE_NUM:
94                 return stat.f_frsize * stat.f_blocks;
95             case FREE_SPACE_NUM:
96                 return stat.f_frsize * stat.f_bfree;
97             case USABLE_SPACE_NUM:
98                 return stat.f_frsize * stat.f_bavail;
99         }
100         throw new IllegalArgumentException("Unknown index: " + index);
101     }
102 
103     /**
104      * @tests java.io.File#canExecute()
105      * @since 1.6
106      */
test_canExecute()107     public void test_canExecute() {
108         assertFalse(testFile.canExecute());
109         assertTrue(testFile.setExecutable(true, false));
110         assertTrue(testFile.canExecute());
111         assertTrue(testFile.setExecutable(true, true));
112         assertTrue(testFile.canExecute());
113 
114         assertTrue(testFile.setExecutable(false, false));
115         assertFalse(testFile.canExecute());
116         assertTrue(testFile.setExecutable(false, true));
117         assertFalse(testFile.canExecute());
118 
119         assertTrue(testFile.setExecutable(true, false));
120         assertTrue(testFile.canExecute());
121 
122         // tests directory
123         assertTrue(testDir.canExecute());
124         assertTrue(testDir.setExecutable(false, true));
125         if (root) {
126             assertTrue(testDir.canExecute());
127         } else {
128             assertFalse(testDir.canExecute());
129         }
130         assertTrue(testDir.setExecutable(true, false));
131         assertTrue(testDir.canExecute());
132     }
133 
134     /**
135      * @tests java.io.File#getFreeSpace()
136      * @since 1.6
137      */
test_getFreeSpace()138     public void test_getFreeSpace() throws Exception {
139         long fileSpace = getLinuxSpace(FREE_SPACE_NUM, testFile);
140         long dirSpace = getLinuxSpace(FREE_SPACE_NUM, testDir);
141         // in case we cannot fetch the value from command line
142         if (fileSpace > 0) {
143             assertEquals(fileSpace, testFile.getFreeSpace());
144         }
145 
146         if (dirSpace > 0) {
147             assertEquals(dirSpace, testDir.getFreeSpace());
148         }
149     }
150 
151     /**
152      * @tests java.io.File#getTotalSpace()
153      * @since 1.6
154      */
test_getTotalSpace()155     public void test_getTotalSpace() throws Exception {
156         long fileSpace = getLinuxSpace(TOTAL_SPACE_NUM, testFile);
157         long dirSpace = getLinuxSpace(TOTAL_SPACE_NUM, testDir);
158         if (fileSpace > 0) {
159             assertEquals(fileSpace, testFile.getTotalSpace());
160         }
161         if (dirSpace > 0) {
162             assertEquals(dirSpace, testDir.getTotalSpace());
163         }
164     }
165 
166     /**
167      * @tests java.io.File#getUsableSpace()
168      * @since 1.6
169      */
test_getUsableSpace()170     public void test_getUsableSpace() throws Exception {
171         long fileSpace = getLinuxSpace(USABLE_SPACE_NUM, testFile);
172         long dirSpace = getLinuxSpace(USABLE_SPACE_NUM, testDir);
173         if (fileSpace > 0) {
174             assertEquals(fileSpace, testFile.getUsableSpace());
175         }
176         if (dirSpace > 0) {
177             assertEquals(dirSpace, testDir.getUsableSpace());
178         }
179     }
180 
181     /**
182      * @tests java.io.File#setExecutable(boolean, boolean)
183      * @since 1.6
184      */
test_setExecutableZZ()185     public void test_setExecutableZZ() {
186         // setExecutable(true, true/false)
187         assertFalse(testFile.canExecute());
188         assertTrue(testFile.setExecutable(true, false));
189         assertTrue(testFile.canExecute());
190         assertTrue(testFile.setExecutable(true, true));
191         assertTrue(testFile.canExecute());
192 
193         // setExecutable(false, true/false)
194         assertTrue(testFile.setExecutable(false, true));
195         if (root) {
196             assertTrue(testFile.canExecute());
197         } else {
198             assertFalse(testFile.canExecute());
199         }
200         assertTrue(testFile.setExecutable(false, false));
201         assertFalse(testFile.canExecute());
202 
203         // tests directory
204         assertTrue(testDir.canExecute());
205         assertTrue(testDir.setExecutable(false, true));
206         if (root) {
207             assertTrue(testDir.canExecute());
208         } else {
209             assertFalse(testDir.canExecute());
210         }
211         assertTrue(testDir.setExecutable(false, false));
212         if (root) {
213             assertTrue(testDir.canExecute());
214         } else {
215             assertFalse(testDir.canExecute());
216         }
217 
218         assertTrue(testDir.setExecutable(true, true));
219         assertTrue(testDir.canExecute());
220         assertTrue(testDir.setExecutable(true, false));
221         assertTrue(testDir.canExecute());
222     }
223 
224     /**
225      * @tests java.io.File#setExecutable(boolean)
226      * @since 1.6
227      */
test_setExecutableZ()228     public void test_setExecutableZ() {
229         // So far this method only deals with the situation that the user is the
230         // owner of the file
231         assertTrue(testFile.setExecutable(true));
232         assertTrue(testFile.canExecute());
233         assertTrue(testFile.setExecutable(false));
234         assertFalse(testFile.canExecute());
235         assertTrue(testFile.setExecutable(true));
236 
237         // tests directory
238         assertTrue(testDir.canExecute());
239         assertTrue(testDir.setExecutable(false));
240         if (root) {
241             assertTrue(testDir.canExecute());
242         } else {
243             assertFalse(testDir.canExecute());
244         }
245         assertTrue(testDir.setExecutable(true));
246         assertTrue(testDir.canExecute());
247     }
248 
249     /**
250      * @tests java.io.File#setReadable(boolean, boolean)
251      * @since 1.6
252      */
test_setReadableZZ()253     public void test_setReadableZZ() throws Exception {
254         // setReadable(false, false/true) succeeds on Linux
255         // However, canRead() always returns true when the user is 'root'.
256         assertTrue(testFile.canRead());
257         assertTrue(testFile.setReadable(false, false));
258         if (root) {
259             assertTrue(testFile.canRead());
260         } else {
261             assertFalse(testFile.canRead());
262         }
263         assertTrue(testFile.setReadable(false, true));
264         if (root) {
265             assertTrue(testFile.canRead());
266         } else {
267             assertFalse(testFile.canRead());
268         }
269 
270         // tests directory, setReadable(false, true/false)
271         assertTrue(testDir.canRead());
272         assertTrue(testDir.setReadable(false, true));
273         if (root) {
274             assertTrue(testDir.canRead());
275         } else {
276             assertFalse(testDir.canRead());
277         }
278         assertTrue(testDir.setReadable(false, false));
279         if (root) {
280             assertTrue(testDir.canRead());
281         } else {
282             assertFalse(testDir.canRead());
283         }
284 
285         // setReadable(true, false/true) and set them in turn
286         assertTrue(testFile.setReadable(true, false));
287         assertTrue(testFile.canRead());
288         assertTrue(testFile.setReadable(false, true));
289         if (root) {
290             assertTrue(testFile.canRead());
291         } else {
292             assertFalse(testFile.canRead());
293         }
294         assertTrue(testFile.setReadable(true, true));
295         assertTrue(testFile.canRead());
296         assertTrue(testFile.setReadable(false, true));
297         if (root) {
298             assertTrue(testFile.canRead());
299         } else {
300             assertFalse(testFile.canRead());
301         }
302 
303         // tests directory, setReadable(true, true/false)
304         assertTrue(testDir.setReadable(true, false));
305         assertTrue(testDir.canRead());
306         assertTrue(testDir.setReadable(true, true));
307         assertTrue(testDir.canRead());
308     }
309 
310     /**
311      * @tests java.io.File#setReadable(boolean)
312      * @since 1.6
313      */
test_setReadableZ()314     public void test_setReadableZ() {
315         // So far this method only deals with the situation that the user is the
316         // owner of the file. setReadable(false) succeeds on Linux
317         // However, canRead() always returns true when the user is 'root'.
318         assertTrue(testFile.canRead());
319         assertTrue(testFile.setReadable(false));
320         if (root) {
321             assertTrue(testFile.canRead());
322         } else {
323             assertFalse(testFile.canRead());
324         }
325         assertTrue(testFile.setReadable(true));
326         assertTrue(testFile.canRead());
327 
328         assertTrue(testDir.canRead());
329         assertTrue(testDir.setReadable(false));
330         if (root) {
331             assertTrue(testDir.canRead());
332         } else {
333             assertFalse(testDir.canRead());
334         }
335     }
336 
337     @Override
setUp()338     protected void setUp() throws Exception {
339         super.setUp();
340         testFile = File.createTempFile("testfile", null);
341         testDir = new File(System.getProperty("java.io.tmpdir") + "/temp");
342         if (!testDir.exists()) {
343             testDir.mkdir();
344         }
345         root = false; //System.getProperty("user.name").equals("root");
346     }
347 
348     @Override
tearDown()349     protected void tearDown() throws Exception {
350         testFile.delete();
351         testDir.delete();
352         testFile = null;
353         testDir = null;
354         root = false;
355         super.tearDown();
356     }
357 
test_getCanonicalPath()358     public void test_getCanonicalPath() throws Exception {
359 
360         File tempFolder = Support_Resources.createTempFolder();
361         File tmpFolder1 = new File(tempFolder, "folder1");
362         tmpFolder1.deleteOnExit();
363 
364         File tmpFolder2 = new File(tmpFolder1.toString() + "/folder2");
365         tmpFolder2.mkdirs();
366         tmpFolder2.deleteOnExit();
367 
368         File tmpFolder3 = new File(tmpFolder2.toString() + "/folder3");
369         tmpFolder3.mkdirs();
370         tmpFolder3.deleteOnExit();
371 
372         File tmpFolder4 = new File(tmpFolder3.toString() + "/folder4");
373         tmpFolder4.mkdirs();
374         tmpFolder4.deleteOnExit();
375 
376         // make a link to folder1/folder2
377         String tempFolderAbsolutePath = tempFolder.getAbsolutePath();
378         String target = tempFolderAbsolutePath + "/folder1/folder2";
379         String linkName = tempFolderAbsolutePath + "/folder2";
380         Libcore.os.symlink(target, linkName);
381         File linkFile = new File(tempFolder, "folder2");
382         linkFile.deleteOnExit();
383 
384         File file = new File(tempFolder, "folder2");
385         assertEquals(tmpFolder2.getCanonicalPath(), file.getCanonicalPath());
386 
387         file = new File(tempFolder, "folder1/folder2");
388         assertEquals(tmpFolder2.getCanonicalPath(), file.getCanonicalPath());
389 
390         file = new File(tempFolder, "folder2/folder3");
391         assertEquals(tmpFolder3.getCanonicalPath(), file.getCanonicalPath());
392 
393         file = new File(tempFolder, "folder2/folder3/folder4");
394         assertEquals(tmpFolder4.getCanonicalPath(), file.getCanonicalPath());
395 
396         file = new File(tempFolder, "folder1/folder2/folder3");
397         assertEquals(tmpFolder3.getCanonicalPath(), file.getCanonicalPath());
398 
399         file = new File(tempFolder, "folder1/folder2/folder3/folder4");
400         assertEquals(tmpFolder4.getCanonicalPath(), file.getCanonicalPath());
401     }
402 }
403