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