xpcom/tests/unit/test_localfile.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 const Cr = Components.results;
michael@0 8 const CC = Components.Constructor;
michael@0 9 const Ci = Components.interfaces;
michael@0 10
michael@0 11 const MAX_TIME_DIFFERENCE = 2500;
michael@0 12 const MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
michael@0 13
michael@0 14 var LocalFile = CC("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath");
michael@0 15
michael@0 16 function run_test()
michael@0 17 {
michael@0 18 test_toplevel_parent_is_null();
michael@0 19 test_normalize_crash_if_media_missing();
michael@0 20 test_file_modification_time();
michael@0 21 test_directory_modification_time();
michael@0 22 test_diskSpaceAvailable();
michael@0 23 }
michael@0 24
michael@0 25 function test_toplevel_parent_is_null()
michael@0 26 {
michael@0 27 try
michael@0 28 {
michael@0 29 var lf = new LocalFile("C:\\");
michael@0 30
michael@0 31 // not required by API, but a property on which the implementation of
michael@0 32 // parent == null relies for correctness
michael@0 33 do_check_true(lf.path.length == 2);
michael@0 34
michael@0 35 do_check_true(lf.parent === null);
michael@0 36 }
michael@0 37 catch (e)
michael@0 38 {
michael@0 39 // not Windows
michael@0 40 do_check_eq(e.result, Cr.NS_ERROR_FILE_UNRECOGNIZED_PATH);
michael@0 41 }
michael@0 42 }
michael@0 43
michael@0 44 function test_normalize_crash_if_media_missing()
michael@0 45 {
michael@0 46 const a="a".charCodeAt(0);
michael@0 47 const z="z".charCodeAt(0);
michael@0 48 for (var i = a; i <= z; ++i)
michael@0 49 {
michael@0 50 try
michael@0 51 {
michael@0 52 LocalFile(String.fromCharCode(i)+":.\\test").normalize();
michael@0 53 }
michael@0 54 catch (e)
michael@0 55 {
michael@0 56 }
michael@0 57 }
michael@0 58 }
michael@0 59
michael@0 60 // Tests that changing a file's modification time is possible
michael@0 61 function test_file_modification_time()
michael@0 62 {
michael@0 63 var file = do_get_profile();
michael@0 64 file.append("testfile");
michael@0 65
michael@0 66 // Should never happen but get rid of it anyway
michael@0 67 if (file.exists())
michael@0 68 file.remove(true);
michael@0 69
michael@0 70 var now = Date.now();
michael@0 71 file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0644);
michael@0 72 do_check_true(file.exists());
michael@0 73
michael@0 74 // Modification time may be out by up to 2 seconds on FAT filesystems. Test
michael@0 75 // with a bit of leeway, close enough probably means it is correct.
michael@0 76 var diff = Math.abs(file.lastModifiedTime - now);
michael@0 77 do_check_true(diff < MAX_TIME_DIFFERENCE);
michael@0 78
michael@0 79 var yesterday = now - MILLIS_PER_DAY;
michael@0 80 file.lastModifiedTime = yesterday;
michael@0 81
michael@0 82 diff = Math.abs(file.lastModifiedTime - yesterday);
michael@0 83 do_check_true(diff < MAX_TIME_DIFFERENCE);
michael@0 84
michael@0 85 var tomorrow = now - MILLIS_PER_DAY;
michael@0 86 file.lastModifiedTime = tomorrow;
michael@0 87
michael@0 88 diff = Math.abs(file.lastModifiedTime - tomorrow);
michael@0 89 do_check_true(diff < MAX_TIME_DIFFERENCE);
michael@0 90
michael@0 91 var bug377307 = 1172950238000;
michael@0 92 file.lastModifiedTime = bug377307;
michael@0 93
michael@0 94 diff = Math.abs(file.lastModifiedTime - bug377307);
michael@0 95 do_check_true(diff < MAX_TIME_DIFFERENCE);
michael@0 96
michael@0 97 file.remove(true);
michael@0 98 }
michael@0 99
michael@0 100 // Tests that changing a directory's modification time is possible
michael@0 101 function test_directory_modification_time()
michael@0 102 {
michael@0 103 var dir = do_get_profile();
michael@0 104 dir.append("testdir");
michael@0 105
michael@0 106 // Should never happen but get rid of it anyway
michael@0 107 if (dir.exists())
michael@0 108 dir.remove(true);
michael@0 109
michael@0 110 var now = Date.now();
michael@0 111 dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
michael@0 112 do_check_true(dir.exists());
michael@0 113
michael@0 114 // Modification time may be out by up to 2 seconds on FAT filesystems. Test
michael@0 115 // with a bit of leeway, close enough probably means it is correct.
michael@0 116 var diff = Math.abs(dir.lastModifiedTime - now);
michael@0 117 do_check_true(diff < MAX_TIME_DIFFERENCE);
michael@0 118
michael@0 119 var yesterday = now - MILLIS_PER_DAY;
michael@0 120 dir.lastModifiedTime = yesterday;
michael@0 121
michael@0 122 diff = Math.abs(dir.lastModifiedTime - yesterday);
michael@0 123 do_check_true(diff < MAX_TIME_DIFFERENCE);
michael@0 124
michael@0 125 var tomorrow = now - MILLIS_PER_DAY;
michael@0 126 dir.lastModifiedTime = tomorrow;
michael@0 127
michael@0 128 diff = Math.abs(dir.lastModifiedTime - tomorrow);
michael@0 129 do_check_true(diff < MAX_TIME_DIFFERENCE);
michael@0 130
michael@0 131 dir.remove(true);
michael@0 132 }
michael@0 133
michael@0 134 function test_diskSpaceAvailable()
michael@0 135 {
michael@0 136 let file = do_get_profile();
michael@0 137 file.QueryInterface(Ci.nsILocalFile);
michael@0 138
michael@0 139 let bytes = file.diskSpaceAvailable;
michael@0 140 do_check_true(bytes > 0);
michael@0 141
michael@0 142 file.append("testfile");
michael@0 143 if (file.exists())
michael@0 144 file.remove(true);
michael@0 145 file.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("644", 8));
michael@0 146
michael@0 147 bytes = file.diskSpaceAvailable;
michael@0 148 do_check_true(bytes > 0);
michael@0 149
michael@0 150 file.remove(true);
michael@0 151 }

mercurial