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

Side by Side Diff: net/cert/ct_log_response_parser_unittest.cc

Issue 230713002: Certificate Transparency: Parse Signed Tree Heads and validate them (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Explicitly exporting symbol Created 6 years, 8 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 unified diff | Download patch
OLDNEW
(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 #include "net/cert/ct_log_response_parser.h"
6
7 #include <string>
8
9 #include "base/base64.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/time/time.h"
12 #include "net/cert/ct_serialization.h"
13 #include "net/cert/signed_tree_head.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace net {
17
18 namespace ct {
19
20 // Basic fields, sha256_root_hash, tree_head_signature.
21 const char kSTHFormatString[] = "{%s\"%s\",\"tree_head_signature\":\"%s\"}";
22 const char kBasicSTHFields[] =
23 "\"tree_size\":2903698,\"timestamp\":1395761621447,\"sha256_root_hash\":";
Ryan Sleevi 2014/04/25 23:33:19 Rather than wrap format strings in constants, crea
Eran Messeri 2014/04/29 15:22:24 Done - it does read much better, the way I origina
24
25 const char kSHA256RootHash[] = "/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoAo=";
26
27 const char kTreeHeadSignature[] =
28 "BAMARzBFAiAB+IIYrkRsZDW0/6TzPgR+aJ26twCQ1JDTwq/"
29 "mpinCjAIhAKDXdXMtqbvQ42r9dBIwV5RM/KpEzNQdIhXHesd9HPv3";
30
31 class CTLogResponseParserTest : public ::testing::Test {
32 public:
33 CTLogResponseParserTest() {}
34
35 virtual void SetUp() OVERRIDE {
36 sample_sth_ = base::StringPrintf(
37 kSTHFormatString, kBasicSTHFields, kSHA256RootHash, kTreeHeadSignature);
38 base::Base64Decode(kSHA256RootHash, &expected_sha256_root_hash_);
39
40 std::string tree_head_signature;
41 base::Base64Decode(kTreeHeadSignature, &tree_head_signature);
42 base::StringPiece sp(tree_head_signature);
43 ASSERT_TRUE(DecodeDigitallySigned(&sp, &expected_signature_));
Ryan Sleevi 2014/04/25 23:33:19 Why do you do these in a SetUp, instead of just in
Eran Messeri 2014/04/29 15:22:24 Done - moved to the test, this is indeed was not n
44 }
45
46 protected:
47 std::string sample_sth_;
48 std::string expected_sha256_root_hash_;
49 DigitallySigned expected_signature_;
50 };
51
52 TEST_F(CTLogResponseParserTest, ParsesValidJsonSTH) {
53 SignedTreeHead tree_head;
54 EXPECT_TRUE(FillSignedTreeHead(sample_sth_, &tree_head));
55
56 base::Time expected_timestamp =
57 base::Time::UnixEpoch() +
58 base::TimeDelta::FromMilliseconds(1395761621447);
59
60 ASSERT_EQ(SignedTreeHead::V1, tree_head.version);
61 ASSERT_EQ(expected_timestamp, tree_head.timestamp);
62 ASSERT_EQ(2903698u, tree_head.tree_size);
63
64 // Copy the field from the SignedTreeHead because it's not null terminated
65 // there and ASSERT_STREQ expects null-terminated strings.
66 char actual_hash[kSthRootHashLength + 1];
67 memcpy(actual_hash, tree_head.sha256_root_hash, kSthRootHashLength);
68 actual_hash[kSthRootHashLength] = '\0';
69 ASSERT_STREQ(expected_sha256_root_hash_.c_str(), actual_hash);
70 ASSERT_EQ(tree_head.signature.hash_algorithm,
71 expected_signature_.hash_algorithm);
72 ASSERT_EQ(tree_head.signature.signature_algorithm,
73 expected_signature_.signature_algorithm);
74 ASSERT_EQ(tree_head.signature.signature_data,
75 expected_signature_.signature_data);
76 }
77
78 TEST_F(CTLogResponseParserTest, FailsToParseMissingFields) {
79 std::string missing_fields_sth =
80 base::StringPrintf("{%s\"%s\"}", kBasicSTHFields, kSHA256RootHash);
81
82 SignedTreeHead tree_head;
83 ASSERT_FALSE(FillSignedTreeHead(missing_fields_sth, &tree_head));
84 }
85
86 TEST_F(CTLogResponseParserTest, FailsToParseIncorrectLengthRootHash) {
87 SignedTreeHead tree_head;
88
89 std::string too_long_hash =
90 base::StringPrintf(kSTHFormatString,
91 kBasicSTHFields,
92 kSHA256RootHash,
93 "/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoArK\n");
94 ASSERT_FALSE(FillSignedTreeHead(too_long_hash, &tree_head));
95
96 std::string too_short_hash =
97 base::StringPrintf(kSTHFormatString,
98 kBasicSTHFields,
99 kSHA256RootHash,
100 "/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoA==\n");
101 ASSERT_FALSE(FillSignedTreeHead(too_short_hash, &tree_head));
102 }
103
104 } // namespace ct
105
106 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698