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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2  * http://creativecommons.org/publicdomain/zero/1.0/ */
     4 "use strict";
     6 importScripts('worker_test_osfile_shared.js');
     8 // The set of samples for communications test. Declare as a global
     9 // variable to prevent this from being garbage-collected too early.
    10 let samples;
    12 self.onmessage = function(msg) {
    13   info("Initializing");
    14   self.onmessage = function on_unexpected_message(msg) {
    15     throw new Error("Unexpected message " + JSON.stringify(msg.data));
    16   };
    17   importScripts("resource://gre/modules/osfile.jsm");
    18   info("Initialization complete");
    20   samples = [
    21     { typename: "OS.Shared.Type.char.in_ptr",
    22       valuedescr: "String",
    23       value: "This is a test",
    24       type: OS.Shared.Type.char.in_ptr,
    25       check: function check_string(candidate, prefix) {
    26         is(candidate, "This is a test", prefix);
    27       }},
    28     { typename: "OS.Shared.Type.char.in_ptr",
    29       valuedescr: "Typed array",
    30       value: (function() {
    31                 let view = new Uint8Array(15);
    32                 for (let i = 0; i < 15; ++i) {
    33                   view[i] = i;
    34                 }
    35                 return view;
    36               })(),
    37       type: OS.Shared.Type.char.in_ptr,
    38       check: function check_ArrayBuffer(candidate, prefix) {
    39         let cast = ctypes.cast(candidate, ctypes.uint8_t.ptr);
    40         for (let i = 0; i < 15; ++i) {
    41           is(cast.contents, i % 256, prefix + "Checking that the contents of the ArrayBuffer were preserved");
    42           cast = cast.increment();
    43         }
    44       }},
    45     { typename: "OS.Shared.Type.char.in_ptr",
    46       valuedescr: "Pointer",
    47       value: new OS.Shared.Type.char.in_ptr.implementation(1),
    48       type: OS.Shared.Type.char.in_ptr,
    49       check: function check_ptr(candidate, prefix) {
    50         let address = ctypes.cast(candidate, ctypes.uintptr_t).value.toString();
    51         is(address, "1", prefix + "Checking that the pointer address was preserved");
    52       }},
    53     { typename: "OS.Shared.Type.char.in_ptr",
    54       valuedescr: "C array",
    55       value: (function() {
    56                 let buf = new (ctypes.ArrayType(ctypes.uint8_t, 15))();
    57                 for (let i = 0; i < 15; ++i) {
    58                   buf[i] = i % 256;
    59                 }
    60                 return buf;
    61               })(),
    62       type: OS.Shared.Type.char.in_ptr,
    63       check: function check_array(candidate, prefix) {
    64         let cast = ctypes.cast(candidate, ctypes.uint8_t.ptr);
    65         for (let i = 0; i < 15; ++i) {
    66           is(cast.contents, i % 256, prefix + "Checking that the contents of the C array were preserved, index " + i);
    67           cast = cast.increment();
    68         }
    69       }
    70     },
    71     { typename: "OS.File.Error",
    72       valuedescr: "OS Error",
    73       type: OS.File.Error,
    74       value: new OS.File.Error("foo", 1),
    75       check: function check_error(candidate, prefix) {
    76         ok(candidate instanceof OS.File.Error,
    77           prefix + "Error is an OS.File.Error");
    78         ok(candidate.unixErrno == 1 || candidate.winLastError == 1,
    79            prefix + "Error code is correct");
    80         try {
    81           let string = candidate.toString();
    82           info(prefix + ".toString() works " + string);
    83         } catch (x) {
    84           ok(false, prefix + ".toString() fails " + x);
    85         }
    86       }
    87     }
    88   ];
    89   samples.forEach(function test(sample) {
    90     let type = sample.type;
    91     let value = sample.value;
    92     let check = sample.check;
    93     info("Testing handling of type " + sample.typename + " communicating " + sample.valuedescr);
    95     // 1. Test serialization
    96     let serialized;
    97     let exn;
    98     try {
    99       serialized = type.toMsg(value);
   100     } catch (ex) {
   101       exn = ex;
   102     }
   103     is(exn, null, "Can I serialize the following value? " + value +
   104       " aka " + JSON.stringify(value));
   105     if (exn) {
   106       return;
   107     }
   109     // 2. Test deserialization
   110     let deserialized;
   111     try {
   112       deserialized = type.fromMsg(serialized);
   113     } catch (ex) {
   114       exn = ex;
   115     }
   116     is(exn, null, "Can I deserialize the following message? " + serialized
   117      + " aka " + JSON.stringify(serialized));
   118     if (exn) {
   119       return;
   120     }
   122     // 3. Local test deserialized value
   123     info("Running test on deserialized value " + serialized);
   124     check(deserialized, "Local test: ");
   126     // 4. Test sending serialized
   127     info("Attempting to send message");
   128     try {
   129       self.postMessage({kind:"value",
   130         typename: sample.typename,
   131         value: serialized,
   132         check: check.toSource()});
   133     } catch (ex) {
   134       exn = ex;
   135     }
   136     is(exn, null, "Can I send the following message? " + serialized
   137      + " aka " + JSON.stringify(serialized));
   138   });
   140   finish();
   141  };

mercurial