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_impl.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 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 // Algorithms include SHA1, SHA224, SHA256, SHA384, and SHA512 | |
|
eroman
2013/08/22 02:25:05
Delete this comment, doesn't explain anything extr
Bryan Eyler
2013/08/22 18:51:03
Done.
| |
| 21 WebKit::WebCryptoAlgorithmId algorithm_ids[] = { | |
|
eroman
2013/08/22 02:25:05
mark this "const", and rename algorithm_ids --> kA
Bryan Eyler
2013/08/22 18:51:03
Done.
| |
| 22 WebKit::WebCryptoAlgorithmIdSha1, | |
| 23 WebKit::WebCryptoAlgorithmIdSha224, | |
| 24 WebKit::WebCryptoAlgorithmIdSha256, | |
| 25 WebKit::WebCryptoAlgorithmIdSha384, | |
| 26 WebKit::WebCryptoAlgorithmIdSha512 | |
| 27 }; | |
| 28 | |
| 29 class TestWebCryptoResult | |
| 30 : public WebKit::WebCryptoResultPrivate, | |
| 31 public base::RefCountedThreadSafe<TestWebCryptoResult> { | |
| 32 public: | |
| 33 TestWebCryptoResult() : error_encountered_(false) {} | |
| 34 | |
| 35 virtual void completeWithError() OVERRIDE { | |
|
eroman
2013/08/22 02:25:05
I think it is better practice to omit OVERRIDE on
jamesr
2013/08/22 02:31:01
no OVERRIDE for cross-repository overrides!
Bryan Eyler
2013/08/22 18:51:03
Done.
| |
| 36 error_encountered_ = true; | |
| 37 } | |
| 38 | |
| 39 virtual void completeWithBuffer( | |
| 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 completeWithBoolean(bool boolean) OVERRIDE { | |
| 48 NOTREACHED(); | |
| 49 } | |
| 50 | |
| 51 virtual void completeWithKey(const WebKit::WebCryptoKey& key) OVERRIDE { | |
| 52 NOTREACHED(); | |
| 53 } | |
| 54 | |
| 55 virtual void ref() { | |
| 56 RefCountedThreadSafe<TestWebCryptoResult>::AddRef(); | |
|
jamesr
2013/08/22 02:31:01
these are a bit odd. what are they implementing?
Bryan Eyler
2013/08/22 18:51:03
Ref counting as required in the WebCryptoResultPri
| |
| 57 } | |
| 58 | |
| 59 virtual void deref() { | |
| 60 RefCountedThreadSafe<TestWebCryptoResult>::Release(); | |
| 61 } | |
| 62 | |
| 63 const char* array_buffer_data_hex() const { | |
| 64 return array_buffer_data_hex_.c_str(); | |
|
eroman
2013/08/22 02:25:05
how about returning a const std::string& instead?
Bryan Eyler
2013/08/22 18:51:03
Done.
| |
| 65 } | |
| 66 | |
| 67 bool error_encountered() const { | |
| 68 return error_encountered_; | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 friend class base::RefCountedThreadSafe<TestWebCryptoResult>; | |
| 73 virtual ~TestWebCryptoResult() {} | |
| 74 | |
| 75 std::string array_buffer_data_hex_; | |
| 76 bool error_encountered_; | |
| 77 }; | |
| 78 | |
| 79 class WebCryptoImplTest : public testing::Test { | |
| 80 public: | |
| 81 WebCryptoImplTest() : crypto_(NULL) {} | |
| 82 | |
| 83 protected: | |
| 84 virtual void SetUp() { | |
| 85 SetRendererClientForTesting(&content_renderer_client_); | |
|
eroman
2013/08/22 02:25:05
Can you just directly instantiate the class under
Bryan Eyler
2013/08/22 18:51:03
No, it doesn't link because the WebCryptoImpl clas
| |
| 86 | |
| 87 webkit_platform_support_.reset(new RendererWebKitPlatformSupportImpl); | |
| 88 | |
| 89 crypto_ = webkit_platform_support_->crypto(); | |
| 90 ASSERT_TRUE(crypto_ != NULL); | |
| 91 } | |
| 92 | |
| 93 virtual void TearDown() { | |
| 94 SetRendererClientForTesting(NULL); | |
| 95 } | |
| 96 | |
| 97 ContentRendererClient content_renderer_client_; | |
| 98 scoped_ptr<RendererWebKitPlatformSupportImpl> webkit_platform_support_; | |
| 99 WebKit::WebCrypto* crypto_; | |
| 100 }; | |
| 101 | |
| 102 TEST_F(WebCryptoImplTest, DigestSampleSets) { | |
| 103 // For readability and maintainability of the results, the results are | |
|
eroman
2013/08/22 02:25:05
nit: redundant use of the word results. Try:
For r
Bryan Eyler
2013/08/22 18:51:03
Done.
| |
| 104 // stored here in hex format. | |
| 105 // | |
| 106 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced | |
| 107 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 | |
| 108 struct { | |
| 109 const char* input; | |
| 110 size_t input_length; | |
| 111 const char* hex_result[arraysize(algorithm_ids)]; | |
| 112 } input_set[] = { | |
| 113 { | |
| 114 "", 0, | |
| 115 { | |
| 116 // echo -n "" | sha1sum | |
| 117 "da39a3ee5e6b4b0d3255bfef95601890afd80709", | |
| 118 // echo -n "" | sha224sum | |
| 119 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", | |
| 120 // echo -n "" | sha256sum | |
| 121 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | |
| 122 // echo -n "" | sha384sum | |
| 123 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e" | |
| 124 "debfe76f65fbd51ad2f14898b95b", | |
| 125 // echo -n "" | sha512sum | |
| 126 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0" | |
| 127 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", | |
| 128 }, | |
| 129 }, | |
| 130 { | |
| 131 "\000", 1, | |
| 132 { | |
| 133 // echo -n -e "\000" | sha1sum | |
| 134 "5ba93c9db0cff93f52b521d7420e43f6eda2784f", | |
| 135 // echo -n -e "\000" | sha224sum | |
| 136 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073", | |
| 137 // echo -n -e "\000" | sha256sum | |
| 138 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", | |
| 139 // echo -n -e "\000" | sha384sum | |
| 140 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933" | |
| 141 "ec2b413465966817a9c208a11717", | |
| 142 // echo -n -e "\000" | sha512sum | |
| 143 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a" | |
| 144 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee", | |
| 145 }, | |
| 146 }, | |
| 147 { | |
| 148 "\000\001\002\003\004\005", 6, | |
| 149 { | |
| 150 // echo -n -e "\000\001\002\003\004\005" | sha1sum | |
| 151 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9", | |
| 152 // echo -n -e "\000\001\002\003\004\005" | sha224sum | |
| 153 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc", | |
| 154 // echo -n -e "\000\001\002\003\004\005" | sha256sum | |
| 155 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43", | |
| 156 // echo -n -e "\000\001\002\003\004\005" | sha384sum | |
| 157 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" | |
| 158 "22780d7e1b95bfeaa86a678e4552", | |
| 159 // echo -n -e "\000\001\002\003\004\005" | sha512sum | |
| 160 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" | |
| 161 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", | |
| 162 }, | |
| 163 }, | |
| 164 }; | |
| 165 | |
| 166 for (size_t id_index = 0; id_index < arraysize(algorithm_ids); id_index++) { | |
| 167 WebKit::WebCryptoAlgorithm algorithm( | |
| 168 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
| 169 algorithm_ids[id_index], "SHA", NULL)); | |
| 170 | |
| 171 for (size_t set_index = 0; | |
| 172 set_index < ARRAYSIZE_UNSAFE(input_set); set_index++) { | |
| 173 scoped_refptr<TestWebCryptoResult> result_impl( | |
| 174 new TestWebCryptoResult); | |
| 175 WebKit::WebCryptoResult result(result_impl.get()); | |
| 176 | |
| 177 crypto_->digest( | |
| 178 algorithm, | |
| 179 reinterpret_cast<const unsigned char*>(input_set[set_index].input), | |
| 180 input_set[set_index].input_length, | |
| 181 result); | |
| 182 | |
| 183 EXPECT_FALSE(result_impl->error_encountered()); | |
| 184 // Ignore case, it's checking the hex value. | |
| 185 EXPECT_STRCASEEQ( | |
| 186 input_set[set_index].hex_result[id_index], | |
| 187 result_impl->array_buffer_data_hex()); | |
| 188 } | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 } // namespace content | |
| OLD | NEW |