| OLD | NEW |
| 1 // FIXME: Delete this (tests should import their own keys). | |
| 2 function importTestKeys() | |
| 3 { | |
| 4 var data = asciiToUint8Array("16 bytes of key!"); | |
| 5 var extractable = true; | |
| 6 var keyUsages = ['wrapKey', 'unwrapKey', 'encrypt', 'decrypt', 'sign', 'veri
fy']; | |
| 7 | |
| 8 var hmacPromise = crypto.subtle.importKey('raw', data, {name: 'hmac', hash:
{name: 'sha-1'}}, extractable, keyUsages); | |
| 9 var aesCbcPromise = crypto.subtle.importKey('raw', data, {name: 'AES-CBC'},
extractable, keyUsages); | |
| 10 var aesCbcJustDecrypt = crypto.subtle.importKey('raw', data, {name: 'AES-CBC
'}, false, ['decrypt']); | |
| 11 // FIXME: use AES-CTR key type once it's implemented | |
| 12 var aesCtrPromise = crypto.subtle.importKey('raw', data, {name: 'AES-CBC'},
extractable, keyUsages); | |
| 13 var aesGcmPromise = crypto.subtle.importKey('raw', data, {name: 'AES-GCM'},
extractable, keyUsages); | |
| 14 var rsaSsaSha1PublicPromise = crypto.subtle.importKey('spki', hexStringToUin
t8Array(kKeyData.rsa1.spki), {name: 'RSASSA-PKCS1-v1_5', hash: {name: 'sha-1'}},
extractable, keyUsages); | |
| 15 var rsaSsaSha1PrivatePromise = crypto.subtle.importKey('pkcs8', hexStringToU
int8Array(kKeyData.rsa1.pkcs8), {name: 'RSASSA-PKCS1-v1_5', hash: {name: 'sha-1'
}}, extractable, keyUsages); | |
| 16 | |
| 17 return Promise.all([hmacPromise, aesCbcPromise, aesCbcJustDecrypt, aesCtrPro
mise, aesGcmPromise, rsaSsaSha1PublicPromise, rsaSsaSha1PrivatePromise]).then(fu
nction(results) { | |
| 18 return { | |
| 19 hmacSha1: results[0], | |
| 20 aesCbc: results[1], | |
| 21 aesCbcJustDecrypt: results[2], | |
| 22 aesCtr: results[3], | |
| 23 aesGcm: results[4], | |
| 24 rsaSsaSha1Public: results[5], | |
| 25 rsaSsaSha1Private: results[6], | |
| 26 }; | |
| 27 }); | |
| 28 } | |
| 29 | |
| 30 // Verifies that the given "bytes" holds the same value as "expectedHexString". | 1 // Verifies that the given "bytes" holds the same value as "expectedHexString". |
| 31 // "bytes" can be anything recognized by "bytesToHexString()". | 2 // "bytes" can be anything recognized by "bytesToHexString()". |
| 32 function bytesShouldMatchHexString(testDescription, expectedHexString, bytes) | 3 function bytesShouldMatchHexString(testDescription, expectedHexString, bytes) |
| 33 { | 4 { |
| 34 expectedHexString = "[" + expectedHexString.toLowerCase() + "]"; | 5 expectedHexString = "[" + expectedHexString.toLowerCase() + "]"; |
| 35 var actualHexString = "[" + bytesToHexString(bytes) + "]"; | 6 var actualHexString = "[" + bytesToHexString(bytes) + "]"; |
| 36 | 7 |
| 37 if (actualHexString === expectedHexString) { | 8 if (actualHexString === expectedHexString) { |
| 38 debug("PASS: " + testDescription + " should be " + expectedHexString + "
and was"); | 9 debug("PASS: " + testDescription + " should be " + expectedHexString + "
and was"); |
| 39 } else { | 10 } else { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 function asciiToUint8Array(str) | 53 function asciiToUint8Array(str) |
| 83 { | 54 { |
| 84 var chars = []; | 55 var chars = []; |
| 85 for (var i = 0; i < str.length; ++i) | 56 for (var i = 0; i < str.length; ++i) |
| 86 chars.push(str.charCodeAt(i)); | 57 chars.push(str.charCodeAt(i)); |
| 87 return new Uint8Array(chars); | 58 return new Uint8Array(chars); |
| 88 } | 59 } |
| 89 | 60 |
| 90 function failAndFinishJSTest(error) | 61 function failAndFinishJSTest(error) |
| 91 { | 62 { |
| 92 if (error) | 63 testFailed(error); |
| 93 debug(error); | |
| 94 finishJSTest(); | 64 finishJSTest(); |
| 95 } | 65 } |
| 96 | 66 |
| 97 // ===================================================== | |
| 98 // FIXME: Delete the addTask() functions (better to test results directly) | |
| 99 // ===================================================== | |
| 100 | |
| 101 numOutstandingTasks = 0; | |
| 102 | |
| 103 function addTask(promise) | |
| 104 { | |
| 105 numOutstandingTasks++; | |
| 106 | |
| 107 function taskFinished() | |
| 108 { | |
| 109 numOutstandingTasks--; | |
| 110 completeTestWhenAllTasksDone(); | |
| 111 } | |
| 112 | |
| 113 promise.then(taskFinished, taskFinished); | |
| 114 } | |
| 115 | |
| 116 function completeTestWhenAllTasksDone() | |
| 117 { | |
| 118 if (numOutstandingTasks == 0) { | |
| 119 finishJSTest(); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 function shouldRejectPromiseWithNull(code) | |
| 124 { | |
| 125 var promise = eval(code); | |
| 126 | |
| 127 function acceptCallback(result) | |
| 128 { | |
| 129 debug("FAIL: '" + code + "' accepted with " + result + " but should have
been rejected"); | |
| 130 } | |
| 131 | |
| 132 function rejectCallback(result) | |
| 133 { | |
| 134 if (result == null) | |
| 135 debug("PASS: '" + code + "' rejected with null"); | |
| 136 else | |
| 137 debug("FAIL: '" + code + "' rejected with " + result + " but was exp
ecting null"); | |
| 138 } | |
| 139 | |
| 140 addTask(promise.then(acceptCallback, rejectCallback)); | |
| 141 } | |
| 142 | |
| 143 function shouldAcceptPromise(code) | |
| 144 { | |
| 145 var promise = eval(code); | |
| 146 | |
| 147 function acceptCallback(result) | |
| 148 { | |
| 149 debug("PASS: '" + code + "' accepted with " + result); | |
| 150 } | |
| 151 | |
| 152 function rejectCallback(result) | |
| 153 { | |
| 154 debug("FAIL: '" + code + "' rejected with " + result); | |
| 155 } | |
| 156 | |
| 157 addTask(promise.then(acceptCallback, rejectCallback)); | |
| 158 } | |
| 159 | |
| 160 // ===================================================== | |
| 161 | |
| 162 // Returns a Promise for the cloned key. | 67 // Returns a Promise for the cloned key. |
| 163 function cloneKey(key) | 68 function cloneKey(key) |
| 164 { | 69 { |
| 165 // Sending an object through a MessagePort implicitly clones it. | 70 // Sending an object through a MessagePort implicitly clones it. |
| 166 // Use a single MessageChannel so requests complete in FIFO order. | 71 // Use a single MessageChannel so requests complete in FIFO order. |
| 167 var self = cloneKey; | 72 var self = cloneKey; |
| 168 if (!self.channel) { | 73 if (!self.channel) { |
| 169 self.channel = new MessageChannel(); | 74 self.channel = new MessageChannel(); |
| 170 self.callbacks = []; | 75 self.callbacks = []; |
| 171 self.channel.port1.addEventListener('message', function(e) { | 76 self.channel.port1.addEventListener('message', function(e) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 187 if (internals) | 92 if (internals) |
| 188 debug("Serialized key bytes: " + bytesToHexString(internals.serializeObj
ect(o))); | 93 debug("Serialized key bytes: " + bytesToHexString(internals.serializeObj
ect(o))); |
| 189 } | 94 } |
| 190 | 95 |
| 191 function shouldEvaluateAs(actual, expectedValue) | 96 function shouldEvaluateAs(actual, expectedValue) |
| 192 { | 97 { |
| 193 if (typeof expectedValue == "string") | 98 if (typeof expectedValue == "string") |
| 194 return shouldBeEqualToString(actual, expectedValue); | 99 return shouldBeEqualToString(actual, expectedValue); |
| 195 return shouldEvaluateTo(actual, expectedValue); | 100 return shouldEvaluateTo(actual, expectedValue); |
| 196 } | 101 } |
| OLD | NEW |