Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: LayoutTests/crypto/normalize-algorithm.html

Issue 20571002: WebCrypto: Add the Key parameter to a number of operations. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Previous upload failed... Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | Source/modules/crypto/SubtleCrypto.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 <!DOCTYPE html> 1 <!DOCTYPE html>
2 <html> 2 <html>
3 <head> 3 <head>
4 <script src="../fast/js/resources/js-test-pre.js"></script> 4 <script src="../fast/js/resources/js-test-pre.js"></script>
5 </head> 5 </head>
6 <body> 6 <body>
7 <p id="description"></p> 7 <p id="description"></p>
8 <div id="console"></div> 8 <div id="console"></div>
9 9
10 <script> 10 <script>
11 description("Tests algorithm normalization."); 11 description("Tests algorithm normalization.");
12 12
13 jsTestIsAsync = true;
14
13 // ------------------------------- 15 // -------------------------------
14 // Helpers to return a normalized algorithm identifier. 16 // Helpers to return a normalized algorithm identifier.
15 // ------------------------------- 17 // -------------------------------
16 18
17 function normalizeDigest(algorithmIdentifier) { 19 key = null;
20
21 function normalizeDigest(algorithmIdentifier)
22 {
18 return crypto.subtle.digest(algorithmIdentifier).algorithm; 23 return crypto.subtle.digest(algorithmIdentifier).algorithm;
19 } 24 }
20 25
21 function normalizeEncrypt(algorithmIdentifier) { 26 function normalizeEncrypt(algorithmIdentifier)
22 // TODO(eroman): Use a valid key. 27 {
23 var key;
24 return crypto.subtle.encrypt(algorithmIdentifier, key).algorithm; 28 return crypto.subtle.encrypt(algorithmIdentifier, key).algorithm;
25 } 29 }
26 30
27 function normalizeSign(algorithmIdentifier) { 31 function normalizeSign(algorithmIdentifier)
28 // TODO(eroman): Use a valid key. 32 {
29 var key;
30 return crypto.subtle.sign(algorithmIdentifier, key).algorithm; 33 return crypto.subtle.sign(algorithmIdentifier, key).algorithm;
31 } 34 }
32 35
33 // ------------------------------- 36 function runTests()
34 // Case insensitivity of "name" 37 {
35 // ------------------------------- 38 // -------------------------------
36 algorithm = normalizeDigest({name: "SHA-1"}); 39 // Case insensitivity of "name"
37 shouldBe("algorithm.name", "'SHA-1'"); 40 // -------------------------------
38 algorithm = normalizeDigest({name: "sHa-256"}); 41 algorithm = normalizeDigest({name: "SHA-1"});
39 shouldBe("algorithm.name", "'SHA-256'"); 42 shouldBe("algorithm.name", "'SHA-1'");
43 algorithm = normalizeDigest({name: "sHa-256"});
44 shouldBe("algorithm.name", "'SHA-256'");
40 45
41 // ------------------------------- 46 // -------------------------------
42 // Failures if "name" is invalid 47 // Failures if "name" is invalid
43 // ------------------------------- 48 // -------------------------------
44 shouldThrow("normalizeDigest({})"); 49 shouldThrow("normalizeDigest({})");
45 shouldThrow("normalizeDigest({name: null})"); 50 shouldThrow("normalizeDigest({name: null})");
46 shouldThrow("normalizeDigest({name: -1})"); 51 shouldThrow("normalizeDigest({name: -1})");
47 shouldThrow("normalizeDigest({name: ''})"); 52 shouldThrow("normalizeDigest({name: ''})");
48 shouldThrow("normalizeDigest({name: 'nosuchalgorithm'})"); 53 shouldThrow("normalizeDigest({name: 'nosuchalgorithm'})");
49 shouldThrow("normalizeDigest({name: '\\u0189'})"); 54 shouldThrow("normalizeDigest({name: '\\u0189'})");
50 55
51 // ------------------------------- 56 // -------------------------------
52 // Failures if the algorithm identifier is not an object 57 // Failures if the algorithm identifier is not an object
53 // ------------------------------- 58 // -------------------------------
54 shouldThrow("normalizeDigest(null)"); 59 shouldThrow("normalizeDigest(null)");
55 shouldThrow("normalizeDigest(0)"); 60 shouldThrow("normalizeDigest(0)");
56 shouldThrow("normalizeDigest(undefined)"); 61 shouldThrow("normalizeDigest(undefined)");
57 shouldThrow("normalizeDigest('')"); 62 shouldThrow("normalizeDigest('')");
58 63
59 // ------------------------------- 64 // -------------------------------
60 // Skip unrecognized parameters. 65 // Skip unrecognized parameters.
61 // ------------------------------- 66 // -------------------------------
62 algorithm = normalizeDigest({name: "sHa-1", noSuchParam: 3}); 67 algorithm = normalizeDigest({name: "sHa-1", noSuchParam: 3});
63 shouldBeUndefined("algorithm.noSuchParam"); 68 shouldBeUndefined("algorithm.noSuchParam");
64 69
65 // ------------------------------- 70 // -------------------------------
66 // Normalized algorithm COPIES all data 71 // Normalized algorithm COPIES all data
67 // ------------------------------- 72 // -------------------------------
68 originalIv = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1 5]); 73 originalIv = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1 4, 15]);
69 algorithm = normalizeEncrypt({ name: "aes-cbc", iv: originalIv }); 74 algorithm = normalizeEncrypt({ name: "aes-cbc", iv: originalIv });
70 75
71 // Make sure it constructed the normalized result. 76 // Make sure it constructed the normalized result.
72 shouldBe("algorithm.name", "'AES-CBC'"); 77 shouldBe("algorithm.name", "'AES-CBC'");
73 shouldBe("algorithm.iv.length", "16"); 78 shouldBe("algorithm.iv.length", "16");
74 shouldBe("algorithm.iv[3]", "3"); 79 shouldBe("algorithm.iv[3]", "3");
75 80
76 // Mutate the original (un-normalized) algorithm. Verify that this doesn't affec t the normalized output. 81 // Mutate the original (un-normalized) algorithm. Verify that this doesn't a ffect the normalized output.
77 originalIv[3] = 0; 82 originalIv[3] = 0;
78 shouldBe("algorithm.iv[3]", "3"); 83 shouldBe("algorithm.iv[3]", "3");
79 84
80 // ------------------------------- 85 // -------------------------------
81 // AES-CBC normalization failures 86 // AES-CBC normalization failures
82 // ------------------------------- 87 // -------------------------------
83 88
84 // The "iv" MUST be 16 bytes long. 89 // The "iv" MUST be 16 bytes long.
85 rawAlgorithm = { 90 rawAlgorithm = {
86 name: "AES-CBC", 91 name: "AES-CBC",
87 iv: new Uint8Array([1, 2, 3]) 92 iv: new Uint8Array([1, 2, 3])
88 }; 93 };
89 shouldThrow("normalizeEncrypt(rawAlgorithm)"); 94 shouldThrow("normalizeEncrypt(rawAlgorithm)");
90 95
91 // ------------------------------- 96 // -------------------------------
92 // Normalize a normalized algorithm (SHA-384) 97 // Normalize a normalized algorithm (SHA-384)
93 // ------------------------------- 98 // -------------------------------
94 algorithm = normalizeDigest({name: "sHa-384"}); 99 algorithm = normalizeDigest({name: "sHa-384"});
95 shouldBe("algorithm.name", "'SHA-384'"); 100 shouldBe("algorithm.name", "'SHA-384'");
96 algorithm = normalizeDigest(algorithm); 101 algorithm = normalizeDigest(algorithm);
97 shouldBe("algorithm.name", "'SHA-384'"); 102 shouldBe("algorithm.name", "'SHA-384'");
98 103
99 // ------------------------------- 104 // -------------------------------
100 // Normalize a normalized algorithm (AES-CBC, encrypt) 105 // Normalize a normalized algorithm (AES-CBC, encrypt)
101 // ------------------------------- 106 // -------------------------------
102 algorithm = normalizeEncrypt({ name: "aEs-cbc", iv: originalIv }); 107 algorithm = normalizeEncrypt({ name: "aEs-cbc", iv: originalIv });
103 // Make sure it constructed the normalized result. 108 // Make sure it constructed the normalized result.
104 shouldBe("algorithm.name", "'AES-CBC'"); 109 shouldBe("algorithm.name", "'AES-CBC'");
105 shouldBe("algorithm.iv.length", "16"); 110 shouldBe("algorithm.iv.length", "16");
106 shouldBe("algorithm.iv[1]", "1"); 111 shouldBe("algorithm.iv[1]", "1");
107 algorithm = normalizeEncrypt(algorithm); 112 algorithm = normalizeEncrypt(algorithm);
108 shouldBe("algorithm.name", "'AES-CBC'"); 113 shouldBe("algorithm.name", "'AES-CBC'");
109 shouldBe("algorithm.iv.length", "16"); 114 shouldBe("algorithm.iv.length", "16");
110 shouldBe("algorithm.iv[1]", "1"); 115 shouldBe("algorithm.iv[1]", "1");
111 116
112 // ------------------------------- 117 // -------------------------------
113 // Unsupported operation on algorithm 118 // Unsupported operation on algorithm
114 // ------------------------------- 119 // -------------------------------
115 shouldThrow("normalizeEncrypt({ name: 'SHA-1' })"); 120 shouldThrow("normalizeEncrypt({ name: 'SHA-1' })");
116 shouldThrow("normalizeDigest({ name: 'AES-CBC', iv: originalIv })"); 121 shouldThrow("normalizeDigest({ name: 'AES-CBC', iv: originalIv })");
117 122
118 // ------------------------------- 123 // -------------------------------
119 // Normalize HMAC 124 // Normalize HMAC
120 // ------------------------------- 125 // -------------------------------
121 shouldThrow("normalizeSign({name: 'hmac'})"); // Missing "hash" 126 shouldThrow("normalizeSign({name: 'hmac'})"); // Missing "hash"
122 shouldThrow("normalizeSign({name: 'hmac', hash: 'foo'})"); // Not a valid "hash" 127 shouldThrow("normalizeSign({name: 'hmac', hash: 'foo'})"); // Not a valid "h ash"
123 shouldThrow("normalizeSign({name: 'hmac', hash: { name: 'AES-CBC', iv: originalI v }})"); // Not a valid "hash" 128 shouldThrow("normalizeSign({name: 'hmac', hash: { name: 'AES-CBC', iv: origi nalIv }})"); // Not a valid "hash"
124 129
125 validHmacSha1 = {name: 'hmac', hash: {name: 'Sha-1'}}; 130 validHmacSha1 = {name: 'hmac', hash: {name: 'Sha-1'}};
126 algorithm = normalizeSign(validHmacSha1); 131 algorithm = normalizeSign(validHmacSha1);
127 shouldBe("algorithm.name", "'HMAC'"); 132 shouldBe("algorithm.name", "'HMAC'");
128 shouldBe("algorithm.hash.name", "'SHA-1'"); 133 shouldBe("algorithm.hash.name", "'SHA-1'");
129 134
130 shouldThrow("normalizeEncrypt(validHmacSha1)"); // Not defined for encrypt() 135 shouldThrow("normalizeEncrypt(validHmacSha1)"); // Not defined for encrypt()
136 }
137
138 function keyImported(newKey)
139 {
140 key = newKey;
141 runTests();
142 finishJSTest();
143 }
144
145 function failedKeyImport(value)
146 {
147 debug("Failed importing key: " + value);
148 finishJSTest();
149 }
150
151 // This is a bogus key import, however the mock constructs something usable.
152 keyFormat = "spki";
153 data = new Uint8Array([]);
154 algorithm = {name: "Sha-256"};
155 extractable = false;
156 keyUsages = [];
157
158 crypto.subtle.importKey(keyFormat, data, algorithm, extractable, keyUsages).then (keyImported, failedKeyImport);
131 159
132 </script> 160 </script>
133 161
134 <script src="../fast/js/resources/js-test-post.js"></script> 162 <script src="../fast/js/resources/js-test-post.js"></script>
135 </body> 163 </body>
136 </html> 164 </html>
OLDNEW
« no previous file with comments | « no previous file | Source/modules/crypto/SubtleCrypto.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698