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.
5 #ifndef COMPONENTS_WEBCRYPTO_ALGORITHMS_TEST_HELPERS_H_
6 #define COMPONENTS_WEBCRYPTO_ALGORITHMS_TEST_HELPERS_H_
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"
17 #define EXPECT_BYTES_EQ(expected, actual) \
18 EXPECT_EQ(CryptoData(expected), CryptoData(actual))
20 #define EXPECT_BYTES_EQ_HEX(expected_hex, actual_bytes) \
21 EXPECT_BYTES_EQ(HexStringToBytes(expected_hex), actual_bytes)
24 class DictionaryValue
;
30 class WebCryptoAlgorithm
;
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
39 class WebCryptoTestBase
: public testing::Test
{
41 static void SetUpTestCase();
47 // These functions are used by GTEST to support EXPECT_EQ() for
48 // webcrypto::Status and webcrypto::CryptoData
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
);
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
);
58 // Gives a human-readable description of |status| and any error it represents.
59 std::string
StatusToString(const Status
& status
);
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
);
67 // Returns a slightly modified version of the input vector.
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
);
73 std::vector
<uint8_t> HexStringToBytes(const std::string
& hex
);
75 std::vector
<uint8_t> MakeJsonVector(const std::string
& json_string
);
76 std::vector
<uint8_t> MakeJsonVector(const base::DictionaryValue
& dict
);
78 // ----------------------------------------------------------------
79 // Helpers for working with JSON data files for test expectations.
80 // ----------------------------------------------------------------
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
);
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.
99 // Returns empty vector on failure.
100 std::vector
<uint8_t> GetBytesFromHexString(const base::DictionaryValue
* dict
,
101 const std::string
& property_name
);
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
);
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
);
111 blink::WebCryptoAlgorithm
CreateAesKeyGenAlgorithm(
112 blink::WebCryptoAlgorithmId aes_alg_id
,
113 unsigned short length
);
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
;
123 // The modulus and exponent (in hex) of kPublicKeySpkiDerHex
124 extern const char* const kPublicKeyModulusHex
;
125 extern const char* const kPublicKeyExponentHex
;
127 blink::WebCryptoKey
ImportSecretKeyFromRaw(
128 const std::vector
<uint8_t>& key_raw
,
129 const blink::WebCryptoAlgorithm
& algorithm
,
130 blink::WebCryptoKeyUsageMask usage
);
132 void ImportRsaKeyPair(const std::vector
<uint8_t>& spki_der
,
133 const std::vector
<uint8_t>& pkcs8_der
,
134 const blink::WebCryptoAlgorithm
& algorithm
,
136 blink::WebCryptoKeyUsageMask public_key_usages
,
137 blink::WebCryptoKeyUsageMask private_key_usages
,
138 blink::WebCryptoKey
* public_key
,
139 blink::WebCryptoKey
* private_key
);
141 Status
ImportKeyJwkFromDict(const base::DictionaryValue
& dict
,
142 const blink::WebCryptoAlgorithm
& algorithm
,
144 blink::WebCryptoKeyUsageMask usages
,
145 blink::WebCryptoKey
* key
);
147 // Parses a vector of JSON into a dictionary.
148 scoped_ptr
<base::DictionaryValue
> GetJwkDictionary(
149 const std::vector
<uint8_t>& json
);
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
);
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
);
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
);
174 // Helper that tests importing ane exporting of symmetric keys as JWK.
175 void ImportExportJwkSymmetricKey(
177 const blink::WebCryptoAlgorithm
& import_algorithm
,
178 blink::WebCryptoKeyUsageMask usages
,
179 const std::string
& jwk_alg
);
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
,
186 blink::WebCryptoKeyUsageMask usages
,
187 blink::WebCryptoKey
* key
);
188 Status
GenerateKeyPair(const blink::WebCryptoAlgorithm
& algorithm
,
190 blink::WebCryptoKeyUsageMask usages
,
191 blink::WebCryptoKey
* public_key
,
192 blink::WebCryptoKey
* private_key
);
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
);
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
);
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
);
209 // Creates an HMAC import algorithm whose inner hash algorithm is determined by
210 // the specified algorithm ID. It is an error to call this method with a hash
211 // algorithm that is not SHA*.
212 blink::WebCryptoAlgorithm
CreateHmacImportAlgorithm(
213 blink::WebCryptoAlgorithmId hash_id
,
214 unsigned int length_bits
);
216 // Same as above but without specifying a length.
217 blink::WebCryptoAlgorithm
CreateHmacImportAlgorithmNoLength(
218 blink::WebCryptoAlgorithmId hash_id
);
220 } // namespace webcrypto
222 #endif // COMPONENTS_WEBCRYPTO_ALGORITHMS_TEST_HELPERS_H_