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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/osfile/tests/mochi/worker_test_osfile_win.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,211 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +importScripts('worker_test_osfile_shared.js');
     1.8 +
     1.9 +self.onmessage = function(msg) {
    1.10 +  self.onmessage = function(msg) {
    1.11 +    log("ignored message "+JSON.stringify(msg.data));
    1.12 +  };
    1.13 +
    1.14 +  test_init();
    1.15 +  test_GetCurrentDirectory();
    1.16 +  test_OpenClose();
    1.17 +  test_CreateFile();
    1.18 +  test_ReadWrite();
    1.19 +  test_passing_undefined();
    1.20 +  finish();
    1.21 +};
    1.22 +
    1.23 +function test_init() {
    1.24 +  info("Starting test_init");
    1.25 +  importScripts("resource://gre/modules/osfile.jsm");
    1.26 +}
    1.27 +
    1.28 +function test_OpenClose() {
    1.29 +  info("Starting test_OpenClose");
    1.30 +  is(typeof OS.Win.File.CreateFile, "function", "OS.Win.File.CreateFile is a function");
    1.31 +  is(OS.Win.File.CloseHandle(OS.Constants.Win.INVALID_HANDLE_VALUE), true, "CloseHandle returns true given the invalid handle");
    1.32 +  is(OS.Win.File.FindClose(OS.Constants.Win.INVALID_HANDLE_VALUE), true, "FindClose returns true given the invalid handle");
    1.33 +  isnot(OS.Constants.Win.GENERIC_READ, undefined, "GENERIC_READ exists");
    1.34 +  isnot(OS.Constants.Win.FILE_SHARE_READ, undefined, "FILE_SHARE_READ exists");
    1.35 +  isnot(OS.Constants.Win.FILE_ATTRIBUTE_NORMAL, undefined, "FILE_ATTRIBUTE_NORMAL exists");
    1.36 +  let file = OS.Win.File.CreateFile(
    1.37 +    "chrome\\toolkit\\components\\osfile\\tests\\mochi\\worker_test_osfile_win.js",
    1.38 +    OS.Constants.Win.GENERIC_READ,
    1.39 +    0,
    1.40 +    null,
    1.41 +    OS.Constants.Win.OPEN_EXISTING,
    1.42 +    0,
    1.43 +    null);
    1.44 +  info("test_OpenClose: Passed open");
    1.45 +  isnot(file, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_OpenClose: file opened");
    1.46 +  let result = OS.Win.File.CloseHandle(file);
    1.47 +  isnot(result, 0, "test_OpenClose: close succeeded");
    1.48 +
    1.49 +  file = OS.Win.File.CreateFile(
    1.50 +    "\\I do not exist",
    1.51 +    OS.Constants.Win.GENERIC_READ,
    1.52 +    OS.Constants.Win.FILE_SHARE_READ,
    1.53 +    null,
    1.54 +    OS.Constants.Win.OPEN_EXISTING,
    1.55 +    OS.Constants.Win.FILE_ATTRIBUTE_NORMAL,
    1.56 +    null);
    1.57 +  is(file, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_OpenClose: cannot open non-existing file");
    1.58 +  is(ctypes.winLastError, OS.Constants.Win.ERROR_FILE_NOT_FOUND, "test_OpenClose: error is ERROR_FILE_NOT_FOUND");
    1.59 +}
    1.60 +
    1.61 +function test_CreateFile()
    1.62 +{
    1.63 +  info("Starting test_CreateFile");
    1.64 +  let file = OS.Win.File.CreateFile(
    1.65 +    "test.tmp",
    1.66 +    OS.Constants.Win.GENERIC_READ | OS.Constants.Win.GENERIC_WRITE,
    1.67 +    OS.Constants.Win.FILE_SHARE_READ | OS.Constants.FILE_SHARE_WRITE,
    1.68 +    null,
    1.69 +    OS.Constants.Win.CREATE_ALWAYS,
    1.70 +    OS.Constants.Win.FILE_ATTRIBUTE_NORMAL,
    1.71 +    null);
    1.72 +  isnot(file, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_CreateFile: opening succeeded");
    1.73 +  let result = OS.Win.File.CloseHandle(file);
    1.74 +  isnot(result, 0, "test_CreateFile: close succeeded");
    1.75 +}
    1.76 +
    1.77 +function test_GetCurrentDirectory()
    1.78 +{
    1.79 +  let array = new (ctypes.ArrayType(ctypes.jschar, 4096))();
    1.80 +  let result = OS.Win.File.GetCurrentDirectory(4096, array);
    1.81 +  ok(result < array.length, "test_GetCurrentDirectory: length sufficient");
    1.82 +  ok(result > 0, "test_GetCurrentDirectory: length != 0");
    1.83 +}
    1.84 +
    1.85 +function test_ReadWrite()
    1.86 +{
    1.87 +  info("Starting test_ReadWrite");
    1.88 +  let output_name = "osfile_copy.tmp";
    1.89 +  // Copy file
    1.90 +  let input = OS.Win.File.CreateFile(
    1.91 +    "chrome\\toolkit\\components\\osfile\\tests\\mochi\\worker_test_osfile_win.js",
    1.92 +     OS.Constants.Win.GENERIC_READ,
    1.93 +     0,
    1.94 +     null,
    1.95 +     OS.Constants.Win.OPEN_EXISTING,
    1.96 +     0,
    1.97 +     null);
    1.98 +  isnot(input, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_ReadWrite: input file opened");
    1.99 +  let output = OS.Win.File.CreateFile(
   1.100 +     "osfile_copy.tmp",
   1.101 +     OS.Constants.Win.GENERIC_READ | OS.Constants.Win.GENERIC_WRITE,
   1.102 +     0,
   1.103 +     null,
   1.104 +     OS.Constants.Win.CREATE_ALWAYS,
   1.105 +     OS.Constants.Win.FILE_ATTRIBUTE_NORMAL,
   1.106 +     null);
   1.107 +  isnot(output, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_ReadWrite: output file opened");
   1.108 +  let array = new (ctypes.ArrayType(ctypes.char, 4096))();
   1.109 +  let bytes_read = new ctypes.uint32_t(0);
   1.110 +  let bytes_read_ptr = bytes_read.address();
   1.111 +  log("We have a pointer for bytes read: "+bytes_read_ptr);
   1.112 +  let bytes_written = new ctypes.uint32_t(0);
   1.113 +  let bytes_written_ptr = bytes_written.address();
   1.114 +  log("We have a pointer for bytes written: "+bytes_written_ptr);
   1.115 +  log("test_ReadWrite: buffer and pointers ready");
   1.116 +  let result;
   1.117 +  while (true) {
   1.118 +    log("test_ReadWrite: reading");
   1.119 +    result = OS.Win.File.ReadFile(input, array, 4096, bytes_read_ptr, null);
   1.120 +    isnot (result, 0, "test_ReadWrite: read success");
   1.121 +    let write_from = 0;
   1.122 +    let bytes_left = bytes_read;
   1.123 +    log("test_ReadWrite: read chunk complete " + bytes_left.value);
   1.124 +    if (bytes_left.value == 0) {
   1.125 +      break;
   1.126 +    }
   1.127 +    while (bytes_left.value > 0) {
   1.128 +      log("test_ReadWrite: writing "+bytes_left.value);
   1.129 +      let ptr = array.addressOfElement(write_from);
   1.130 +      // Note: |WriteFile| launches an exception in case of error
   1.131 +      result = OS.Win.File.WriteFile(output, array, bytes_left, bytes_written_ptr, null);
   1.132 +      isnot (result, 0, "test_ReadWrite: write success");
   1.133 +      write_from += bytes_written;
   1.134 +      bytes_left -= bytes_written;
   1.135 +    }
   1.136 +  }
   1.137 +  info("test_ReadWrite: copy complete");
   1.138 +
   1.139 +  // Compare files
   1.140 +  result = OS.Win.File.SetFilePointer(input, 0, null, OS.Constants.Win.FILE_BEGIN);
   1.141 +  isnot (result, OS.Constants.Win.INVALID_SET_FILE_POINTER, "test_ReadWrite: input reset");
   1.142 +
   1.143 +  result = OS.Win.File.SetFilePointer(output, 0, null, OS.Constants.Win.FILE_BEGIN);
   1.144 +  isnot (result, OS.Constants.Win.INVALID_SET_FILE_POINTER, "test_ReadWrite: output reset");
   1.145 +
   1.146 +  let array2 = new (ctypes.ArrayType(ctypes.char, 4096))();
   1.147 +  let bytes_read2 = new ctypes.uint32_t(0);
   1.148 +  let bytes_read2_ptr = bytes_read2.address();
   1.149 +  let pos = 0;
   1.150 +  while (true) {
   1.151 +    result = OS.Win.File.ReadFile(input, array, 4096, bytes_read_ptr, null);
   1.152 +    isnot(result, 0, "test_ReadWrite: input read succeeded");
   1.153 +
   1.154 +    result = OS.Win.File.ReadFile(output, array2, 4096, bytes_read2_ptr, null);
   1.155 +    isnot(result, 0, "test_ReadWrite: output read succeeded");
   1.156 +
   1.157 +    is(bytes_read.value > 0, bytes_read2.value > 0,
   1.158 +       "Both files contain data or neither does " + bytes_read.value + ", " + bytes_read2.value);
   1.159 +    if (bytes_read.value == 0) {
   1.160 +      break;
   1.161 +    }
   1.162 +    let bytes;
   1.163 +    if (bytes_read.value != bytes_read2.value) {
   1.164 +      // This would be surprising, but theoretically possible with a
   1.165 +      // remote file system, I believe.
   1.166 +      bytes = Math.min(bytes_read.value, bytes_read2.value);
   1.167 +      pos += bytes;
   1.168 +      result = OS.Win.File.SetFilePointer(input,  pos, null, OS.Constants.Win.FILE_BEGIN);
   1.169 +      isnot(result, 0, "test_ReadWrite: input seek succeeded");
   1.170 +
   1.171 +      result = OS.Win.File.SetFilePointer(output, pos, null, OS.Constants.Win.FILE_BEGIN);
   1.172 +      isnot(result, 0, "test_ReadWrite: output seek succeeded");
   1.173 +
   1.174 +    } else {
   1.175 +      bytes = bytes_read.value;
   1.176 +      pos += bytes;
   1.177 +    }
   1.178 +    for (let i = 0; i < bytes; ++i) {
   1.179 +      if (array[i] != array2[i]) {
   1.180 +        ok(false, "Files do not match at position " + i
   1.181 +           + " ("+array[i] + "/"+array2[i] + ")");
   1.182 +      }
   1.183 +    }
   1.184 +  }
   1.185 +  info("test_ReadWrite test complete");
   1.186 +  result = OS.Win.File.CloseHandle(input);
   1.187 +  isnot(result, 0, "test_ReadWrite: inpout close succeeded");
   1.188 +  result = OS.Win.File.CloseHandle(output);
   1.189 +  isnot(result, 0, "test_ReadWrite: outpout close succeeded");
   1.190 +  result = OS.Win.File.DeleteFile(output_name);
   1.191 +  isnot(result, 0, "test_ReadWrite: output remove succeeded");
   1.192 +  info("test_ReadWrite cleanup complete");
   1.193 +}
   1.194 +
   1.195 +function test_passing_undefined()
   1.196 +{
   1.197 +  info("Testing that an exception gets thrown when an FFI function is passed undefined");
   1.198 +  let exceptionRaised = false;
   1.199 +
   1.200 +  try {
   1.201 +    let file = OS.Win.File.CreateFile(
   1.202 +      undefined,
   1.203 +      OS.Constants.Win.GENERIC_READ,
   1.204 +      0,
   1.205 +      null,
   1.206 +      OS.Constants.Win.OPEN_EXISTING,
   1.207 +      0,
   1.208 +      null);
   1.209 +  } catch(e if e instanceof TypeError && e.message.indexOf("CreateFile") > -1) {
   1.210 +    exceptionRaised = true;
   1.211 +  }
   1.212 +
   1.213 +  ok(exceptionRaised, "test_passing_undefined: exception gets thrown")
   1.214 +}

mercurial