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 ~LogGetResponseHandler() override {} |
87 | 95 |
88 // URLRequestInterceptor implementation: | 96 // URLRequestInterceptor implementation: |
89 net::URLRequestJob* MaybeInterceptRequest( | 97 net::URLRequestJob* MaybeInterceptRequest( |
90 net::URLRequest* request, | 98 net::URLRequest* request, |
91 net::NetworkDelegate* network_delegate) const override { | 99 net::NetworkDelegate* network_delegate) const override { |
92 std::string expected_url = base::StringPrintf( | 100 EXPECT_EQ(expected_url_, request->url()); |
93 "%s://%s/%s/ct/v1/get-sth", kLogSchema, kLogHost, kLogPathPrefix); | 101 |
94 EXPECT_EQ(GURL(expected_url), request->url()); | 102 LogFetchTestJob* job = new LogFetchTestJob( |
95 FetchSTHTestJob* job = new FetchSTHTestJob( | |
96 response_body_, response_headers_, request, network_delegate); | 103 response_body_, response_headers_, request, network_delegate); |
97 job->set_async_io(async_io_); | 104 job->set_async_io(async_io_); |
98 return job; | 105 return job; |
99 } | 106 } |
100 | 107 |
101 void set_response_body(const std::string& response_body) { | 108 void set_response_body(const std::string& response_body) { |
102 response_body_ = response_body; | 109 response_body_ = response_body; |
103 } | 110 } |
104 | 111 |
105 void set_response_headers(const std::string& response_headers) { | 112 void set_response_headers(const std::string& response_headers) { |
106 response_headers_ = response_headers; | 113 response_headers_ = response_headers; |
107 } | 114 } |
108 | 115 |
109 void set_async_io(bool async_io) { async_io_ = async_io; } | 116 void set_async_io(bool async_io) { async_io_ = async_io; } |
110 | 117 |
118 void set_expected_url(const GURL& url) { expected_url_ = url; } | |
119 | |
111 private: | 120 private: |
112 bool async_io_; | 121 bool async_io_; |
113 std::string response_body_; | 122 std::string response_body_; |
114 std::string response_headers_; | 123 std::string response_headers_; |
115 | 124 |
116 DISALLOW_COPY_AND_ASSIGN(GetSTHResponseHandler); | 125 // Stored for test body to assert on |
126 GURL expected_url_; | |
127 | |
128 DISALLOW_COPY_AND_ASSIGN(LogGetResponseHandler); | |
129 }; | |
130 | |
131 enum InterceptedResultType { | |
132 NOTHING, | |
133 FAILURE, | |
134 STH_FETCH, | |
135 CONSISTENCY_PROOF_FETCH | |
117 }; | 136 }; |
118 | 137 |
119 class RecordFetchCallbackInvocations { | 138 class RecordFetchCallbackInvocations { |
120 public: | 139 public: |
121 RecordFetchCallbackInvocations(bool expect_success) | 140 RecordFetchCallbackInvocations(bool expect_success) |
122 : expect_success_(expect_success), | 141 : expect_success_(expect_success), |
123 invoked_(false), | |
124 net_error_(net::OK), | 142 net_error_(net::OK), |
125 http_response_code_(-1) {} | 143 http_response_code_(-1), |
144 request_type_(NOTHING) {} | |
126 | 145 |
127 void STHFetched(const std::string& log_id, | 146 void STHFetched(const std::string& log_id, |
128 const net::ct::SignedTreeHead& sth) { | 147 const net::ct::SignedTreeHead& sth) { |
129 ASSERT_TRUE(expect_success_); | 148 ASSERT_TRUE(expect_success_); |
130 ASSERT_FALSE(invoked_); | 149 ASSERT_EQ(NOTHING, request_type_); |
131 invoked_ = true; | 150 request_type_ = STH_FETCH; |
132 // If expected to succeed, expecting the known_good STH. | 151 sth_ = sth; |
133 net::ct::SignedTreeHead expected_sth; | 152 log_id_ = log_id; |
134 net::ct::GetSampleSignedTreeHead(&expected_sth); | 153 run_loop_->Quit(); |
154 } | |
135 | 155 |
136 EXPECT_EQ(kLogID, log_id); | 156 void ConsistencyProofFetched( |
137 EXPECT_EQ(expected_sth.version, sth.version); | 157 const std::string& log_id, |
138 EXPECT_EQ(expected_sth.timestamp, sth.timestamp); | 158 const std::vector<std::string>& consistency_proof) { |
139 EXPECT_EQ(expected_sth.tree_size, sth.tree_size); | 159 ASSERT_TRUE(expect_success_); |
140 EXPECT_STREQ(expected_sth.sha256_root_hash, sth.sha256_root_hash); | 160 ASSERT_EQ(NOTHING, request_type_); |
141 EXPECT_EQ(expected_sth.signature.hash_algorithm, | 161 request_type_ = CONSISTENCY_PROOF_FETCH; |
142 sth.signature.hash_algorithm); | 162 consistency_proof_.assign(consistency_proof.begin(), |
143 EXPECT_EQ(expected_sth.signature.signature_algorithm, | 163 consistency_proof.end()); |
144 sth.signature.signature_algorithm); | 164 log_id_ = log_id; |
145 EXPECT_EQ(expected_sth.signature.signature_data, | 165 run_loop_->Quit(); |
146 sth.signature.signature_data); | |
147 } | 166 } |
148 | 167 |
149 void FetchingFailed(const std::string& log_id, | 168 void FetchingFailed(const std::string& log_id, |
150 int net_error, | 169 int net_error, |
151 int http_response_code) { | 170 int http_response_code) { |
152 ASSERT_FALSE(expect_success_); | 171 ASSERT_FALSE(expect_success_); |
153 ASSERT_FALSE(invoked_); | 172 ASSERT_EQ(NOTHING, request_type_); |
154 invoked_ = true; | 173 request_type_ = FAILURE; |
155 net_error_ = net_error; | 174 net_error_ = net_error; |
156 http_response_code_ = http_response_code; | 175 http_response_code_ = http_response_code; |
157 if (net_error_ == net::OK) { | 176 if (net_error_ == net::OK) { |
158 EXPECT_NE(net::HTTP_OK, http_response_code_); | 177 EXPECT_NE(net::HTTP_OK, http_response_code_); |
159 } | 178 } |
179 | |
180 run_loop_->Quit(); | |
160 } | 181 } |
161 | 182 |
162 bool invoked() const { return invoked_; } | 183 InterceptedResultType intercepted_result_type() const { |
184 return request_type_; | |
185 } | |
163 | 186 |
164 int net_error() const { return net_error_; } | 187 int net_error() const { return net_error_; } |
165 | 188 |
166 int http_response_code() const { return http_response_code_; } | 189 int http_response_code() const { return http_response_code_; } |
167 | 190 |
191 const net::ct::SignedTreeHead& intercepted_sth() const { return sth_; } | |
192 | |
193 const std::string& intercepted_log_id() const { return log_id_; } | |
194 | |
195 const std::vector<std::string>& intercepted_proof() const { | |
196 return consistency_proof_; | |
197 } | |
198 | |
199 void SetRunLoop(base::RunLoop* run_loop) { run_loop_ = run_loop; } | |
Ryan Sleevi
2016/01/12 22:15:01
Suggestion: You could just make your fetch callbac
Eran Messeri
2016/01/18 15:50:56
Done.
| |
200 | |
168 private: | 201 private: |
169 const bool expect_success_; | 202 const bool expect_success_; |
170 bool invoked_; | |
171 int net_error_; | 203 int net_error_; |
172 int http_response_code_; | 204 int http_response_code_; |
205 InterceptedResultType request_type_; | |
206 net::ct::SignedTreeHead sth_; | |
207 std::string log_id_; | |
208 std::vector<std::string> consistency_proof_; | |
209 | |
210 base::RunLoop* run_loop_; | |
173 }; | 211 }; |
174 | 212 |
175 class LogProofFetcherTest : public ::testing::Test { | 213 class LogProofFetcherTest : public ::testing::Test { |
176 public: | 214 public: |
177 LogProofFetcherTest() | 215 LogProofFetcherTest() |
178 : log_url_(base::StringPrintf("%s://%s/%s/", | 216 : log_url_(base::StringPrintf("%s://%s/%s/", |
179 kLogSchema, | 217 kLogSchema, |
180 kLogHost, | 218 kLogHost, |
181 kLogPathPrefix)) { | 219 kLogPathPrefix)) { |
182 scoped_ptr<GetSTHResponseHandler> handler(new GetSTHResponseHandler()); | 220 scoped_ptr<LogGetResponseHandler> handler(new LogGetResponseHandler()); |
183 handler_ = handler.get(); | 221 handler_ = handler.get(); |
184 | 222 |
185 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( | 223 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( |
186 kLogSchema, kLogHost, handler.Pass()); | 224 kLogSchema, kLogHost, handler.Pass()); |
187 | 225 |
188 fetcher_.reset(new LogProofFetcher(&context_)); | 226 fetcher_.reset(new LogProofFetcher(&context_)); |
189 } | 227 } |
190 | 228 |
191 ~LogProofFetcherTest() override { | 229 ~LogProofFetcherTest() override { |
192 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(kLogSchema, | 230 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(kLogSchema, |
193 kLogHost); | 231 kLogHost); |
194 } | 232 } |
195 | 233 |
196 protected: | 234 protected: |
197 void SetValidSTHJSONResponse() { | 235 void SetValidSTHJSONResponse() { |
198 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); | 236 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); |
199 handler_->set_response_body(sth_json_reply_data); | 237 handler_->set_response_body(sth_json_reply_data); |
238 handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth")); | |
200 } | 239 } |
201 | 240 |
202 void RunFetcherWithCallback(RecordFetchCallbackInvocations* callback) { | 241 void RunFetcherWithCallback(RecordFetchCallbackInvocations* callback) { |
242 callback->SetRunLoop(&run_loop_); | |
203 fetcher_->FetchSignedTreeHead( | 243 fetcher_->FetchSignedTreeHead( |
204 log_url_, kLogID, | 244 log_url_, kLogID, |
205 base::Bind(&RecordFetchCallbackInvocations::STHFetched, | 245 base::Bind(&RecordFetchCallbackInvocations::STHFetched, |
206 base::Unretained(callback)), | 246 base::Unretained(callback)), |
207 base::Bind(&RecordFetchCallbackInvocations::FetchingFailed, | 247 base::Bind(&RecordFetchCallbackInvocations::FetchingFailed, |
208 base::Unretained(callback))); | 248 base::Unretained(callback))); |
209 message_loop_.RunUntilIdle(); | 249 run_loop_.Run(); |
210 } | 250 } |
211 | 251 |
252 void RunGetConsistencyFetcherWithCallback( | |
253 RecordFetchCallbackInvocations* callback) { | |
Ryan Sleevi
2016/01/12 22:15:01
NAMING: This is more a remark about the test in ge
Eran Messeri
2016/01/18 15:50:56
Acknowledged - Do I understand correctly that you
| |
254 const uint64_t kOldTree = 5; | |
255 const uint64_t kNewTree = 8; | |
256 handler_->set_expected_url(log_url_.Resolve(base::StringPrintf( | |
257 "ct/v1/get-sth-consistency?first=%" PRIu64 "&second=%" PRIu64, kOldTree, | |
258 kNewTree))); | |
259 callback->SetRunLoop(&run_loop_); | |
260 fetcher_->FetchConsistencyProof( | |
261 log_url_, kLogID, kOldTree, kNewTree, | |
262 base::Bind(&RecordFetchCallbackInvocations::ConsistencyProofFetched, | |
263 base::Unretained(callback)), | |
264 base::Bind(&RecordFetchCallbackInvocations::FetchingFailed, | |
265 base::Unretained(callback))); | |
266 run_loop_.Run(); | |
267 } | |
268 | |
269 void VerifyReceivedSTH(const std::string& log_id, | |
270 const net::ct::SignedTreeHead& sth) { | |
271 net::ct::SignedTreeHead expected_sth; | |
272 net::ct::GetSampleSignedTreeHead(&expected_sth); | |
273 | |
274 EXPECT_EQ(kLogID, log_id); | |
275 EXPECT_EQ(expected_sth.version, sth.version); | |
276 EXPECT_EQ(expected_sth.timestamp, sth.timestamp); | |
277 EXPECT_EQ(expected_sth.tree_size, sth.tree_size); | |
278 EXPECT_STREQ(expected_sth.sha256_root_hash, sth.sha256_root_hash); | |
279 EXPECT_EQ(expected_sth.signature.hash_algorithm, | |
280 sth.signature.hash_algorithm); | |
281 EXPECT_EQ(expected_sth.signature.signature_algorithm, | |
282 sth.signature.signature_algorithm); | |
283 EXPECT_EQ(expected_sth.signature.signature_data, | |
284 sth.signature.signature_data); | |
285 } | |
286 | |
287 void VerifyConsistencyProof( | |
288 const std::string& log_id, | |
289 const std::vector<std::string>& consistency_proof) { | |
290 EXPECT_EQ(kLogID, log_id); | |
291 EXPECT_EQ(kDummyConsistencyProofLength, consistency_proof.size()); | |
292 for (uint64_t i = 0; i < kDummyConsistencyProofLength; ++i) { | |
293 EXPECT_EQ(GetDummyConsistencyProofNode(i), consistency_proof[i]) | |
294 << " node: " << i; | |
295 } | |
296 } | |
297 | |
298 // The |message_loop_|, while seemingly unused, is necessary | |
299 // for URL request interception. That is the message loop that | |
300 // will be used by the RunLoop. | |
212 base::MessageLoopForIO message_loop_; | 301 base::MessageLoopForIO message_loop_; |
302 base::RunLoop run_loop_; | |
213 net::TestURLRequestContext context_; | 303 net::TestURLRequestContext context_; |
214 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_; | 304 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_; |
215 scoped_ptr<LogProofFetcher> fetcher_; | 305 scoped_ptr<LogProofFetcher> fetcher_; |
216 const GURL log_url_; | 306 const GURL log_url_; |
217 GetSTHResponseHandler* handler_; | 307 LogGetResponseHandler* handler_; |
218 }; | 308 }; |
219 | 309 |
220 TEST_F(LogProofFetcherTest, TestValidGetReply) { | 310 TEST_F(LogProofFetcherTest, TestValidGetReply) { |
221 SetValidSTHJSONResponse(); | 311 SetValidSTHJSONResponse(); |
222 | 312 |
223 RecordFetchCallbackInvocations callback(true); | 313 RecordFetchCallbackInvocations callback(true); |
224 | 314 |
225 RunFetcherWithCallback(&callback); | 315 RunFetcherWithCallback(&callback); |
226 | 316 |
227 ASSERT_TRUE(callback.invoked()); | 317 ASSERT_EQ(STH_FETCH, callback.intercepted_result_type()); |
318 VerifyReceivedSTH(callback.intercepted_log_id(), callback.intercepted_sth()); | |
228 } | 319 } |
229 | 320 |
230 TEST_F(LogProofFetcherTest, TestValidGetReplyAsyncIO) { | 321 TEST_F(LogProofFetcherTest, TestValidGetReplyAsyncIO) { |
231 SetValidSTHJSONResponse(); | 322 SetValidSTHJSONResponse(); |
232 handler_->set_async_io(true); | 323 handler_->set_async_io(true); |
233 | 324 |
234 RecordFetchCallbackInvocations callback(true); | 325 RecordFetchCallbackInvocations callback(true); |
235 RunFetcherWithCallback(&callback); | 326 RunFetcherWithCallback(&callback); |
236 | 327 |
237 ASSERT_TRUE(callback.invoked()); | 328 ASSERT_EQ(STH_FETCH, callback.intercepted_result_type()); |
329 VerifyReceivedSTH(callback.intercepted_log_id(), callback.intercepted_sth()); | |
238 } | 330 } |
239 | 331 |
240 TEST_F(LogProofFetcherTest, TestInvalidGetReplyIncompleteJSON) { | 332 TEST_F(LogProofFetcherTest, TestInvalidGetReplyIncompleteJSON) { |
241 std::string sth_json_reply_data = net::ct::CreateSignedTreeHeadJsonString( | 333 std::string sth_json_reply_data = net::ct::CreateSignedTreeHeadJsonString( |
242 21 /* tree_size */, 123456u /* timestamp */, std::string(), | 334 21 /* tree_size */, 123456u /* timestamp */, std::string(), |
243 std::string()); | 335 std::string()); |
244 handler_->set_response_body(sth_json_reply_data); | 336 handler_->set_response_body(sth_json_reply_data); |
337 handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth")); | |
245 | 338 |
246 RecordFetchCallbackInvocations callback(false); | 339 RecordFetchCallbackInvocations callback(false); |
247 RunFetcherWithCallback(&callback); | 340 RunFetcherWithCallback(&callback); |
248 | 341 |
249 ASSERT_TRUE(callback.invoked()); | 342 ASSERT_EQ(FAILURE, callback.intercepted_result_type()); |
250 EXPECT_EQ(net::ERR_CT_STH_INCOMPLETE, callback.net_error()); | 343 EXPECT_EQ(net::ERR_CT_STH_INCOMPLETE, callback.net_error()); |
251 } | 344 } |
252 | 345 |
253 TEST_F(LogProofFetcherTest, TestInvalidGetReplyInvalidJSON) { | 346 TEST_F(LogProofFetcherTest, TestInvalidGetReplyInvalidJSON) { |
254 std::string sth_json_reply_data = "{\"tree_size\":21,\"timestamp\":}"; | 347 std::string sth_json_reply_data = "{\"tree_size\":21,\"timestamp\":}"; |
255 handler_->set_response_body(sth_json_reply_data); | 348 handler_->set_response_body(sth_json_reply_data); |
349 handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth")); | |
256 | 350 |
257 RecordFetchCallbackInvocations callback(false); | 351 RecordFetchCallbackInvocations callback(false); |
258 RunFetcherWithCallback(&callback); | 352 RunFetcherWithCallback(&callback); |
259 | 353 |
260 ASSERT_TRUE(callback.invoked()); | 354 ASSERT_EQ(FAILURE, callback.intercepted_result_type()); |
261 EXPECT_EQ(net::ERR_CT_STH_PARSING_FAILED, callback.net_error()); | 355 EXPECT_EQ(net::ERR_CT_STH_PARSING_FAILED, callback.net_error()); |
262 } | 356 } |
263 | 357 |
264 TEST_F(LogProofFetcherTest, TestLogReplyIsTooLong) { | 358 TEST_F(LogProofFetcherTest, TestLogReplyIsTooLong) { |
265 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); | 359 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); |
266 // Add kMaxLogResponseSizeInBytes to make sure the response is too big. | 360 // Add kMaxLogResponseSizeInBytes to make sure the response is too big. |
267 sth_json_reply_data.append( | 361 sth_json_reply_data.append( |
268 std::string(LogProofFetcher::kMaxLogResponseSizeInBytes, ' ')); | 362 std::string(LogProofFetcher::kMaxLogResponseSizeInBytes, ' ')); |
269 handler_->set_response_body(sth_json_reply_data); | 363 handler_->set_response_body(sth_json_reply_data); |
364 handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth")); | |
270 | 365 |
271 RecordFetchCallbackInvocations callback(false); | 366 RecordFetchCallbackInvocations callback(false); |
272 RunFetcherWithCallback(&callback); | 367 RunFetcherWithCallback(&callback); |
273 | 368 |
274 ASSERT_TRUE(callback.invoked()); | 369 ASSERT_EQ(FAILURE, callback.intercepted_result_type()); |
275 EXPECT_EQ(net::ERR_FILE_TOO_BIG, callback.net_error()); | 370 EXPECT_EQ(net::ERR_FILE_TOO_BIG, callback.net_error()); |
276 EXPECT_EQ(net::HTTP_OK, callback.http_response_code()); | 371 EXPECT_EQ(net::HTTP_OK, callback.http_response_code()); |
277 } | 372 } |
278 | 373 |
279 TEST_F(LogProofFetcherTest, TestLogReplyIsExactlyMaxSize) { | 374 TEST_F(LogProofFetcherTest, TestLogReplyIsExactlyMaxSize) { |
280 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); | 375 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); |
281 // Extend the reply to be exactly kMaxLogResponseSizeInBytes. | 376 // Extend the reply to be exactly kMaxLogResponseSizeInBytes. |
282 sth_json_reply_data.append(std::string( | 377 sth_json_reply_data.append(std::string( |
283 LogProofFetcher::kMaxLogResponseSizeInBytes - sth_json_reply_data.size(), | 378 LogProofFetcher::kMaxLogResponseSizeInBytes - sth_json_reply_data.size(), |
284 ' ')); | 379 ' ')); |
285 handler_->set_response_body(sth_json_reply_data); | 380 handler_->set_response_body(sth_json_reply_data); |
381 handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth")); | |
286 | 382 |
287 RecordFetchCallbackInvocations callback(true); | 383 RecordFetchCallbackInvocations callback(true); |
288 RunFetcherWithCallback(&callback); | 384 RunFetcherWithCallback(&callback); |
289 | 385 |
290 ASSERT_TRUE(callback.invoked()); | 386 ASSERT_EQ(STH_FETCH, callback.intercepted_result_type()); |
387 VerifyReceivedSTH(callback.intercepted_log_id(), callback.intercepted_sth()); | |
291 } | 388 } |
292 | 389 |
293 TEST_F(LogProofFetcherTest, TestLogRepliesWithHttpError) { | 390 TEST_F(LogProofFetcherTest, TestLogRepliesWithHttpError) { |
294 handler_->set_response_headers( | 391 handler_->set_response_headers(std::string( |
295 std::string(kGetSTHNotFoundHeaders, arraysize(kGetSTHNotFoundHeaders))); | 392 kGetResponseNotFoundHeaders, arraysize(kGetResponseNotFoundHeaders))); |
393 handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth")); | |
296 | 394 |
297 RecordFetchCallbackInvocations callback(false); | 395 RecordFetchCallbackInvocations callback(false); |
298 RunFetcherWithCallback(&callback); | 396 RunFetcherWithCallback(&callback); |
299 | 397 |
300 ASSERT_TRUE(callback.invoked()); | 398 ASSERT_EQ(FAILURE, callback.intercepted_result_type()); |
301 EXPECT_EQ(net::OK, callback.net_error()); | 399 EXPECT_EQ(net::OK, callback.net_error()); |
302 EXPECT_EQ(net::HTTP_NOT_FOUND, callback.http_response_code()); | 400 EXPECT_EQ(net::HTTP_NOT_FOUND, callback.http_response_code()); |
303 } | 401 } |
304 | 402 |
403 TEST_F(LogProofFetcherTest, TestValidGetConsistencyValidReply) { | |
404 std::vector<std::string> proof; | |
405 for (uint64_t i = 0; i < kDummyConsistencyProofLength; ++i) | |
406 proof.push_back(GetDummyConsistencyProofNode(i)); | |
407 | |
408 std::string consistency_proof_reply_data = | |
409 net::ct::CreateConsistencyProofJsonString(proof); | |
410 handler_->set_response_body(consistency_proof_reply_data); | |
411 | |
412 RecordFetchCallbackInvocations callback(true); | |
413 RunGetConsistencyFetcherWithCallback(&callback); | |
414 | |
415 ASSERT_EQ(CONSISTENCY_PROOF_FETCH, callback.intercepted_result_type()); | |
416 VerifyConsistencyProof(callback.intercepted_log_id(), | |
417 callback.intercepted_proof()); | |
418 } | |
419 | |
420 TEST_F(LogProofFetcherTest, TestInvalidGetConsistencyReplyInvalidJSON) { | |
421 std::string consistency_proof_reply_data = "{\"consistency\": [1,2]}"; | |
422 handler_->set_response_body(consistency_proof_reply_data); | |
423 | |
424 RecordFetchCallbackInvocations callback(false); | |
425 RunGetConsistencyFetcherWithCallback(&callback); | |
426 | |
427 ASSERT_EQ(FAILURE, callback.intercepted_result_type()); | |
428 EXPECT_EQ(net::ERR_CT_CONSISTENCY_PROOF_PARSING_FAILED, callback.net_error()); | |
429 EXPECT_EQ(net::HTTP_OK, callback.http_response_code()); | |
430 } | |
431 | |
305 } // namespace | 432 } // namespace |
306 | 433 |
307 } // namespace certificate_transparency | 434 } // namespace certificate_transparency |
OLD | NEW |