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

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

Issue 19757011: WebCrypto: Implement digest() using NSS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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_sha_digest.h"
6
7 #include "base/basictypes.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
12
13 namespace content {
14
15 // Algorithms include SHA1, SHA224, SHA256, SHA384, and SHA512
16 WebKit::WebCryptoAlgorithmId algorithm_ids[] = {
17 WebKit::WebCryptoAlgorithmIdSha1,
18 WebKit::WebCryptoAlgorithmIdSha224,
19 WebKit::WebCryptoAlgorithmIdSha256,
20 WebKit::WebCryptoAlgorithmIdSha384,
21 WebKit::WebCryptoAlgorithmIdSha512
22 };
23
24 class TestWebCryptoOperationResult :
eroman 2013/07/31 02:23:50 style nits: move the colon to the next line.
Bryan Eyler 2013/08/02 00:49:05 Done.
25 public WebKit::WebCryptoOperationResultPrivate,
eroman 2013/07/31 02:23:50 indent by 4
Bryan Eyler 2013/08/02 00:49:05 Done.
26 public base::RefCountedThreadSafe<TestWebCryptoOperationResult> {
27 public:
28 TestWebCryptoOperationResult() : error_encountered_(false) {}
29
30 // There's no need to implement the initialization* routines because the
31 // initialization is already checked for failure in the tests.
32 virtual void initializationFailed() OVERRIDE {
33 }
34
35 virtual void initializationSucceeded(
36 WebKit::WebCryptoOperation* op) OVERRIDE {
37 }
38
39 virtual void completeWithArrayBuffer(
40 const WebKit::WebArrayBuffer& array_buffer) OVERRIDE {
41 // Might as well just store the array_buffer in hex, since it's going to be
42 // converted anyway for better error reporting. +1 for the nul terminator.
43 array_buffer_data_hex_ =
44 base::HexEncode(array_buffer.data(), array_buffer.byteLength());
45 }
46
47 virtual void completeWithError() OVERRIDE {
48 error_encountered_ = true;
49 }
50
51 virtual void ref() {
52 RefCountedThreadSafe<TestWebCryptoOperationResult>::AddRef();
53 }
54
55 virtual void deref() {
56 RefCountedThreadSafe<TestWebCryptoOperationResult>::Release();
57 }
58
59 const char* array_buffer_data_hex() const {
60 return array_buffer_data_hex_.c_str();
61 }
62
63 bool error_encountered() const {
64 return error_encountered_;
65 }
66
67 protected:
68 std::string array_buffer_data_hex_;
69 bool error_encountered_;
70
71 private:
72 friend class base::RefCountedThreadSafe<TestWebCryptoOperationResult>;
73 virtual ~TestWebCryptoOperationResult() {}
74 };
75
76 TEST(WebCryptoSHADigest, SampleEmptySet) {
77 // For readability and maintainability of the results, the results are
78 // stored here in hex format.
79 const char* hex_result[arraysize(algorithm_ids)] = {
80 // echo -n "" | sha1sum
81 "da39a3ee5e6b4b0d3255bfef95601890afd80709",
82 // echo -n "" | sha224sum
83 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
84 // echo -n "" | sha256sum
85 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
86 // echo -n "" | sha384sum
87 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e"
88 "debfe76f65fbd51ad2f14898b95b",
89 // echo -n "" | sha512sum
90 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0"
91 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
92 };
93
94 for (unsigned int id_index = 0;
95 id_index < arraysize(algorithm_ids); id_index++) {
96 scoped_refptr<TestWebCryptoOperationResult> result_impl(
97 new TestWebCryptoOperationResult);
98 WebKit::WebCryptoOperationResult result(result_impl.get());
eroman 2013/07/31 02:23:50 apologies for this complicated API
99
100 WebCryptoSHADigest* digest =
101 new WebCryptoSHADigest(algorithm_ids[id_index], result);
102 ASSERT_TRUE(digest);
eroman 2013/07/31 02:23:50 remove this as it can never trigger in Chrome (all
Bryan Eyler 2013/08/02 00:49:05 Done.
103
104 EXPECT_TRUE(digest->Initialize());
105
106 digest->finish();
107
108 // Ignore case, it's checking the hex value.
109 EXPECT_STRCASEEQ(
110 result_impl->array_buffer_data_hex(),
111 hex_result[id_index]);
112 EXPECT_FALSE(result_impl->error_encountered());
eroman 2013/07/31 02:23:50 [optional]: I suggest doing this test first.
Bryan Eyler 2013/08/02 00:49:05 Done.
113 }
114 }
115
116 TEST(WebCryptoSHADigest, SampleSimpleSet) {
117 // For readability and maintainability of the results, the results are
118 // stored here in hex format.
119 const unsigned char* kInput = reinterpret_cast<const unsigned char*>("\0");
120 const size_t kInputLength = 1;
121 const char* hex_result[arraysize(algorithm_ids)] = {
122 // echo -n -e "\000" | sha1sum
123 "5ba93c9db0cff93f52b521d7420e43f6eda2784f",
124 // echo -n -e "\000" | sha224sum
125 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073",
126 // echo -n -e "\000" | sha256sum
127 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d",
128 // echo -n -e "\000" | sha384sum
129 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933"
130 "ec2b413465966817a9c208a11717",
131 // echo -n -e "\000" | sha512sum
132 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a"
133 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee",
134 };
135
136 for (unsigned int id_index = 0;
137 id_index < arraysize(algorithm_ids); id_index++) {
138 scoped_refptr<TestWebCryptoOperationResult> result_impl(
139 new TestWebCryptoOperationResult);
140 WebKit::WebCryptoOperationResult result(result_impl.get());
141
142 WebCryptoSHADigest* digest =
143 new WebCryptoSHADigest(algorithm_ids[id_index], result);
eroman 2013/07/31 02:23:50 Once https://codereview.chromium.org/21185005/ lan
Bryan Eyler 2013/08/02 00:49:05 Done.
144 ASSERT_TRUE(digest);
145
146 EXPECT_TRUE(digest->Initialize());
147
148 digest->process(kInput, kInputLength);
149
150 digest->finish();
151
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 EXPECT_FALSE(result_impl->error_encountered());
157 }
158 }
159
160 TEST(WebCryptoSHADigest, SampleMultipartSet) {
161 // For readability and maintainability of the results, the results are
162 // stored here in hex format.
163 struct {
164 const unsigned char* input;
165 size_t input_length;
166 } input_set[] = {
167 { reinterpret_cast<const unsigned char*>("\x00"), 1},
168 { reinterpret_cast<const unsigned char*>("\x01"), 1},
169 { reinterpret_cast<const unsigned char*>("\x02"), 1},
170 { reinterpret_cast<const unsigned char*>("\x03"), 1},
171 { reinterpret_cast<const unsigned char*>("\x04"), 1},
172 { reinterpret_cast<const unsigned char*>("\x05"), 1},
173 };
174 const char* hex_result[arraysize(algorithm_ids)] = {
175 // echo -n -e "\000\001\002\003\004\005" | sha1sum
eroman 2013/07/31 02:23:50 These comments are useful, thanks
176 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9",
177 // echo -n -e "\000\001\002\003\004\005" | sha224sum
178 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc",
179 // echo -n -e "\000\001\002\003\004\005" | sha256sum
180 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43",
181 // echo -n -e "\000\001\002\003\004\005" | sha384sum
182 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc"
183 "22780d7e1b95bfeaa86a678e4552",
184 // echo -n -e "\000\001\002\003\004\005" | sha512sum
185 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9"
186 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c",
187 };
188
189 for (unsigned int id_index = 0;
190 id_index < arraysize(algorithm_ids); id_index++) {
191 scoped_refptr<TestWebCryptoOperationResult> result_impl(
192 new TestWebCryptoOperationResult);
193 WebKit::WebCryptoOperationResult result(result_impl.get());
194
195 WebCryptoSHADigest* digest =
196 new WebCryptoSHADigest(algorithm_ids[id_index], result);
197 ASSERT_TRUE(digest);
198
199 EXPECT_TRUE(digest->Initialize());
200
201 for (unsigned int i = 0; i < arraysize(input_set); i++) {
202 digest->process(input_set[i].input, input_set[i].input_length);
203 }
204
205 digest->finish();
206
207 // Ignore case, it's checking the hex value.
208 EXPECT_STRCASEEQ(
209 result_impl->array_buffer_data_hex(),
210 hex_result[id_index]);
211 EXPECT_FALSE(result_impl->error_encountered());
212 }
213 }
eroman 2013/07/31 02:23:50 Another test which I think is worth adding, is wha
Bryan Eyler 2013/08/02 00:49:05 Added this into the empty set test.
214
215 } // namespace content
OLDNEW
« content/renderer/webcrypto_sha_digest_nss.cc ('K') | « content/renderer/webcrypto_sha_digest_nss.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698