toolkit/components/osfile/tests/xpcshell/test_osfile_async_largefiles.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 "use strict";
michael@0 5
michael@0 6 Components.utils.import("resource://gre/modules/ctypes.jsm");
michael@0 7 Components.utils.import("resource://gre/modules/osfile.jsm");
michael@0 8 Components.utils.import("resource://gre/modules/Task.jsm");
michael@0 9
michael@0 10 /**
michael@0 11 * A test to check that .getPosition/.setPosition work with large files.
michael@0 12 * (see bug 952997)
michael@0 13 */
michael@0 14
michael@0 15 // Test setPosition/getPosition.
michael@0 16 function test_setPosition(forward, current, backward) {
michael@0 17 let path = OS.Path.join(OS.Constants.Path.tmpDir,
michael@0 18 "test_osfile_async_largefiles.tmp");
michael@0 19
michael@0 20 // Clear any left-over files from previous runs.
michael@0 21 try {
michael@0 22 yield OS.File.remove(path);
michael@0 23 } catch (ex if ex.becauseNoSuchFile) {
michael@0 24 // ignore
michael@0 25 }
michael@0 26
michael@0 27 try {
michael@0 28 let file = yield OS.File.open(path, {write:true, append:false});
michael@0 29 try {
michael@0 30 let pos = 0;
michael@0 31
michael@0 32 // 1. seek forward from start
michael@0 33 do_print("Moving forward: " + forward);
michael@0 34 yield file.setPosition(forward, OS.File.POS_START);
michael@0 35 pos += forward;
michael@0 36 do_check_eq((yield file.getPosition()), pos);
michael@0 37
michael@0 38 // 2. seek forward from current position
michael@0 39 do_print("Moving current: " + current);
michael@0 40 yield file.setPosition(current, OS.File.POS_CURRENT);
michael@0 41 pos += current;
michael@0 42 do_check_eq((yield file.getPosition()), pos);
michael@0 43
michael@0 44 // 3. seek backward from current position
michael@0 45 do_print("Moving current backward: " + backward);
michael@0 46 yield file.setPosition(-backward, OS.File.POS_CURRENT);
michael@0 47 pos -= backward;
michael@0 48 do_check_eq((yield file.getPosition()), pos);
michael@0 49
michael@0 50 } finally {
michael@0 51 yield file.setPosition(0, OS.File.POS_START);
michael@0 52 yield file.close();
michael@0 53 }
michael@0 54 } catch(ex) {
michael@0 55 try {
michael@0 56 yield OS.File.remove(path);
michael@0 57 } catch (ex if ex.becauseNoSuchFile) {
michael@0 58 // ignore.
michael@0 59 }
michael@0 60 do_throw(ex);
michael@0 61 }
michael@0 62 }
michael@0 63
michael@0 64 // Test setPosition/getPosition expected failures.
michael@0 65 function test_setPosition_failures() {
michael@0 66 let path = OS.Path.join(OS.Constants.Path.tmpDir,
michael@0 67 "test_osfile_async_largefiles.tmp");
michael@0 68
michael@0 69 // Clear any left-over files from previous runs.
michael@0 70 try {
michael@0 71 yield OS.File.remove(path);
michael@0 72 } catch (ex if ex.becauseNoSuchFile) {
michael@0 73 // ignore
michael@0 74 }
michael@0 75
michael@0 76 try {
michael@0 77 let file = yield OS.File.open(path, {write:true, append:false});
michael@0 78 try {
michael@0 79 let pos = 0;
michael@0 80
michael@0 81 // 1. Use an invalid position value
michael@0 82 try {
michael@0 83 yield file.setPosition(0.5, OS.File.POS_START);
michael@0 84 do_throw("Shouldn't have succeeded");
michael@0 85 } catch (ex) {
michael@0 86 do_check_true(ex.toString().contains("expected type"));
michael@0 87 }
michael@0 88 // Since setPosition should have bailed, it shouldn't have moved the
michael@0 89 // file pointer at all.
michael@0 90 do_check_eq((yield file.getPosition()), 0);
michael@0 91
michael@0 92 // 2. Use an invalid position value
michael@0 93 try {
michael@0 94 yield file.setPosition(0xffffffff + 0.5, OS.File.POS_START);
michael@0 95 do_throw("Shouldn't have succeeded");
michael@0 96 } catch (ex) {
michael@0 97 do_check_true(ex.toString().contains("expected type"));
michael@0 98 }
michael@0 99 // Since setPosition should have bailed, it shouldn't have moved the
michael@0 100 // file pointer at all.
michael@0 101 do_check_eq((yield file.getPosition()), 0);
michael@0 102
michael@0 103 // 3. Use a position that cannot be represented as a double
michael@0 104 try {
michael@0 105 // Not all numbers after 9007199254740992 can be represented as a
michael@0 106 // double. E.g. in js 9007199254740992 + 1 == 9007199254740992
michael@0 107 yield file.setPosition(9007199254740992, OS.File.POS_START);
michael@0 108 yield file.setPosition(1, OS.File.POS_CURRENT);
michael@0 109 do_throw("Shouldn't have succeeded");
michael@0 110 } catch (ex) {
michael@0 111 do_print(ex.toString());
michael@0 112 do_check_true(!!ex);
michael@0 113 }
michael@0 114
michael@0 115 } finally {
michael@0 116 yield file.setPosition(0, OS.File.POS_START);
michael@0 117 yield file.close();
michael@0 118 try {
michael@0 119 yield OS.File.remove(path);
michael@0 120 } catch (ex if ex.becauseNoSuchFile) {
michael@0 121 // ignore.
michael@0 122 }
michael@0 123 }
michael@0 124 } catch(ex) {
michael@0 125 do_throw(ex);
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 function run_test() {
michael@0 130 // First verify stuff works for small values.
michael@0 131 add_task(test_setPosition.bind(null, 0, 100, 50));
michael@0 132 add_task(test_setPosition.bind(null, 1000, 100, 50));
michael@0 133 add_task(test_setPosition.bind(null, 1000, -100, -50));
michael@0 134
michael@0 135 if (OS.Constants.Win || ctypes.off_t.size >= 8) {
michael@0 136 // Now verify stuff still works for large values.
michael@0 137 // 1. Multiple small seeks, which add up to > MAXINT32
michael@0 138 add_task(test_setPosition.bind(null, 0x7fffffff, 0x7fffffff, 0));
michael@0 139 // 2. Plain large seek, that should end up at 0 again.
michael@0 140 // 0xffffffff also happens to be the INVALID_SET_FILE_POINTER value on
michael@0 141 // Windows, so this also tests the error handling
michael@0 142 add_task(test_setPosition.bind(null, 0, 0xffffffff, 0xffffffff));
michael@0 143 // 3. Multiple large seeks that should end up > MAXINT32.
michael@0 144 add_task(test_setPosition.bind(null, 0xffffffff, 0xffffffff, 0xffffffff));
michael@0 145 // 5. Multiple large seeks with negative offsets.
michael@0 146 add_task(test_setPosition.bind(null, 0xffffffff, -0x7fffffff, 0x7fffffff));
michael@0 147
michael@0 148 // 6. Check failures
michael@0 149 add_task(test_setPosition_failures);
michael@0 150 }
michael@0 151
michael@0 152 run_next_test();
michael@0 153 }

mercurial