()
| 1 | function define_tests() { |
| 2 | // May want to test prefixed implementations. |
| 3 | var subtle = self.crypto.subtle; |
| 4 | |
| 5 | // pbkdf2_vectors sets up test data with the correct derivations for each |
| 6 | // test case. |
| 7 | var testData = getTestData(); |
| 8 | var passwords = testData.passwords; |
| 9 | var salts = testData.salts; |
| 10 | var derivations = testData.derivations; |
| 11 | |
| 12 | // What kinds of keys can be created with deriveKey? The following: |
| 13 | var derivedKeyTypes = testData.derivedKeyTypes; |
| 14 | |
| 15 | return setUpBaseKeys(passwords) |
| 16 | .then(function(allKeys) { |
| 17 | // We get several kinds of base keys. Normal ones that can be used for |
| 18 | // derivation operations, ones that lack the deriveBits usage, ones |
| 19 | // that lack the deriveKeys usage, and one key that is for the wrong |
| 20 | // algorithm (not PBKDF2 in this case). |
| 21 | var baseKeys = allKeys.baseKeys; |
| 22 | var noBits = allKeys.noBits; |
| 23 | var noKey = allKeys.noKey; |
| 24 | var wrongKey = allKeys.wrongKey; |
| 25 | |
| 26 | // Test each combination of password size, salt size, hash function, |
| 27 | // and number of iterations. The derivations object is structured in |
| 28 | // that way, so navigate it to run tests and compare with correct results. |
| 29 | Object.keys(derivations).forEach(function(passwordSize) { |
| 30 | Object.keys(derivations[passwordSize]).forEach(function(saltSize) { |
| 31 | Object.keys(derivations[passwordSize][saltSize]).forEach(function(hashName) { |
| 32 | Object.keys(derivations[passwordSize][saltSize][hashName]).forEach(function(iterations) { |
| 33 | var testName = passwordSize + " password, " + saltSize + " salt, " + hashName + ", with " + iterations + " iterations"; |
| 34 | |
| 35 | // Check for correct deriveBits result |
| 36 | subsetTest(promise_test, function(test) { |
| 37 | return subtle.deriveBits({name: "PBKDF2", salt: salts[saltSize], hash: hashName, iterations: parseInt(iterations)}, baseKeys[passwordSize], 256) |
| 38 | .then(function(derivation) { |
| 39 | assert_true(equalBuffers(derivation, derivations[passwordSize][saltSize][hashName][iterations]), "Derived correct key"); |
| 40 | }, function(err) { |
| 41 | assert_unreached("deriveBits failed with error " + err.name + ": " + err.message); |
| 42 | }); |
| 43 | }, testName); |
| 44 | |
| 45 | // 0 length |
| 46 | subsetTest(promise_test, function(test) { |
| 47 | return subtle.deriveBits({name: "PBKDF2", salt: salts[saltSize], hash: hashName, iterations: parseInt(iterations)}, baseKeys[passwordSize], 0) |
| 48 | .then(function(derivation) { |
| 49 | assert_true(equalBuffers(derivation.byteLength, 0, "Derived correctly empty key")); |
| 50 | }, function(err) { |
| 51 | assert_unreached("deriveBits failed with error " + err.name + ": " + err.message); |
| 52 | }); |
| 53 | }, testName + " with 0 length"); |
| 54 | |
| 55 | // Check for correct deriveKey results for every kind of |
| 56 | // key that can be created by the deriveKeys operation. |
| 57 | derivedKeyTypes.forEach(function(derivedKeyType) { |
| 58 | var testName = "Derived key of type "; |
| 59 | Object.keys(derivedKeyType.algorithm).forEach(function(prop) { |
| 60 | testName += prop + ": " + derivedKeyType.algorithm[prop] + " "; |
nothing calls this directly
no test coverage detected