 Chromium Code Reviews
 Chromium Code Reviews Issue 19757011:
  WebCrypto: Implement digest() using NSS  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 19757011:
  WebCrypto: Implement digest() using NSS  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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 const WebKit::WebCryptoAlgorithmId kAlgorithmIds[] = { | |
| 21 WebKit::WebCryptoAlgorithmIdSha1, | |
| 
jamesr
2013/08/23 01:27:40
why the 4-space indent?
 
Bryan Eyler
2013/08/23 17:45:31
See comments in patch set 5: https://codereview.ch
 | |
| 22 WebKit::WebCryptoAlgorithmIdSha224, | |
| 23 WebKit::WebCryptoAlgorithmIdSha256, | |
| 24 WebKit::WebCryptoAlgorithmIdSha384, | |
| 25 WebKit::WebCryptoAlgorithmIdSha512 | |
| 26 }; | |
| 27 | |
| 28 class TestWebCryptoResult | |
| 29 : public WebKit::WebCryptoResultPrivate, | |
| 30 public base::RefCountedThreadSafe<TestWebCryptoResult> { | |
| 31 public: | |
| 32 TestWebCryptoResult() : error_encountered_(false) {} | |
| 33 | |
| 34 virtual void completeWithError() { | |
| 35 error_encountered_ = true; | |
| 36 } | |
| 37 | |
| 38 virtual void completeWithBuffer( | |
| 39 const WebKit::WebArrayBuffer& array_buffer) { | |
| 40 // Might as well just store the array_buffer in hex, since it's going to be | |
| 41 // converted anyway for better error reporting. +1 for the nul terminator. | |
| 
jamesr
2013/08/23 01:27:40
the comment says +1, but I don't see anything of t
 
Bryan Eyler
2013/08/23 17:45:31
Sorry, that part of the comment is stale.  Removed
 | |
| 42 array_buffer_data_hex_ = | |
| 43 base::HexEncode(array_buffer.data(), array_buffer.byteLength()); | |
| 44 } | |
| 45 | |
| 46 virtual void completeWithBoolean(bool boolean) { | |
| 
jamesr
2013/08/23 01:27:40
"bool boolean"? is there no better name?
 
Bryan Eyler
2013/08/23 17:45:31
Changed to "result".
 | |
| 47 NOTREACHED(); | |
| 
jamesr
2013/08/23 01:27:40
would NOTIMPLEMENTED() be a better match?
 
Bryan Eyler
2013/08/23 17:45:31
Yes, I think so, since I eventually intend to impl
 | |
| 48 } | |
| 49 | |
| 50 virtual void completeWithKey(const WebKit::WebCryptoKey& key) { | |
| 51 NOTREACHED(); | |
| 52 } | |
| 53 | |
| 54 virtual void ref() { | |
| 55 RefCountedThreadSafe<TestWebCryptoResult>::AddRef(); | |
| 56 } | |
| 57 | |
| 58 virtual void deref() { | |
| 59 RefCountedThreadSafe<TestWebCryptoResult>::Release(); | |
| 60 } | |
| 61 | |
| 62 const std::string& array_buffer_data_hex() const { | |
| 63 return array_buffer_data_hex_; | |
| 64 } | |
| 65 | |
| 66 bool error_encountered() const { | |
| 67 return error_encountered_; | |
| 68 } | |
| 69 | |
| 70 private: | |
| 71 friend class base::RefCountedThreadSafe<TestWebCryptoResult>; | |
| 72 virtual ~TestWebCryptoResult() {} | |
| 73 | |
| 74 std::string array_buffer_data_hex_; | |
| 75 bool error_encountered_; | |
| 76 }; | |
| 77 | |
| 78 class WebCryptoImplTest : public testing::Test { | |
| 79 public: | |
| 80 WebCryptoImplTest() : crypto_(NULL) {} | |
| 81 | |
| 82 protected: | |
| 83 virtual void SetUp() { | |
| 84 SetRendererClientForTesting(&content_renderer_client_); | |
| 85 | |
| 86 webkit_platform_support_.reset(new RendererWebKitPlatformSupportImpl); | |
| 
jamesr
2013/08/23 01:27:40
do you need to instantiate an entire RendererWebKi
 
Bryan Eyler
2013/08/23 17:45:31
WebCryptoImpl is not exported as part of the conte
 
jamesr
2013/08/23 18:16:58
NAK, if you need access to WebCryptoImpl in a test
 | |
| 87 | |
| 88 crypto_ = webkit_platform_support_->crypto(); | |
| 89 ASSERT_TRUE(crypto_ != NULL); | |
| 90 } | |
| 91 | |
| 92 virtual void TearDown() { | |
| 93 SetRendererClientForTesting(NULL); | |
| 94 } | |
| 95 | |
| 96 ContentRendererClient content_renderer_client_; | |
| 97 scoped_ptr<RendererWebKitPlatformSupportImpl> webkit_platform_support_; | |
| 98 WebKit::WebCrypto* crypto_; | |
| 99 }; | |
| 100 | |
| 101 TEST_F(WebCryptoImplTest, DigestSampleSets) { | |
| 102 // The results are stored here in hex format for readability. | |
| 103 // | |
| 104 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced | |
| 105 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 | |
| 106 struct { | |
| 107 const char* input; | |
| 108 size_t input_length; | |
| 109 const char* hex_result[arraysize(kAlgorithmIds)]; | |
| 110 } input_set[] = { | |
| 111 { | |
| 112 "", 0, | |
| 113 { | |
| 114 // echo -n "" | sha1sum | |
| 115 "da39a3ee5e6b4b0d3255bfef95601890afd80709", | |
| 116 // echo -n "" | sha224sum | |
| 117 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", | |
| 118 // echo -n "" | sha256sum | |
| 119 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | |
| 120 // echo -n "" | sha384sum | |
| 121 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e" | |
| 122 "debfe76f65fbd51ad2f14898b95b", | |
| 123 // echo -n "" | sha512sum | |
| 124 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0" | |
| 125 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", | |
| 126 }, | |
| 127 }, | |
| 128 { | |
| 129 "\000", 1, | |
| 130 { | |
| 131 // echo -n -e "\000" | sha1sum | |
| 132 "5ba93c9db0cff93f52b521d7420e43f6eda2784f", | |
| 133 // echo -n -e "\000" | sha224sum | |
| 134 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073", | |
| 135 // echo -n -e "\000" | sha256sum | |
| 136 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", | |
| 137 // echo -n -e "\000" | sha384sum | |
| 138 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933" | |
| 139 "ec2b413465966817a9c208a11717", | |
| 140 // echo -n -e "\000" | sha512sum | |
| 141 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a" | |
| 142 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee", | |
| 143 }, | |
| 144 }, | |
| 145 { | |
| 146 "\000\001\002\003\004\005", 6, | |
| 147 { | |
| 148 // echo -n -e "\000\001\002\003\004\005" | sha1sum | |
| 149 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9", | |
| 150 // echo -n -e "\000\001\002\003\004\005" | sha224sum | |
| 151 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc", | |
| 152 // echo -n -e "\000\001\002\003\004\005" | sha256sum | |
| 153 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43", | |
| 154 // echo -n -e "\000\001\002\003\004\005" | sha384sum | |
| 155 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" | |
| 156 "22780d7e1b95bfeaa86a678e4552", | |
| 157 // echo -n -e "\000\001\002\003\004\005" | sha512sum | |
| 158 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" | |
| 159 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", | |
| 160 }, | |
| 161 }, | |
| 162 }; | |
| 163 | |
| 164 for (size_t id_index = 0; id_index < arraysize(kAlgorithmIds); id_index++) { | |
| 165 WebKit::WebCryptoAlgorithm algorithm( | |
| 166 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
| 167 kAlgorithmIds[id_index], "SHA", NULL)); | |
| 168 | |
| 169 for (size_t set_index = 0; | |
| 170 set_index < ARRAYSIZE_UNSAFE(input_set); set_index++) { | |
| 
jamesr
2013/08/23 01:27:40
nit: since the first and second statements in the
 
Bryan Eyler
2013/08/23 17:45:31
Done.
 | |
| 171 scoped_refptr<TestWebCryptoResult> result_impl( | |
| 172 new TestWebCryptoResult); | |
| 173 WebKit::WebCryptoResult result(result_impl.get()); | |
| 174 | |
| 175 crypto_->digest( | |
| 176 algorithm, | |
| 177 reinterpret_cast<const unsigned char*>(input_set[set_index].input), | |
| 178 input_set[set_index].input_length, | |
| 179 result); | |
| 180 | |
| 181 EXPECT_FALSE(result_impl->error_encountered()); | |
| 182 // Ignore case, it's checking the hex value. | |
| 183 EXPECT_STRCASEEQ( | |
| 184 input_set[set_index].hex_result[id_index], | |
| 185 result_impl->array_buffer_data_hex().c_str()); | |
| 186 } | |
| 187 } | |
| 188 } | |
| 189 | |
| 190 } // namespace content | |
| OLD | NEW |