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/public/renderer/content_renderer_client.h" | |
12 #include "content/renderer/renderer_webkitplatformsupport_impl.h" | |
13 #include "content/renderer/webcrypto_impl.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | |
16 | |
17 namespace content { | |
18 | |
19 // Algorithms include SHA1, SHA224, SHA256, SHA384, and SHA512 | |
20 WebKit::WebCryptoAlgorithmId algorithm_ids[] = { | |
21 WebKit::WebCryptoAlgorithmIdSha1, | |
22 WebKit::WebCryptoAlgorithmIdSha224, | |
23 WebKit::WebCryptoAlgorithmIdSha256, | |
24 WebKit::WebCryptoAlgorithmIdSha384, | |
25 WebKit::WebCryptoAlgorithmIdSha512 | |
26 }; | |
27 | |
28 class TestWebCryptoOperationResult | |
29 : public WebKit::WebCryptoOperationResultPrivate, | |
30 public base::RefCountedThreadSafe<TestWebCryptoOperationResult> { | |
31 public: | |
32 TestWebCryptoOperationResult() | |
33 : error_encountered_(false), | |
34 operation_(NULL) { | |
35 } | |
36 | |
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 class WebCryptoDigestTest : public testing::Test { | |
92 public: | |
93 WebCryptoDigestTest() : crypto_(NULL) {} | |
94 | |
95 protected: | |
96 virtual void SetUp() { | |
97 SetRendererClientForTesting(&content_renderer_client_); | |
98 | |
99 webkit_platform_support_.reset(new RendererWebKitPlatformSupportImpl); | |
100 | |
101 crypto_ = webkit_platform_support_->crypto(); | |
102 ASSERT_TRUE(crypto_ != NULL); | |
103 } | |
104 | |
105 virtual void TearDown() { | |
106 SetRendererClientForTesting(NULL); | |
107 } | |
108 | |
109 ContentRendererClient content_renderer_client_; | |
110 scoped_ptr<RendererWebKitPlatformSupportImpl> webkit_platform_support_; | |
111 WebKit::WebCrypto* crypto_; | |
112 }; | |
113 | |
114 TEST_F(WebCryptoDigestTest, SampleEmptySet) { | |
115 // For readability and maintainability of the results, the results are | |
116 // stored here in hex format. | |
117 const char* hex_result[arraysize(algorithm_ids)] = { | |
118 // echo -n "" | sha1sum | |
119 "da39a3ee5e6b4b0d3255bfef95601890afd80709", | |
120 // echo -n "" | sha224sum | |
121 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", | |
122 // echo -n "" | sha256sum | |
123 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | |
124 // echo -n "" | sha384sum | |
125 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e" | |
126 "debfe76f65fbd51ad2f14898b95b", | |
127 // echo -n "" | sha512sum | |
128 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0" | |
129 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", | |
130 }; | |
131 | |
132 for (unsigned int id_index = 0; | |
jamesr
2013/08/09 20:04:55
why not size_t for this index?
Bryan Eyler
2013/08/13 18:22:21
Done.
| |
133 id_index < arraysize(algorithm_ids); id_index++) { | |
134 WebKit::WebCryptoAlgorithm algorithm( | |
135 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
136 algorithm_ids[id_index], "SHA", NULL)); | |
137 | |
138 // No process() call. | |
139 { | |
140 scoped_refptr<TestWebCryptoOperationResult> result_impl( | |
141 new TestWebCryptoOperationResult); | |
142 WebKit::WebCryptoOperationResult result(result_impl.get()); | |
143 | |
144 crypto_->digest(algorithm, result); | |
145 | |
146 EXPECT_FALSE(result_impl->error_encountered()); | |
147 ASSERT_TRUE(result_impl->operation() != NULL); | |
148 | |
149 result_impl->operation()->finish(); | |
150 | |
151 EXPECT_FALSE(result_impl->error_encountered()); | |
152 // Ignore case, it's checking the hex value. | |
153 EXPECT_STRCASEEQ( | |
154 result_impl->array_buffer_data_hex(), | |
155 hex_result[id_index]); | |
156 } | |
157 | |
158 // No-op process() call. | |
159 { | |
160 scoped_refptr<TestWebCryptoOperationResult> result_impl( | |
161 new TestWebCryptoOperationResult); | |
162 WebKit::WebCryptoOperationResult result(result_impl.get()); | |
163 | |
164 crypto_->digest(algorithm, result); | |
165 | |
166 EXPECT_FALSE(result_impl->error_encountered()); | |
167 ASSERT_TRUE(result_impl->operation() != NULL); | |
168 | |
169 result_impl->operation()->process(NULL, 0); | |
170 result_impl->operation()->process(NULL, 0); | |
171 result_impl->operation()->finish(); | |
172 | |
173 EXPECT_FALSE(result_impl->error_encountered()); | |
174 // Ignore case, it's checking the hex value. | |
175 EXPECT_STRCASEEQ( | |
176 result_impl->array_buffer_data_hex(), | |
177 hex_result[id_index]); | |
178 } | |
179 } | |
180 } | |
181 | |
182 TEST_F(WebCryptoDigestTest, SampleSimpleSet) { | |
183 // For readability and maintainability of the results, the results are | |
184 // stored here in hex format. | |
185 const unsigned char* kInput = reinterpret_cast<const unsigned char*>("\0"); | |
186 const size_t kInputLength = 1; | |
187 const char* hex_result[arraysize(algorithm_ids)] = { | |
188 // echo -n -e "\000" | sha1sum | |
189 "5ba93c9db0cff93f52b521d7420e43f6eda2784f", | |
190 // echo -n -e "\000" | sha224sum | |
191 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073", | |
192 // echo -n -e "\000" | sha256sum | |
193 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", | |
194 // echo -n -e "\000" | sha384sum | |
195 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933" | |
196 "ec2b413465966817a9c208a11717", | |
197 // echo -n -e "\000" | sha512sum | |
198 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a" | |
199 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee", | |
200 }; | |
201 | |
202 for (unsigned int id_index = 0; | |
203 id_index < arraysize(algorithm_ids); id_index++) { | |
204 scoped_refptr<TestWebCryptoOperationResult> result_impl( | |
205 new TestWebCryptoOperationResult); | |
206 WebKit::WebCryptoOperationResult result(result_impl.get()); | |
207 WebKit::WebCryptoAlgorithm algorithm( | |
208 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
209 algorithm_ids[id_index], "SHA", NULL)); | |
210 | |
211 crypto_->digest(algorithm, result); | |
212 | |
213 EXPECT_FALSE(result_impl->error_encountered()); | |
214 ASSERT_TRUE(result_impl->operation() != NULL); | |
215 | |
216 result_impl->operation()->process(kInput, kInputLength); | |
217 result_impl->operation()->finish(); | |
218 | |
219 EXPECT_FALSE(result_impl->error_encountered()); | |
220 // Ignore case, it's checking the hex value. | |
221 EXPECT_STRCASEEQ( | |
222 result_impl->array_buffer_data_hex(), | |
223 hex_result[id_index]); | |
224 } | |
225 } | |
226 | |
227 TEST_F(WebCryptoDigestTest, SampleMultipartSet) { | |
228 // For readability and maintainability of the results, the results are | |
229 // stored here in hex format. | |
230 struct { | |
231 const unsigned char* input; | |
232 size_t input_length; | |
233 } input_set[] = { | |
234 { reinterpret_cast<const unsigned char*>("\x00"), 1}, | |
235 { reinterpret_cast<const unsigned char*>("\x01"), 1}, | |
236 { reinterpret_cast<const unsigned char*>("\x02"), 1}, | |
237 { reinterpret_cast<const unsigned char*>("\x03"), 1}, | |
238 { reinterpret_cast<const unsigned char*>("\x04"), 1}, | |
239 { reinterpret_cast<const unsigned char*>("\x05"), 1}, | |
240 }; | |
241 const char* hex_result[arraysize(algorithm_ids)] = { | |
242 // echo -n -e "\000\001\002\003\004\005" | sha1sum | |
243 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9", | |
244 // echo -n -e "\000\001\002\003\004\005" | sha224sum | |
245 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc", | |
246 // echo -n -e "\000\001\002\003\004\005" | sha256sum | |
247 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43", | |
248 // echo -n -e "\000\001\002\003\004\005" | sha384sum | |
249 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" | |
250 "22780d7e1b95bfeaa86a678e4552", | |
251 // echo -n -e "\000\001\002\003\004\005" | sha512sum | |
252 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" | |
253 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", | |
254 }; | |
255 | |
256 for (unsigned int id_index = 0; | |
257 id_index < arraysize(algorithm_ids); id_index++) { | |
258 scoped_refptr<TestWebCryptoOperationResult> result_impl( | |
259 new TestWebCryptoOperationResult); | |
260 WebKit::WebCryptoOperationResult result(result_impl.get()); | |
261 WebKit::WebCryptoAlgorithm algorithm( | |
262 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
263 algorithm_ids[id_index], "SHA", NULL)); | |
264 | |
265 crypto_->digest(algorithm, result); | |
266 | |
267 EXPECT_FALSE(result_impl->error_encountered()); | |
268 ASSERT_TRUE(result_impl->operation() != NULL); | |
269 | |
270 for (unsigned int i = 0; i < ARRAYSIZE_UNSAFE(input_set); i++) { | |
271 result_impl->operation()->process(input_set[i].input, | |
272 input_set[i].input_length); | |
273 } | |
274 | |
275 result_impl->operation()->finish(); | |
276 | |
277 EXPECT_FALSE(result_impl->error_encountered()); | |
278 // Ignore case, it's checking the hex value. | |
279 EXPECT_STRCASEEQ( | |
280 result_impl->array_buffer_data_hex(), | |
281 hex_result[id_index]); | |
282 } | |
283 } | |
284 | |
285 } // namespace content | |
OLD | NEW |