1 function logError(error
)
3 debug("error is: " + error
.toString());
6 // Verifies that the given "bytes" holds the same value as "expectedHexString".
7 // "bytes" can be anything recognized by "bytesToHexString()".
8 function bytesShouldMatchHexString(testDescription
, expectedHexString
, bytes
)
10 expectedHexString
= "[" + expectedHexString
.toLowerCase() + "]";
11 var actualHexString
= "[" + bytesToHexString(bytes
) + "]";
13 if (actualHexString
=== expectedHexString
) {
14 debug("PASS: " + testDescription
+ " should be " + expectedHexString
+ " and was");
16 debug("FAIL: " + testDescription
+ " should be " + expectedHexString
+ " but was " + actualHexString
);
20 // Builds a hex string representation for an array-like input.
21 // "bytes" can be an Array of bytes, an ArrayBuffer, or any TypedArray.
22 // The output looks like this:
24 function bytesToHexString(bytes
)
29 bytes
= new Uint8Array(bytes
);
32 for (var i
= 0; i
< bytes
.length
; ++i
) {
33 var byteString
= bytes
[i
].toString(16);
34 if (byteString
.length
< 2)
35 byteString
= "0" + byteString
;
36 hexBytes
.push(byteString
);
39 return hexBytes
.join("");
42 function bytesToASCIIString(bytes
)
44 return String
.fromCharCode
.apply(null, new Uint8Array(bytes
));
47 function hexStringToUint8Array(hexString
)
49 if (hexString
.length
% 2 != 0)
50 throw "Invalid hexString";
51 var arrayBuffer
= new Uint8Array(hexString
.length
/ 2);
53 for (var i
= 0; i
< hexString
.length
; i
+= 2) {
54 var byteValue
= parseInt(hexString
.substr(i
, 2), 16);
56 throw "Invalid hexString";
57 arrayBuffer
[i
/2] = byteValue
;
63 function asciiToUint8Array(str
)
66 for (var i
= 0; i
< str
.length
; ++i
)
67 chars
.push(str
.charCodeAt(i
));
68 return new Uint8Array(chars
);
72 stringify: function (a
) {
73 var base64string
= btoa(String
.fromCharCode
.apply(0, a
));
74 return base64string
.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
77 s
= s
.replace(/-/g, "+").replace(/_
/g, "/").replace(/\s/g, '');
78 return new Uint8Array(Array.prototype.map.call(atob(s), function (c) { return c.charCodeAt(0) }));
82 function failAndFinishJSTest(error)
84 testFailed('' + error);
88 // Returns a Promise for the cloned key.
89 function cloneKey(key)
91 // Sending an object through a MessagePort implicitly clones it.
92 // Use a single MessageChannel so requests complete in FIFO order.
95 self.channel = new MessageChannel();
97 self.channel.port1.addEventListener('message', function(e) {
98 var callback = self.callbacks.shift();
101 self.channel.port1.start();
104 return new Promise(function(resolve, reject) {
105 self.callbacks.push(resolve);
106 self.channel.port2.postMessage(key);
110 // Logging the serialized format ensures that if it changes it will break tests.
111 function logSerializedKey(o)
114 // Removing the version tag from the output so serialization format changes don't need to update all the crypto tests.
115 var serialized = internals.serializeObject(o);
116 var serializedWithoutVersion = new Uint8Array(serialized, 2);
117 debug("Serialized key bytes
: " + bytesToHexString(serializedWithoutVersion));
121 function shouldEvaluateAs(actual, expectedValue)
123 if (typeof expectedValue == "string
")
124 return shouldBeEqualToString(actual, expectedValue);
125 return shouldEvaluateTo(actual, expectedValue);