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_sha_digest.h" | |
6 | |
7 #include "testing/gtest/include/gtest/gtest.h" | |
8 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | |
9 | |
10 namespace content { | |
11 | |
12 // Algorithms include SHA1, SHA224, SHA256, SHA384, and SHA512 | |
13 const int kNumAlgorithms = 5; | |
eroman
2013/07/19 01:11:13
Instead of specifying this, let the compiler figur
Bryan Eyler
2013/07/24 00:33:53
Done.
| |
14 WebKit::WebCryptoAlgorithmId algorithm_ids[kNumAlgorithms] = { | |
15 WebKit::WebCryptoAlgorithmIdSha1, | |
16 WebKit::WebCryptoAlgorithmIdSha224, | |
17 WebKit::WebCryptoAlgorithmIdSha256, | |
18 WebKit::WebCryptoAlgorithmIdSha384, | |
19 WebKit::WebCryptoAlgorithmIdSha512 | |
20 }; | |
21 | |
22 class TestWebCryptoOperationResult : public WebKit::WebCryptoOperationResult { | |
23 public: | |
24 virtual void setArrayBuffer(const WebKit::WebArrayBuffer& array_buffer) { | |
25 // Might as well just store the array_buffer in hex, since it's going to be | |
26 // converted anyway for better error reporting. +1 for the nul terminator. | |
27 array_buffer_data_hex_ = new char[array_buffer.byteLength() * 2 + 1]; | |
eroman
2013/07/19 01:11:13
Where is this freed?
Try using a std::vector<> in
Bryan Eyler
2013/07/24 00:33:53
Using std::string instead since that's the return
| |
28 | |
29 unsigned char* array_buffer_ptr = | |
30 static_cast<unsigned char*>(array_buffer.data()); | |
31 | |
32 for (unsigned i = 0; i < array_buffer.byteLength(); i++) { | |
eroman
2013/07/19 01:11:13
use HexEncode() from base/strings/string_number_co
Bryan Eyler
2013/07/24 00:33:53
Done. I do a case-insensitive compare anyway.
| |
33 array_buffer_data_hex_[i * 2] = | |
34 array_buffer_ptr[i] / 16 >= 10 ? | |
35 array_buffer_ptr[i] / 16 - 10 + 'a' : | |
36 array_buffer_ptr[i] / 16 + '0'; | |
37 array_buffer_data_hex_[i * 2 + 1] = | |
38 array_buffer_ptr[i] % 16 >= 10 ? | |
39 array_buffer_ptr[i] % 16 - 10 + 'a' : | |
40 array_buffer_ptr[i] % 16 + '0'; | |
41 } | |
42 array_buffer_data_hex_[array_buffer.byteLength() * 2] = 0; | |
43 } | |
44 | |
45 const char* array_buffer_data_hex() const { | |
46 return array_buffer_data_hex_; | |
47 } | |
48 | |
49 protected: | |
50 virtual ~TestWebCryptoOperationResult() { } | |
51 | |
52 char* array_buffer_data_hex_; | |
53 }; | |
54 | |
55 TEST(WebCryptoSHADigest, SampleEmptySet) { | |
56 // For readability and maintainability of the results, the results are | |
57 // stored here in hex format. | |
58 const char* hex_result[kNumAlgorithms] = { | |
eroman
2013/07/19 01:11:13
easier to let the compiler figure it out.
Bryan Eyler
2013/07/24 00:33:53
I made this, and future ones, refer back to arrays
| |
59 // echo -n "" | sha1sum | |
60 "da39a3ee5e6b4b0d3255bfef95601890afd80709", | |
61 // echo -n "" | sha224sum | |
62 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", | |
63 // echo -n "" | sha256sum | |
64 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | |
65 // echo -n "" | sha384sum | |
66 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e" | |
67 "debfe76f65fbd51ad2f14898b95b", | |
68 // echo -n "" | sha512sum | |
69 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0" | |
70 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", | |
71 }; | |
72 | |
73 for (int id_index = 0; id_index < kNumAlgorithms; id_index++) { | |
74 WebCryptoSHADigest* digest = | |
75 new WebCryptoSHADigest(algorithm_ids[id_index]); | |
76 | |
77 TestWebCryptoOperationResult* result = | |
78 new TestWebCryptoOperationResult(); | |
eroman
2013/07/19 01:11:13
I believe this is leaked.
Would be better to stack
Bryan Eyler
2013/07/24 00:33:53
Done.
| |
79 | |
80 digest->finish(result); | |
81 | |
82 // Ignore case, it's checking the hex value. | |
83 EXPECT_STRCASEEQ( | |
84 result->array_buffer_data_hex(), | |
85 hex_result[id_index]);; | |
86 } | |
87 } | |
88 | |
89 TEST(WebCryptoSHADigest, SampleSimpleSet) { | |
90 // For readability and maintainability of the results, the results are | |
91 // stored here in hex format. | |
92 const unsigned char* kInput = reinterpret_cast<const unsigned char*>("\0"); | |
93 const size_t kInputLength = 1; | |
94 const char* hex_result[kNumAlgorithms] = { | |
eroman
2013/07/19 01:11:13
easier to let the compiler figure it out.
Bryan Eyler
2013/07/24 00:33:53
See above.
| |
95 // echo -n -e "\000" | sha1sum | |
96 "5ba93c9db0cff93f52b521d7420e43f6eda2784f", | |
97 // echo -n -e "\000" | sha224sum | |
98 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073", | |
99 // echo -n -e "\000" | sha256sum | |
100 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", | |
101 // echo -n -e "\000" | sha384sum | |
102 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933" | |
103 "ec2b413465966817a9c208a11717", | |
104 // echo -n -e "\000" | sha512sum | |
105 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a" | |
106 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee", | |
107 }; | |
108 | |
109 for (int id_index = 0; id_index < kNumAlgorithms; id_index++) { | |
110 WebCryptoSHADigest* digest = | |
111 new WebCryptoSHADigest(algorithm_ids[id_index]); | |
112 | |
113 digest->process(kInput, kInputLength); | |
114 | |
115 TestWebCryptoOperationResult* result = | |
116 new TestWebCryptoOperationResult(); | |
117 | |
118 digest->finish(result); | |
119 | |
120 // Ignore case, it's checking the hex value. | |
121 EXPECT_STRCASEEQ( | |
122 result->array_buffer_data_hex(), | |
123 hex_result[id_index]);; | |
124 } | |
125 } | |
126 | |
127 TEST(WebCryptoSHADigest, SampleMultipartSet) { | |
128 // For readability and maintainability of the results, the results are | |
129 // stored here in hex format. | |
130 const int kNumInputSets = 6; | |
eroman
2013/07/19 01:11:13
easier to let the compiler figure it out.
However
Bryan Eyler
2013/07/24 00:33:53
Done. Seems to be fine with just arraysize()?
| |
131 struct { | |
132 const unsigned char* input; | |
133 size_t input_length; | |
134 } input_set[kNumInputSets] = { | |
135 { reinterpret_cast<const unsigned char*>("\x00"), 1}, | |
136 { reinterpret_cast<const unsigned char*>("\x01"), 1}, | |
137 { reinterpret_cast<const unsigned char*>("\x02"), 1}, | |
138 { reinterpret_cast<const unsigned char*>("\x03"), 1}, | |
139 { reinterpret_cast<const unsigned char*>("\x04"), 1}, | |
140 { reinterpret_cast<const unsigned char*>("\x05"), 1}, | |
141 }; | |
142 const char* hex_result[kNumAlgorithms] = { | |
143 // echo -n -e "\000\001\002\003\004\005" | sha1sum | |
144 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9", | |
145 // echo -n -e "\000\001\002\003\004\005" | sha224sum | |
146 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc", | |
147 // echo -n -e "\000\001\002\003\004\005" | sha256sum | |
148 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43", | |
149 // echo -n -e "\000\001\002\003\004\005" | sha384sum | |
150 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" | |
151 "22780d7e1b95bfeaa86a678e4552", | |
152 // echo -n -e "\000\001\002\003\004\005" | sha512sum | |
153 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" | |
154 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", | |
155 }; | |
156 | |
157 for (int id_index = 0; id_index < kNumAlgorithms; id_index++) { | |
158 WebCryptoSHADigest* digest = | |
159 new WebCryptoSHADigest(algorithm_ids[id_index]); | |
160 | |
161 for (int i = 0; i < kNumInputSets; i++) { | |
162 digest->process(input_set[i].input, input_set[i].input_length); | |
163 } | |
164 | |
165 TestWebCryptoOperationResult* result = | |
166 new TestWebCryptoOperationResult(); | |
167 | |
168 digest->finish(result); | |
169 | |
170 // Ignore case, it's checking the hex value. | |
171 EXPECT_STRCASEEQ( | |
172 result->array_buffer_data_hex(), | |
173 hex_result[id_index]);; | |
174 } | |
175 } | |
176 | |
177 } // namespace content | |
OLD | NEW |