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