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.

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

mercurial