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