toolkit/components/osfile/tests/mochi/worker_test_osfile_win.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 importScripts('worker_test_osfile_shared.js');
michael@0 5
michael@0 6 self.onmessage = function(msg) {
michael@0 7 self.onmessage = function(msg) {
michael@0 8 log("ignored message "+JSON.stringify(msg.data));
michael@0 9 };
michael@0 10
michael@0 11 test_init();
michael@0 12 test_GetCurrentDirectory();
michael@0 13 test_OpenClose();
michael@0 14 test_CreateFile();
michael@0 15 test_ReadWrite();
michael@0 16 test_passing_undefined();
michael@0 17 finish();
michael@0 18 };
michael@0 19
michael@0 20 function test_init() {
michael@0 21 info("Starting test_init");
michael@0 22 importScripts("resource://gre/modules/osfile.jsm");
michael@0 23 }
michael@0 24
michael@0 25 function test_OpenClose() {
michael@0 26 info("Starting test_OpenClose");
michael@0 27 is(typeof OS.Win.File.CreateFile, "function", "OS.Win.File.CreateFile is a function");
michael@0 28 is(OS.Win.File.CloseHandle(OS.Constants.Win.INVALID_HANDLE_VALUE), true, "CloseHandle returns true given the invalid handle");
michael@0 29 is(OS.Win.File.FindClose(OS.Constants.Win.INVALID_HANDLE_VALUE), true, "FindClose returns true given the invalid handle");
michael@0 30 isnot(OS.Constants.Win.GENERIC_READ, undefined, "GENERIC_READ exists");
michael@0 31 isnot(OS.Constants.Win.FILE_SHARE_READ, undefined, "FILE_SHARE_READ exists");
michael@0 32 isnot(OS.Constants.Win.FILE_ATTRIBUTE_NORMAL, undefined, "FILE_ATTRIBUTE_NORMAL exists");
michael@0 33 let file = OS.Win.File.CreateFile(
michael@0 34 "chrome\\toolkit\\components\\osfile\\tests\\mochi\\worker_test_osfile_win.js",
michael@0 35 OS.Constants.Win.GENERIC_READ,
michael@0 36 0,
michael@0 37 null,
michael@0 38 OS.Constants.Win.OPEN_EXISTING,
michael@0 39 0,
michael@0 40 null);
michael@0 41 info("test_OpenClose: Passed open");
michael@0 42 isnot(file, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_OpenClose: file opened");
michael@0 43 let result = OS.Win.File.CloseHandle(file);
michael@0 44 isnot(result, 0, "test_OpenClose: close succeeded");
michael@0 45
michael@0 46 file = OS.Win.File.CreateFile(
michael@0 47 "\\I do not exist",
michael@0 48 OS.Constants.Win.GENERIC_READ,
michael@0 49 OS.Constants.Win.FILE_SHARE_READ,
michael@0 50 null,
michael@0 51 OS.Constants.Win.OPEN_EXISTING,
michael@0 52 OS.Constants.Win.FILE_ATTRIBUTE_NORMAL,
michael@0 53 null);
michael@0 54 is(file, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_OpenClose: cannot open non-existing file");
michael@0 55 is(ctypes.winLastError, OS.Constants.Win.ERROR_FILE_NOT_FOUND, "test_OpenClose: error is ERROR_FILE_NOT_FOUND");
michael@0 56 }
michael@0 57
michael@0 58 function test_CreateFile()
michael@0 59 {
michael@0 60 info("Starting test_CreateFile");
michael@0 61 let file = OS.Win.File.CreateFile(
michael@0 62 "test.tmp",
michael@0 63 OS.Constants.Win.GENERIC_READ | OS.Constants.Win.GENERIC_WRITE,
michael@0 64 OS.Constants.Win.FILE_SHARE_READ | OS.Constants.FILE_SHARE_WRITE,
michael@0 65 null,
michael@0 66 OS.Constants.Win.CREATE_ALWAYS,
michael@0 67 OS.Constants.Win.FILE_ATTRIBUTE_NORMAL,
michael@0 68 null);
michael@0 69 isnot(file, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_CreateFile: opening succeeded");
michael@0 70 let result = OS.Win.File.CloseHandle(file);
michael@0 71 isnot(result, 0, "test_CreateFile: close succeeded");
michael@0 72 }
michael@0 73
michael@0 74 function test_GetCurrentDirectory()
michael@0 75 {
michael@0 76 let array = new (ctypes.ArrayType(ctypes.jschar, 4096))();
michael@0 77 let result = OS.Win.File.GetCurrentDirectory(4096, array);
michael@0 78 ok(result < array.length, "test_GetCurrentDirectory: length sufficient");
michael@0 79 ok(result > 0, "test_GetCurrentDirectory: length != 0");
michael@0 80 }
michael@0 81
michael@0 82 function test_ReadWrite()
michael@0 83 {
michael@0 84 info("Starting test_ReadWrite");
michael@0 85 let output_name = "osfile_copy.tmp";
michael@0 86 // Copy file
michael@0 87 let input = OS.Win.File.CreateFile(
michael@0 88 "chrome\\toolkit\\components\\osfile\\tests\\mochi\\worker_test_osfile_win.js",
michael@0 89 OS.Constants.Win.GENERIC_READ,
michael@0 90 0,
michael@0 91 null,
michael@0 92 OS.Constants.Win.OPEN_EXISTING,
michael@0 93 0,
michael@0 94 null);
michael@0 95 isnot(input, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_ReadWrite: input file opened");
michael@0 96 let output = OS.Win.File.CreateFile(
michael@0 97 "osfile_copy.tmp",
michael@0 98 OS.Constants.Win.GENERIC_READ | OS.Constants.Win.GENERIC_WRITE,
michael@0 99 0,
michael@0 100 null,
michael@0 101 OS.Constants.Win.CREATE_ALWAYS,
michael@0 102 OS.Constants.Win.FILE_ATTRIBUTE_NORMAL,
michael@0 103 null);
michael@0 104 isnot(output, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_ReadWrite: output file opened");
michael@0 105 let array = new (ctypes.ArrayType(ctypes.char, 4096))();
michael@0 106 let bytes_read = new ctypes.uint32_t(0);
michael@0 107 let bytes_read_ptr = bytes_read.address();
michael@0 108 log("We have a pointer for bytes read: "+bytes_read_ptr);
michael@0 109 let bytes_written = new ctypes.uint32_t(0);
michael@0 110 let bytes_written_ptr = bytes_written.address();
michael@0 111 log("We have a pointer for bytes written: "+bytes_written_ptr);
michael@0 112 log("test_ReadWrite: buffer and pointers ready");
michael@0 113 let result;
michael@0 114 while (true) {
michael@0 115 log("test_ReadWrite: reading");
michael@0 116 result = OS.Win.File.ReadFile(input, array, 4096, bytes_read_ptr, null);
michael@0 117 isnot (result, 0, "test_ReadWrite: read success");
michael@0 118 let write_from = 0;
michael@0 119 let bytes_left = bytes_read;
michael@0 120 log("test_ReadWrite: read chunk complete " + bytes_left.value);
michael@0 121 if (bytes_left.value == 0) {
michael@0 122 break;
michael@0 123 }
michael@0 124 while (bytes_left.value > 0) {
michael@0 125 log("test_ReadWrite: writing "+bytes_left.value);
michael@0 126 let ptr = array.addressOfElement(write_from);
michael@0 127 // Note: |WriteFile| launches an exception in case of error
michael@0 128 result = OS.Win.File.WriteFile(output, array, bytes_left, bytes_written_ptr, null);
michael@0 129 isnot (result, 0, "test_ReadWrite: write success");
michael@0 130 write_from += bytes_written;
michael@0 131 bytes_left -= bytes_written;
michael@0 132 }
michael@0 133 }
michael@0 134 info("test_ReadWrite: copy complete");
michael@0 135
michael@0 136 // Compare files
michael@0 137 result = OS.Win.File.SetFilePointer(input, 0, null, OS.Constants.Win.FILE_BEGIN);
michael@0 138 isnot (result, OS.Constants.Win.INVALID_SET_FILE_POINTER, "test_ReadWrite: input reset");
michael@0 139
michael@0 140 result = OS.Win.File.SetFilePointer(output, 0, null, OS.Constants.Win.FILE_BEGIN);
michael@0 141 isnot (result, OS.Constants.Win.INVALID_SET_FILE_POINTER, "test_ReadWrite: output reset");
michael@0 142
michael@0 143 let array2 = new (ctypes.ArrayType(ctypes.char, 4096))();
michael@0 144 let bytes_read2 = new ctypes.uint32_t(0);
michael@0 145 let bytes_read2_ptr = bytes_read2.address();
michael@0 146 let pos = 0;
michael@0 147 while (true) {
michael@0 148 result = OS.Win.File.ReadFile(input, array, 4096, bytes_read_ptr, null);
michael@0 149 isnot(result, 0, "test_ReadWrite: input read succeeded");
michael@0 150
michael@0 151 result = OS.Win.File.ReadFile(output, array2, 4096, bytes_read2_ptr, null);
michael@0 152 isnot(result, 0, "test_ReadWrite: output read succeeded");
michael@0 153
michael@0 154 is(bytes_read.value > 0, bytes_read2.value > 0,
michael@0 155 "Both files contain data or neither does " + bytes_read.value + ", " + bytes_read2.value);
michael@0 156 if (bytes_read.value == 0) {
michael@0 157 break;
michael@0 158 }
michael@0 159 let bytes;
michael@0 160 if (bytes_read.value != bytes_read2.value) {
michael@0 161 // This would be surprising, but theoretically possible with a
michael@0 162 // remote file system, I believe.
michael@0 163 bytes = Math.min(bytes_read.value, bytes_read2.value);
michael@0 164 pos += bytes;
michael@0 165 result = OS.Win.File.SetFilePointer(input, pos, null, OS.Constants.Win.FILE_BEGIN);
michael@0 166 isnot(result, 0, "test_ReadWrite: input seek succeeded");
michael@0 167
michael@0 168 result = OS.Win.File.SetFilePointer(output, pos, null, OS.Constants.Win.FILE_BEGIN);
michael@0 169 isnot(result, 0, "test_ReadWrite: output seek succeeded");
michael@0 170
michael@0 171 } else {
michael@0 172 bytes = bytes_read.value;
michael@0 173 pos += bytes;
michael@0 174 }
michael@0 175 for (let i = 0; i < bytes; ++i) {
michael@0 176 if (array[i] != array2[i]) {
michael@0 177 ok(false, "Files do not match at position " + i
michael@0 178 + " ("+array[i] + "/"+array2[i] + ")");
michael@0 179 }
michael@0 180 }
michael@0 181 }
michael@0 182 info("test_ReadWrite test complete");
michael@0 183 result = OS.Win.File.CloseHandle(input);
michael@0 184 isnot(result, 0, "test_ReadWrite: inpout close succeeded");
michael@0 185 result = OS.Win.File.CloseHandle(output);
michael@0 186 isnot(result, 0, "test_ReadWrite: outpout close succeeded");
michael@0 187 result = OS.Win.File.DeleteFile(output_name);
michael@0 188 isnot(result, 0, "test_ReadWrite: output remove succeeded");
michael@0 189 info("test_ReadWrite cleanup complete");
michael@0 190 }
michael@0 191
michael@0 192 function test_passing_undefined()
michael@0 193 {
michael@0 194 info("Testing that an exception gets thrown when an FFI function is passed undefined");
michael@0 195 let exceptionRaised = false;
michael@0 196
michael@0 197 try {
michael@0 198 let file = OS.Win.File.CreateFile(
michael@0 199 undefined,
michael@0 200 OS.Constants.Win.GENERIC_READ,
michael@0 201 0,
michael@0 202 null,
michael@0 203 OS.Constants.Win.OPEN_EXISTING,
michael@0 204 0,
michael@0 205 null);
michael@0 206 } catch(e if e instanceof TypeError && e.message.indexOf("CreateFile") > -1) {
michael@0 207 exceptionRaised = true;
michael@0 208 }
michael@0 209
michael@0 210 ok(exceptionRaised, "test_passing_undefined: exception gets thrown")
michael@0 211 }

mercurial