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