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