| 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)) { |
| 29 *result_listener << "expected value was not a valid hex string"; |
| 30 return false; |
| 31 } |
| 32 |
| 33 if (bytes.size() != arg.size()) { |
| 34 *result_listener << "expected and actual are different lengths"; |
| 35 return false; |
| 36 } |
| 37 |
| 38 // Make sure we don't pass nullptrs to memcmp |
| 39 if (arg.empty()) |
| 40 return true; |
| 41 |
| 42 // Print hex string (easier to read than default GTest representation) |
| 43 *result_listener << "a.k.a. 0x" << base::HexEncode(arg.data(), arg.size()); |
| 44 return memcmp(arg.data(), bytes.data(), bytes.size()) == 0; |
| 45 } |
| 46 |
| 21 class MerkleTreeLeafTest : public ::testing::Test { | 47 class MerkleTreeLeafTest : public ::testing::Test { |
| 22 public: | 48 public: |
| 23 void SetUp() override { | 49 void SetUp() override { |
| 24 std::string der_test_cert(ct::GetDerEncodedX509Cert()); | 50 std::string der_test_cert(ct::GetDerEncodedX509Cert()); |
| 25 test_cert_ = X509Certificate::CreateFromBytes(der_test_cert.data(), | 51 test_cert_ = X509Certificate::CreateFromBytes(der_test_cert.data(), |
| 26 der_test_cert.length()); | 52 der_test_cert.length()); |
| 27 ASSERT_TRUE(test_cert_); | 53 ASSERT_TRUE(test_cert_); |
| 28 | 54 |
| 29 GetX509CertSCT(&x509_sct_); | 55 GetX509CertSCT(&x509_sct_); |
| 30 x509_sct_->origin = SignedCertificateTimestamp::SCT_FROM_OCSP_RESPONSE; | 56 x509_sct_->origin = SignedCertificateTimestamp::SCT_FROM_OCSP_RESPONSE; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 96 |
| 71 EXPECT_EQ(precert_sct_->timestamp, leaf.timestamp); | 97 EXPECT_EQ(precert_sct_->timestamp, leaf.timestamp); |
| 72 EXPECT_EQ(precert_sct_->extensions, leaf.extensions); | 98 EXPECT_EQ(precert_sct_->extensions, leaf.extensions); |
| 73 } | 99 } |
| 74 | 100 |
| 75 TEST_F(MerkleTreeLeafTest, DoesNotCreateForEmbeddedSCTButNotPrecert) { | 101 TEST_F(MerkleTreeLeafTest, DoesNotCreateForEmbeddedSCTButNotPrecert) { |
| 76 MerkleTreeLeaf leaf; | 102 MerkleTreeLeaf leaf; |
| 77 ASSERT_FALSE(GetMerkleTreeLeaf(test_cert_.get(), precert_sct_.get(), &leaf)); | 103 ASSERT_FALSE(GetMerkleTreeLeaf(test_cert_.get(), precert_sct_.get(), &leaf)); |
| 78 } | 104 } |
| 79 | 105 |
| 106 // Expected hashes calculated by: |
| 107 // 1. Writing the serialized tree leaves from |
| 108 // CtSerialization::EncodesLogEntryFor{X509Cert,Precert} to files. |
| 109 // 2. Prepending a zero byte to both files. |
| 110 // 3. Passing each file through the sha256sum tool. |
| 111 |
| 112 TEST_F(MerkleTreeLeafTest, HashForX509Cert) { |
| 113 MerkleTreeLeaf leaf; |
| 114 ct::GetX509CertTreeLeaf(&leaf); |
| 115 |
| 116 std::string hash; |
| 117 ASSERT_TRUE(Hash(leaf, &hash)); |
| 118 EXPECT_THAT(hash, HexEq("452da788b3b8d15872ff0bb0777354b2a7f1c1887b5633201e76" |
| 119 "2ba5a4b143fc")); |
| 120 } |
| 121 |
| 122 TEST_F(MerkleTreeLeafTest, HashForPrecert) { |
| 123 MerkleTreeLeaf leaf; |
| 124 ct::GetPrecertTreeLeaf(&leaf); |
| 125 |
| 126 std::string hash; |
| 127 ASSERT_TRUE(Hash(leaf, &hash)); |
| 128 EXPECT_THAT(hash, HexEq("257ae85f08810445511e35e33f7aee99ee19407971e35e95822b" |
| 129 "bf42a74be223")); |
| 130 } |
| 131 |
| 80 } // namespace | 132 } // namespace |
| 81 | 133 |
| 82 } // namespace ct | 134 } // namespace ct |
| 83 | 135 |
| 84 } // namespace net | 136 } // namespace net |
| OLD | NEW |