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

Side by Side Diff: content/renderer/webcrypto_digest_unittest.cc

Issue 19757011: WebCrypto: Implement digest() using NSS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More documentation of interfaces, use of scoped_ptr for better memory management. Created 7 years, 4 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
OLDNEW
(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 (size_t id_index = 0; id_index < arraysize(algorithm_ids); id_index++) {
133 WebKit::WebCryptoAlgorithm algorithm(
134 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
135 algorithm_ids[id_index], "SHA", NULL));
136
137 // No process() call.
138 {
139 scoped_refptr<TestWebCryptoOperationResult> result_impl(
140 new TestWebCryptoOperationResult);
141 WebKit::WebCryptoOperationResult result(result_impl.get());
142
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()->finish();
149
150 EXPECT_FALSE(result_impl->error_encountered());
151 // Ignore case, it's checking the hex value.
152 EXPECT_STRCASEEQ(
153 result_impl->array_buffer_data_hex(),
154 hex_result[id_index]);
155 }
156
157 // No-op process() call.
158 {
159 scoped_refptr<TestWebCryptoOperationResult> result_impl(
160 new TestWebCryptoOperationResult);
161 WebKit::WebCryptoOperationResult result(result_impl.get());
162
163 crypto_->digest(algorithm, result);
164
165 EXPECT_FALSE(result_impl->error_encountered());
166 ASSERT_TRUE(result_impl->operation() != NULL);
167
168 result_impl->operation()->process(NULL, 0);
169 result_impl->operation()->process(NULL, 0);
170 result_impl->operation()->finish();
171
172 EXPECT_FALSE(result_impl->error_encountered());
173 // Ignore case, it's checking the hex value.
174 EXPECT_STRCASEEQ(
175 result_impl->array_buffer_data_hex(),
176 hex_result[id_index]);
177 }
178 }
179 }
180
181 TEST_F(WebCryptoDigestTest, SampleSimpleSet) {
182 // For readability and maintainability of the results, the results are
183 // stored here in hex format.
184 const unsigned char* kInput = reinterpret_cast<const unsigned char*>("\0");
185 const size_t kInputLength = 1;
186 const char* hex_result[arraysize(algorithm_ids)] = {
187 // echo -n -e "\000" | sha1sum
188 "5ba93c9db0cff93f52b521d7420e43f6eda2784f",
189 // echo -n -e "\000" | sha224sum
190 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073",
191 // echo -n -e "\000" | sha256sum
192 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d",
193 // echo -n -e "\000" | sha384sum
194 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933"
195 "ec2b413465966817a9c208a11717",
196 // echo -n -e "\000" | sha512sum
197 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a"
198 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee",
199 };
200
201 for (size_t id_index = 0; id_index < arraysize(algorithm_ids); id_index++) {
202 scoped_refptr<TestWebCryptoOperationResult> result_impl(
203 new TestWebCryptoOperationResult);
204 WebKit::WebCryptoOperationResult result(result_impl.get());
205 WebKit::WebCryptoAlgorithm algorithm(
206 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
207 algorithm_ids[id_index], "SHA", NULL));
208
209 crypto_->digest(algorithm, result);
210
211 EXPECT_FALSE(result_impl->error_encountered());
212 ASSERT_TRUE(result_impl->operation() != NULL);
213
214 result_impl->operation()->process(kInput, kInputLength);
215 result_impl->operation()->finish();
216
217 EXPECT_FALSE(result_impl->error_encountered());
218 // Ignore case, it's checking the hex value.
219 EXPECT_STRCASEEQ(
220 result_impl->array_buffer_data_hex(),
221 hex_result[id_index]);
222 }
223 }
224
225 TEST_F(WebCryptoDigestTest, SampleMultipartSet) {
226 // For readability and maintainability of the results, the results are
227 // stored here in hex format.
228 struct {
229 const unsigned char* input;
230 size_t input_length;
231 } input_set[] = {
232 { reinterpret_cast<const unsigned char*>("\x00"), 1},
233 { reinterpret_cast<const unsigned char*>("\x01"), 1},
234 { reinterpret_cast<const unsigned char*>("\x02"), 1},
235 { reinterpret_cast<const unsigned char*>("\x03"), 1},
236 { reinterpret_cast<const unsigned char*>("\x04"), 1},
237 { reinterpret_cast<const unsigned char*>("\x05"), 1},
238 };
239 const char* hex_result[arraysize(algorithm_ids)] = {
240 // echo -n -e "\000\001\002\003\004\005" | sha1sum
241 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9",
242 // echo -n -e "\000\001\002\003\004\005" | sha224sum
243 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc",
244 // echo -n -e "\000\001\002\003\004\005" | sha256sum
245 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43",
246 // echo -n -e "\000\001\002\003\004\005" | sha384sum
247 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc"
248 "22780d7e1b95bfeaa86a678e4552",
249 // echo -n -e "\000\001\002\003\004\005" | sha512sum
250 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9"
251 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c",
252 };
253
254 for (size_t id_index = 0; id_index < arraysize(algorithm_ids); id_index++) {
255 scoped_refptr<TestWebCryptoOperationResult> result_impl(
256 new TestWebCryptoOperationResult);
257 WebKit::WebCryptoOperationResult result(result_impl.get());
258 WebKit::WebCryptoAlgorithm algorithm(
259 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
260 algorithm_ids[id_index], "SHA", NULL));
261
262 crypto_->digest(algorithm, result);
263
264 EXPECT_FALSE(result_impl->error_encountered());
265 ASSERT_TRUE(result_impl->operation() != NULL);
266
267 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_set); i++) {
268 result_impl->operation()->process(input_set[i].input,
269 input_set[i].input_length);
270 }
271
272 result_impl->operation()->finish();
273
274 EXPECT_FALSE(result_impl->error_encountered());
275 // Ignore case, it's checking the hex value.
276 EXPECT_STRCASEEQ(
277 result_impl->array_buffer_data_hex(),
278 hex_result[id_index]);
279 }
280 }
281
282 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698