OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/certificate_transparency/log_proof_fetcher.h" | 5 #include "components/certificate_transparency/log_proof_fetcher.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
| 9 #include "base/format_macros.h" |
| 10 #include "base/run_loop.h" |
9 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
10 #include "components/safe_json/testing_json_parser.h" | 12 #include "components/safe_json/testing_json_parser.h" |
11 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
12 #include "net/base/network_delegate.h" | 14 #include "net/base/network_delegate.h" |
13 #include "net/cert/signed_tree_head.h" | 15 #include "net/cert/signed_tree_head.h" |
14 #include "net/http/http_status_code.h" | 16 #include "net/http/http_status_code.h" |
15 #include "net/test/ct_test_util.h" | 17 #include "net/test/ct_test_util.h" |
16 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
17 #include "net/url_request/url_request_filter.h" | 19 #include "net/url_request/url_request_filter.h" |
18 #include "net/url_request/url_request_interceptor.h" | 20 #include "net/url_request/url_request_interceptor.h" |
19 #include "net/url_request/url_request_job.h" | 21 #include "net/url_request/url_request_job.h" |
20 #include "net/url_request/url_request_test_job.h" | 22 #include "net/url_request/url_request_test_job.h" |
21 #include "net/url_request/url_request_test_util.h" | 23 #include "net/url_request/url_request_test_util.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
23 | 25 |
24 namespace certificate_transparency { | 26 namespace certificate_transparency { |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
28 const char kGetSTHHeaders[] = | 30 const char kGetResponseHeaders[] = |
29 "HTTP/1.1 200 OK\n" | 31 "HTTP/1.1 200 OK\n" |
30 "Content-Type: application/json; charset=ISO-8859-1\n"; | 32 "Content-Type: application/json; charset=ISO-8859-1\n"; |
31 | 33 |
32 const char kGetSTHNotFoundHeaders[] = | 34 const char kGetResponseNotFoundHeaders[] = |
33 "HTTP/1.1 404 Not Found\n" | 35 "HTTP/1.1 404 Not Found\n" |
34 "Content-Type: text/html; charset=iso-8859-1\n"; | 36 "Content-Type: text/html; charset=iso-8859-1\n"; |
35 | 37 |
36 const char kLogSchema[] = "https"; | 38 const char kLogSchema[] = "https"; |
37 const char kLogHost[] = "ct.log.example.com"; | 39 const char kLogHost[] = "ct.log.example.com"; |
38 const char kLogPathPrefix[] = "somelog"; | 40 const char kLogPathPrefix[] = "somelog"; |
39 const char kLogID[] = "some_id"; | 41 const char kLogID[] = "some_id"; |
40 | 42 |
41 class FetchSTHTestJob : public net::URLRequestTestJob { | 43 // Node returned will be chr(node_id) * 32. |
| 44 std::string GetDummyConsistencyProofNode(uint64_t node_id) { |
| 45 return std::string(32, static_cast<char>(node_id)); |
| 46 } |
| 47 |
| 48 const size_t kDummyConsistencyProofLength = 4; |
| 49 |
| 50 class LogFetchTestJob : public net::URLRequestTestJob { |
42 public: | 51 public: |
43 FetchSTHTestJob(const std::string& get_sth_data, | 52 LogFetchTestJob(const std::string& get_log_data, |
44 const std::string& get_sth_headers, | 53 const std::string& get_log_headers, |
45 net::URLRequest* request, | 54 net::URLRequest* request, |
46 net::NetworkDelegate* network_delegate) | 55 net::NetworkDelegate* network_delegate) |
47 : URLRequestTestJob(request, | 56 : URLRequestTestJob(request, |
48 network_delegate, | 57 network_delegate, |
49 get_sth_headers, | 58 get_log_headers, |
50 get_sth_data, | 59 get_log_data, |
51 true), | 60 true), |
52 async_io_(false) {} | 61 async_io_(false) {} |
53 | 62 |
54 void set_async_io(bool async_io) { async_io_ = async_io; } | 63 void set_async_io(bool async_io) { async_io_ = async_io; } |
55 | 64 |
56 private: | 65 private: |
57 ~FetchSTHTestJob() override {} | 66 ~LogFetchTestJob() override {} |
58 | 67 |
59 bool NextReadAsync() override { | 68 bool NextReadAsync() override { |
60 // Response with indication of async IO only once, otherwise the final | 69 // Response with indication of async IO only once, otherwise the final |
61 // Read would (incorrectly) be classified as async, causing the | 70 // Read would (incorrectly) be classified as async, causing the |
62 // URLRequestJob to try reading another time and failing on a CHECK | 71 // URLRequestJob to try reading another time and failing on a CHECK |
63 // that the raw_read_buffer_ is not null. | 72 // that the raw_read_buffer_ is not null. |
64 // According to mmenke@, this is a bug in the URLRequestTestJob code. | 73 // 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 | 74 // TODO(eranm): Once said bug is fixed, switch most tests to using async |
66 // IO. | 75 // IO. |
67 if (async_io_) { | 76 if (async_io_) { |
68 async_io_ = false; | 77 async_io_ = false; |
69 return true; | 78 return true; |
70 } | 79 } |
71 return false; | 80 return false; |
72 } | 81 } |
73 | 82 |
74 bool async_io_; | 83 bool async_io_; |
75 | 84 |
76 DISALLOW_COPY_AND_ASSIGN(FetchSTHTestJob); | 85 DISALLOW_COPY_AND_ASSIGN(LogFetchTestJob); |
77 }; | 86 }; |
78 | 87 |
79 class GetSTHResponseHandler : public net::URLRequestInterceptor { | 88 class LogGetResponseHandler : public net::URLRequestInterceptor { |
80 public: | 89 public: |
81 GetSTHResponseHandler() | 90 LogGetResponseHandler() |
82 : async_io_(false), | 91 : async_io_(false), |
83 response_body_(""), | |
84 response_headers_( | 92 response_headers_( |
85 std::string(kGetSTHHeaders, arraysize(kGetSTHHeaders))) {} | 93 std::string(kGetResponseHeaders, arraysize(kGetResponseHeaders))), |
86 ~GetSTHResponseHandler() override {} | 94 expected_old_tree_size_(0), |
| 95 expected_new_tree_size_(0) {} |
| 96 ~LogGetResponseHandler() override {} |
87 | 97 |
88 // URLRequestInterceptor implementation: | 98 // URLRequestInterceptor implementation: |
89 net::URLRequestJob* MaybeInterceptRequest( | 99 net::URLRequestJob* MaybeInterceptRequest( |
90 net::URLRequest* request, | 100 net::URLRequest* request, |
91 net::NetworkDelegate* network_delegate) const override { | 101 net::NetworkDelegate* network_delegate) const override { |
92 std::string expected_url = base::StringPrintf( | 102 EXPECT_EQ(expected_url_, request->url()); |
93 "%s://%s/%s/ct/v1/get-sth", kLogSchema, kLogHost, kLogPathPrefix); | 103 |
94 EXPECT_EQ(GURL(expected_url), request->url()); | 104 LogFetchTestJob* job = new LogFetchTestJob( |
95 FetchSTHTestJob* job = new FetchSTHTestJob( | |
96 response_body_, response_headers_, request, network_delegate); | 105 response_body_, response_headers_, request, network_delegate); |
97 job->set_async_io(async_io_); | 106 job->set_async_io(async_io_); |
98 return job; | 107 return job; |
99 } | 108 } |
100 | 109 |
101 void set_response_body(const std::string& response_body) { | 110 void set_response_body(const std::string& response_body) { |
102 response_body_ = response_body; | 111 response_body_ = response_body; |
103 } | 112 } |
104 | 113 |
105 void set_response_headers(const std::string& response_headers) { | 114 void set_response_headers(const std::string& response_headers) { |
106 response_headers_ = response_headers; | 115 response_headers_ = response_headers; |
107 } | 116 } |
108 | 117 |
| 118 void set_expect_get_consistency_proof(uint64_t expected_old_tree_size, |
| 119 uint64_t expected_new_tree_size) { |
| 120 expected_old_tree_size_ = expected_old_tree_size; |
| 121 expected_new_tree_size_ = expected_new_tree_size; |
| 122 } |
| 123 |
109 void set_async_io(bool async_io) { async_io_ = async_io; } | 124 void set_async_io(bool async_io) { async_io_ = async_io; } |
110 | 125 |
| 126 void set_expected_url(const GURL& url) { expected_url_ = url; } |
| 127 |
111 private: | 128 private: |
112 bool async_io_; | 129 bool async_io_; |
113 std::string response_body_; | 130 std::string response_body_; |
114 std::string response_headers_; | 131 std::string response_headers_; |
115 | 132 |
116 DISALLOW_COPY_AND_ASSIGN(GetSTHResponseHandler); | 133 uint64_t expected_old_tree_size_; |
| 134 uint64_t expected_new_tree_size_; |
| 135 |
| 136 // Stored for test body to assert on |
| 137 GURL expected_url_; |
| 138 |
| 139 DISALLOW_COPY_AND_ASSIGN(LogGetResponseHandler); |
| 140 }; |
| 141 |
| 142 enum InterceptedResultType { |
| 143 NOTHING, |
| 144 FAILURE, |
| 145 STH_FETCH, |
| 146 CONSISTENCY_PROOF_FETCH |
117 }; | 147 }; |
118 | 148 |
119 class RecordFetchCallbackInvocations { | 149 class RecordFetchCallbackInvocations { |
120 public: | 150 public: |
121 RecordFetchCallbackInvocations(bool expect_success) | 151 RecordFetchCallbackInvocations(bool expect_success) |
122 : expect_success_(expect_success), | 152 : expect_success_(expect_success), |
123 invoked_(false), | |
124 net_error_(net::OK), | 153 net_error_(net::OK), |
125 http_response_code_(-1) {} | 154 http_response_code_(-1), |
| 155 request_type_(NOTHING) {} |
126 | 156 |
127 void STHFetched(const std::string& log_id, | 157 void STHFetched(const std::string& log_id, |
128 const net::ct::SignedTreeHead& sth) { | 158 const net::ct::SignedTreeHead& sth) { |
129 ASSERT_TRUE(expect_success_); | 159 ASSERT_TRUE(expect_success_); |
130 ASSERT_FALSE(invoked_); | 160 ASSERT_EQ(NOTHING, request_type_); |
131 invoked_ = true; | 161 request_type_ = STH_FETCH; |
132 // If expected to succeed, expecting the known_good STH. | 162 sth_ = sth; |
133 net::ct::SignedTreeHead expected_sth; | 163 log_id_ = log_id; |
134 net::ct::GetSampleSignedTreeHead(&expected_sth); | 164 run_loop_->Quit(); |
| 165 } |
135 | 166 |
136 EXPECT_EQ(kLogID, log_id); | 167 void ConsistencyProofFetched( |
137 EXPECT_EQ(expected_sth.version, sth.version); | 168 const std::string& log_id, |
138 EXPECT_EQ(expected_sth.timestamp, sth.timestamp); | 169 const std::vector<std::string>& consistency_proof) { |
139 EXPECT_EQ(expected_sth.tree_size, sth.tree_size); | 170 ASSERT_TRUE(expect_success_); |
140 EXPECT_STREQ(expected_sth.sha256_root_hash, sth.sha256_root_hash); | 171 ASSERT_EQ(NOTHING, request_type_); |
141 EXPECT_EQ(expected_sth.signature.hash_algorithm, | 172 request_type_ = CONSISTENCY_PROOF_FETCH; |
142 sth.signature.hash_algorithm); | 173 consistency_proof_.assign(consistency_proof.begin(), |
143 EXPECT_EQ(expected_sth.signature.signature_algorithm, | 174 consistency_proof.end()); |
144 sth.signature.signature_algorithm); | 175 log_id_ = log_id; |
145 EXPECT_EQ(expected_sth.signature.signature_data, | 176 run_loop_->Quit(); |
146 sth.signature.signature_data); | |
147 } | 177 } |
148 | 178 |
149 void FetchingFailed(const std::string& log_id, | 179 void FetchingFailed(const std::string& log_id, |
150 int net_error, | 180 int net_error, |
151 int http_response_code) { | 181 int http_response_code) { |
152 ASSERT_FALSE(expect_success_); | 182 ASSERT_FALSE(expect_success_); |
153 ASSERT_FALSE(invoked_); | 183 ASSERT_EQ(NOTHING, request_type_); |
154 invoked_ = true; | 184 request_type_ = FAILURE; |
155 net_error_ = net_error; | 185 net_error_ = net_error; |
156 http_response_code_ = http_response_code; | 186 http_response_code_ = http_response_code; |
157 if (net_error_ == net::OK) { | 187 if (net_error_ == net::OK) { |
158 EXPECT_NE(net::HTTP_OK, http_response_code_); | 188 EXPECT_NE(net::HTTP_OK, http_response_code_); |
159 } | 189 } |
| 190 |
| 191 run_loop_->Quit(); |
160 } | 192 } |
161 | 193 |
162 bool invoked() const { return invoked_; } | 194 InterceptedResultType intercepted_result_type() const { |
| 195 return request_type_; |
| 196 } |
163 | 197 |
164 int net_error() const { return net_error_; } | 198 int net_error() const { return net_error_; } |
165 | 199 |
166 int http_response_code() const { return http_response_code_; } | 200 int http_response_code() const { return http_response_code_; } |
167 | 201 |
| 202 const net::ct::SignedTreeHead& intercepted_sth() const { return sth_; } |
| 203 |
| 204 const std::string& intercepted_log_id() const { return log_id_; } |
| 205 |
| 206 const std::vector<std::string>& intercepted_proof() const { |
| 207 return consistency_proof_; |
| 208 } |
| 209 |
| 210 void SetRunLoop(base::RunLoop* run_loop) { run_loop_ = run_loop; } |
| 211 |
168 private: | 212 private: |
169 const bool expect_success_; | 213 const bool expect_success_; |
170 bool invoked_; | |
171 int net_error_; | 214 int net_error_; |
172 int http_response_code_; | 215 int http_response_code_; |
| 216 InterceptedResultType request_type_; |
| 217 net::ct::SignedTreeHead sth_; |
| 218 std::string log_id_; |
| 219 std::vector<std::string> consistency_proof_; |
| 220 |
| 221 base::RunLoop* run_loop_; |
173 }; | 222 }; |
174 | 223 |
175 class LogProofFetcherTest : public ::testing::Test { | 224 class LogProofFetcherTest : public ::testing::Test { |
176 public: | 225 public: |
177 LogProofFetcherTest() | 226 LogProofFetcherTest() |
178 : log_url_(base::StringPrintf("%s://%s/%s/", | 227 : log_url_(base::StringPrintf("%s://%s/%s/", |
179 kLogSchema, | 228 kLogSchema, |
180 kLogHost, | 229 kLogHost, |
181 kLogPathPrefix)) { | 230 kLogPathPrefix)) { |
182 scoped_ptr<GetSTHResponseHandler> handler(new GetSTHResponseHandler()); | 231 scoped_ptr<LogGetResponseHandler> handler(new LogGetResponseHandler()); |
183 handler_ = handler.get(); | 232 handler_ = handler.get(); |
184 | 233 |
185 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( | 234 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( |
186 kLogSchema, kLogHost, handler.Pass()); | 235 kLogSchema, kLogHost, handler.Pass()); |
187 | 236 |
188 fetcher_.reset(new LogProofFetcher(&context_)); | 237 fetcher_.reset(new LogProofFetcher(&context_)); |
189 } | 238 } |
190 | 239 |
191 ~LogProofFetcherTest() override { | 240 ~LogProofFetcherTest() override { |
192 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(kLogSchema, | 241 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(kLogSchema, |
193 kLogHost); | 242 kLogHost); |
194 } | 243 } |
195 | 244 |
196 protected: | 245 protected: |
197 void SetValidSTHJSONResponse() { | 246 void SetValidSTHJSONResponse() { |
198 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); | 247 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); |
199 handler_->set_response_body(sth_json_reply_data); | 248 handler_->set_response_body(sth_json_reply_data); |
| 249 handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth")); |
200 } | 250 } |
201 | 251 |
202 void RunFetcherWithCallback(RecordFetchCallbackInvocations* callback) { | 252 void RunFetcherWithCallback(RecordFetchCallbackInvocations* callback) { |
| 253 callback->SetRunLoop(&run_loop_); |
203 fetcher_->FetchSignedTreeHead( | 254 fetcher_->FetchSignedTreeHead( |
204 log_url_, kLogID, | 255 log_url_, kLogID, |
205 base::Bind(&RecordFetchCallbackInvocations::STHFetched, | 256 base::Bind(&RecordFetchCallbackInvocations::STHFetched, |
206 base::Unretained(callback)), | 257 base::Unretained(callback)), |
207 base::Bind(&RecordFetchCallbackInvocations::FetchingFailed, | 258 base::Bind(&RecordFetchCallbackInvocations::FetchingFailed, |
208 base::Unretained(callback))); | 259 base::Unretained(callback))); |
209 message_loop_.RunUntilIdle(); | 260 run_loop_.Run(); |
| 261 } |
| 262 |
| 263 void RunGetConsistencyFetcherWithCallback( |
| 264 RecordFetchCallbackInvocations* callback) { |
| 265 const uint64_t kOldTree = 5; |
| 266 const uint64_t kNewTree = 8; |
| 267 handler_->set_expected_url(log_url_.Resolve(base::StringPrintf( |
| 268 "ct/v1/get-sth-consistency?first=%" PRIuS "&second=%" PRIuS, kOldTree, |
| 269 kNewTree))); |
| 270 handler_->set_expect_get_consistency_proof(kOldTree, kNewTree); |
| 271 callback->SetRunLoop(&run_loop_); |
| 272 fetcher_->FetchConsistencyProof( |
| 273 log_url_, kLogID, kOldTree, kNewTree, |
| 274 base::Bind(&RecordFetchCallbackInvocations::ConsistencyProofFetched, |
| 275 base::Unretained(callback)), |
| 276 base::Bind(&RecordFetchCallbackInvocations::FetchingFailed, |
| 277 base::Unretained(callback))); |
| 278 run_loop_.Run(); |
| 279 } |
| 280 |
| 281 void VerifyReceivedSTH(const std::string& log_id, |
| 282 const net::ct::SignedTreeHead& sth) { |
| 283 net::ct::SignedTreeHead expected_sth; |
| 284 net::ct::GetSampleSignedTreeHead(&expected_sth); |
| 285 |
| 286 EXPECT_EQ(kLogID, log_id); |
| 287 EXPECT_EQ(expected_sth.version, sth.version); |
| 288 EXPECT_EQ(expected_sth.timestamp, sth.timestamp); |
| 289 EXPECT_EQ(expected_sth.tree_size, sth.tree_size); |
| 290 EXPECT_STREQ(expected_sth.sha256_root_hash, sth.sha256_root_hash); |
| 291 EXPECT_EQ(expected_sth.signature.hash_algorithm, |
| 292 sth.signature.hash_algorithm); |
| 293 EXPECT_EQ(expected_sth.signature.signature_algorithm, |
| 294 sth.signature.signature_algorithm); |
| 295 EXPECT_EQ(expected_sth.signature.signature_data, |
| 296 sth.signature.signature_data); |
| 297 } |
| 298 |
| 299 void VerifyConsistencyProof( |
| 300 const std::string& log_id, |
| 301 const std::vector<std::string>& consistency_proof) { |
| 302 EXPECT_EQ(kLogID, log_id); |
| 303 EXPECT_EQ(kDummyConsistencyProofLength, consistency_proof.size()); |
| 304 for (uint64_t i = 0; i < kDummyConsistencyProofLength; ++i) { |
| 305 EXPECT_EQ(GetDummyConsistencyProofNode(i), consistency_proof[i]) |
| 306 << " node: " << i; |
| 307 } |
210 } | 308 } |
211 | 309 |
212 base::MessageLoopForIO message_loop_; | 310 base::MessageLoopForIO message_loop_; |
| 311 base::RunLoop run_loop_; |
213 net::TestURLRequestContext context_; | 312 net::TestURLRequestContext context_; |
214 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_; | 313 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_; |
215 scoped_ptr<LogProofFetcher> fetcher_; | 314 scoped_ptr<LogProofFetcher> fetcher_; |
216 const GURL log_url_; | 315 const GURL log_url_; |
217 GetSTHResponseHandler* handler_; | 316 LogGetResponseHandler* handler_; |
218 }; | 317 }; |
219 | 318 |
220 TEST_F(LogProofFetcherTest, TestValidGetReply) { | 319 TEST_F(LogProofFetcherTest, TestValidGetReply) { |
221 SetValidSTHJSONResponse(); | 320 SetValidSTHJSONResponse(); |
222 | 321 |
223 RecordFetchCallbackInvocations callback(true); | 322 RecordFetchCallbackInvocations callback(true); |
224 | 323 |
225 RunFetcherWithCallback(&callback); | 324 RunFetcherWithCallback(&callback); |
226 | 325 |
227 ASSERT_TRUE(callback.invoked()); | 326 ASSERT_EQ(STH_FETCH, callback.intercepted_result_type()); |
| 327 VerifyReceivedSTH(callback.intercepted_log_id(), callback.intercepted_sth()); |
228 } | 328 } |
229 | 329 |
230 TEST_F(LogProofFetcherTest, TestValidGetReplyAsyncIO) { | 330 TEST_F(LogProofFetcherTest, TestValidGetReplyAsyncIO) { |
231 SetValidSTHJSONResponse(); | 331 SetValidSTHJSONResponse(); |
232 handler_->set_async_io(true); | 332 handler_->set_async_io(true); |
233 | 333 |
234 RecordFetchCallbackInvocations callback(true); | 334 RecordFetchCallbackInvocations callback(true); |
235 RunFetcherWithCallback(&callback); | 335 RunFetcherWithCallback(&callback); |
236 | 336 |
237 ASSERT_TRUE(callback.invoked()); | 337 ASSERT_EQ(STH_FETCH, callback.intercepted_result_type()); |
| 338 VerifyReceivedSTH(callback.intercepted_log_id(), callback.intercepted_sth()); |
238 } | 339 } |
239 | 340 |
240 TEST_F(LogProofFetcherTest, TestInvalidGetReplyIncompleteJSON) { | 341 TEST_F(LogProofFetcherTest, TestInvalidGetReplyIncompleteJSON) { |
241 std::string sth_json_reply_data = net::ct::CreateSignedTreeHeadJsonString( | 342 std::string sth_json_reply_data = net::ct::CreateSignedTreeHeadJsonString( |
242 21 /* tree_size */, 123456u /* timestamp */, std::string(), | 343 21 /* tree_size */, 123456u /* timestamp */, std::string(), |
243 std::string()); | 344 std::string()); |
244 handler_->set_response_body(sth_json_reply_data); | 345 handler_->set_response_body(sth_json_reply_data); |
| 346 handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth")); |
245 | 347 |
246 RecordFetchCallbackInvocations callback(false); | 348 RecordFetchCallbackInvocations callback(false); |
247 RunFetcherWithCallback(&callback); | 349 RunFetcherWithCallback(&callback); |
248 | 350 |
249 ASSERT_TRUE(callback.invoked()); | 351 ASSERT_EQ(FAILURE, callback.intercepted_result_type()); |
250 EXPECT_EQ(net::ERR_CT_STH_INCOMPLETE, callback.net_error()); | 352 EXPECT_EQ(net::ERR_CT_STH_INCOMPLETE, callback.net_error()); |
251 } | 353 } |
252 | 354 |
253 TEST_F(LogProofFetcherTest, TestInvalidGetReplyInvalidJSON) { | 355 TEST_F(LogProofFetcherTest, TestInvalidGetReplyInvalidJSON) { |
254 std::string sth_json_reply_data = "{\"tree_size\":21,\"timestamp\":}"; | 356 std::string sth_json_reply_data = "{\"tree_size\":21,\"timestamp\":}"; |
255 handler_->set_response_body(sth_json_reply_data); | 357 handler_->set_response_body(sth_json_reply_data); |
| 358 handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth")); |
256 | 359 |
257 RecordFetchCallbackInvocations callback(false); | 360 RecordFetchCallbackInvocations callback(false); |
258 RunFetcherWithCallback(&callback); | 361 RunFetcherWithCallback(&callback); |
259 | 362 |
260 ASSERT_TRUE(callback.invoked()); | 363 ASSERT_EQ(FAILURE, callback.intercepted_result_type()); |
261 EXPECT_EQ(net::ERR_CT_STH_PARSING_FAILED, callback.net_error()); | 364 EXPECT_EQ(net::ERR_CT_STH_PARSING_FAILED, callback.net_error()); |
262 } | 365 } |
263 | 366 |
264 TEST_F(LogProofFetcherTest, TestLogReplyIsTooLong) { | 367 TEST_F(LogProofFetcherTest, TestLogReplyIsTooLong) { |
265 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); | 368 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); |
266 // Add kMaxLogResponseSizeInBytes to make sure the response is too big. | 369 // Add kMaxLogResponseSizeInBytes to make sure the response is too big. |
267 sth_json_reply_data.append( | 370 sth_json_reply_data.append( |
268 std::string(LogProofFetcher::kMaxLogResponseSizeInBytes, ' ')); | 371 std::string(LogProofFetcher::kMaxLogResponseSizeInBytes, ' ')); |
269 handler_->set_response_body(sth_json_reply_data); | 372 handler_->set_response_body(sth_json_reply_data); |
| 373 handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth")); |
270 | 374 |
271 RecordFetchCallbackInvocations callback(false); | 375 RecordFetchCallbackInvocations callback(false); |
272 RunFetcherWithCallback(&callback); | 376 RunFetcherWithCallback(&callback); |
273 | 377 |
274 ASSERT_TRUE(callback.invoked()); | 378 ASSERT_EQ(FAILURE, callback.intercepted_result_type()); |
275 EXPECT_EQ(net::ERR_FILE_TOO_BIG, callback.net_error()); | 379 EXPECT_EQ(net::ERR_FILE_TOO_BIG, callback.net_error()); |
276 EXPECT_EQ(net::HTTP_OK, callback.http_response_code()); | 380 EXPECT_EQ(net::HTTP_OK, callback.http_response_code()); |
277 } | 381 } |
278 | 382 |
279 TEST_F(LogProofFetcherTest, TestLogReplyIsExactlyMaxSize) { | 383 TEST_F(LogProofFetcherTest, TestLogReplyIsExactlyMaxSize) { |
280 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); | 384 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); |
281 // Extend the reply to be exactly kMaxLogResponseSizeInBytes. | 385 // Extend the reply to be exactly kMaxLogResponseSizeInBytes. |
282 sth_json_reply_data.append(std::string( | 386 sth_json_reply_data.append(std::string( |
283 LogProofFetcher::kMaxLogResponseSizeInBytes - sth_json_reply_data.size(), | 387 LogProofFetcher::kMaxLogResponseSizeInBytes - sth_json_reply_data.size(), |
284 ' ')); | 388 ' ')); |
285 handler_->set_response_body(sth_json_reply_data); | 389 handler_->set_response_body(sth_json_reply_data); |
| 390 handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth")); |
286 | 391 |
287 RecordFetchCallbackInvocations callback(true); | 392 RecordFetchCallbackInvocations callback(true); |
288 RunFetcherWithCallback(&callback); | 393 RunFetcherWithCallback(&callback); |
289 | 394 |
290 ASSERT_TRUE(callback.invoked()); | 395 ASSERT_EQ(STH_FETCH, callback.intercepted_result_type()); |
| 396 VerifyReceivedSTH(callback.intercepted_log_id(), callback.intercepted_sth()); |
291 } | 397 } |
292 | 398 |
293 TEST_F(LogProofFetcherTest, TestLogRepliesWithHttpError) { | 399 TEST_F(LogProofFetcherTest, TestLogRepliesWithHttpError) { |
294 handler_->set_response_headers( | 400 handler_->set_response_headers(std::string( |
295 std::string(kGetSTHNotFoundHeaders, arraysize(kGetSTHNotFoundHeaders))); | 401 kGetResponseNotFoundHeaders, arraysize(kGetResponseNotFoundHeaders))); |
| 402 handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth")); |
296 | 403 |
297 RecordFetchCallbackInvocations callback(false); | 404 RecordFetchCallbackInvocations callback(false); |
298 RunFetcherWithCallback(&callback); | 405 RunFetcherWithCallback(&callback); |
299 | 406 |
300 ASSERT_TRUE(callback.invoked()); | 407 ASSERT_EQ(FAILURE, callback.intercepted_result_type()); |
301 EXPECT_EQ(net::OK, callback.net_error()); | 408 EXPECT_EQ(net::OK, callback.net_error()); |
302 EXPECT_EQ(net::HTTP_NOT_FOUND, callback.http_response_code()); | 409 EXPECT_EQ(net::HTTP_NOT_FOUND, callback.http_response_code()); |
303 } | 410 } |
304 | 411 |
| 412 TEST_F(LogProofFetcherTest, TestValidGetConsistencyValidReply) { |
| 413 std::vector<std::string> proof; |
| 414 for (uint64_t i = 0; i < kDummyConsistencyProofLength; ++i) |
| 415 proof.push_back(GetDummyConsistencyProofNode(i)); |
| 416 |
| 417 std::string consistency_proof_reply_data = |
| 418 net::ct::CreateConsistencyProofJsonString(proof); |
| 419 handler_->set_response_body(consistency_proof_reply_data); |
| 420 |
| 421 RecordFetchCallbackInvocations callback(true); |
| 422 RunGetConsistencyFetcherWithCallback(&callback); |
| 423 |
| 424 ASSERT_EQ(CONSISTENCY_PROOF_FETCH, callback.intercepted_result_type()); |
| 425 VerifyConsistencyProof(callback.intercepted_log_id(), |
| 426 callback.intercepted_proof()); |
| 427 } |
| 428 |
| 429 TEST_F(LogProofFetcherTest, TestInvalidGetConsistencyReplyInvalidJSON) { |
| 430 std::string consistency_proof_reply_data = "{\"consistency\": [1,2]}"; |
| 431 handler_->set_response_body(consistency_proof_reply_data); |
| 432 |
| 433 RecordFetchCallbackInvocations callback(false); |
| 434 RunGetConsistencyFetcherWithCallback(&callback); |
| 435 |
| 436 ASSERT_EQ(FAILURE, callback.intercepted_result_type()); |
| 437 EXPECT_EQ(net::ERR_CT_CONSISTENCY_PROOF_PARSING_FAILED, callback.net_error()); |
| 438 EXPECT_EQ(net::HTTP_OK, callback.http_response_code()); |
| 439 } |
| 440 |
305 } // namespace | 441 } // namespace |
306 | 442 |
307 } // namespace certificate_transparency | 443 } // namespace certificate_transparency |
OLD | NEW |