toolkit/components/osfile/tests/mochi/worker_test_osfile_win.js

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

mercurial