Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(667)

Side by Side Diff: content/renderer/webcrypto_impl_unittest.cc

Issue 19757011: WebCrypto: Implement digest() using NSS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Export WebCryptoImpl instead of getting an instance from WebKitPlatformSupportImpl. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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,
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.
42 array_buffer_data_hex_ =
43 base::HexEncode(array_buffer.data(), array_buffer.byteLength());
44 }
45
46 virtual void completeWithBoolean(bool result) {
47 NOTIMPLEMENTED();
48 }
49
50 virtual void completeWithKey(const WebKit::WebCryptoKey& key) {
51 NOTIMPLEMENTED();
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 TEST(WebCryptoImplTest, DigestSampleSets) {
79 // The results are stored here in hex format for readability.
80 //
81 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced
82 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03
83 struct {
84 const char* input;
85 size_t input_length;
86 const char* hex_result[arraysize(kAlgorithmIds)];
87 } input_set[] = {
88 {
89 "", 0,
90 {
91 // echo -n "" | sha1sum
92 "da39a3ee5e6b4b0d3255bfef95601890afd80709",
93 // echo -n "" | sha224sum
94 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
95 // echo -n "" | sha256sum
96 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
97 // echo -n "" | sha384sum
98 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e"
99 "debfe76f65fbd51ad2f14898b95b",
100 // echo -n "" | sha512sum
101 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0"
102 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
103 },
104 },
105 {
106 "\000", 1,
107 {
108 // echo -n -e "\000" | sha1sum
109 "5ba93c9db0cff93f52b521d7420e43f6eda2784f",
110 // echo -n -e "\000" | sha224sum
111 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073",
112 // echo -n -e "\000" | sha256sum
113 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d",
114 // echo -n -e "\000" | sha384sum
115 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933"
116 "ec2b413465966817a9c208a11717",
117 // echo -n -e "\000" | sha512sum
118 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a"
119 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee",
120 },
121 },
122 {
123 "\000\001\002\003\004\005", 6,
124 {
125 // echo -n -e "\000\001\002\003\004\005" | sha1sum
126 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9",
127 // echo -n -e "\000\001\002\003\004\005" | sha224sum
128 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc",
129 // echo -n -e "\000\001\002\003\004\005" | sha256sum
130 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43",
131 // echo -n -e "\000\001\002\003\004\005" | sha384sum
132 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc"
133 "22780d7e1b95bfeaa86a678e4552",
134 // echo -n -e "\000\001\002\003\004\005" | sha512sum
135 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9"
136 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c",
137 },
138 },
139 };
140
141 for (size_t id_index = 0; id_index < arraysize(kAlgorithmIds); id_index++) {
142 WebKit::WebCryptoAlgorithm algorithm(
143 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
144 kAlgorithmIds[id_index], "SHA", NULL));
145
146 for (size_t set_index = 0;
147 set_index < ARRAYSIZE_UNSAFE(input_set);
148 set_index++) {
149 scoped_refptr<TestWebCryptoResult> result_impl(
150 new TestWebCryptoResult);
151 WebKit::WebCryptoResult result(result_impl.get());
152
153 WebCryptoImpl crypto;
154 crypto.digest(
155 algorithm,
156 reinterpret_cast<const unsigned char*>(input_set[set_index].input),
157 input_set[set_index].input_length,
158 result);
159
160 EXPECT_FALSE(result_impl->error_encountered());
161 // Ignore case, it's checking the hex value.
162 EXPECT_STRCASEEQ(
163 input_set[set_index].hex_result[id_index],
164 result_impl->array_buffer_data_hex().c_str());
165 }
166 }
167 }
168
169 } // namespace content
OLDNEW
« content/renderer/webcrypto_impl_openssl.cc ('K') | « content/renderer/webcrypto_impl_openssl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698