OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webcrypto_digest.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/logging.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "content/renderer/webcrypto_impl.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | |
14 | |
15 namespace content { | |
16 | |
17 // Algorithms include SHA1, SHA224, SHA256, SHA384, and SHA512 | |
18 WebKit::WebCryptoAlgorithmId algorithm_ids[] = { | |
19 WebKit::WebCryptoAlgorithmIdSha1, | |
20 WebKit::WebCryptoAlgorithmIdSha224, | |
21 WebKit::WebCryptoAlgorithmIdSha256, | |
22 WebKit::WebCryptoAlgorithmIdSha384, | |
23 WebKit::WebCryptoAlgorithmIdSha512 | |
Ryan Sleevi
2013/08/02 23:29:27
style nit: four spaces
Bryan Eyler
2013/08/03 00:16:45
Done.
| |
24 }; | |
25 | |
26 class TestWebCryptoOperationResult | |
27 : public WebKit::WebCryptoOperationResultPrivate, | |
28 public base::RefCountedThreadSafe<TestWebCryptoOperationResult> { | |
29 public: | |
30 TestWebCryptoOperationResult() | |
31 : error_encountered_(false), | |
32 operation_(NULL) { | |
33 } | |
34 | |
35 // There's no need to implement the initialization* routines because the | |
eroman
2013/08/02 21:41:39
Perhaps this comment is no longer relevant.
Bryan Eyler
2013/08/03 00:16:45
Removed.
| |
36 // initialization is already checked for failure in the tests. | |
37 virtual void initializationFailed() OVERRIDE { | |
38 error_encountered_ = true; | |
39 } | |
40 | |
41 virtual void initializationSucceeded( | |
42 WebKit::WebCryptoOperation* op) OVERRIDE { | |
43 operation_ = op; | |
44 } | |
45 | |
46 virtual void completeWithArrayBuffer( | |
47 const WebKit::WebArrayBuffer& array_buffer) OVERRIDE { | |
48 // Might as well just store the array_buffer in hex, since it's going to be | |
49 // converted anyway for better error reporting. +1 for the nul terminator. | |
50 array_buffer_data_hex_ = | |
51 base::HexEncode(array_buffer.data(), array_buffer.byteLength()); | |
52 } | |
53 | |
54 virtual void completeWithBoolean(bool boolean) OVERRIDE { | |
55 NOTREACHED(); | |
56 } | |
57 | |
58 virtual void completeWithError() OVERRIDE { | |
59 error_encountered_ = true; | |
60 } | |
61 | |
62 virtual void ref() { | |
63 RefCountedThreadSafe<TestWebCryptoOperationResult>::AddRef(); | |
64 } | |
65 | |
66 virtual void deref() { | |
67 RefCountedThreadSafe<TestWebCryptoOperationResult>::Release(); | |
68 } | |
69 | |
70 const char* array_buffer_data_hex() const { | |
71 return array_buffer_data_hex_.c_str(); | |
72 } | |
73 | |
74 bool error_encountered() const { | |
75 return error_encountered_; | |
76 } | |
77 | |
78 WebKit::WebCryptoOperation* operation() { | |
79 return operation_; | |
80 } | |
81 | |
82 private: | |
83 friend class base::RefCountedThreadSafe<TestWebCryptoOperationResult>; | |
84 virtual ~TestWebCryptoOperationResult() {} | |
85 | |
86 std::string array_buffer_data_hex_; | |
87 bool error_encountered_; | |
88 WebKit::WebCryptoOperation* operation_; | |
89 }; | |
90 | |
91 TEST(WebCryptoDigest, SampleEmptySet) { | |
Ryan Sleevi
2013/08/02 23:29:27
As we start implementing layers, I'm a little nerv
eroman
2013/08/03 00:11:19
tl;dr: Let's keep these unittests here.
I asked B
Bryan Eyler
2013/08/03 00:16:45
This certainly could be changed to use the NIST sa
| |
92 // For readability and maintainability of the results, the results are | |
93 // stored here in hex format. | |
94 const char* hex_result[arraysize(algorithm_ids)] = { | |
95 // echo -n "" | sha1sum | |
96 "da39a3ee5e6b4b0d3255bfef95601890afd80709", | |
97 // echo -n "" | sha224sum | |
98 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", | |
99 // echo -n "" | sha256sum | |
100 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | |
101 // echo -n "" | sha384sum | |
102 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e" | |
103 "debfe76f65fbd51ad2f14898b95b", | |
104 // echo -n "" | sha512sum | |
105 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0" | |
106 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", | |
107 }; | |
108 | |
109 for (unsigned int id_index = 0; | |
110 id_index < arraysize(algorithm_ids); id_index++) { | |
111 WebKit::WebCryptoAlgorithm algorithm( | |
112 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
113 algorithm_ids[id_index], "SHA", NULL)); | |
114 | |
115 // No process() call. | |
116 { | |
117 scoped_refptr<TestWebCryptoOperationResult> result_impl( | |
118 new TestWebCryptoOperationResult); | |
119 WebKit::WebCryptoOperationResult result(result_impl.get()); | |
120 | |
121 WebCryptoImpl crypto; | |
122 crypto.digest(algorithm, result); | |
123 | |
124 EXPECT_FALSE(result_impl->error_encountered()); | |
125 ASSERT_TRUE(result_impl->operation() != NULL); | |
126 | |
127 result_impl->operation()->finish(); | |
128 | |
129 EXPECT_FALSE(result_impl->error_encountered()); | |
130 // Ignore case, it's checking the hex value. | |
131 EXPECT_STRCASEEQ( | |
132 result_impl->array_buffer_data_hex(), | |
133 hex_result[id_index]); | |
134 } | |
135 | |
136 // No-op process() call. | |
137 { | |
138 scoped_refptr<TestWebCryptoOperationResult> result_impl( | |
139 new TestWebCryptoOperationResult); | |
140 WebKit::WebCryptoOperationResult result(result_impl.get()); | |
141 | |
142 WebCryptoImpl crypto; | |
143 crypto.digest(algorithm, result); | |
144 | |
145 EXPECT_FALSE(result_impl->error_encountered()); | |
146 ASSERT_TRUE(result_impl->operation() != NULL); | |
147 | |
148 result_impl->operation()->process(NULL, 0); | |
149 result_impl->operation()->process(NULL, 0); | |
150 result_impl->operation()->finish(); | |
151 | |
152 EXPECT_FALSE(result_impl->error_encountered()); | |
153 // Ignore case, it's checking the hex value. | |
154 EXPECT_STRCASEEQ( | |
155 result_impl->array_buffer_data_hex(), | |
156 hex_result[id_index]); | |
157 } | |
158 } | |
159 } | |
160 | |
161 TEST(WebCryptoDigest, SampleSimpleSet) { | |
162 // For readability and maintainability of the results, the results are | |
163 // stored here in hex format. | |
164 const unsigned char* kInput = reinterpret_cast<const unsigned char*>("\0"); | |
165 const size_t kInputLength = 1; | |
166 const char* hex_result[arraysize(algorithm_ids)] = { | |
167 // echo -n -e "\000" | sha1sum | |
168 "5ba93c9db0cff93f52b521d7420e43f6eda2784f", | |
169 // echo -n -e "\000" | sha224sum | |
170 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073", | |
171 // echo -n -e "\000" | sha256sum | |
172 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", | |
173 // echo -n -e "\000" | sha384sum | |
174 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933" | |
175 "ec2b413465966817a9c208a11717", | |
176 // echo -n -e "\000" | sha512sum | |
177 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a" | |
178 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee", | |
179 }; | |
180 | |
181 for (unsigned int id_index = 0; | |
182 id_index < arraysize(algorithm_ids); id_index++) { | |
183 scoped_refptr<TestWebCryptoOperationResult> result_impl( | |
184 new TestWebCryptoOperationResult); | |
185 WebKit::WebCryptoOperationResult result(result_impl.get()); | |
186 WebKit::WebCryptoAlgorithm algorithm( | |
187 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
188 algorithm_ids[id_index], "SHA", NULL)); | |
189 | |
190 WebCryptoImpl crypto; | |
191 crypto.digest(algorithm, result); | |
192 | |
193 EXPECT_FALSE(result_impl->error_encountered()); | |
194 ASSERT_TRUE(result_impl->operation() != NULL); | |
195 | |
196 result_impl->operation()->process(kInput, kInputLength); | |
197 result_impl->operation()->finish(); | |
198 | |
199 EXPECT_FALSE(result_impl->error_encountered()); | |
200 // Ignore case, it's checking the hex value. | |
201 EXPECT_STRCASEEQ( | |
202 result_impl->array_buffer_data_hex(), | |
203 hex_result[id_index]); | |
204 } | |
205 } | |
206 | |
207 TEST(WebCryptoDigest, SampleMultipartSet) { | |
208 // For readability and maintainability of the results, the results are | |
209 // stored here in hex format. | |
210 struct { | |
211 const unsigned char* input; | |
212 size_t input_length; | |
213 } input_set[] = { | |
214 { reinterpret_cast<const unsigned char*>("\x00"), 1}, | |
215 { reinterpret_cast<const unsigned char*>("\x01"), 1}, | |
216 { reinterpret_cast<const unsigned char*>("\x02"), 1}, | |
217 { reinterpret_cast<const unsigned char*>("\x03"), 1}, | |
218 { reinterpret_cast<const unsigned char*>("\x04"), 1}, | |
219 { reinterpret_cast<const unsigned char*>("\x05"), 1}, | |
220 }; | |
221 const char* hex_result[arraysize(algorithm_ids)] = { | |
222 // echo -n -e "\000\001\002\003\004\005" | sha1sum | |
223 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9", | |
224 // echo -n -e "\000\001\002\003\004\005" | sha224sum | |
225 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc", | |
226 // echo -n -e "\000\001\002\003\004\005" | sha256sum | |
227 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43", | |
228 // echo -n -e "\000\001\002\003\004\005" | sha384sum | |
229 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" | |
230 "22780d7e1b95bfeaa86a678e4552", | |
231 // echo -n -e "\000\001\002\003\004\005" | sha512sum | |
232 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" | |
233 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", | |
234 }; | |
235 | |
236 for (unsigned int id_index = 0; | |
237 id_index < arraysize(algorithm_ids); id_index++) { | |
238 scoped_refptr<TestWebCryptoOperationResult> result_impl( | |
239 new TestWebCryptoOperationResult); | |
240 WebKit::WebCryptoOperationResult result(result_impl.get()); | |
241 WebKit::WebCryptoAlgorithm algorithm( | |
242 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
243 algorithm_ids[id_index], "SHA", NULL)); | |
244 | |
245 WebCryptoImpl crypto; | |
246 crypto.digest(algorithm, result); | |
247 | |
248 EXPECT_FALSE(result_impl->error_encountered()); | |
249 ASSERT_TRUE(result_impl->operation() != NULL); | |
250 | |
251 for (unsigned int i = 0; i < ARRAYSIZE_UNSAFE(input_set); i++) { | |
252 result_impl->operation()->process(input_set[i].input, | |
253 input_set[i].input_length); | |
254 } | |
255 | |
256 result_impl->operation()->finish(); | |
257 | |
258 EXPECT_FALSE(result_impl->error_encountered()); | |
259 // Ignore case, it's checking the hex value. | |
260 EXPECT_STRCASEEQ( | |
261 result_impl->array_buffer_data_hex(), | |
262 hex_result[id_index]); | |
263 } | |
264 } | |
265 | |
266 } // namespace content | |
OLD | NEW |