1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/cert/ct_log_response_parser.h"
#include <string>
#include "base/base64.h"
#include "base/json/json_reader.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "base/values.h"
#include "net/cert/ct_serialization.h"
#include "net/cert/signed_tree_head.h"
#include "net/test/ct_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace ct {
namespace {
scoped_ptr<base::Value> ParseJson(const std::string& json) {
base::JSONReader json_reader;
return json_reader.Read(json);
}
}
TEST(CTLogResponseParserTest, ParsesValidJsonSTH) {
scoped_ptr<base::Value> sample_sth_json = ParseJson(GetSampleSTHAsJson());
SignedTreeHead tree_head;
EXPECT_TRUE(FillSignedTreeHead(*sample_sth_json.get(), &tree_head));
SignedTreeHead sample_sth;
ASSERT_TRUE(GetSampleSignedTreeHead(&sample_sth));
ASSERT_EQ(SignedTreeHead::V1, tree_head.version);
ASSERT_EQ(sample_sth.timestamp, tree_head.timestamp);
ASSERT_EQ(sample_sth.tree_size, tree_head.tree_size);
// Copy the field from the SignedTreeHead because it's not null terminated
// there and ASSERT_STREQ expects null-terminated strings.
char actual_hash[kSthRootHashLength + 1];
memcpy(actual_hash, tree_head.sha256_root_hash, kSthRootHashLength);
actual_hash[kSthRootHashLength] = '\0';
std::string expected_sha256_root_hash = GetSampleSTHSHA256RootHash();
ASSERT_STREQ(expected_sha256_root_hash.c_str(), actual_hash);
const DigitallySigned& expected_signature(sample_sth.signature);
ASSERT_EQ(tree_head.signature.hash_algorithm,
expected_signature.hash_algorithm);
ASSERT_EQ(tree_head.signature.signature_algorithm,
expected_signature.signature_algorithm);
ASSERT_EQ(tree_head.signature.signature_data,
expected_signature.signature_data);
}
TEST(CTLogResponseParserTest, FailsToParseMissingFields) {
scoped_ptr<base::Value> missing_signature_sth = ParseJson(
CreateSignedTreeHeadJsonString(1 /* tree_size */, 123456u /* timestamp */,
GetSampleSTHSHA256RootHash(), ""));
SignedTreeHead tree_head;
ASSERT_FALSE(FillSignedTreeHead(*missing_signature_sth.get(), &tree_head));
scoped_ptr<base::Value> missing_root_hash_sth = ParseJson(
CreateSignedTreeHeadJsonString(1 /* tree_size */, 123456u /* timestamp */,
"", GetSampleSTHTreeHeadSignature()));
ASSERT_FALSE(FillSignedTreeHead(*missing_root_hash_sth.get(), &tree_head));
}
TEST(CTLogResponseParserTest, FailsToParseIncorrectLengthRootHash) {
SignedTreeHead tree_head;
std::string too_long_hash;
base::Base64Decode(
base::StringPiece("/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoArK\n"),
&too_long_hash);
scoped_ptr<base::Value> too_long_hash_json =
ParseJson(CreateSignedTreeHeadJsonString(
1 /* tree_size */, 123456u /* timestamp */,
GetSampleSTHSHA256RootHash(), too_long_hash));
ASSERT_FALSE(FillSignedTreeHead(*too_long_hash_json.get(), &tree_head));
std::string too_short_hash;
base::Base64Decode(
base::StringPiece("/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoA==\n"),
&too_short_hash);
scoped_ptr<base::Value> too_short_hash_json =
ParseJson(CreateSignedTreeHeadJsonString(
1 /* tree_size */, 123456u /* timestamp */,
GetSampleSTHSHA256RootHash(), too_short_hash));
ASSERT_FALSE(FillSignedTreeHead(*too_short_hash_json.get(), &tree_head));
}
TEST(CTLogResponseParserTest, ParsesConsistencyProofSuccessfully) {
std::string first(32, 'a');
std::string second(32, 'b');
std::string third(32, 'c');
std::vector<std::string> raw_nodes;
raw_nodes.push_back(first);
raw_nodes.push_back(second);
raw_nodes.push_back(third);
scoped_ptr<base::Value> sample_consistency_proof =
ParseJson(CreateConsistencyProofJsonString(raw_nodes));
std::vector<std::string> output;
ASSERT_TRUE(FillConsistencyProof(*sample_consistency_proof.get(), &output));
EXPECT_EQ(output[0], first);
EXPECT_EQ(output[1], second);
EXPECT_EQ(output[2], third);
}
TEST(CTLogResponseParserTest, FailsOnInvalidProofJson) {
std::vector<std::string> output;
scoped_ptr<base::Value> badly_encoded =
ParseJson(std::string("{\"consistency\": [\"notbase64\"]}"));
EXPECT_FALSE(FillConsistencyProof(*badly_encoded.get(), &output));
scoped_ptr<base::Value> not_a_string =
ParseJson(std::string("{\"consistency\": [42, 16]}"));
EXPECT_FALSE(FillConsistencyProof(*badly_encoded.get(), &output));
scoped_ptr<base::Value> missing_consistency = ParseJson(std::string("{}"));
EXPECT_FALSE(FillConsistencyProof(*missing_consistency.get(), &output));
scoped_ptr<base::Value> not_a_dict = ParseJson(std::string("[]"));
EXPECT_FALSE(FillConsistencyProof(*not_a_dict.get(), &output));
}
TEST(CTLogResponseParserTest, ParsesProofJsonWithExtraFields) {
std::vector<std::string> output;
scoped_ptr<base::Value> badly_encoded =
ParseJson(std::string("{\"consistency\": [], \"somethingelse\": 3}"));
EXPECT_TRUE(FillConsistencyProof(*badly_encoded.get(), &output));
}
} // namespace ct
} // namespace net
|