toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.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_unix.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,201 @@
     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 +  log("received message "+JSON.stringify(msg.data));
    1.11 +  self.onmessage = function(msg) {
    1.12 +    log("ignored message "+JSON.stringify(msg.data));
    1.13 +  };
    1.14 +  test_init();
    1.15 +  test_getcwd();
    1.16 +  test_open_close();
    1.17 +  test_create_file();
    1.18 +  test_access();
    1.19 +  test_read_write();
    1.20 +  test_passing_undefined();
    1.21 +  finish();
    1.22 +};
    1.23 +
    1.24 +function test_init() {
    1.25 +  info("Starting test_init");
    1.26 +  importScripts("resource://gre/modules/osfile.jsm");
    1.27 +}
    1.28 +
    1.29 +function test_open_close() {
    1.30 +  info("Starting test_open_close");
    1.31 +  is(typeof OS.Unix.File.open, "function", "OS.Unix.File.open is a function");
    1.32 +  let file = OS.Unix.File.open("chrome/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js", OS.Constants.libc.O_RDONLY, 0);
    1.33 +  isnot(file, -1, "test_open_close: opening succeeded");
    1.34 +  info("Close: "+OS.Unix.File.close.toSource());
    1.35 +  let result = OS.Unix.File.close(file);
    1.36 +  is(result, 0, "test_open_close: close succeeded");
    1.37 +
    1.38 +  file = OS.Unix.File.open("/i do not exist", OS.Constants.libc.O_RDONLY, 0);
    1.39 +  is(file, -1, "test_open_close: opening of non-existing file failed");
    1.40 +  is(ctypes.errno, OS.Constants.libc.ENOENT, "test_open_close: error is ENOENT");
    1.41 +}
    1.42 +
    1.43 +function test_create_file()
    1.44 +{
    1.45 +  info("Starting test_create_file");
    1.46 +  let file = OS.Unix.File.open("test.tmp", OS.Constants.libc.O_RDWR
    1.47 +                               | OS.Constants.libc.O_CREAT
    1.48 +                               | OS.Constants.libc.O_TRUNC,
    1.49 +                               OS.Constants.libc.S_IRWXU);
    1.50 +  isnot(file, -1, "test_create_file: file created");
    1.51 +  OS.Unix.File.close(file);
    1.52 +}
    1.53 +
    1.54 +function test_access()
    1.55 +{
    1.56 +  info("Starting test_access");
    1.57 +  let file = OS.Unix.File.open("test1.tmp", OS.Constants.libc.O_RDWR
    1.58 +                               | OS.Constants.libc.O_CREAT
    1.59 +                               | OS.Constants.libc.O_TRUNC,
    1.60 +                               OS.Constants.libc.S_IRWXU);
    1.61 +  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);
    1.62 +  is(result, 0, "first call to access() succeeded");
    1.63 +  OS.Unix.File.close(file);
    1.64 +
    1.65 +  file = OS.Unix.File.open("test1.tmp", OS.Constants.libc.O_WRONLY
    1.66 +                               | OS.Constants.libc.O_CREAT
    1.67 +                               | OS.Constants.libc.O_TRUNC,
    1.68 +                               OS.Constants.libc.S_IWUSR);
    1.69 +
    1.70 +  info("test_access: preparing second call to access()");
    1.71 +  result = OS.Unix.File.access("test2.tmp", OS.Constants.libc.R_OK
    1.72 +                        | OS.Constants.libc.W_OK
    1.73 +                        | OS.Constants.libc.X_OK
    1.74 +                        | OS.Constants.libc.F_OK);
    1.75 +  is(result, -1, "test_access: second call to access() failed as expected");
    1.76 +  is(ctypes.errno, OS.Constants.libc.ENOENT, "This is the correct error");
    1.77 +  OS.Unix.File.close(file);
    1.78 +}
    1.79 +
    1.80 +function test_getcwd()
    1.81 +{
    1.82 +  let array = new (ctypes.ArrayType(ctypes.char, 32768))();
    1.83 +  let path = OS.Unix.File.getcwd(array, array.length);
    1.84 +  if (ctypes.char.ptr(path).isNull()) {
    1.85 +    ok(false, "test_get_cwd: getcwd returned null, errno: " + ctypes.errno);
    1.86 +  }
    1.87 +  let path2;
    1.88 +  if (OS.Unix.File.get_current_dir_name) {
    1.89 +    path2 = OS.Unix.File.get_current_dir_name();
    1.90 +  } else {
    1.91 +    path2 = OS.Unix.File.getwd_auto(null);
    1.92 +  }
    1.93 +  if (ctypes.char.ptr(path2).isNull()) {
    1.94 +    ok(false, "test_get_cwd: getwd_auto/get_current_dir_name returned null, errno: " + ctypes.errno);
    1.95 +  }
    1.96 +  is(path.readString(), path2.readString(), "test_get_cwd: getcwd and getwd return the same path");
    1.97 +}
    1.98 +
    1.99 +function test_read_write()
   1.100 +{
   1.101 +  let output_name = "osfile_copy.tmp";
   1.102 +  // Copy file
   1.103 +  let input = OS.Unix.File.open(
   1.104 +    "chrome/toolkit/components/osfile/tests/mochi/worker_test_osfile_unix.js",
   1.105 +    OS.Constants.libc.O_RDONLY, 0);
   1.106 +  isnot(input, -1, "test_read_write: input file opened");
   1.107 +  let output = OS.Unix.File.open("osfile_copy.tmp", OS.Constants.libc.O_RDWR
   1.108 +    | OS.Constants.libc.O_CREAT
   1.109 +    | OS.Constants.libc.O_TRUNC,
   1.110 +    OS.Constants.libc.S_IRWXU);
   1.111 +  isnot(output, -1, "test_read_write: output file opened");
   1.112 +
   1.113 +  let array = new (ctypes.ArrayType(ctypes.char, 4096))();
   1.114 +  let bytes = -1;
   1.115 +  let total = 0;
   1.116 +  while (true) {
   1.117 +    bytes = OS.Unix.File.read(input, array, 4096);
   1.118 +    ok(bytes != undefined, "test_read_write: bytes is defined");
   1.119 +    isnot(bytes, -1, "test_read_write: no read error");
   1.120 +    let write_from = 0;
   1.121 +    if (bytes == 0) {
   1.122 +      break;
   1.123 +    }
   1.124 +    while (bytes > 0) {
   1.125 +      let ptr = array.addressOfElement(write_from);
   1.126 +      // Note: |write| launches an exception in case of error
   1.127 +      let written = OS.Unix.File.write(output, array, bytes);
   1.128 +      isnot(written, -1, "test_read_write: no write error");
   1.129 +      write_from += written;
   1.130 +      bytes -= written;
   1.131 +    }
   1.132 +    total += write_from;
   1.133 +  }
   1.134 +  info("test_read_write: copy complete " + total);
   1.135 +
   1.136 +  // Compare files
   1.137 +  let result;
   1.138 +  info("SEEK_SET: " + OS.Constants.libc.SEEK_SET);
   1.139 +  info("Input: " + input + "(" + input.toSource() + ")");
   1.140 +  info("Output: " + output + "(" + output.toSource() + ")");
   1.141 +  result = OS.Unix.File.lseek(input, 0, OS.Constants.libc.SEEK_SET);
   1.142 +  info("Result of lseek: " + result);
   1.143 +  isnot(result, -1, "test_read_write: input seek succeeded " + ctypes.errno);
   1.144 +  result = OS.Unix.File.lseek(output, 0, OS.Constants.libc.SEEK_SET);
   1.145 +  isnot(result, -1, "test_read_write: output seek succeeded " + ctypes.errno);
   1.146 +
   1.147 +  let array2 = new (ctypes.ArrayType(ctypes.char, 4096))();
   1.148 +  let bytes2 = -1;
   1.149 +  let pos = 0;
   1.150 +  while (true) {
   1.151 +    bytes = OS.Unix.File.read(input, array, 4096);
   1.152 +    isnot(bytes, -1, "test_read_write: input read succeeded");
   1.153 +    bytes2 = OS.Unix.File.read(output, array2, 4096);
   1.154 +    isnot(bytes, -1, "test_read_write: output read succeeded");
   1.155 +    is(bytes > 0, bytes2 > 0, "Both files contain data or neither does "+bytes+", "+bytes2);
   1.156 +    if (bytes == 0) {
   1.157 +      break;
   1.158 +    }
   1.159 +    if (bytes != bytes2) {
   1.160 +      // This would be surprising, but theoretically possible with a
   1.161 +      // remote file system, I believe.
   1.162 +      bytes = Math.min(bytes, bytes2);
   1.163 +      pos += bytes;
   1.164 +      result = OS.Unix.File.lseek(input, pos, OS.Constants.libc.SEEK_SET);
   1.165 +      isnot(result, -1, "test_read_write: input seek succeeded");
   1.166 +      result = OS.Unix.File.lseek(output, pos, OS.Constants.libc.SEEK_SET);
   1.167 +      isnot(result, -1, "test_read_write: output seek succeeded");
   1.168 +    } else {
   1.169 +      pos += bytes;
   1.170 +    }
   1.171 +    for (let i = 0; i < bytes; ++i) {
   1.172 +      if (array[i] != array2[i]) {
   1.173 +        ok(false, "Files do not match at position " + i
   1.174 +           + " ("+array[i] + "/"+array2[i] + ")");
   1.175 +      }
   1.176 +    }
   1.177 +  }
   1.178 +  info("test_read_write test complete");
   1.179 +  result = OS.Unix.File.close(input);
   1.180 +  isnot(result, -1, "test_read_write: input close succeeded");
   1.181 +  result = OS.Unix.File.close(output);
   1.182 +  isnot(result, -1, "test_read_write: output close succeeded");
   1.183 +  result = OS.Unix.File.unlink(output_name);
   1.184 +  isnot(result, -1, "test_read_write: input remove succeeded");
   1.185 +  info("test_read_write cleanup complete");
   1.186 +}
   1.187 +
   1.188 +function test_passing_undefined()
   1.189 +{
   1.190 +  info("Testing that an exception gets thrown when an FFI function is passed undefined");
   1.191 +  let exceptionRaised = false;
   1.192 +
   1.193 +  try {
   1.194 +    let file = OS.Unix.File.open(undefined, OS.Constants.libc.O_RDWR
   1.195 +                                            | OS.Constants.libc.O_CREAT
   1.196 +                                            | OS.Constants.libc.O_TRUNC,
   1.197 +                                            OS.Constants.libc.S_IRWXU);
   1.198 +  } catch(e if e instanceof TypeError && e.message.indexOf("open") > -1) {
   1.199 +    exceptionRaised = true;
   1.200 +  }
   1.201 +
   1.202 +  ok(exceptionRaised, "test_passing_undefined: exception gets thrown")
   1.203 +}
   1.204 +

mercurial