| OLD | NEW |
| (Empty) |
| 1 function logError(error) | |
| 2 { | |
| 3 debug("error is: " + error.toString()); | |
| 4 } | |
| 5 | |
| 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) | |
| 9 { | |
| 10 expectedHexString = "[" + expectedHexString.toLowerCase() + "]"; | |
| 11 var actualHexString = "[" + bytesToHexString(bytes) + "]"; | |
| 12 | |
| 13 if (actualHexString === expectedHexString) { | |
| 14 debug("PASS: " + testDescription + " should be " + expectedHexString + "
and was"); | |
| 15 } else { | |
| 16 debug("FAIL: " + testDescription + " should be " + expectedHexString + "
but was " + actualHexString); | |
| 17 } | |
| 18 } | |
| 19 | |
| 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: | |
| 23 // ab034c99 | |
| 24 function bytesToHexString(bytes) | |
| 25 { | |
| 26 if (!bytes) | |
| 27 return null; | |
| 28 | |
| 29 bytes = new Uint8Array(bytes); | |
| 30 var hexBytes = []; | |
| 31 | |
| 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); | |
| 37 } | |
| 38 | |
| 39 return hexBytes.join(""); | |
| 40 } | |
| 41 | |
| 42 function bytesToASCIIString(bytes) | |
| 43 { | |
| 44 return String.fromCharCode.apply(null, new Uint8Array(bytes)); | |
| 45 } | |
| 46 | |
| 47 function hexStringToUint8Array(hexString) | |
| 48 { | |
| 49 if (hexString.length % 2 != 0) | |
| 50 throw "Invalid hexString"; | |
| 51 var arrayBuffer = new Uint8Array(hexString.length / 2); | |
| 52 | |
| 53 for (var i = 0; i < hexString.length; i += 2) { | |
| 54 var byteValue = parseInt(hexString.substr(i, 2), 16); | |
| 55 if (byteValue == NaN) | |
| 56 throw "Invalid hexString"; | |
| 57 arrayBuffer[i/2] = byteValue; | |
| 58 } | |
| 59 | |
| 60 return arrayBuffer; | |
| 61 } | |
| 62 | |
| 63 function asciiToUint8Array(str) | |
| 64 { | |
| 65 var chars = []; | |
| 66 for (var i = 0; i < str.length; ++i) | |
| 67 chars.push(str.charCodeAt(i)); | |
| 68 return new Uint8Array(chars); | |
| 69 } | |
| 70 | |
| 71 var Base64URL = { | |
| 72 stringify: function (a) { | |
| 73 var base64string = btoa(String.fromCharCode.apply(0, a)); | |
| 74 return base64string.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g,
"_"); | |
| 75 }, | |
| 76 parse: function (s) { | |
| 77 s = s.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, ''); | |
| 78 return new Uint8Array(Array.prototype.map.call(atob(s), function (c) { r
eturn c.charCodeAt(0) })); | |
| 79 } | |
| 80 }; | |
| 81 | |
| 82 function failAndFinishJSTest(error) | |
| 83 { | |
| 84 testFailed('' + error); | |
| 85 finishJSTest(); | |
| 86 } | |
| 87 | |
| 88 // Returns a Promise for the cloned key. | |
| 89 function cloneKey(key) | |
| 90 { | |
| 91 // Sending an object through a MessagePort implicitly clones it. | |
| 92 // Use a single MessageChannel so requests complete in FIFO order. | |
| 93 var self = cloneKey; | |
| 94 if (!self.channel) { | |
| 95 self.channel = new MessageChannel(); | |
| 96 self.callbacks = []; | |
| 97 self.channel.port1.addEventListener('message', function(e) { | |
| 98 var callback = self.callbacks.shift(); | |
| 99 callback(e.data); | |
| 100 }, false); | |
| 101 self.channel.port1.start(); | |
| 102 } | |
| 103 | |
| 104 return new Promise(function(resolve, reject) { | |
| 105 self.callbacks.push(resolve); | |
| 106 self.channel.port2.postMessage(key); | |
| 107 }); | |
| 108 } | |
| 109 | |
| 110 // Logging the serialized format ensures that if it changes it will break tests. | |
| 111 function logSerializedKey(o) | |
| 112 { | |
| 113 if (internals) { | |
| 114 // Removing the version tag from the output so serialization format chan
ges 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(serializedWithoutVersi
on)); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 function shouldEvaluateAs(actual, expectedValue) | |
| 122 { | |
| 123 if (typeof expectedValue == "string") | |
| 124 return shouldBeEqualToString(actual, expectedValue); | |
| 125 return shouldEvaluateTo(actual, expectedValue); | |
| 126 } | |
| OLD | NEW |