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 CONTENT_CHILD_WEBCRYPTO_TEST_TEST_HELPERS_H_
6 #define CONTENT_CHILD_WEBCRYPTO_TEST_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
;
40 // These functions are used by GTEST to support EXPECT_EQ() for
41 // webcrypto::Status and webcrypto::CryptoData
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
);
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
);
51 // TODO(eroman): For Linux builds using system NSS, AES-GCM and RSA-OAEP, and
52 // RSA key import are a runtime dependency.
53 bool SupportsAesGcm();
54 bool SupportsRsaOaep();
55 bool SupportsRsaPrivateKeyImport();
57 blink::WebCryptoAlgorithm
CreateRsaHashedKeyGenAlgorithm(
58 blink::WebCryptoAlgorithmId algorithm_id
,
59 const blink::WebCryptoAlgorithmId hash_id
,
60 unsigned int modulus_length
,
61 const std::vector
<uint8_t>& public_exponent
);
63 // Returns a slightly modified version of the input vector.
65 // - For non-empty inputs a single bit is inverted.
66 // - For empty inputs, a byte is added.
67 std::vector
<uint8_t> Corrupted(const std::vector
<uint8_t>& input
);
69 std::vector
<uint8_t> HexStringToBytes(const std::string
& hex
);
71 std::vector
<uint8_t> MakeJsonVector(const std::string
& json_string
);
72 std::vector
<uint8_t> MakeJsonVector(const base::DictionaryValue
& dict
);
74 // ----------------------------------------------------------------
75 // Helpers for working with JSON data files for test expectations.
76 // ----------------------------------------------------------------
78 // Reads a file in "src/content/test/data/webcrypto" to a base::Value.
79 // The file must be JSON, however it can also include C++ style comments.
80 ::testing::AssertionResult
ReadJsonTestFile(const char* test_file_name
,
81 scoped_ptr
<base::Value
>* value
);
82 // Same as ReadJsonTestFile(), but returns the value as a List.
83 ::testing::AssertionResult
ReadJsonTestFileToList(
84 const char* test_file_name
,
85 scoped_ptr
<base::ListValue
>* list
);
87 // Reads a string property from the dictionary with path |property_name|
88 // (which can include periods for nested dictionaries). Interprets the
89 // string as a hex encoded string and converts it to a bytes list.
91 // Returns empty vector on failure.
92 std::vector
<uint8_t> GetBytesFromHexString(base::DictionaryValue
* dict
,
93 const char* property_name
);
95 // Reads a string property with path "property_name" and converts it to a
96 // WebCryptoAlgorith. Returns null algorithm on failure.
97 blink::WebCryptoAlgorithm
GetDigestAlgorithm(base::DictionaryValue
* dict
,
98 const char* property_name
);
100 // Returns true if any of the vectors in the input list have identical content.
101 bool CopiesExist(const std::vector
<std::vector
<uint8_t> >& bufs
);
103 blink::WebCryptoAlgorithm
CreateAesKeyGenAlgorithm(
104 blink::WebCryptoAlgorithmId aes_alg_id
,
105 unsigned short length
);
107 // The following key pair is comprised of the SPKI (public key) and PKCS#8
108 // (private key) representations of the key pair provided in Example 1 of the
109 // NIST test vectors at
110 // ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15sign-vectors.txt
111 extern const unsigned int kModulusLengthBits
;
112 extern const char* const kPublicKeySpkiDerHex
;
113 extern const char* const kPrivateKeyPkcs8DerHex
;
115 // The modulus and exponent (in hex) of kPublicKeySpkiDerHex
116 extern const char* const kPublicKeyModulusHex
;
117 extern const char* const kPublicKeyExponentHex
;
119 blink::WebCryptoKey
ImportSecretKeyFromRaw(
120 const std::vector
<uint8_t>& key_raw
,
121 const blink::WebCryptoAlgorithm
& algorithm
,
122 blink::WebCryptoKeyUsageMask usage
);
124 void ImportRsaKeyPair(const std::vector
<uint8_t>& spki_der
,
125 const std::vector
<uint8_t>& pkcs8_der
,
126 const blink::WebCryptoAlgorithm
& algorithm
,
128 blink::WebCryptoKeyUsageMask public_key_usage_mask
,
129 blink::WebCryptoKeyUsageMask private_key_usage_mask
,
130 blink::WebCryptoKey
* public_key
,
131 blink::WebCryptoKey
* private_key
);
133 Status
ImportKeyJwkFromDict(const base::DictionaryValue
& dict
,
134 const blink::WebCryptoAlgorithm
& algorithm
,
136 blink::WebCryptoKeyUsageMask usage_mask
,
137 blink::WebCryptoKey
* key
);
139 // Parses a vector of JSON into a dictionary.
140 scoped_ptr
<base::DictionaryValue
> GetJwkDictionary(
141 const std::vector
<uint8_t>& json
);
143 // Verifies the input dictionary contains the expected values. Exact matches are
144 // required on the fields examined.
145 ::testing::AssertionResult
VerifyJwk(
146 const scoped_ptr
<base::DictionaryValue
>& dict
,
147 const std::string
& kty_expected
,
148 const std::string
& alg_expected
,
149 blink::WebCryptoKeyUsageMask use_mask_expected
);
151 ::testing::AssertionResult
VerifySecretJwk(
152 const std::vector
<uint8_t>& json
,
153 const std::string
& alg_expected
,
154 const std::string
& k_expected_hex
,
155 blink::WebCryptoKeyUsageMask use_mask_expected
);
157 // Verifies that the JSON in the input vector contains the provided
158 // expected values. Exact matches are required on the fields examined.
159 ::testing::AssertionResult
VerifyPublicJwk(
160 const std::vector
<uint8_t>& json
,
161 const std::string
& alg_expected
,
162 const std::string
& n_expected_hex
,
163 const std::string
& e_expected_hex
,
164 blink::WebCryptoKeyUsageMask use_mask_expected
);
166 // Helper that tests importing ane exporting of symmetric keys as JWK.
167 void ImportExportJwkSymmetricKey(
169 const blink::WebCryptoAlgorithm
& import_algorithm
,
170 blink::WebCryptoKeyUsageMask usages
,
171 const std::string
& jwk_alg
);
173 } // namespace webcrypto
175 } // namesapce content
177 #endif // CONTENT_CHILD_WEBCRYPTO_TEST_TEST_HELPERS_H_