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

Side by Side Diff: components/certificate_transparency/log_proof_fetcher_unittest.cc

Issue 1222953002: Certificate Transparency: Add STH Fetching capability. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing review comments Created 5 years, 5 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 2015 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 "components/certificate_transparency/log_proof_fetcher.h"
6
7 #include <string>
8
9 #include "components/safe_json/testing_json_parser.h"
10 #include "net/base/network_delegate.h"
11 #include "net/cert/signed_tree_head.h"
12 #include "net/test/ct_test_util.h"
13 #include "net/url_request/url_request_context.h"
14 #include "net/url_request/url_request_filter.h"
15 #include "net/url_request/url_request_interceptor.h"
16 #include "net/url_request/url_request_job.h"
17 #include "net/url_request/url_request_test_job.h"
18 #include "net/url_request/url_request_test_util.h"
19
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace certificate_transparency {
23
24 namespace {
25
26 const char kGetSTHHeaders[] =
27 "HTTP/1.1 200 OK\0"
28 "Content-Type: application/json; charset=ISO-8859-1\0"
29 "\0";
30
31 const char kLogSchema[] = "https";
32 const char kLogURL[] = "ct.log.example.com";
33
34 std::string GetLogID() {
35 return std::string("some_id");
36 }
37
38 class GetSTHResponseHandler : public net::URLRequestInterceptor {
39 public:
40 GetSTHResponseHandler(const std::string& headers,
41 const std::string& get_sth_data)
42 : headers_(headers), sth_data_(get_sth_data) {}
43 ~GetSTHResponseHandler() override {}
44
45 // URLRequestInterceptor implementation:
46 net::URLRequestJob* MaybeInterceptRequest(
47 net::URLRequest* request,
48 net::NetworkDelegate* network_delegate) const override {
49 return new net::URLRequestTestJob(request, network_delegate, headers_,
50 sth_data_, true);
51 }
52
53 private:
54 std::string headers_;
55 const std::string& sth_data_;
56
57 DISALLOW_COPY_AND_ASSIGN(GetSTHResponseHandler);
58 };
59
60 class LogProofFetcherTest : public ::testing::Test {
61 public:
62 LogProofFetcherTest()
63 : context_(true),
64 log_url_(std::string(kLogSchema) + "://" + std::string(kLogURL) + "/") {
65 context_.Init();
66 }
67
68 void SetUp() override {
69 scoped_ptr<GetSTHResponseHandler> handler(
70 new GetSTHResponseHandler(kGetSTHHeaders, sth_json_reply_data_));
71
72 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
73 kLogSchema, kLogURL, handler.Pass());
74
75 fetcher_.reset(new LogProofFetcher(&context_));
76 }
77
78 void TearDown() override {
79 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(kLogSchema,
80 kLogURL);
81 }
82
83 protected:
84 base::MessageLoopForIO message_loop_;
85 net::TestURLRequestContext context_;
86 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_;
87 scoped_ptr<LogProofFetcher> fetcher_;
88 std::string sth_json_reply_data_;
89 GURL log_url_;
90 };
91
92 class RecordFetchCallbackInvocations {
93 public:
94 RecordFetchCallbackInvocations() : invoked_(false), failed_(false) {}
95
96 virtual void STHFetched(const std::string& log_id,
97 const net::ct::SignedTreeHead& sth) {
98 invoked_ = true;
99 }
100
101 void FetchingFailed(const std::string& log_id, int error_value) {
102 invoked_ = true;
103 failed_ = true;
104 }
105
106 bool invoked() { return invoked_; }
107
108 bool failed() { return failed_; }
109
110 private:
111 bool invoked_;
112 bool failed_;
113 };
114
115 class ExpectedSuccessCallback : public RecordFetchCallbackInvocations {
116 public:
117 ExpectedSuccessCallback(const net::ct::SignedTreeHead& sth)
118 : known_sth_(sth) {}
119
120 void STHFetched(const std::string& log_id,
121 const net::ct::SignedTreeHead& sth) override {
122 ASSERT_EQ(GetLogID(), log_id);
123 ASSERT_EQ(sth.version, known_sth_.version);
124 ASSERT_EQ(sth.timestamp, known_sth_.timestamp);
125 ASSERT_EQ(sth.tree_size, known_sth_.tree_size);
126 ASSERT_STREQ(sth.sha256_root_hash, known_sth_.sha256_root_hash);
127 ASSERT_EQ(sth.signature.hash_algorithm,
128 known_sth_.signature.hash_algorithm);
129 ASSERT_EQ(sth.signature.signature_algorithm,
130 known_sth_.signature.signature_algorithm);
131 ASSERT_EQ(sth.signature.signature_data,
132 known_sth_.signature.signature_data);
133
134 RecordFetchCallbackInvocations::STHFetched(log_id, sth);
135 }
136
137 private:
138 net::ct::SignedTreeHead known_sth_;
139 };
140
141 TEST_F(LogProofFetcherTest, TestValidGetSTHReply) {
142 sth_json_reply_data_ = net::ct::GetSampleSTHAsJson();
143
144 net::ct::SignedTreeHead valid_sth;
145 net::ct::GetSampleSignedTreeHead(&valid_sth);
146 ExpectedSuccessCallback cb(valid_sth);
147
148 fetcher_->FetchSTH(
149 log_url_, GetLogID(),
150 base::Bind(&ExpectedSuccessCallback::STHFetched, base::Unretained(&cb)),
151 base::Bind(&ExpectedSuccessCallback::FetchingFailed,
152 base::Unretained(&cb)));
153 message_loop_.RunUntilIdle();
154
155 ASSERT_TRUE(cb.invoked());
156 ASSERT_FALSE(cb.failed());
157 }
158
159 TEST_F(LogProofFetcherTest, TestInvalidGetSTHReplyIncompleteSTH) {
160 RecordFetchCallbackInvocations cb;
161 sth_json_reply_data_ = net::ct::CreateSignedTreeHeadJsonString(
162 21 /* tree_size */, 123456u /* timestamp */, std::string(""),
163 std::string(""));
164
165 fetcher_->FetchSTH(log_url_, GetLogID(),
166 base::Bind(&RecordFetchCallbackInvocations::STHFetched,
167 base::Unretained(&cb)),
168 base::Bind(&RecordFetchCallbackInvocations::FetchingFailed,
169 base::Unretained(&cb)));
170 message_loop_.RunUntilIdle();
171
172 ASSERT_TRUE(cb.invoked());
173 ASSERT_TRUE(cb.failed());
174 }
175
176 TEST_F(LogProofFetcherTest, TestInvalidGetSTHReplyInvalidJSON) {
177 RecordFetchCallbackInvocations cb;
178 sth_json_reply_data_ = "{\"tree_size\":21,\"timestamp\":}";
179
180 fetcher_->FetchSTH(log_url_, GetLogID(),
181 base::Bind(&RecordFetchCallbackInvocations::STHFetched,
182 base::Unretained(&cb)),
183 base::Bind(&RecordFetchCallbackInvocations::FetchingFailed,
184 base::Unretained(&cb)));
185 message_loop_.RunUntilIdle();
186
187 ASSERT_TRUE(cb.invoked());
188 ASSERT_TRUE(cb.failed());
189 }
190
191 } // namespace
192
193 } // namespace certificate_transparency
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698