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 // 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(); |
| 56 |
| 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); |
| 62 |
| 63 // Returns a slightly modified version of the input vector. |
| 64 // |
| 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); |
| 68 |
| 69 std::vector<uint8_t> HexStringToBytes(const std::string& hex); |
| 70 |
| 71 std::vector<uint8_t> MakeJsonVector(const std::string& json_string); |
| 72 std::vector<uint8_t> MakeJsonVector(const base::DictionaryValue& dict); |
| 73 |
| 74 // ---------------------------------------------------------------- |
| 75 // Helpers for working with JSON data files for test expectations. |
| 76 // ---------------------------------------------------------------- |
| 77 |
| 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); |
| 86 |
| 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. |
| 90 // |
| 91 // Returns empty vector on failure. |
| 92 std::vector<uint8_t> GetBytesFromHexString(base::DictionaryValue* dict, |
| 93 const char* property_name); |
| 94 |
| 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); |
| 99 |
| 100 // Returns true if any of the vectors in the input list have identical content. |
| 101 // Dumb O(n^2) implementation but should be fast enough for the input sizes that |
| 102 // are used. |
| 103 bool CopiesExist(const std::vector<std::vector<uint8_t> >& bufs); |
| 104 |
| 105 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm( |
| 106 blink::WebCryptoAlgorithmId aes_alg_id, |
| 107 unsigned short length); |
| 108 |
| 109 // The following key pair is comprised of the SPKI (public key) and PKCS#8 |
| 110 // (private key) representations of the key pair provided in Example 1 of the |
| 111 // NIST test vectors at |
| 112 // ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15sign-vectors.txt |
| 113 extern const unsigned int kModulusLengthBits; |
| 114 extern const char* const kPublicKeySpkiDerHex; |
| 115 extern const char* const kPrivateKeyPkcs8DerHex; |
| 116 |
| 117 // The modulus and exponent (in hex) of kPublicKeySpkiDerHex |
| 118 extern const char* const kPublicKeyModulusHex; |
| 119 extern const char* const kPublicKeyExponentHex; |
| 120 |
| 121 blink::WebCryptoKey ImportSecretKeyFromRaw( |
| 122 const std::vector<uint8_t>& key_raw, |
| 123 const blink::WebCryptoAlgorithm& algorithm, |
| 124 blink::WebCryptoKeyUsageMask usage); |
| 125 |
| 126 void ImportRsaKeyPair(const std::vector<uint8_t>& spki_der, |
| 127 const std::vector<uint8_t>& pkcs8_der, |
| 128 const blink::WebCryptoAlgorithm& algorithm, |
| 129 bool extractable, |
| 130 blink::WebCryptoKeyUsageMask public_key_usage_mask, |
| 131 blink::WebCryptoKeyUsageMask private_key_usage_mask, |
| 132 blink::WebCryptoKey* public_key, |
| 133 blink::WebCryptoKey* private_key); |
| 134 |
| 135 Status ImportKeyJwkFromDict(const base::DictionaryValue& dict, |
| 136 const blink::WebCryptoAlgorithm& algorithm, |
| 137 bool extractable, |
| 138 blink::WebCryptoKeyUsageMask usage_mask, |
| 139 blink::WebCryptoKey* key); |
| 140 |
| 141 // Parses a vector of JSON into a dictionary. |
| 142 scoped_ptr<base::DictionaryValue> GetJwkDictionary( |
| 143 const std::vector<uint8_t>& json); |
| 144 |
| 145 // Verifies the input dictionary contains the expected values. Exact matches are |
| 146 // required on the fields examined. |
| 147 ::testing::AssertionResult VerifyJwk( |
| 148 const scoped_ptr<base::DictionaryValue>& dict, |
| 149 const std::string& kty_expected, |
| 150 const std::string& alg_expected, |
| 151 blink::WebCryptoKeyUsageMask use_mask_expected); |
| 152 |
| 153 ::testing::AssertionResult VerifySecretJwk( |
| 154 const std::vector<uint8_t>& json, |
| 155 const std::string& alg_expected, |
| 156 const std::string& k_expected_hex, |
| 157 blink::WebCryptoKeyUsageMask use_mask_expected); |
| 158 |
| 159 // Verifies that the JSON in the input vector contains the provided |
| 160 // expected values. Exact matches are required on the fields examined. |
| 161 ::testing::AssertionResult VerifyPublicJwk( |
| 162 const std::vector<uint8_t>& json, |
| 163 const std::string& alg_expected, |
| 164 const std::string& n_expected_hex, |
| 165 const std::string& e_expected_hex, |
| 166 blink::WebCryptoKeyUsageMask use_mask_expected); |
| 167 |
| 168 // Helper that tests importing ane exporting of symmetric keys as JWK. |
| 169 void ImportExportJwkSymmetricKey( |
| 170 int key_len_bits, |
| 171 const blink::WebCryptoAlgorithm& import_algorithm, |
| 172 blink::WebCryptoKeyUsageMask usages, |
| 173 const std::string& jwk_alg); |
| 174 |
| 175 } // namespace webcrypto |
| 176 |
| 177 } // namesapce content |
| 178 |
| 179 #endif // CONTENT_CHILD_WEBCRYPTO_TEST_TEST_HELPERS_H_ |
OLD | NEW |