Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/cert/merkle_tree_leaf.h" | 5 #include "net/cert/merkle_tree_leaf.h" |
| 6 | 6 |
| 7 #include <string.h> | |
| 8 | |
| 7 #include <string> | 9 #include <string> |
| 8 | 10 |
| 11 #include "base/strings/string_number_conversions.h" | |
| 9 #include "net/base/test_data_directory.h" | 12 #include "net/base/test_data_directory.h" |
| 10 #include "net/cert/x509_certificate.h" | 13 #include "net/cert/x509_certificate.h" |
| 11 #include "net/test/cert_test_util.h" | 14 #include "net/test/cert_test_util.h" |
| 12 #include "net/test/ct_test_util.h" | 15 #include "net/test/ct_test_util.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 18 |
| 15 namespace net { | 19 namespace net { |
| 16 | 20 |
| 17 namespace ct { | 21 namespace ct { |
| 18 | 22 |
| 19 namespace { | 23 namespace { |
| 20 | 24 |
| 25 MATCHER_P(HexEq, hexStr, "") { | |
| 26 std::vector<uint8_t> bytes; | |
| 27 | |
| 28 if (base::HexStringToBytes(hexStr, &bytes)) { | |
|
Eran Messeri
2016/05/05 16:03:26
Return early by flipping the condition of the if s
Rob Percival
2016/05/05 16:26:37
Done.
| |
| 29 if (bytes.size() != arg.size()) { | |
| 30 *result_listener << "expected and actual are different lengths"; | |
| 31 return false; | |
| 32 } | |
| 33 | |
| 34 // Print hex string (easier to read than default GTest representation) | |
| 35 *result_listener << "a.k.a. 0x" << base::HexEncode(arg.data(), arg.size()); | |
| 36 return memcmp(arg.data(), bytes.data(), bytes.size()) == 0; | |
| 37 } | |
| 38 | |
| 39 *result_listener << "expected value was not a valid hex string"; | |
| 40 return false; | |
| 41 } | |
| 42 | |
| 21 class MerkleTreeLeafTest : public ::testing::Test { | 43 class MerkleTreeLeafTest : public ::testing::Test { |
| 22 public: | 44 public: |
| 23 void SetUp() override { | 45 void SetUp() override { |
| 24 std::string der_test_cert(ct::GetDerEncodedX509Cert()); | 46 std::string der_test_cert(ct::GetDerEncodedX509Cert()); |
| 25 test_cert_ = X509Certificate::CreateFromBytes(der_test_cert.data(), | 47 test_cert_ = X509Certificate::CreateFromBytes(der_test_cert.data(), |
| 26 der_test_cert.length()); | 48 der_test_cert.length()); |
| 27 ASSERT_TRUE(test_cert_); | 49 ASSERT_TRUE(test_cert_); |
| 28 | 50 |
| 29 GetX509CertSCT(&x509_sct_); | 51 GetX509CertSCT(&x509_sct_); |
| 30 x509_sct_->origin = SignedCertificateTimestamp::SCT_FROM_OCSP_RESPONSE; | 52 x509_sct_->origin = SignedCertificateTimestamp::SCT_FROM_OCSP_RESPONSE; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 | 92 |
| 71 EXPECT_EQ(precert_sct_->timestamp, leaf.timestamp); | 93 EXPECT_EQ(precert_sct_->timestamp, leaf.timestamp); |
| 72 EXPECT_EQ(precert_sct_->extensions, leaf.extensions); | 94 EXPECT_EQ(precert_sct_->extensions, leaf.extensions); |
| 73 } | 95 } |
| 74 | 96 |
| 75 TEST_F(MerkleTreeLeafTest, DoesNotCreateForEmbeddedSCTButNotPrecert) { | 97 TEST_F(MerkleTreeLeafTest, DoesNotCreateForEmbeddedSCTButNotPrecert) { |
| 76 MerkleTreeLeaf leaf; | 98 MerkleTreeLeaf leaf; |
| 77 ASSERT_FALSE(GetMerkleTreeLeaf(test_cert_.get(), precert_sct_.get(), &leaf)); | 99 ASSERT_FALSE(GetMerkleTreeLeaf(test_cert_.get(), precert_sct_.get(), &leaf)); |
| 78 } | 100 } |
| 79 | 101 |
| 102 TEST_F(MerkleTreeLeafTest, HashForX509Cert) { | |
| 103 MerkleTreeLeaf leaf; | |
| 104 ct::GetX509CertTreeLeaf(&leaf); | |
| 105 | |
| 106 std::string hash; | |
| 107 ASSERT_TRUE(leaf.Hash(&hash)); | |
| 108 EXPECT_THAT(hash, HexEq("d306c83ef45ac97bba7a0d99331f23433e5fa97cf3a08ed90b24" | |
| 109 "2f7aec99f2ad")); | |
| 110 } | |
| 111 | |
| 112 TEST_F(MerkleTreeLeafTest, HashForPrecert) { | |
| 113 MerkleTreeLeaf leaf; | |
| 114 ct::GetPrecertTreeLeaf(&leaf); | |
| 115 | |
| 116 std::string hash; | |
| 117 ASSERT_TRUE(leaf.Hash(&hash)); | |
| 118 EXPECT_THAT(hash, HexEq("d6f71fef189695a815b98d02d5b66f624323303172f75ff62f62" | |
| 119 "44c01cde7cd3")); | |
| 120 } | |
| 121 | |
| 80 } // namespace | 122 } // namespace |
| 81 | 123 |
| 82 } // namespace ct | 124 } // namespace ct |
| 83 | 125 |
| 84 } // namespace net | 126 } // namespace net |
| OLD | NEW |