|
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 log("received message "+JSON.stringify(msg.data)); |
|
8 self.onmessage = function(msg) { |
|
9 log("ignored message "+JSON.stringify(msg.data)); |
|
10 }; |
|
11 test_init(); |
|
12 test_getcwd(); |
|
13 test_open_close(); |
|
14 test_create_file(); |
|
15 test_access(); |
|
16 test_read_write(); |
|
17 test_passing_undefined(); |
|
18 finish(); |
|
19 }; |
|
20 |
|
21 function test_init() { |
|
22 info("Starting test_init"); |
|
23 importScripts("resource://gre/modules/osfile.jsm"); |
|
24 } |
|
25 |
|
26 function test_open_close() { |
|
27 info("Starting test_open_close"); |
|
28 is(typeof OS.Unix.File.open, "function", "OS.Unix.File.open is a function"); |
|
29 let file = OS.Unix.File.open("chrome/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js", OS.Constants.libc.O_RDONLY, 0); |
|
30 isnot(file, -1, "test_open_close: opening succeeded"); |
|
31 info("Close: "+OS.Unix.File.close.toSource()); |
|
32 let result = OS.Unix.File.close(file); |
|
33 is(result, 0, "test_open_close: close succeeded"); |
|
34 |
|
35 file = OS.Unix.File.open("/i do not exist", OS.Constants.libc.O_RDONLY, 0); |
|
36 is(file, -1, "test_open_close: opening of non-existing file failed"); |
|
37 is(ctypes.errno, OS.Constants.libc.ENOENT, "test_open_close: error is ENOENT"); |
|
38 } |
|
39 |
|
40 function test_create_file() |
|
41 { |
|
42 info("Starting test_create_file"); |
|
43 let file = OS.Unix.File.open("test.tmp", OS.Constants.libc.O_RDWR |
|
44 | OS.Constants.libc.O_CREAT |
|
45 | OS.Constants.libc.O_TRUNC, |
|
46 OS.Constants.libc.S_IRWXU); |
|
47 isnot(file, -1, "test_create_file: file created"); |
|
48 OS.Unix.File.close(file); |
|
49 } |
|
50 |
|
51 function test_access() |
|
52 { |
|
53 info("Starting test_access"); |
|
54 let file = OS.Unix.File.open("test1.tmp", OS.Constants.libc.O_RDWR |
|
55 | OS.Constants.libc.O_CREAT |
|
56 | OS.Constants.libc.O_TRUNC, |
|
57 OS.Constants.libc.S_IRWXU); |
|
58 let result = OS.Unix.File.access("test1.tmp", OS.Constants.libc.R_OK | OS.Constants.libc.W_OK | OS.Constants.libc.X_OK | OS.Constants.libc.F_OK); |
|
59 is(result, 0, "first call to access() succeeded"); |
|
60 OS.Unix.File.close(file); |
|
61 |
|
62 file = OS.Unix.File.open("test1.tmp", OS.Constants.libc.O_WRONLY |
|
63 | OS.Constants.libc.O_CREAT |
|
64 | OS.Constants.libc.O_TRUNC, |
|
65 OS.Constants.libc.S_IWUSR); |
|
66 |
|
67 info("test_access: preparing second call to access()"); |
|
68 result = OS.Unix.File.access("test2.tmp", OS.Constants.libc.R_OK |
|
69 | OS.Constants.libc.W_OK |
|
70 | OS.Constants.libc.X_OK |
|
71 | OS.Constants.libc.F_OK); |
|
72 is(result, -1, "test_access: second call to access() failed as expected"); |
|
73 is(ctypes.errno, OS.Constants.libc.ENOENT, "This is the correct error"); |
|
74 OS.Unix.File.close(file); |
|
75 } |
|
76 |
|
77 function test_getcwd() |
|
78 { |
|
79 let array = new (ctypes.ArrayType(ctypes.char, 32768))(); |
|
80 let path = OS.Unix.File.getcwd(array, array.length); |
|
81 if (ctypes.char.ptr(path).isNull()) { |
|
82 ok(false, "test_get_cwd: getcwd returned null, errno: " + ctypes.errno); |
|
83 } |
|
84 let path2; |
|
85 if (OS.Unix.File.get_current_dir_name) { |
|
86 path2 = OS.Unix.File.get_current_dir_name(); |
|
87 } else { |
|
88 path2 = OS.Unix.File.getwd_auto(null); |
|
89 } |
|
90 if (ctypes.char.ptr(path2).isNull()) { |
|
91 ok(false, "test_get_cwd: getwd_auto/get_current_dir_name returned null, errno: " + ctypes.errno); |
|
92 } |
|
93 is(path.readString(), path2.readString(), "test_get_cwd: getcwd and getwd return the same path"); |
|
94 } |
|
95 |
|
96 function test_read_write() |
|
97 { |
|
98 let output_name = "osfile_copy.tmp"; |
|
99 // Copy file |
|
100 let input = OS.Unix.File.open( |
|
101 "chrome/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js", |
|
102 OS.Constants.libc.O_RDONLY, 0); |
|
103 isnot(input, -1, "test_read_write: input file opened"); |
|
104 let output = OS.Unix.File.open("osfile_copy.tmp", OS.Constants.libc.O_RDWR |
|
105 | OS.Constants.libc.O_CREAT |
|
106 | OS.Constants.libc.O_TRUNC, |
|
107 OS.Constants.libc.S_IRWXU); |
|
108 isnot(output, -1, "test_read_write: output file opened"); |
|
109 |
|
110 let array = new (ctypes.ArrayType(ctypes.char, 4096))(); |
|
111 let bytes = -1; |
|
112 let total = 0; |
|
113 while (true) { |
|
114 bytes = OS.Unix.File.read(input, array, 4096); |
|
115 ok(bytes != undefined, "test_read_write: bytes is defined"); |
|
116 isnot(bytes, -1, "test_read_write: no read error"); |
|
117 let write_from = 0; |
|
118 if (bytes == 0) { |
|
119 break; |
|
120 } |
|
121 while (bytes > 0) { |
|
122 let ptr = array.addressOfElement(write_from); |
|
123 // Note: |write| launches an exception in case of error |
|
124 let written = OS.Unix.File.write(output, array, bytes); |
|
125 isnot(written, -1, "test_read_write: no write error"); |
|
126 write_from += written; |
|
127 bytes -= written; |
|
128 } |
|
129 total += write_from; |
|
130 } |
|
131 info("test_read_write: copy complete " + total); |
|
132 |
|
133 // Compare files |
|
134 let result; |
|
135 info("SEEK_SET: " + OS.Constants.libc.SEEK_SET); |
|
136 info("Input: " + input + "(" + input.toSource() + ")"); |
|
137 info("Output: " + output + "(" + output.toSource() + ")"); |
|
138 result = OS.Unix.File.lseek(input, 0, OS.Constants.libc.SEEK_SET); |
|
139 info("Result of lseek: " + result); |
|
140 isnot(result, -1, "test_read_write: input seek succeeded " + ctypes.errno); |
|
141 result = OS.Unix.File.lseek(output, 0, OS.Constants.libc.SEEK_SET); |
|
142 isnot(result, -1, "test_read_write: output seek succeeded " + ctypes.errno); |
|
143 |
|
144 let array2 = new (ctypes.ArrayType(ctypes.char, 4096))(); |
|
145 let bytes2 = -1; |
|
146 let pos = 0; |
|
147 while (true) { |
|
148 bytes = OS.Unix.File.read(input, array, 4096); |
|
149 isnot(bytes, -1, "test_read_write: input read succeeded"); |
|
150 bytes2 = OS.Unix.File.read(output, array2, 4096); |
|
151 isnot(bytes, -1, "test_read_write: output read succeeded"); |
|
152 is(bytes > 0, bytes2 > 0, "Both files contain data or neither does "+bytes+", "+bytes2); |
|
153 if (bytes == 0) { |
|
154 break; |
|
155 } |
|
156 if (bytes != bytes2) { |
|
157 // This would be surprising, but theoretically possible with a |
|
158 // remote file system, I believe. |
|
159 bytes = Math.min(bytes, bytes2); |
|
160 pos += bytes; |
|
161 result = OS.Unix.File.lseek(input, pos, OS.Constants.libc.SEEK_SET); |
|
162 isnot(result, -1, "test_read_write: input seek succeeded"); |
|
163 result = OS.Unix.File.lseek(output, pos, OS.Constants.libc.SEEK_SET); |
|
164 isnot(result, -1, "test_read_write: output seek succeeded"); |
|
165 } else { |
|
166 pos += bytes; |
|
167 } |
|
168 for (let i = 0; i < bytes; ++i) { |
|
169 if (array[i] != array2[i]) { |
|
170 ok(false, "Files do not match at position " + i |
|
171 + " ("+array[i] + "/"+array2[i] + ")"); |
|
172 } |
|
173 } |
|
174 } |
|
175 info("test_read_write test complete"); |
|
176 result = OS.Unix.File.close(input); |
|
177 isnot(result, -1, "test_read_write: input close succeeded"); |
|
178 result = OS.Unix.File.close(output); |
|
179 isnot(result, -1, "test_read_write: output close succeeded"); |
|
180 result = OS.Unix.File.unlink(output_name); |
|
181 isnot(result, -1, "test_read_write: input remove succeeded"); |
|
182 info("test_read_write cleanup complete"); |
|
183 } |
|
184 |
|
185 function test_passing_undefined() |
|
186 { |
|
187 info("Testing that an exception gets thrown when an FFI function is passed undefined"); |
|
188 let exceptionRaised = false; |
|
189 |
|
190 try { |
|
191 let file = OS.Unix.File.open(undefined, OS.Constants.libc.O_RDWR |
|
192 | OS.Constants.libc.O_CREAT |
|
193 | OS.Constants.libc.O_TRUNC, |
|
194 OS.Constants.libc.S_IRWXU); |
|
195 } catch(e if e instanceof TypeError && e.message.indexOf("open") > -1) { |
|
196 exceptionRaised = true; |
|
197 } |
|
198 |
|
199 ok(exceptionRaised, "test_passing_undefined: exception gets thrown") |
|
200 } |
|
201 |