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

Side by Side Diff: components/webcrypto/test/test_helpers.h

Issue 1304063015: [refactor] Rename the webcrypto/openssl and webcrypto/test directories. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@jwk_refactor
Patch Set: fix filename in gn build Created 5 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
« no previous file with comments | « components/webcrypto/test/status_unittest.cc ('k') | components/webcrypto/test/test_helpers.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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 #ifndef COMPONENTS_WEBCRYPTO_TEST_TEST_HELPERS_H_
6 #define COMPONENTS_WEBCRYPTO_TEST_TEST_HELPERS_H_
7
8 #include <ostream>
9 #include <string>
10 #include <vector>
11
12 #include "base/memory/scoped_ptr.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
15 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
16
17 #define EXPECT_BYTES_EQ(expected, actual) \
18 EXPECT_EQ(CryptoData(expected), CryptoData(actual))
19
20 #define EXPECT_BYTES_EQ_HEX(expected_hex, actual_bytes) \
21 EXPECT_BYTES_EQ(HexStringToBytes(expected_hex), actual_bytes)
22
23 namespace base {
24 class DictionaryValue;
25 class ListValue;
26 class Value;
27 }
28
29 namespace blink {
30 class WebCryptoAlgorithm;
31 }
32
33 namespace webcrypto {
34
35 // Base class for WebCrypto tests. All WebCrypto tests must derive from this
36 // to ensure that Blink has been properly initialized. In particular,
37 // the WebCrypto tests use blink::WebCryptoAlgorithm, which in turn relies on
38 // PartitionAlloc.
39 class WebCryptoTestBase : public testing::Test {
40 protected:
41 static void SetUpTestCase();
42 };
43
44 class Status;
45 class CryptoData;
46
47 // These functions are used by GTEST to support EXPECT_EQ() for
48 // webcrypto::Status and webcrypto::CryptoData
49
50 void PrintTo(const Status& status, ::std::ostream* os);
51 bool operator==(const Status& a, const Status& b);
52 bool operator!=(const Status& a, const Status& b);
53
54 void PrintTo(const CryptoData& data, ::std::ostream* os);
55 bool operator==(const CryptoData& a, const CryptoData& b);
56 bool operator!=(const CryptoData& a, const CryptoData& b);
57
58 // Gives a human-readable description of |status| and any error it represents.
59 std::string StatusToString(const Status& status);
60
61 blink::WebCryptoAlgorithm CreateRsaHashedKeyGenAlgorithm(
62 blink::WebCryptoAlgorithmId algorithm_id,
63 const blink::WebCryptoAlgorithmId hash_id,
64 unsigned int modulus_length,
65 const std::vector<uint8_t>& public_exponent);
66
67 // Returns a slightly modified version of the input vector.
68 //
69 // - For non-empty inputs a single bit is inverted.
70 // - For empty inputs, a byte is added.
71 std::vector<uint8_t> Corrupted(const std::vector<uint8_t>& input);
72
73 std::vector<uint8_t> HexStringToBytes(const std::string& hex);
74
75 std::vector<uint8_t> MakeJsonVector(const std::string& json_string);
76 std::vector<uint8_t> MakeJsonVector(const base::DictionaryValue& dict);
77
78 // ----------------------------------------------------------------
79 // Helpers for working with JSON data files for test expectations.
80 // ----------------------------------------------------------------
81
82 // Reads a file in "src/content/test/data/webcrypto" to a base::Value.
83 // The file must be JSON, however it can also include C++ style comments.
84 ::testing::AssertionResult ReadJsonTestFile(const char* test_file_name,
85 scoped_ptr<base::Value>* value);
86 // Same as ReadJsonTestFile(), but returns the value as a List.
87 ::testing::AssertionResult ReadJsonTestFileToList(
88 const char* test_file_name,
89 scoped_ptr<base::ListValue>* list);
90 // Same as ReadJsonTestFile(), but returns the value as a Dictionary.
91 ::testing::AssertionResult ReadJsonTestFileToDictionary(
92 const char* test_file_name,
93 scoped_ptr<base::DictionaryValue>* dict);
94
95 // Reads a string property from the dictionary with path |property_name|
96 // (which can include periods for nested dictionaries). Interprets the
97 // string as a hex encoded string and converts it to a bytes list.
98 //
99 // Returns empty vector on failure.
100 std::vector<uint8_t> GetBytesFromHexString(const base::DictionaryValue* dict,
101 const std::string& property_name);
102
103 // Reads a string property with path "property_name" and converts it to a
104 // WebCryptoAlgorith. Returns null algorithm on failure.
105 blink::WebCryptoAlgorithm GetDigestAlgorithm(const base::DictionaryValue* dict,
106 const char* property_name);
107
108 // Returns true if any of the vectors in the input list have identical content.
109 bool CopiesExist(const std::vector<std::vector<uint8_t>>& bufs);
110
111 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm(
112 blink::WebCryptoAlgorithmId aes_alg_id,
113 unsigned short length);
114
115 // The following key pair is comprised of the SPKI (public key) and PKCS#8
116 // (private key) representations of the key pair provided in Example 1 of the
117 // NIST test vectors at
118 // ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15sign-vectors.txt
119 extern const unsigned int kModulusLengthBits;
120 extern const char* const kPublicKeySpkiDerHex;
121 extern const char* const kPrivateKeyPkcs8DerHex;
122
123 // The modulus and exponent (in hex) of kPublicKeySpkiDerHex
124 extern const char* const kPublicKeyModulusHex;
125 extern const char* const kPublicKeyExponentHex;
126
127 blink::WebCryptoKey ImportSecretKeyFromRaw(
128 const std::vector<uint8_t>& key_raw,
129 const blink::WebCryptoAlgorithm& algorithm,
130 blink::WebCryptoKeyUsageMask usage);
131
132 void ImportRsaKeyPair(const std::vector<uint8_t>& spki_der,
133 const std::vector<uint8_t>& pkcs8_der,
134 const blink::WebCryptoAlgorithm& algorithm,
135 bool extractable,
136 blink::WebCryptoKeyUsageMask public_key_usages,
137 blink::WebCryptoKeyUsageMask private_key_usages,
138 blink::WebCryptoKey* public_key,
139 blink::WebCryptoKey* private_key);
140
141 Status ImportKeyJwkFromDict(const base::DictionaryValue& dict,
142 const blink::WebCryptoAlgorithm& algorithm,
143 bool extractable,
144 blink::WebCryptoKeyUsageMask usages,
145 blink::WebCryptoKey* key);
146
147 // Parses a vector of JSON into a dictionary.
148 scoped_ptr<base::DictionaryValue> GetJwkDictionary(
149 const std::vector<uint8_t>& json);
150
151 // Verifies the input dictionary contains the expected values. Exact matches are
152 // required on the fields examined.
153 ::testing::AssertionResult VerifyJwk(
154 const scoped_ptr<base::DictionaryValue>& dict,
155 const std::string& kty_expected,
156 const std::string& alg_expected,
157 blink::WebCryptoKeyUsageMask use_mask_expected);
158
159 ::testing::AssertionResult VerifySecretJwk(
160 const std::vector<uint8_t>& json,
161 const std::string& alg_expected,
162 const std::string& k_expected_hex,
163 blink::WebCryptoKeyUsageMask use_mask_expected);
164
165 // Verifies that the JSON in the input vector contains the provided
166 // expected values. Exact matches are required on the fields examined.
167 ::testing::AssertionResult VerifyPublicJwk(
168 const std::vector<uint8_t>& json,
169 const std::string& alg_expected,
170 const std::string& n_expected_hex,
171 const std::string& e_expected_hex,
172 blink::WebCryptoKeyUsageMask use_mask_expected);
173
174 // Helper that tests importing ane exporting of symmetric keys as JWK.
175 void ImportExportJwkSymmetricKey(
176 int key_len_bits,
177 const blink::WebCryptoAlgorithm& import_algorithm,
178 blink::WebCryptoKeyUsageMask usages,
179 const std::string& jwk_alg);
180
181 // Wrappers around GenerateKey() which expect the result to be either a secret
182 // key or a public/private keypair. If the result does not match the
183 // expectation, then it fails with Status::ErrorUnexpected().
184 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm,
185 bool extractable,
186 blink::WebCryptoKeyUsageMask usages,
187 blink::WebCryptoKey* key);
188 Status GenerateKeyPair(const blink::WebCryptoAlgorithm& algorithm,
189 bool extractable,
190 blink::WebCryptoKeyUsageMask usages,
191 blink::WebCryptoKey* public_key,
192 blink::WebCryptoKey* private_key);
193
194 // Reads a key format string as used in some JSON test files and converts it to
195 // a WebCryptoKeyFormat.
196 blink::WebCryptoKeyFormat GetKeyFormatFromJsonTestCase(
197 const base::DictionaryValue* test);
198
199 // Extracts the key data bytes from |test| as used insome JSON test files.
200 std::vector<uint8_t> GetKeyDataFromJsonTestCase(
201 const base::DictionaryValue* test,
202 blink::WebCryptoKeyFormat key_format);
203
204 // Reads the "crv" string from a JSON test case and returns it as a
205 // WebCryptoNamedCurve.
206 blink::WebCryptoNamedCurve GetCurveNameFromDictionary(
207 const base::DictionaryValue* dict);
208
209 } // namespace webcrypto
210
211 #endif // COMPONENTS_WEBCRYPTO_TEST_TEST_HELPERS_H_
OLDNEW
« no previous file with comments | « components/webcrypto/test/status_unittest.cc ('k') | components/webcrypto/test/test_helpers.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698