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.
5 #include "components/certificate_transparency/log_proof_fetcher.h"
9 #include "base/strings/stringprintf.h"
10 #include "components/safe_json/testing_json_parser.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/network_delegate.h"
13 #include "net/cert/signed_tree_head.h"
14 #include "net/http/http_status_code.h"
15 #include "net/test/ct_test_util.h"
16 #include "net/url_request/url_request_context.h"
17 #include "net/url_request/url_request_filter.h"
18 #include "net/url_request/url_request_interceptor.h"
19 #include "net/url_request/url_request_job.h"
20 #include "net/url_request/url_request_test_job.h"
21 #include "net/url_request/url_request_test_util.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace certificate_transparency
{
28 const char kGetSTHHeaders
[] =
30 "Content-Type: application/json; charset=ISO-8859-1\0";
32 const char kGetSTHNotFoundHeaders
[] =
33 "HTTP/1.1 404 Not Found\0"
34 "Content-Type: text/html; charset=iso-8859-1\0";
36 const char kLogSchema
[] = "https";
37 const char kLogHost
[] = "ct.log.example.com";
38 const char kLogPathPrefix
[] = "somelog";
39 const char kLogID
[] = "some_id";
41 class FetchSTHTestJob
: public net::URLRequestTestJob
{
43 FetchSTHTestJob(const std::string
& get_sth_data
,
44 const std::string
& get_sth_headers
,
45 net::URLRequest
* request
,
46 net::NetworkDelegate
* network_delegate
)
47 : URLRequestTestJob(request
,
54 void set_async_io(bool async_io
) { async_io_
= async_io
; }
57 ~FetchSTHTestJob() override
{}
59 bool NextReadAsync() override
{
60 // Response with indication of async IO only once, otherwise the final
61 // Read would (incorrectly) be classified as async, causing the
62 // URLRequestJob to try reading another time and failing on a CHECK
63 // that the raw_read_buffer_ is not null.
64 // According to mmenke@, this is a bug in the URLRequestTestJob code.
65 // TODO(eranm): Once said bug is fixed, switch most tests to using async
76 DISALLOW_COPY_AND_ASSIGN(FetchSTHTestJob
);
79 class GetSTHResponseHandler
: public net::URLRequestInterceptor
{
81 GetSTHResponseHandler()
85 std::string(kGetSTHHeaders
, arraysize(kGetSTHHeaders
))) {}
86 ~GetSTHResponseHandler() override
{}
88 // URLRequestInterceptor implementation:
89 net::URLRequestJob
* MaybeInterceptRequest(
90 net::URLRequest
* request
,
91 net::NetworkDelegate
* network_delegate
) const override
{
92 std::string expected_url
= base::StringPrintf(
93 "%s://%s/%s/ct/v1/get-sth", kLogSchema
, kLogHost
, kLogPathPrefix
);
94 EXPECT_EQ(GURL(expected_url
), request
->url());
95 FetchSTHTestJob
* job
= new FetchSTHTestJob(
96 response_body_
, response_headers_
, request
, network_delegate
);
97 job
->set_async_io(async_io_
);
101 void set_response_body(std::string response_body
) {
102 response_body_
= response_body
;
105 void set_response_headers(std::string response_headers
) {
106 response_headers_
= response_headers
;
109 void set_async_io(bool async_io
) { async_io_
= async_io
; }
113 std::string response_body_
;
114 std::string response_headers_
;
116 DISALLOW_COPY_AND_ASSIGN(GetSTHResponseHandler
);
119 class RecordFetchCallbackInvocations
{
121 RecordFetchCallbackInvocations(bool expect_success
)
122 : expect_success_(expect_success
),
125 http_response_code_(-1) {}
127 void STHFetched(const std::string
& log_id
,
128 const net::ct::SignedTreeHead
& sth
) {
129 ASSERT_TRUE(expect_success_
);
130 ASSERT_FALSE(invoked_
);
132 // If expected to succeed, expecting the known_good STH.
133 net::ct::SignedTreeHead expected_sth
;
134 net::ct::GetSampleSignedTreeHead(&expected_sth
);
136 EXPECT_EQ(kLogID
, log_id
);
137 EXPECT_EQ(expected_sth
.version
, sth
.version
);
138 EXPECT_EQ(expected_sth
.timestamp
, sth
.timestamp
);
139 EXPECT_EQ(expected_sth
.tree_size
, sth
.tree_size
);
140 EXPECT_STREQ(expected_sth
.sha256_root_hash
, sth
.sha256_root_hash
);
141 EXPECT_EQ(expected_sth
.signature
.hash_algorithm
,
142 sth
.signature
.hash_algorithm
);
143 EXPECT_EQ(expected_sth
.signature
.signature_algorithm
,
144 sth
.signature
.signature_algorithm
);
145 EXPECT_EQ(expected_sth
.signature
.signature_data
,
146 sth
.signature
.signature_data
);
149 void FetchingFailed(const std::string
& log_id
,
151 int http_response_code
) {
152 ASSERT_FALSE(expect_success_
);
153 ASSERT_FALSE(invoked_
);
155 net_error_
= net_error
;
156 http_response_code_
= http_response_code
;
157 if (net_error_
== net::OK
) {
158 EXPECT_NE(net::HTTP_OK
, http_response_code_
);
162 bool invoked() const { return invoked_
; }
164 int net_error() const { return net_error_
; }
166 int http_response_code() const { return http_response_code_
; }
169 const bool expect_success_
;
172 int http_response_code_
;
175 class LogProofFetcherTest
: public ::testing::Test
{
177 LogProofFetcherTest()
178 : log_url_(base::StringPrintf("%s://%s/%s/",
182 scoped_ptr
<GetSTHResponseHandler
> handler(new GetSTHResponseHandler());
183 handler_
= handler
.get();
185 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
186 kLogSchema
, kLogHost
, handler
.Pass());
188 fetcher_
.reset(new LogProofFetcher(&context_
));
191 ~LogProofFetcherTest() override
{
192 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(kLogSchema
,
197 void SetValidSTHJSONResponse() {
198 std::string sth_json_reply_data
= net::ct::GetSampleSTHAsJson();
199 handler_
->set_response_body(sth_json_reply_data
);
202 void RunFetcherWithCallback(RecordFetchCallbackInvocations
* callback
) {
203 fetcher_
->FetchSignedTreeHead(
205 base::Bind(&RecordFetchCallbackInvocations::STHFetched
,
206 base::Unretained(callback
)),
207 base::Bind(&RecordFetchCallbackInvocations::FetchingFailed
,
208 base::Unretained(callback
)));
209 message_loop_
.RunUntilIdle();
212 base::MessageLoopForIO message_loop_
;
213 net::TestURLRequestContext context_
;
214 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_
;
215 scoped_ptr
<LogProofFetcher
> fetcher_
;
217 GetSTHResponseHandler
* handler_
;
220 TEST_F(LogProofFetcherTest
, TestValidGetReply
) {
221 SetValidSTHJSONResponse();
223 RecordFetchCallbackInvocations
callback(true);
225 RunFetcherWithCallback(&callback
);
227 ASSERT_TRUE(callback
.invoked());
230 TEST_F(LogProofFetcherTest
, TestValidGetReplyAsyncIO
) {
231 SetValidSTHJSONResponse();
232 handler_
->set_async_io(true);
234 RecordFetchCallbackInvocations
callback(true);
235 RunFetcherWithCallback(&callback
);
237 ASSERT_TRUE(callback
.invoked());
240 TEST_F(LogProofFetcherTest
, TestInvalidGetReplyIncompleteJSON
) {
241 std::string sth_json_reply_data
= net::ct::CreateSignedTreeHeadJsonString(
242 21 /* tree_size */, 123456u /* timestamp */, std::string(),
244 handler_
->set_response_body(sth_json_reply_data
);
246 RecordFetchCallbackInvocations
callback(false);
247 RunFetcherWithCallback(&callback
);
249 ASSERT_TRUE(callback
.invoked());
250 EXPECT_EQ(net::ERR_CT_STH_INCOMPLETE
, callback
.net_error());
253 TEST_F(LogProofFetcherTest
, TestInvalidGetReplyInvalidJSON
) {
254 std::string sth_json_reply_data
= "{\"tree_size\":21,\"timestamp\":}";
255 handler_
->set_response_body(sth_json_reply_data
);
257 RecordFetchCallbackInvocations
callback(false);
258 RunFetcherWithCallback(&callback
);
260 ASSERT_TRUE(callback
.invoked());
261 EXPECT_EQ(net::ERR_CT_STH_PARSING_FAILED
, callback
.net_error());
264 TEST_F(LogProofFetcherTest
, TestLogReplyIsTooLong
) {
265 std::string sth_json_reply_data
= net::ct::GetSampleSTHAsJson();
266 // Add kMaxLogResponseSizeInBytes to make sure the response is too big.
267 sth_json_reply_data
.append(
268 std::string(LogProofFetcher::kMaxLogResponseSizeInBytes
, ' '));
269 handler_
->set_response_body(sth_json_reply_data
);
271 RecordFetchCallbackInvocations
callback(false);
272 RunFetcherWithCallback(&callback
);
274 ASSERT_TRUE(callback
.invoked());
275 EXPECT_EQ(net::ERR_FILE_TOO_BIG
, callback
.net_error());
276 EXPECT_EQ(net::HTTP_OK
, callback
.http_response_code());
279 TEST_F(LogProofFetcherTest
, TestLogReplyIsExactlyMaxSize
) {
280 std::string sth_json_reply_data
= net::ct::GetSampleSTHAsJson();
281 // Extend the reply to be exactly kMaxLogResponseSizeInBytes.
282 sth_json_reply_data
.append(std::string(
283 LogProofFetcher::kMaxLogResponseSizeInBytes
- sth_json_reply_data
.size(),
285 handler_
->set_response_body(sth_json_reply_data
);
287 RecordFetchCallbackInvocations
callback(true);
288 RunFetcherWithCallback(&callback
);
290 ASSERT_TRUE(callback
.invoked());
293 TEST_F(LogProofFetcherTest
, TestLogRepliesWithHttpError
) {
294 handler_
->set_response_headers(
295 std::string(kGetSTHNotFoundHeaders
, arraysize(kGetSTHNotFoundHeaders
)));
297 RecordFetchCallbackInvocations
callback(false);
298 RunFetcherWithCallback(&callback
);
300 ASSERT_TRUE(callback
.invoked());
301 EXPECT_EQ(net::OK
, callback
.net_error());
302 EXPECT_EQ(net::HTTP_NOT_FOUND
, callback
.http_response_code());
307 } // namespace certificate_transparency