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/strings/string_number_conversions.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | |
| 11 | |
| 12 namespace content { | |
| 13 | |
| 14 // Algorithms include SHA1, SHA224, SHA256, SHA384, and SHA512 | |
| 15 WebKit::WebCryptoAlgorithmId algorithm_ids[] = { | |
| 16 WebKit::WebCryptoAlgorithmIdSha1, | |
| 17 WebKit::WebCryptoAlgorithmIdSha224, | |
| 18 WebKit::WebCryptoAlgorithmIdSha256, | |
| 19 WebKit::WebCryptoAlgorithmIdSha384, | |
| 20 WebKit::WebCryptoAlgorithmIdSha512 | |
| 21 }; | |
| 22 | |
| 23 class TestWebCryptoOperationResult : public WebKit::WebCryptoOperationResult { | |
| 24 public: | |
| 25 TestWebCryptoOperationResult() | |
| 26 : was_error_encountered_(false) { | |
|
eroman
2013/07/24 01:33:50
indentation. also remove the "was_"
Bryan Eyler
2013/07/31 00:28:44
Done.
| |
| 27 } | |
| 28 | |
| 29 virtual ~TestWebCryptoOperationResult() { | |
| 30 } | |
| 31 | |
| 32 // There's no need to implement the initialization* routines because the | |
| 33 // initialization is already checked for failure in the tests. | |
| 34 virtual void initializationFailed() OVERRIDE { | |
| 35 } | |
| 36 | |
| 37 virtual void initializationSucceeded( | |
| 38 WebKit::WebCryptoOperation* op) OVERRIDE { | |
| 39 } | |
| 40 | |
| 41 virtual void completeWithArrayBuffer( | |
| 42 const WebKit::WebArrayBuffer& array_buffer) OVERRIDE { | |
| 43 // Might as well just store the array_buffer in hex, since it's going to be | |
| 44 // converted anyway for better error reporting. +1 for the nul terminator. | |
| 45 array_buffer_data_hex_ = | |
| 46 base::HexEncode(array_buffer.data(), array_buffer.byteLength()); | |
| 47 } | |
| 48 | |
| 49 virtual void completeWithError() OVERRIDE { | |
| 50 was_error_encountered_ = true; | |
| 51 } | |
| 52 | |
| 53 const char* array_buffer_data_hex() const { | |
| 54 return array_buffer_data_hex_.c_str(); | |
| 55 } | |
| 56 | |
| 57 bool was_error_encountered() const { | |
| 58 return was_error_encountered_; | |
| 59 } | |
| 60 | |
| 61 protected: | |
| 62 std::string array_buffer_data_hex_; | |
| 63 bool was_error_encountered_; | |
| 64 }; | |
| 65 | |
| 66 TEST(WebCryptoSHADigest, SampleEmptySet) { | |
| 67 // For readability and maintainability of the results, the results are | |
| 68 // stored here in hex format. | |
| 69 const char* hex_result[arraysize(algorithm_ids)] = { | |
| 70 // echo -n "" | sha1sum | |
| 71 "da39a3ee5e6b4b0d3255bfef95601890afd80709", | |
| 72 // echo -n "" | sha224sum | |
| 73 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", | |
| 74 // echo -n "" | sha256sum | |
| 75 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | |
| 76 // echo -n "" | sha384sum | |
| 77 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e" | |
| 78 "debfe76f65fbd51ad2f14898b95b", | |
| 79 // echo -n "" | sha512sum | |
| 80 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0" | |
| 81 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", | |
| 82 }; | |
| 83 | |
| 84 for (unsigned int id_index = 0; | |
| 85 id_index < arraysize(algorithm_ids); id_index++) { | |
| 86 TestWebCryptoOperationResult result; | |
| 87 | |
| 88 WebCryptoSHADigest* digest = | |
| 89 new WebCryptoSHADigest(algorithm_ids[id_index], &result); | |
| 90 ASSERT_TRUE(digest); | |
| 91 | |
| 92 EXPECT_TRUE(digest->Initialize()); | |
| 93 | |
| 94 digest->finish(); | |
| 95 | |
| 96 // Ignore case, it's checking the hex value. | |
| 97 EXPECT_STRCASEEQ( | |
| 98 result.array_buffer_data_hex(), | |
| 99 hex_result[id_index]); | |
| 100 EXPECT_FALSE(result.was_error_encountered()); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 TEST(WebCryptoSHADigest, SampleSimpleSet) { | |
| 105 // For readability and maintainability of the results, the results are | |
| 106 // stored here in hex format. | |
| 107 const unsigned char* kInput = reinterpret_cast<const unsigned char*>("\0"); | |
| 108 const size_t kInputLength = 1; | |
| 109 const char* hex_result[arraysize(algorithm_ids)] = { | |
| 110 // echo -n -e "\000" | sha1sum | |
| 111 "5ba93c9db0cff93f52b521d7420e43f6eda2784f", | |
| 112 // echo -n -e "\000" | sha224sum | |
| 113 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073", | |
| 114 // echo -n -e "\000" | sha256sum | |
| 115 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", | |
| 116 // echo -n -e "\000" | sha384sum | |
| 117 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933" | |
| 118 "ec2b413465966817a9c208a11717", | |
| 119 // echo -n -e "\000" | sha512sum | |
| 120 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a" | |
| 121 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee", | |
| 122 }; | |
| 123 | |
| 124 for (unsigned int id_index = 0; | |
| 125 id_index < arraysize(algorithm_ids); id_index++) { | |
| 126 TestWebCryptoOperationResult result; | |
| 127 | |
| 128 WebCryptoSHADigest* digest = | |
| 129 new WebCryptoSHADigest(algorithm_ids[id_index], &result); | |
| 130 ASSERT_TRUE(digest); | |
| 131 | |
| 132 EXPECT_TRUE(digest->Initialize()); | |
| 133 | |
| 134 digest->process(kInput, kInputLength); | |
| 135 | |
| 136 digest->finish(); | |
| 137 | |
| 138 // Ignore case, it's checking the hex value. | |
| 139 EXPECT_STRCASEEQ( | |
| 140 result.array_buffer_data_hex(), | |
| 141 hex_result[id_index]); | |
| 142 EXPECT_FALSE(result.was_error_encountered()); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 TEST(WebCryptoSHADigest, SampleMultipartSet) { | |
| 147 // For readability and maintainability of the results, the results are | |
| 148 // stored here in hex format. | |
| 149 struct { | |
| 150 const unsigned char* input; | |
| 151 size_t input_length; | |
| 152 } input_set[] = { | |
| 153 { reinterpret_cast<const unsigned char*>("\x00"), 1}, | |
| 154 { reinterpret_cast<const unsigned char*>("\x01"), 1}, | |
| 155 { reinterpret_cast<const unsigned char*>("\x02"), 1}, | |
| 156 { reinterpret_cast<const unsigned char*>("\x03"), 1}, | |
| 157 { reinterpret_cast<const unsigned char*>("\x04"), 1}, | |
| 158 { reinterpret_cast<const unsigned char*>("\x05"), 1}, | |
| 159 }; | |
| 160 const char* hex_result[arraysize(algorithm_ids)] = { | |
| 161 // echo -n -e "\000\001\002\003\004\005" | sha1sum | |
| 162 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9", | |
| 163 // echo -n -e "\000\001\002\003\004\005" | sha224sum | |
| 164 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc", | |
| 165 // echo -n -e "\000\001\002\003\004\005" | sha256sum | |
| 166 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43", | |
| 167 // echo -n -e "\000\001\002\003\004\005" | sha384sum | |
| 168 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" | |
| 169 "22780d7e1b95bfeaa86a678e4552", | |
| 170 // echo -n -e "\000\001\002\003\004\005" | sha512sum | |
| 171 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" | |
| 172 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", | |
| 173 }; | |
| 174 | |
| 175 for (unsigned int id_index = 0; | |
| 176 id_index < arraysize(algorithm_ids); id_index++) { | |
| 177 TestWebCryptoOperationResult result; | |
| 178 | |
| 179 WebCryptoSHADigest* digest = | |
| 180 new WebCryptoSHADigest(algorithm_ids[id_index], &result); | |
| 181 ASSERT_TRUE(digest); | |
| 182 | |
| 183 EXPECT_TRUE(digest->Initialize()); | |
| 184 | |
| 185 for (unsigned int i = 0; i < arraysize(input_set); i++) { | |
| 186 digest->process(input_set[i].input, input_set[i].input_length); | |
| 187 } | |
| 188 | |
| 189 digest->finish(); | |
| 190 | |
| 191 // Ignore case, it's checking the hex value. | |
| 192 EXPECT_STRCASEEQ( | |
| 193 result.array_buffer_data_hex(), | |
| 194 hex_result[id_index]); | |
| 195 EXPECT_FALSE(result.was_error_encountered()); | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 } // namespace content | |
| OLD | NEW |