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.io;
18  
19  import org.apache.commons.io.testtools.FileBasedTestCase;
20  import org.junit.Assert;
21  import org.junit.Test;
22  
23  import java.io.*;
24  import java.util.Locale;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.fail;
28  
29  /**
30   * This is used to test FileSystemUtils.
31   *
32   * @version $Id: FileSystemUtilsTestCase.java 1718944 2015-12-09 19:50:30Z krosenvold $
33   */
34  public class FileSystemUtilsTestCase extends FileBasedTestCase {
35  
36      //-----------------------------------------------------------------------
37      @Test
38      public void testGetFreeSpace_String() throws Exception {
39          // test coverage, as we can't check value
40          if (File.separatorChar == '/') {
41              // have to figure out unix block size
42              final String[] cmd;
43              String osName = System.getProperty("os.name");
44              osName = osName.toLowerCase(Locale.ENGLISH);
45  
46              if (osName.contains("hp-ux") || osName.contains("aix")) {
47                  cmd = new String[]{"df", "-P", "/"};
48              } else if (osName.contains("sunos") || osName.contains("sun os")
49                      || osName.contains("solaris")) {
50                  cmd = new String[]{"/usr/xpg4/bin/df", "-P", "/"};
51              } else {
52                  cmd = new String[]{"df", "/"};
53              }
54              final Process proc = Runtime.getRuntime().exec(cmd);
55              boolean kilobyteBlock = true;
56              BufferedReader r = null;
57              try {
58                  r = new BufferedReader(new InputStreamReader(proc.getInputStream()));
59                  final String line = r.readLine();
60                  Assert.assertNotNull("Unexpected null line", line);
61                  if (line.contains("512")) {
62                      kilobyteBlock = false;
63                  }
64              } finally {
65                  IOUtils.closeQuietly(r);
66              }
67  
68              // now perform the test
69              @SuppressWarnings("deprecation")
70              final
71              long free = FileSystemUtils.freeSpace("/");
72              final long kb = FileSystemUtils.freeSpaceKb("/");
73              if (kilobyteBlock) {
74                  assertEquals(free, kb, 256d);
75              } else {
76                  assertEquals(free / 2d, kb, 256d);
77              }
78          } else {
79              @SuppressWarnings("deprecation")
80              final
81              long bytes = FileSystemUtils.freeSpace("");
82              final long kb = FileSystemUtils.freeSpaceKb("");
83              assertEquals((double) bytes / 1024, kb, 256d);
84          }
85      }
86  
87      //-----------------------------------------------------------------------
88      @Test
89      public void testGetFreeSpaceOS_String_NullPath() throws Exception {
90          final FileSystemUtils fsu = new FileSystemUtils();
91          try {
92              fsu.freeSpaceOS(null, 1, false, -1);
93              fail();
94          } catch (final IllegalArgumentException ignore) {
95          }
96          try {
97              fsu.freeSpaceOS(null, 1, true, -1);
98              fail();
99          } catch (final IllegalArgumentException ignore) {
100         }
101     }
102 
103     @Test
104     public void testGetFreeSpaceOS_String_InitError() throws Exception {
105         final FileSystemUtils fsu = new FileSystemUtils();
106         try {
107             fsu.freeSpaceOS("", -1, false, -1);
108             fail();
109         } catch (final IllegalStateException ignore) {
110         }
111         try {
112             fsu.freeSpaceOS("", -1, true, -1);
113             fail();
114         } catch (final IllegalStateException ignore) {
115         }
116     }
117 
118     @Test
119     public void testGetFreeSpaceOS_String_Other() throws Exception {
120         final FileSystemUtils fsu = new FileSystemUtils();
121         try {
122             fsu.freeSpaceOS("", 0, false, -1);
123             fail();
124         } catch (final IllegalStateException ignore) {
125         }
126         try {
127             fsu.freeSpaceOS("", 0, true, -1);
128             fail();
129         } catch (final IllegalStateException ignore) {
130         }
131     }
132 
133     @Test
134     public void testGetFreeSpaceOS_String_Windows() throws Exception {
135         final FileSystemUtils fsu = new FileSystemUtils() {
136             @Override
137             protected long freeSpaceWindows(final String path, final long timeout) throws IOException {
138                 return 12345L;
139             }
140         };
141         assertEquals(12345L, fsu.freeSpaceOS("", 1, false, -1));
142         assertEquals(12345L / 1024, fsu.freeSpaceOS("", 1, true, -1));
143     }
144 
145     @Test
146     public void testGetFreeSpaceOS_String_Unix() throws Exception {
147         final FileSystemUtils fsu = new FileSystemUtils() {
148             @Override
149             protected long freeSpaceUnix(final String path, final boolean kb, final boolean posix, final long timeout) throws IOException {
150                 return kb ? 12345L : 54321;
151             }
152         };
153         assertEquals(54321L, fsu.freeSpaceOS("", 2, false, -1));
154         assertEquals(12345L, fsu.freeSpaceOS("", 2, true, -1));
155     }
156 
157     //-----------------------------------------------------------------------
158     @Test
159     public void testGetFreeSpaceWindows_String_ParseCommaFormatBytes() throws Exception {
160         // this is the format of response when calling dir /c
161         // we have now switched to dir /-c, so we should never get this
162         final String lines =
163                 " Volume in drive C is HDD\n" +
164                         " Volume Serial Number is XXXX-YYYY\n" +
165                         "\n" +
166                         " Directory of C:\\Documents and Settings\\Xxxx\n" +
167                         "\n" +
168                         "19/08/2005  22:43    <DIR>          .\n" +
169                         "19/08/2005  22:43    <DIR>          ..\n" +
170                         "11/08/2005  01:07                81 build.properties\n" +
171                         "17/08/2005  21:44    <DIR>          Desktop\n" +
172                         "               7 File(s)        180,260 bytes\n" +
173                         "              10 Dir(s)  41,411,551,232 bytes free";
174         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
175         assertEquals(41411551232L, fsu.freeSpaceWindows("", -1));
176     }
177 
178     //-----------------------------------------------------------------------
179     @Test
180     public void testGetFreeSpaceWindows_String_EmptyPath() throws Exception {
181         final String lines =
182                 " Volume in drive C is HDD\n" +
183                         " Volume Serial Number is XXXX-YYYY\n" +
184                         "\n" +
185                         " Directory of C:\\Documents and Settings\\Xxxx\n" +
186                         "\n" +
187                         "19/08/2005  22:43    <DIR>          .\n" +
188                         "19/08/2005  22:43    <DIR>          ..\n" +
189                         "11/08/2005  01:07                81 build.properties\n" +
190                         "17/08/2005  21:44    <DIR>          Desktop\n" +
191                         "               7 File(s)         180260 bytes\n" +
192                         "              10 Dir(s)     41411551232 bytes free";
193         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines, "dir /a /-c ");
194         assertEquals(41411551232L, fsu.freeSpaceWindows("", -1));
195     }
196 
197     @Test
198     public void testGetFreeSpaceWindows_String_NormalResponse() throws Exception {
199         final String lines =
200                 " Volume in drive C is HDD\n" +
201                         " Volume Serial Number is XXXX-YYYY\n" +
202                         "\n" +
203                         " Directory of C:\\Documents and Settings\\Xxxx\n" +
204                         "\n" +
205                         "19/08/2005  22:43    <DIR>          .\n" +
206                         "19/08/2005  22:43    <DIR>          ..\n" +
207                         "11/08/2005  01:07                81 build.properties\n" +
208                         "17/08/2005  21:44    <DIR>          Desktop\n" +
209                         "               7 File(s)         180260 bytes\n" +
210                         "              10 Dir(s)     41411551232 bytes free";
211         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines, "dir /a /-c \"C:\"");
212         assertEquals(41411551232L, fsu.freeSpaceWindows("C:", -1));
213     }
214 
215     @Test
216     public void testGetFreeSpaceWindows_String_StripDrive() throws Exception {
217         final String lines =
218                 " Volume in drive C is HDD\n" +
219                         " Volume Serial Number is XXXX-YYYY\n" +
220                         "\n" +
221                         " Directory of C:\\Documents and Settings\\Xxxx\n" +
222                         "\n" +
223                         "19/08/2005  22:43    <DIR>          .\n" +
224                         "19/08/2005  22:43    <DIR>          ..\n" +
225                         "11/08/2005  01:07                81 build.properties\n" +
226                         "17/08/2005  21:44    <DIR>          Desktop\n" +
227                         "               7 File(s)         180260 bytes\n" +
228                         "              10 Dir(s)     41411551232 bytes free";
229         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines, "dir /a /-c \"C:\\somedir\"");
230         assertEquals(41411551232L, fsu.freeSpaceWindows("C:\\somedir", -1));
231     }
232 
233     @Test
234     public void testGetFreeSpaceWindows_String_quoted() throws Exception {
235         final String lines =
236                 " Volume in drive C is HDD\n" +
237                         " Volume Serial Number is XXXX-YYYY\n" +
238                         "\n" +
239                         " Directory of C:\\Documents and Settings\\Xxxx\n" +
240                         "\n" +
241                         "19/08/2005  22:43    <DIR>          .\n" +
242                         "19/08/2005  22:43    <DIR>          ..\n" +
243                         "11/08/2005  01:07                81 build.properties\n" +
244                         "17/08/2005  21:44    <DIR>          Desktop\n" +
245                         "               7 File(s)         180260 bytes\n" +
246                         "              10 Dir(s)     41411551232 bytes free";
247         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines, "dir /a /-c \"C:\\somedir\"");
248         assertEquals(41411551232L, fsu.freeSpaceWindows("\"C:\\somedir\"", -1));
249     }
250 
251     @Test
252     public void testGetFreeSpaceWindows_String_EmptyResponse() throws Exception {
253         final String lines = "";
254         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
255         try {
256             fsu.freeSpaceWindows("C:", -1);
257             fail();
258         } catch (final IOException ignore) {
259         }
260     }
261 
262     @Test(expected = IOException.class)
263     public void testGetFreeSpaceWindows_String_EmptyMultiLineResponse() throws Exception {
264         final String lines = "\n\n";
265         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
266         fsu.freeSpaceWindows("C:", -1);
267     }
268 
269     @Test(expected = IOException.class)
270     public void testGetFreeSpaceWindows_String_InvalidTextResponse() throws Exception {
271         final String lines = "BlueScreenOfDeath";
272         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
273         fsu.freeSpaceWindows("C:", -1);
274     }
275 
276     @Test(expected = IOException.class)
277     public void testGetFreeSpaceWindows_String_NoSuchDirectoryResponse() throws Exception {
278         final String lines =
279                 " Volume in drive C is HDD\n" +
280                         " Volume Serial Number is XXXX-YYYY\n" +
281                         "\n" +
282                         " Directory of C:\\Documents and Settings\\empty" +
283                         "\n";
284         final FileSystemUtils fsu = new MockFileSystemUtils(1, lines);
285         fsu.freeSpaceWindows("C:", -1);
286     }
287 
288     //-----------------------------------------------------------------------
289     @Test
290     public void testGetFreeSpaceUnix_String_EmptyPath() throws Exception {
291         final String lines =
292                 "Filesystem           1K-blocks      Used Available Use% Mounted on\n" +
293                         "xxx:/home/users/s     14428928  12956424   1472504  90% /home/users/s";
294         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
295         try {
296             fsu.freeSpaceUnix("", false, false, -1);
297             fail();
298         } catch (final IllegalArgumentException ignore) {
299         }
300         try {
301             fsu.freeSpaceUnix("", true, false, -1);
302             fail();
303         } catch (final IllegalArgumentException ignore) {
304         }
305         try {
306             fsu.freeSpaceUnix("", true, true, -1);
307             fail();
308         } catch (final IllegalArgumentException ignore) {
309         }
310         try {
311             fsu.freeSpaceUnix("", false, true, -1);
312             fail();
313         } catch (final IllegalArgumentException ignore) {
314         }
315 
316     }
317 
318     @Test
319     public void testGetFreeSpaceUnix_String_NormalResponseLinux() throws Exception {
320         // from Sourceforge 'GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu)'
321         final String lines =
322                 "Filesystem           1K-blocks      Used Available Use% Mounted on\n" +
323                         "/dev/xxx                497944    308528    189416  62% /";
324         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
325         assertEquals(189416L, fsu.freeSpaceUnix("/", false, false, -1));
326     }
327 
328     @Test
329     public void testGetFreeSpaceUnix_String_NormalResponseFreeBSD() throws Exception {
330         // from Apache 'FreeBSD 6.1-RELEASE (SMP-turbo)'
331         final String lines =
332                 "Filesystem  1K-blocks      Used    Avail Capacity  Mounted on\n" +
333                         "/dev/xxxxxx    128990    102902    15770    87%    /";
334         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
335         assertEquals(15770L, fsu.freeSpaceUnix("/", false, false, -1));
336     }
337 
338     //-----------------------------------------------------------------------
339     @Test
340     public void testGetFreeSpaceUnix_String_NormalResponseKbLinux() throws Exception {
341         // from Sourceforge 'GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu)'
342         // df, df -k and df -kP are all identical
343         final String lines =
344                 "Filesystem           1K-blocks      Used Available Use% Mounted on\n" +
345                         "/dev/xxx                497944    308528    189416  62% /";
346         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
347         assertEquals(189416L, fsu.freeSpaceUnix("/", true, false, -1));
348     }
349 
350     @Test
351     public void testGetFreeSpaceUnix_String_NormalResponseKbFreeBSD() throws Exception {
352         // from Apache 'FreeBSD 6.1-RELEASE (SMP-turbo)'
353         // df and df -k are identical, but df -kP uses 512 blocks (not relevant as not used)
354         final String lines =
355                 "Filesystem  1K-blocks      Used    Avail Capacity  Mounted on\n" +
356                         "/dev/xxxxxx    128990    102902    15770    87%    /";
357         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
358         assertEquals(15770L, fsu.freeSpaceUnix("/", true, false, -1));
359     }
360 
361     @Test
362     public void testGetFreeSpaceUnix_String_NormalResponseKbSolaris() throws Exception {
363         // from IO-91 - ' SunOS et 5.10 Generic_118822-25 sun4u sparc SUNW,Ultra-4'
364         // non-kb response does not contain free space - see IO-91
365         final String lines =
366                 "Filesystem            kbytes    used   avail capacity  Mounted on\n" +
367                         "/dev/dsk/x0x0x0x0    1350955  815754  481163    63%";
368         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
369         assertEquals(481163L, fsu.freeSpaceUnix("/dev/dsk/x0x0x0x0", true, false, -1));
370     }
371 
372     @Test
373     public void testGetFreeSpaceUnix_String_LongResponse() throws Exception {
374         final String lines =
375                 "Filesystem           1K-blocks      Used Available Use% Mounted on\n" +
376                         "xxx-yyyyyyy-zzz:/home/users/s\n" +
377                         "                      14428928  12956424   1472504  90% /home/users/s";
378         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
379         assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", false, false, -1));
380     }
381 
382     @Test
383     public void testGetFreeSpaceUnix_String_LongResponseKb() throws Exception {
384         final String lines =
385                 "Filesystem           1K-blocks      Used Available Use% Mounted on\n" +
386                         "xxx-yyyyyyy-zzz:/home/users/s\n" +
387                         "                      14428928  12956424   1472504  90% /home/users/s";
388         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
389         assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", true, false, -1));
390     }
391     @Test
392 
393     public void testGetFreeSpaceUnix_String_EmptyResponse() throws Exception {
394         final String lines = "";
395         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
396         try {
397             fsu.freeSpaceUnix("/home/users/s", false, false, -1);
398             fail();
399         } catch (final IOException ignore) {
400         }
401         try {
402             fsu.freeSpaceUnix("/home/users/s", true, false, -1);
403             fail();
404         } catch (final IOException ignore) {
405         }
406         try {
407             fsu.freeSpaceUnix("/home/users/s", false, true, -1);
408             fail();
409         } catch (final IOException ignore) {
410         }
411         try {
412             fsu.freeSpaceUnix("/home/users/s", true, true, -1);
413             fail();
414         } catch (final IOException ignore) {
415         }
416     }
417 
418     @Test
419     public void testGetFreeSpaceUnix_String_InvalidResponse1() throws Exception {
420         final String lines =
421                 "Filesystem           1K-blocks      Used Available Use% Mounted on\n" +
422                         "                      14428928  12956424       100";
423         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
424         try {
425             fsu.freeSpaceUnix("/home/users/s", false, false, -1);
426             fail();
427         } catch (final IOException ignore) {
428         }
429         try {
430             fsu.freeSpaceUnix("/home/users/s", true, false, -1);
431             fail();
432         } catch (final IOException ignore) {
433         }
434         try {
435             fsu.freeSpaceUnix("/home/users/s", false, true, -1);
436             fail();
437         } catch (final IOException ignore) {
438         }
439         try {
440             fsu.freeSpaceUnix("/home/users/s", true, true, -1);
441             fail();
442         } catch (final IOException ignore) {
443         }
444     }
445 
446     @Test
447     public void testGetFreeSpaceUnix_String_InvalidResponse2() throws Exception {
448         final String lines =
449                 "Filesystem           1K-blocks      Used Available Use% Mounted on\n" +
450                         "xxx:/home/users/s     14428928  12956424   nnnnnnn  90% /home/users/s";
451         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
452         try {
453             fsu.freeSpaceUnix("/home/users/s", false, false, -1);
454             fail();
455         } catch (final IOException ignore) {
456         }
457         try {
458             fsu.freeSpaceUnix("/home/users/s", true, false, -1);
459             fail();
460         } catch (final IOException ignore) {
461         }
462         try {
463             fsu.freeSpaceUnix("/home/users/s", false, true, -1);
464             fail();
465         } catch (final IOException ignore) {
466         }
467         try {
468             fsu.freeSpaceUnix("/home/users/s", true, true, -1);
469             fail();
470         } catch (final IOException ignore) {
471         }
472     }
473 
474     @Test
475     public void testGetFreeSpaceUnix_String_InvalidResponse3() throws Exception {
476         final String lines =
477                 "Filesystem           1K-blocks      Used Available Use% Mounted on\n" +
478                         "xxx:/home/users/s     14428928  12956424        -1  90% /home/users/s";
479         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
480         try {
481             fsu.freeSpaceUnix("/home/users/s", false, false, -1);
482             fail();
483         } catch (final IOException ignore) {
484         }
485         try {
486             fsu.freeSpaceUnix("/home/users/s", true, false, -1);
487             fail();
488         } catch (final IOException ignore) {
489         }
490         try {
491             fsu.freeSpaceUnix("/home/users/s", false, true, -1);
492             fail();
493         } catch (final IOException ignore) {
494         }
495         try {
496             fsu.freeSpaceUnix("/home/users/s", true, true, -1);
497             fail();
498         } catch (final IOException ignore) {
499         }
500     }
501 
502     @Test
503     public void testGetFreeSpaceUnix_String_InvalidResponse4() throws Exception {
504         final String lines =
505                 "Filesystem           1K-blocks      Used Available Use% Mounted on\n" +
506                         "xxx-yyyyyyy-zzz:/home/users/s";
507         final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
508         try {
509             fsu.freeSpaceUnix("/home/users/s", false, false, -1);
510             fail();
511         } catch (final IOException ignore) {
512         }
513         try {
514             fsu.freeSpaceUnix("/home/users/s", true, false, -1);
515             fail();
516         } catch (final IOException ignore) {
517         }
518         try {
519             fsu.freeSpaceUnix("/home/users/s", false, true, -1);
520             fail();
521         } catch (final IOException ignore) {
522         }
523         try {
524             fsu.freeSpaceUnix("/home/users/s", true, true, -1);
525             fail();
526         } catch (final IOException ignore) {
527         }
528     }
529 
530     //-----------------------------------------------------------------------
531     static class MockFileSystemUtils extends FileSystemUtils {
532         private final int exitCode;
533         private final byte[] bytes;
534         private final String cmd;
535 
536         public MockFileSystemUtils(final int exitCode, final String lines) {
537             this(exitCode, lines, null);
538         }
539 
540         public MockFileSystemUtils(final int exitCode, final String lines, final String cmd) {
541             this.exitCode = exitCode;
542             this.bytes = lines.getBytes();
543             this.cmd = cmd;
544         }
545 
546         @Override
547         Process openProcess(final String[] params) {
548             if (cmd != null) {
549                 assertEquals(cmd, params[params.length - 1]);
550             }
551             return new Process() {
552                 @Override
553                 public InputStream getErrorStream() {
554                     return null;
555                 }
556 
557                 @Override
558                 public InputStream getInputStream() {
559                     return new ByteArrayInputStream(bytes);
560                 }
561 
562                 @Override
563                 public OutputStream getOutputStream() {
564                     return null;
565                 }
566 
567                 @Override
568                 public int waitFor() throws InterruptedException {
569                     return exitCode;
570                 }
571 
572                 @Override
573                 public int exitValue() {
574                     return exitCode;
575                 }
576 
577                 @Override
578                 public void destroy() {
579                 }
580             };
581         }
582     }
583 
584 }