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

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: 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
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 class Status;
36 class CryptoData;
37
38 // These functions are used by GTEST to support EXPECT_EQ() for
39 // webcrypto::Status and webcrypto::CryptoData
40
41 void PrintTo(const Status& status, ::std::ostream* os);
42 bool operator==(const Status& a, const Status& b);
43 bool operator!=(const Status& a, const Status& b);
44
45 void PrintTo(const CryptoData& data, ::std::ostream* os);
46 bool operator==(const CryptoData& a, const CryptoData& b);
47 bool operator!=(const CryptoData& a, const CryptoData& b);
48
49 // Gives a human-readable description of |status| and any error it represents.
50 std::string StatusToString(const Status& status);
51
52 blink::WebCryptoAlgorithm CreateRsaHashedKeyGenAlgorithm(
53 blink::WebCryptoAlgorithmId algorithm_id,
54 const blink::WebCryptoAlgorithmId hash_id,
55 unsigned int modulus_length,
56 const std::vector<uint8_t>& public_exponent);
57
58 // Returns a slightly modified version of the input vector.
59 //
60 // - For non-empty inputs a single bit is inverted.
61 // - For empty inputs, a byte is added.
62 std::vector<uint8_t> Corrupted(const std::vector<uint8_t>& input);
63
64 std::vector<uint8_t> HexStringToBytes(const std::string& hex);
65
66 std::vector<uint8_t> MakeJsonVector(const std::string& json_string);
67 std::vector<uint8_t> MakeJsonVector(const base::DictionaryValue& dict);
68
69 // ----------------------------------------------------------------
70 // Helpers for working with JSON data files for test expectations.
71 // ----------------------------------------------------------------
72
73 // Reads a file in "src/content/test/data/webcrypto" to a base::Value.
74 // The file must be JSON, however it can also include C++ style comments.
75 ::testing::AssertionResult ReadJsonTestFile(const char* test_file_name,
76 scoped_ptr<base::Value>* value);
77 // Same as ReadJsonTestFile(), but returns the value as a List.
78 ::testing::AssertionResult ReadJsonTestFileToList(
79 const char* test_file_name,
80 scoped_ptr<base::ListValue>* list);
81 // Same as ReadJsonTestFile(), but returns the value as a Dictionary.
82 ::testing::AssertionResult ReadJsonTestFileToDictionary(
83 const char* test_file_name,
84 scoped_ptr<base::DictionaryValue>* dict);
85
86 // Reads a string property from the dictionary with path |property_name|
87 // (which can include periods for nested dictionaries). Interprets the
88 // string as a hex encoded string and converts it to a bytes list.
89 //
90 // Returns empty vector on failure.
91 std::vector<uint8_t> GetBytesFromHexString(const base::DictionaryValue* dict,
92 const std::string& property_name);
93
94 // Reads a string property with path "property_name" and converts it to a
95 // WebCryptoAlgorith. Returns null algorithm on failure.
96 blink::WebCryptoAlgorithm GetDigestAlgorithm(const base::DictionaryValue* dict,
97 const char* property_name);
98
99 // Returns true if any of the vectors in the input list have identical content.
100 bool CopiesExist(const std::vector<std::vector<uint8_t>>& bufs);
101
102 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm(
103 blink::WebCryptoAlgorithmId aes_alg_id,
104 unsigned short length);
105
106 // The following key pair is comprised of the SPKI (public key) and PKCS#8
107 // (private key) representations of the key pair provided in Example 1 of the
108 // NIST test vectors at
109 // ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15sign-vectors.txt
110 extern const unsigned int kModulusLengthBits;
111 extern const char* const kPublicKeySpkiDerHex;
112 extern const char* const kPrivateKeyPkcs8DerHex;
113
114 // The modulus and exponent (in hex) of kPublicKeySpkiDerHex
115 extern const char* const kPublicKeyModulusHex;
116 extern const char* const kPublicKeyExponentHex;
117
118 blink::WebCryptoKey ImportSecretKeyFromRaw(
119 const std::vector<uint8_t>& key_raw,
120 const blink::WebCryptoAlgorithm& algorithm,
121 blink::WebCryptoKeyUsageMask usage);
122
123 void ImportRsaKeyPair(const std::vector<uint8_t>& spki_der,
124 const std::vector<uint8_t>& pkcs8_der,
125 const blink::WebCryptoAlgorithm& algorithm,
126 bool extractable,
127 blink::WebCryptoKeyUsageMask public_key_usages,
128 blink::WebCryptoKeyUsageMask private_key_usages,
129 blink::WebCryptoKey* public_key,
130 blink::WebCryptoKey* private_key);
131
132 Status ImportKeyJwkFromDict(const base::DictionaryValue& dict,
133 const blink::WebCryptoAlgorithm& algorithm,
134 bool extractable,
135 blink::WebCryptoKeyUsageMask usages,
136 blink::WebCryptoKey* key);
137
138 // Parses a vector of JSON into a dictionary.
139 scoped_ptr<base::DictionaryValue> GetJwkDictionary(
140 const std::vector<uint8_t>& json);
141
142 // Verifies the input dictionary contains the expected values. Exact matches are
143 // required on the fields examined.
144 ::testing::AssertionResult VerifyJwk(
145 const scoped_ptr<base::DictionaryValue>& dict,
146 const std::string& kty_expected,
147 const std::string& alg_expected,
148 blink::WebCryptoKeyUsageMask use_mask_expected);
149
150 ::testing::AssertionResult VerifySecretJwk(
151 const std::vector<uint8_t>& json,
152 const std::string& alg_expected,
153 const std::string& k_expected_hex,
154 blink::WebCryptoKeyUsageMask use_mask_expected);
155
156 // Verifies that the JSON in the input vector contains the provided
157 // expected values. Exact matches are required on the fields examined.
158 ::testing::AssertionResult VerifyPublicJwk(
159 const std::vector<uint8_t>& json,
160 const std::string& alg_expected,
161 const std::string& n_expected_hex,
162 const std::string& e_expected_hex,
163 blink::WebCryptoKeyUsageMask use_mask_expected);
164
165 // Helper that tests importing ane exporting of symmetric keys as JWK.
166 void ImportExportJwkSymmetricKey(
167 int key_len_bits,
168 const blink::WebCryptoAlgorithm& import_algorithm,
169 blink::WebCryptoKeyUsageMask usages,
170 const std::string& jwk_alg);
171
172 // Wrappers around GenerateKey() which expect the result to be either a secret
173 // key or a public/private keypair. If the result does not match the
174 // expectation, then it fails with Status::ErrorUnexpected().
175 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm,
176 bool extractable,
177 blink::WebCryptoKeyUsageMask usages,
178 blink::WebCryptoKey* key);
179 Status GenerateKeyPair(const blink::WebCryptoAlgorithm& algorithm,
180 bool extractable,
181 blink::WebCryptoKeyUsageMask usages,
182 blink::WebCryptoKey* public_key,
183 blink::WebCryptoKey* private_key);
184
185 // Reads a key format string as used in some JSON test files and converts it to
186 // a WebCryptoKeyFormat.
187 blink::WebCryptoKeyFormat GetKeyFormatFromJsonTestCase(
188 const base::DictionaryValue* test);
189
190 // Extracts the key data bytes from |test| as used insome JSON test files.
191 std::vector<uint8_t> GetKeyDataFromJsonTestCase(
192 const base::DictionaryValue* test,
193 blink::WebCryptoKeyFormat key_format);
194
195 // Reads the "crv" string from a JSON test case and returns it as a
196 // WebCryptoNamedCurve.
197 blink::WebCryptoNamedCurve GetCurveNameFromDictionary(
198 const base::DictionaryValue* dict);
199
200 } // namespace webcrypto
201
202 #endif // COMPONENTS_WEBCRYPTO_TEST_TEST_HELPERS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698