michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: importScripts('worker_test_osfile_shared.js'); michael@0: michael@0: self.onmessage = function(msg) { michael@0: log("received message "+JSON.stringify(msg.data)); michael@0: self.onmessage = function(msg) { michael@0: log("ignored message "+JSON.stringify(msg.data)); michael@0: }; michael@0: test_init(); michael@0: test_getcwd(); michael@0: test_open_close(); michael@0: test_create_file(); michael@0: test_access(); michael@0: test_read_write(); michael@0: test_passing_undefined(); michael@0: finish(); michael@0: }; michael@0: michael@0: function test_init() { michael@0: info("Starting test_init"); michael@0: importScripts("resource://gre/modules/osfile.jsm"); michael@0: } michael@0: michael@0: function test_open_close() { michael@0: info("Starting test_open_close"); michael@0: is(typeof OS.Unix.File.open, "function", "OS.Unix.File.open is a function"); michael@0: 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: isnot(file, -1, "test_open_close: opening succeeded"); michael@0: info("Close: "+OS.Unix.File.close.toSource()); michael@0: let result = OS.Unix.File.close(file); michael@0: is(result, 0, "test_open_close: close succeeded"); michael@0: michael@0: file = OS.Unix.File.open("/i do not exist", OS.Constants.libc.O_RDONLY, 0); michael@0: is(file, -1, "test_open_close: opening of non-existing file failed"); michael@0: is(ctypes.errno, OS.Constants.libc.ENOENT, "test_open_close: error is ENOENT"); michael@0: } michael@0: michael@0: function test_create_file() michael@0: { michael@0: info("Starting test_create_file"); michael@0: let file = OS.Unix.File.open("test.tmp", OS.Constants.libc.O_RDWR michael@0: | OS.Constants.libc.O_CREAT michael@0: | OS.Constants.libc.O_TRUNC, michael@0: OS.Constants.libc.S_IRWXU); michael@0: isnot(file, -1, "test_create_file: file created"); michael@0: OS.Unix.File.close(file); michael@0: } michael@0: michael@0: function test_access() michael@0: { michael@0: info("Starting test_access"); michael@0: let file = OS.Unix.File.open("test1.tmp", OS.Constants.libc.O_RDWR michael@0: | OS.Constants.libc.O_CREAT michael@0: | OS.Constants.libc.O_TRUNC, michael@0: OS.Constants.libc.S_IRWXU); michael@0: 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: is(result, 0, "first call to access() succeeded"); michael@0: OS.Unix.File.close(file); michael@0: michael@0: file = OS.Unix.File.open("test1.tmp", OS.Constants.libc.O_WRONLY michael@0: | OS.Constants.libc.O_CREAT michael@0: | OS.Constants.libc.O_TRUNC, michael@0: OS.Constants.libc.S_IWUSR); michael@0: michael@0: info("test_access: preparing second call to access()"); michael@0: result = OS.Unix.File.access("test2.tmp", OS.Constants.libc.R_OK michael@0: | OS.Constants.libc.W_OK michael@0: | OS.Constants.libc.X_OK michael@0: | OS.Constants.libc.F_OK); michael@0: is(result, -1, "test_access: second call to access() failed as expected"); michael@0: is(ctypes.errno, OS.Constants.libc.ENOENT, "This is the correct error"); michael@0: OS.Unix.File.close(file); michael@0: } michael@0: michael@0: function test_getcwd() michael@0: { michael@0: let array = new (ctypes.ArrayType(ctypes.char, 32768))(); michael@0: let path = OS.Unix.File.getcwd(array, array.length); michael@0: if (ctypes.char.ptr(path).isNull()) { michael@0: ok(false, "test_get_cwd: getcwd returned null, errno: " + ctypes.errno); michael@0: } michael@0: let path2; michael@0: if (OS.Unix.File.get_current_dir_name) { michael@0: path2 = OS.Unix.File.get_current_dir_name(); michael@0: } else { michael@0: path2 = OS.Unix.File.getwd_auto(null); michael@0: } michael@0: if (ctypes.char.ptr(path2).isNull()) { michael@0: ok(false, "test_get_cwd: getwd_auto/get_current_dir_name returned null, errno: " + ctypes.errno); michael@0: } michael@0: is(path.readString(), path2.readString(), "test_get_cwd: getcwd and getwd return the same path"); michael@0: } michael@0: michael@0: function test_read_write() michael@0: { michael@0: let output_name = "osfile_copy.tmp"; michael@0: // Copy file michael@0: let input = OS.Unix.File.open( michael@0: "chrome/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js", michael@0: OS.Constants.libc.O_RDONLY, 0); michael@0: isnot(input, -1, "test_read_write: input file opened"); michael@0: let output = OS.Unix.File.open("osfile_copy.tmp", OS.Constants.libc.O_RDWR michael@0: | OS.Constants.libc.O_CREAT michael@0: | OS.Constants.libc.O_TRUNC, michael@0: OS.Constants.libc.S_IRWXU); michael@0: isnot(output, -1, "test_read_write: output file opened"); michael@0: michael@0: let array = new (ctypes.ArrayType(ctypes.char, 4096))(); michael@0: let bytes = -1; michael@0: let total = 0; michael@0: while (true) { michael@0: bytes = OS.Unix.File.read(input, array, 4096); michael@0: ok(bytes != undefined, "test_read_write: bytes is defined"); michael@0: isnot(bytes, -1, "test_read_write: no read error"); michael@0: let write_from = 0; michael@0: if (bytes == 0) { michael@0: break; michael@0: } michael@0: while (bytes > 0) { michael@0: let ptr = array.addressOfElement(write_from); michael@0: // Note: |write| launches an exception in case of error michael@0: let written = OS.Unix.File.write(output, array, bytes); michael@0: isnot(written, -1, "test_read_write: no write error"); michael@0: write_from += written; michael@0: bytes -= written; michael@0: } michael@0: total += write_from; michael@0: } michael@0: info("test_read_write: copy complete " + total); michael@0: michael@0: // Compare files michael@0: let result; michael@0: info("SEEK_SET: " + OS.Constants.libc.SEEK_SET); michael@0: info("Input: " + input + "(" + input.toSource() + ")"); michael@0: info("Output: " + output + "(" + output.toSource() + ")"); michael@0: result = OS.Unix.File.lseek(input, 0, OS.Constants.libc.SEEK_SET); michael@0: info("Result of lseek: " + result); michael@0: isnot(result, -1, "test_read_write: input seek succeeded " + ctypes.errno); michael@0: result = OS.Unix.File.lseek(output, 0, OS.Constants.libc.SEEK_SET); michael@0: isnot(result, -1, "test_read_write: output seek succeeded " + ctypes.errno); michael@0: michael@0: let array2 = new (ctypes.ArrayType(ctypes.char, 4096))(); michael@0: let bytes2 = -1; michael@0: let pos = 0; michael@0: while (true) { michael@0: bytes = OS.Unix.File.read(input, array, 4096); michael@0: isnot(bytes, -1, "test_read_write: input read succeeded"); michael@0: bytes2 = OS.Unix.File.read(output, array2, 4096); michael@0: isnot(bytes, -1, "test_read_write: output read succeeded"); michael@0: is(bytes > 0, bytes2 > 0, "Both files contain data or neither does "+bytes+", "+bytes2); michael@0: if (bytes == 0) { michael@0: break; michael@0: } michael@0: if (bytes != bytes2) { michael@0: // This would be surprising, but theoretically possible with a michael@0: // remote file system, I believe. michael@0: bytes = Math.min(bytes, bytes2); michael@0: pos += bytes; michael@0: result = OS.Unix.File.lseek(input, pos, OS.Constants.libc.SEEK_SET); michael@0: isnot(result, -1, "test_read_write: input seek succeeded"); michael@0: result = OS.Unix.File.lseek(output, pos, OS.Constants.libc.SEEK_SET); michael@0: isnot(result, -1, "test_read_write: output seek succeeded"); michael@0: } else { michael@0: pos += bytes; michael@0: } michael@0: for (let i = 0; i < bytes; ++i) { michael@0: if (array[i] != array2[i]) { michael@0: ok(false, "Files do not match at position " + i michael@0: + " ("+array[i] + "/"+array2[i] + ")"); michael@0: } michael@0: } michael@0: } michael@0: info("test_read_write test complete"); michael@0: result = OS.Unix.File.close(input); michael@0: isnot(result, -1, "test_read_write: input close succeeded"); michael@0: result = OS.Unix.File.close(output); michael@0: isnot(result, -1, "test_read_write: output close succeeded"); michael@0: result = OS.Unix.File.unlink(output_name); michael@0: isnot(result, -1, "test_read_write: input remove succeeded"); michael@0: info("test_read_write cleanup complete"); michael@0: } michael@0: michael@0: function test_passing_undefined() michael@0: { michael@0: info("Testing that an exception gets thrown when an FFI function is passed undefined"); michael@0: let exceptionRaised = false; michael@0: michael@0: try { michael@0: let file = OS.Unix.File.open(undefined, OS.Constants.libc.O_RDWR michael@0: | OS.Constants.libc.O_CREAT michael@0: | OS.Constants.libc.O_TRUNC, michael@0: OS.Constants.libc.S_IRWXU); michael@0: } catch(e if e instanceof TypeError && e.message.indexOf("open") > -1) { michael@0: exceptionRaised = true; michael@0: } michael@0: michael@0: ok(exceptionRaised, "test_passing_undefined: exception gets thrown") michael@0: } michael@0: