Revert "Add sbox tests related to warming up of locales."
[chromium-blink-merge.git] / components / webcrypto / test / test_helpers.h
blobbf68a2fbbb495ed10d413fa41569d630575df455
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_TEST_TEST_HELPERS_H_
6 #define COMPONENTS_WEBCRYPTO_TEST_TEST_HELPERS_H_
8 #include <ostream>
9 #include <string>
10 #include <vector>
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)
23 namespace base {
24 class DictionaryValue;
25 class ListValue;
26 class Value;
29 namespace blink {
30 class WebCryptoAlgorithm;
33 namespace webcrypto {
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();
44 class Status;
45 class CryptoData;
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 // TODO(eroman): For Linux builds using system NSS, AES-GCM and RSA-OAEP, and
62 // RSA key import are a runtime dependency.
63 bool SupportsAesGcm();
64 bool SupportsRsaOaep();
65 bool SupportsRsaPrivateKeyImport();
67 blink::WebCryptoAlgorithm CreateRsaHashedKeyGenAlgorithm(
68 blink::WebCryptoAlgorithmId algorithm_id,
69 const blink::WebCryptoAlgorithmId hash_id,
70 unsigned int modulus_length,
71 const std::vector<uint8_t>& public_exponent);
73 // Returns a slightly modified version of the input vector.
75 // - For non-empty inputs a single bit is inverted.
76 // - For empty inputs, a byte is added.
77 std::vector<uint8_t> Corrupted(const std::vector<uint8_t>& input);
79 std::vector<uint8_t> HexStringToBytes(const std::string& hex);
81 std::vector<uint8_t> MakeJsonVector(const std::string& json_string);
82 std::vector<uint8_t> MakeJsonVector(const base::DictionaryValue& dict);
84 // ----------------------------------------------------------------
85 // Helpers for working with JSON data files for test expectations.
86 // ----------------------------------------------------------------
88 // Reads a file in "src/content/test/data/webcrypto" to a base::Value.
89 // The file must be JSON, however it can also include C++ style comments.
90 ::testing::AssertionResult ReadJsonTestFile(const char* test_file_name,
91 scoped_ptr<base::Value>* value);
92 // Same as ReadJsonTestFile(), but returns the value as a List.
93 ::testing::AssertionResult ReadJsonTestFileToList(
94 const char* test_file_name,
95 scoped_ptr<base::ListValue>* list);
96 // Same as ReadJsonTestFile(), but returns the value as a Dictionary.
97 ::testing::AssertionResult ReadJsonTestFileToDictionary(
98 const char* test_file_name,
99 scoped_ptr<base::DictionaryValue>* dict);
101 // Reads a string property from the dictionary with path |property_name|
102 // (which can include periods for nested dictionaries). Interprets the
103 // string as a hex encoded string and converts it to a bytes list.
105 // Returns empty vector on failure.
106 std::vector<uint8_t> GetBytesFromHexString(const base::DictionaryValue* dict,
107 const std::string& property_name);
109 // Reads a string property with path "property_name" and converts it to a
110 // WebCryptoAlgorith. Returns null algorithm on failure.
111 blink::WebCryptoAlgorithm GetDigestAlgorithm(const base::DictionaryValue* dict,
112 const char* property_name);
114 // Returns true if any of the vectors in the input list have identical content.
115 bool CopiesExist(const std::vector<std::vector<uint8_t>>& bufs);
117 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm(
118 blink::WebCryptoAlgorithmId aes_alg_id,
119 unsigned short length);
121 // The following key pair is comprised of the SPKI (public key) and PKCS#8
122 // (private key) representations of the key pair provided in Example 1 of the
123 // NIST test vectors at
124 // ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15sign-vectors.txt
125 extern const unsigned int kModulusLengthBits;
126 extern const char* const kPublicKeySpkiDerHex;
127 extern const char* const kPrivateKeyPkcs8DerHex;
129 // The modulus and exponent (in hex) of kPublicKeySpkiDerHex
130 extern const char* const kPublicKeyModulusHex;
131 extern const char* const kPublicKeyExponentHex;
133 blink::WebCryptoKey ImportSecretKeyFromRaw(
134 const std::vector<uint8_t>& key_raw,
135 const blink::WebCryptoAlgorithm& algorithm,
136 blink::WebCryptoKeyUsageMask usage);
138 void ImportRsaKeyPair(const std::vector<uint8_t>& spki_der,
139 const std::vector<uint8_t>& pkcs8_der,
140 const blink::WebCryptoAlgorithm& algorithm,
141 bool extractable,
142 blink::WebCryptoKeyUsageMask public_key_usages,
143 blink::WebCryptoKeyUsageMask private_key_usages,
144 blink::WebCryptoKey* public_key,
145 blink::WebCryptoKey* private_key);
147 Status ImportKeyJwkFromDict(const base::DictionaryValue& dict,
148 const blink::WebCryptoAlgorithm& algorithm,
149 bool extractable,
150 blink::WebCryptoKeyUsageMask usages,
151 blink::WebCryptoKey* key);
153 // Parses a vector of JSON into a dictionary.
154 scoped_ptr<base::DictionaryValue> GetJwkDictionary(
155 const std::vector<uint8_t>& json);
157 // Verifies the input dictionary contains the expected values. Exact matches are
158 // required on the fields examined.
159 ::testing::AssertionResult VerifyJwk(
160 const scoped_ptr<base::DictionaryValue>& dict,
161 const std::string& kty_expected,
162 const std::string& alg_expected,
163 blink::WebCryptoKeyUsageMask use_mask_expected);
165 ::testing::AssertionResult VerifySecretJwk(
166 const std::vector<uint8_t>& json,
167 const std::string& alg_expected,
168 const std::string& k_expected_hex,
169 blink::WebCryptoKeyUsageMask use_mask_expected);
171 // Verifies that the JSON in the input vector contains the provided
172 // expected values. Exact matches are required on the fields examined.
173 ::testing::AssertionResult VerifyPublicJwk(
174 const std::vector<uint8_t>& json,
175 const std::string& alg_expected,
176 const std::string& n_expected_hex,
177 const std::string& e_expected_hex,
178 blink::WebCryptoKeyUsageMask use_mask_expected);
180 // Helper that tests importing ane exporting of symmetric keys as JWK.
181 void ImportExportJwkSymmetricKey(
182 int key_len_bits,
183 const blink::WebCryptoAlgorithm& import_algorithm,
184 blink::WebCryptoKeyUsageMask usages,
185 const std::string& jwk_alg);
187 // Wrappers around GenerateKey() which expect the result to be either a secret
188 // key or a public/private keypair. If the result does not match the
189 // expectation, then it fails with Status::ErrorUnexpected().
190 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm,
191 bool extractable,
192 blink::WebCryptoKeyUsageMask usages,
193 blink::WebCryptoKey* key);
194 Status GenerateKeyPair(const blink::WebCryptoAlgorithm& algorithm,
195 bool extractable,
196 blink::WebCryptoKeyUsageMask usages,
197 blink::WebCryptoKey* public_key,
198 blink::WebCryptoKey* private_key);
200 // Reads a key format string as used in some JSON test files and converts it to
201 // a WebCryptoKeyFormat.
202 blink::WebCryptoKeyFormat GetKeyFormatFromJsonTestCase(
203 const base::DictionaryValue* test);
205 // Extracts the key data bytes from |test| as used insome JSON test files.
206 std::vector<uint8_t> GetKeyDataFromJsonTestCase(
207 const base::DictionaryValue* test,
208 blink::WebCryptoKeyFormat key_format);
210 // Reads the "crv" string from a JSON test case and returns it as a
211 // WebCryptoNamedCurve.
212 blink::WebCryptoNamedCurve GetCurveNameFromDictionary(
213 const base::DictionaryValue* dict);
215 } // namespace webcrypto
217 #endif // COMPONENTS_WEBCRYPTO_TEST_TEST_HELPERS_H_