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: self.onmessage = function(msg) { michael@0: log("ignored message "+JSON.stringify(msg.data)); michael@0: }; michael@0: michael@0: test_init(); michael@0: test_GetCurrentDirectory(); michael@0: test_OpenClose(); michael@0: test_CreateFile(); michael@0: test_ReadWrite(); 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_OpenClose() { michael@0: info("Starting test_OpenClose"); michael@0: is(typeof OS.Win.File.CreateFile, "function", "OS.Win.File.CreateFile is a function"); michael@0: is(OS.Win.File.CloseHandle(OS.Constants.Win.INVALID_HANDLE_VALUE), true, "CloseHandle returns true given the invalid handle"); michael@0: is(OS.Win.File.FindClose(OS.Constants.Win.INVALID_HANDLE_VALUE), true, "FindClose returns true given the invalid handle"); michael@0: isnot(OS.Constants.Win.GENERIC_READ, undefined, "GENERIC_READ exists"); michael@0: isnot(OS.Constants.Win.FILE_SHARE_READ, undefined, "FILE_SHARE_READ exists"); michael@0: isnot(OS.Constants.Win.FILE_ATTRIBUTE_NORMAL, undefined, "FILE_ATTRIBUTE_NORMAL exists"); michael@0: let file = OS.Win.File.CreateFile( michael@0: "chrome\\toolkit\\components\\osfile\\tests\\mochi\\worker_test_osfile_win.js", michael@0: OS.Constants.Win.GENERIC_READ, michael@0: 0, michael@0: null, michael@0: OS.Constants.Win.OPEN_EXISTING, michael@0: 0, michael@0: null); michael@0: info("test_OpenClose: Passed open"); michael@0: isnot(file, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_OpenClose: file opened"); michael@0: let result = OS.Win.File.CloseHandle(file); michael@0: isnot(result, 0, "test_OpenClose: close succeeded"); michael@0: michael@0: file = OS.Win.File.CreateFile( michael@0: "\\I do not exist", michael@0: OS.Constants.Win.GENERIC_READ, michael@0: OS.Constants.Win.FILE_SHARE_READ, michael@0: null, michael@0: OS.Constants.Win.OPEN_EXISTING, michael@0: OS.Constants.Win.FILE_ATTRIBUTE_NORMAL, michael@0: null); michael@0: is(file, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_OpenClose: cannot open non-existing file"); michael@0: is(ctypes.winLastError, OS.Constants.Win.ERROR_FILE_NOT_FOUND, "test_OpenClose: error is ERROR_FILE_NOT_FOUND"); michael@0: } michael@0: michael@0: function test_CreateFile() michael@0: { michael@0: info("Starting test_CreateFile"); michael@0: let file = OS.Win.File.CreateFile( michael@0: "test.tmp", michael@0: OS.Constants.Win.GENERIC_READ | OS.Constants.Win.GENERIC_WRITE, michael@0: OS.Constants.Win.FILE_SHARE_READ | OS.Constants.FILE_SHARE_WRITE, michael@0: null, michael@0: OS.Constants.Win.CREATE_ALWAYS, michael@0: OS.Constants.Win.FILE_ATTRIBUTE_NORMAL, michael@0: null); michael@0: isnot(file, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_CreateFile: opening succeeded"); michael@0: let result = OS.Win.File.CloseHandle(file); michael@0: isnot(result, 0, "test_CreateFile: close succeeded"); michael@0: } michael@0: michael@0: function test_GetCurrentDirectory() michael@0: { michael@0: let array = new (ctypes.ArrayType(ctypes.jschar, 4096))(); michael@0: let result = OS.Win.File.GetCurrentDirectory(4096, array); michael@0: ok(result < array.length, "test_GetCurrentDirectory: length sufficient"); michael@0: ok(result > 0, "test_GetCurrentDirectory: length != 0"); michael@0: } michael@0: michael@0: function test_ReadWrite() michael@0: { michael@0: info("Starting test_ReadWrite"); michael@0: let output_name = "osfile_copy.tmp"; michael@0: // Copy file michael@0: let input = OS.Win.File.CreateFile( michael@0: "chrome\\toolkit\\components\\osfile\\tests\\mochi\\worker_test_osfile_win.js", michael@0: OS.Constants.Win.GENERIC_READ, michael@0: 0, michael@0: null, michael@0: OS.Constants.Win.OPEN_EXISTING, michael@0: 0, michael@0: null); michael@0: isnot(input, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_ReadWrite: input file opened"); michael@0: let output = OS.Win.File.CreateFile( michael@0: "osfile_copy.tmp", michael@0: OS.Constants.Win.GENERIC_READ | OS.Constants.Win.GENERIC_WRITE, michael@0: 0, michael@0: null, michael@0: OS.Constants.Win.CREATE_ALWAYS, michael@0: OS.Constants.Win.FILE_ATTRIBUTE_NORMAL, michael@0: null); michael@0: isnot(output, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_ReadWrite: output file opened"); michael@0: let array = new (ctypes.ArrayType(ctypes.char, 4096))(); michael@0: let bytes_read = new ctypes.uint32_t(0); michael@0: let bytes_read_ptr = bytes_read.address(); michael@0: log("We have a pointer for bytes read: "+bytes_read_ptr); michael@0: let bytes_written = new ctypes.uint32_t(0); michael@0: let bytes_written_ptr = bytes_written.address(); michael@0: log("We have a pointer for bytes written: "+bytes_written_ptr); michael@0: log("test_ReadWrite: buffer and pointers ready"); michael@0: let result; michael@0: while (true) { michael@0: log("test_ReadWrite: reading"); michael@0: result = OS.Win.File.ReadFile(input, array, 4096, bytes_read_ptr, null); michael@0: isnot (result, 0, "test_ReadWrite: read success"); michael@0: let write_from = 0; michael@0: let bytes_left = bytes_read; michael@0: log("test_ReadWrite: read chunk complete " + bytes_left.value); michael@0: if (bytes_left.value == 0) { michael@0: break; michael@0: } michael@0: while (bytes_left.value > 0) { michael@0: log("test_ReadWrite: writing "+bytes_left.value); michael@0: let ptr = array.addressOfElement(write_from); michael@0: // Note: |WriteFile| launches an exception in case of error michael@0: result = OS.Win.File.WriteFile(output, array, bytes_left, bytes_written_ptr, null); michael@0: isnot (result, 0, "test_ReadWrite: write success"); michael@0: write_from += bytes_written; michael@0: bytes_left -= bytes_written; michael@0: } michael@0: } michael@0: info("test_ReadWrite: copy complete"); michael@0: michael@0: // Compare files michael@0: result = OS.Win.File.SetFilePointer(input, 0, null, OS.Constants.Win.FILE_BEGIN); michael@0: isnot (result, OS.Constants.Win.INVALID_SET_FILE_POINTER, "test_ReadWrite: input reset"); michael@0: michael@0: result = OS.Win.File.SetFilePointer(output, 0, null, OS.Constants.Win.FILE_BEGIN); michael@0: isnot (result, OS.Constants.Win.INVALID_SET_FILE_POINTER, "test_ReadWrite: output reset"); michael@0: michael@0: let array2 = new (ctypes.ArrayType(ctypes.char, 4096))(); michael@0: let bytes_read2 = new ctypes.uint32_t(0); michael@0: let bytes_read2_ptr = bytes_read2.address(); michael@0: let pos = 0; michael@0: while (true) { michael@0: result = OS.Win.File.ReadFile(input, array, 4096, bytes_read_ptr, null); michael@0: isnot(result, 0, "test_ReadWrite: input read succeeded"); michael@0: michael@0: result = OS.Win.File.ReadFile(output, array2, 4096, bytes_read2_ptr, null); michael@0: isnot(result, 0, "test_ReadWrite: output read succeeded"); michael@0: michael@0: is(bytes_read.value > 0, bytes_read2.value > 0, michael@0: "Both files contain data or neither does " + bytes_read.value + ", " + bytes_read2.value); michael@0: if (bytes_read.value == 0) { michael@0: break; michael@0: } michael@0: let bytes; michael@0: if (bytes_read.value != bytes_read2.value) { michael@0: // This would be surprising, but theoretically possible with a michael@0: // remote file system, I believe. michael@0: bytes = Math.min(bytes_read.value, bytes_read2.value); michael@0: pos += bytes; michael@0: result = OS.Win.File.SetFilePointer(input, pos, null, OS.Constants.Win.FILE_BEGIN); michael@0: isnot(result, 0, "test_ReadWrite: input seek succeeded"); michael@0: michael@0: result = OS.Win.File.SetFilePointer(output, pos, null, OS.Constants.Win.FILE_BEGIN); michael@0: isnot(result, 0, "test_ReadWrite: output seek succeeded"); michael@0: michael@0: } else { michael@0: bytes = bytes_read.value; 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_ReadWrite test complete"); michael@0: result = OS.Win.File.CloseHandle(input); michael@0: isnot(result, 0, "test_ReadWrite: inpout close succeeded"); michael@0: result = OS.Win.File.CloseHandle(output); michael@0: isnot(result, 0, "test_ReadWrite: outpout close succeeded"); michael@0: result = OS.Win.File.DeleteFile(output_name); michael@0: isnot(result, 0, "test_ReadWrite: output remove succeeded"); michael@0: info("test_ReadWrite 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.Win.File.CreateFile( michael@0: undefined, michael@0: OS.Constants.Win.GENERIC_READ, michael@0: 0, michael@0: null, michael@0: OS.Constants.Win.OPEN_EXISTING, michael@0: 0, michael@0: null); michael@0: } catch(e if e instanceof TypeError && e.message.indexOf("CreateFile") > -1) { michael@0: exceptionRaised = true; michael@0: } michael@0: michael@0: ok(exceptionRaised, "test_passing_undefined: exception gets thrown") michael@0: }