Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(380)

Unified Diff: net/cert/merkle_tree_leaf_unittest.cc

Issue 1945183005: Adds function for generating MerkleTreeLeaf hash. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ct_merkle_tree_leaf_encode
Patch Set: Rebase Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/cert/merkle_tree_leaf.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/cert/merkle_tree_leaf_unittest.cc
diff --git a/net/cert/merkle_tree_leaf_unittest.cc b/net/cert/merkle_tree_leaf_unittest.cc
index ab59237a199a3bed983b1d76483605a92da9de91..c685d134014c9d471d3377b43229299934fa6bbf 100644
--- a/net/cert/merkle_tree_leaf_unittest.cc
+++ b/net/cert/merkle_tree_leaf_unittest.cc
@@ -4,12 +4,16 @@
#include "net/cert/merkle_tree_leaf.h"
+#include <string.h>
+
#include <string>
+#include "base/strings/string_number_conversions.h"
#include "net/base/test_data_directory.h"
#include "net/cert/x509_certificate.h"
#include "net/test/cert_test_util.h"
#include "net/test/ct_test_util.h"
+#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
@@ -18,6 +22,28 @@ namespace ct {
namespace {
+MATCHER_P(HexEq, hexStr, "") {
+ std::vector<uint8_t> bytes;
+
+ if (!base::HexStringToBytes(hexStr, &bytes)) {
+ *result_listener << "expected value was not a valid hex string";
+ return false;
+ }
+
+ if (bytes.size() != arg.size()) {
+ *result_listener << "expected and actual are different lengths";
+ return false;
+ }
+
+ // Make sure we don't pass nullptrs to memcmp
+ if (arg.empty())
+ return true;
+
+ // Print hex string (easier to read than default GTest representation)
+ *result_listener << "a.k.a. 0x" << base::HexEncode(arg.data(), arg.size());
+ return memcmp(arg.data(), bytes.data(), bytes.size()) == 0;
+}
+
class MerkleTreeLeafTest : public ::testing::Test {
public:
void SetUp() override {
@@ -77,6 +103,32 @@ TEST_F(MerkleTreeLeafTest, DoesNotCreateForEmbeddedSCTButNotPrecert) {
ASSERT_FALSE(GetMerkleTreeLeaf(test_cert_.get(), precert_sct_.get(), &leaf));
}
+// Expected hashes calculated by:
+// 1. Writing the serialized tree leaves from
+// CtSerialization::EncodesLogEntryFor{X509Cert,Precert} to files.
+// 2. Prepending a zero byte to both files.
+// 3. Passing each file through the sha256sum tool.
+
+TEST_F(MerkleTreeLeafTest, HashForX509Cert) {
+ MerkleTreeLeaf leaf;
+ ct::GetX509CertTreeLeaf(&leaf);
+
+ std::string hash;
+ ASSERT_TRUE(Hash(leaf, &hash));
+ EXPECT_THAT(hash, HexEq("452da788b3b8d15872ff0bb0777354b2a7f1c1887b5633201e76"
+ "2ba5a4b143fc"));
+}
+
+TEST_F(MerkleTreeLeafTest, HashForPrecert) {
+ MerkleTreeLeaf leaf;
+ ct::GetPrecertTreeLeaf(&leaf);
+
+ std::string hash;
+ ASSERT_TRUE(Hash(leaf, &hash));
+ EXPECT_THAT(hash, HexEq("257ae85f08810445511e35e33f7aee99ee19407971e35e95822b"
+ "bf42a74be223"));
+}
+
} // namespace
} // namespace ct
« no previous file with comments | « net/cert/merkle_tree_leaf.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698