| OLD | NEW |
| (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/net_errors.h" |
| 11 #include "net/base/network_delegate.h" |
| 12 #include "net/cert/signed_tree_head.h" |
| 13 #include "net/http/http_status_code.h" |
| 14 #include "net/test/ct_test_util.h" |
| 15 #include "net/url_request/url_request_context.h" |
| 16 #include "net/url_request/url_request_filter.h" |
| 17 #include "net/url_request/url_request_interceptor.h" |
| 18 #include "net/url_request/url_request_job.h" |
| 19 #include "net/url_request/url_request_test_job.h" |
| 20 #include "net/url_request/url_request_test_util.h" |
| 21 |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 |
| 24 namespace certificate_transparency { |
| 25 |
| 26 namespace { |
| 27 |
| 28 const char kGetSTHHeaders[] = |
| 29 "HTTP/1.1 200 OK\0" |
| 30 "Content-Type: application/json; charset=ISO-8859-1\0" |
| 31 "\0"; |
| 32 |
| 33 const char kLogSchema[] = "https"; |
| 34 const char kLogURL[] = "ct.log.example.com"; |
| 35 |
| 36 std::string GetLogID() { |
| 37 return std::string("some_id"); |
| 38 } |
| 39 |
| 40 class FetchSTHTestJob : public net::URLRequestTestJob { |
| 41 public: |
| 42 FetchSTHTestJob(const std::string& get_sth_data, |
| 43 net::URLRequest* request, |
| 44 net::NetworkDelegate* network_delegate) |
| 45 : URLRequestTestJob(request, |
| 46 network_delegate, |
| 47 std::string(kGetSTHHeaders), |
| 48 get_sth_data, |
| 49 true), |
| 50 async_io_(false), |
| 51 response_code_(net::HTTP_OK) {} |
| 52 |
| 53 void SetAsyncIO(bool async_io) { async_io_ = async_io; } |
| 54 |
| 55 void SetResponseCode(int response_code) { response_code_ = response_code; } |
| 56 |
| 57 int GetResponseCode() const override { return response_code_; } |
| 58 |
| 59 protected: |
| 60 bool NextReadAsync() override { return async_io_; } |
| 61 |
| 62 private: |
| 63 bool async_io_; |
| 64 int response_code_; |
| 65 |
| 66 ~FetchSTHTestJob() override {} |
| 67 DISALLOW_COPY_AND_ASSIGN(FetchSTHTestJob); |
| 68 }; |
| 69 |
| 70 class GetSTHResponseHandler : public net::URLRequestInterceptor { |
| 71 public: |
| 72 GetSTHResponseHandler() |
| 73 : async_io_(false), response_data_(""), response_code_(net::HTTP_OK) {} |
| 74 ~GetSTHResponseHandler() override {} |
| 75 |
| 76 // URLRequestInterceptor implementation: |
| 77 net::URLRequestJob* MaybeInterceptRequest( |
| 78 net::URLRequest* request, |
| 79 net::NetworkDelegate* network_delegate) const override { |
| 80 FetchSTHTestJob* job = |
| 81 new FetchSTHTestJob(response_data_, request, network_delegate); |
| 82 job->SetAsyncIO(async_io_); |
| 83 job->SetResponseCode(response_code_); |
| 84 return job; |
| 85 } |
| 86 |
| 87 void SetResponseData(std::string response_data) { |
| 88 response_data_ = response_data; |
| 89 } |
| 90 |
| 91 void SetAsyncIO(bool async_io) { async_io_ = async_io; } |
| 92 |
| 93 void SetResponseCode(int response_code) { response_code_ = response_code; } |
| 94 |
| 95 private: |
| 96 bool async_io_; |
| 97 std::string response_data_; |
| 98 int response_code_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(GetSTHResponseHandler); |
| 101 }; |
| 102 |
| 103 class RecordFetchCallbackInvocations { |
| 104 public: |
| 105 RecordFetchCallbackInvocations() : invoked_(false), failed_(false) {} |
| 106 |
| 107 virtual void STHFetched(const std::string& log_id, |
| 108 const net::ct::SignedTreeHead& sth) { |
| 109 invoked_ = true; |
| 110 } |
| 111 |
| 112 void FetchingFailed(const std::string& log_id, |
| 113 int net_error, |
| 114 int http_response_code) { |
| 115 invoked_ = true; |
| 116 failed_ = true; |
| 117 net_error_ = net_error; |
| 118 http_response_code_ = http_response_code; |
| 119 } |
| 120 |
| 121 bool invoked() { return invoked_; } |
| 122 |
| 123 bool failed() { return failed_; } |
| 124 |
| 125 int net_error() { return net_error_; } |
| 126 |
| 127 int http_response_code() { return http_response_code_; } |
| 128 |
| 129 private: |
| 130 bool invoked_; |
| 131 bool failed_; |
| 132 int net_error_; |
| 133 int http_response_code_; |
| 134 }; |
| 135 |
| 136 class ExpectedSuccessCallback : public RecordFetchCallbackInvocations { |
| 137 public: |
| 138 ExpectedSuccessCallback() { net::ct::GetSampleSignedTreeHead(&known_sth_); } |
| 139 |
| 140 explicit ExpectedSuccessCallback(const net::ct::SignedTreeHead& sth) |
| 141 : known_sth_(sth) {} |
| 142 |
| 143 void STHFetched(const std::string& log_id, |
| 144 const net::ct::SignedTreeHead& sth) override { |
| 145 ASSERT_EQ(GetLogID(), log_id); |
| 146 ASSERT_EQ(sth.version, known_sth_.version); |
| 147 ASSERT_EQ(sth.timestamp, known_sth_.timestamp); |
| 148 ASSERT_EQ(sth.tree_size, known_sth_.tree_size); |
| 149 ASSERT_STREQ(sth.sha256_root_hash, known_sth_.sha256_root_hash); |
| 150 ASSERT_EQ(sth.signature.hash_algorithm, |
| 151 known_sth_.signature.hash_algorithm); |
| 152 ASSERT_EQ(sth.signature.signature_algorithm, |
| 153 known_sth_.signature.signature_algorithm); |
| 154 ASSERT_EQ(sth.signature.signature_data, |
| 155 known_sth_.signature.signature_data); |
| 156 |
| 157 RecordFetchCallbackInvocations::STHFetched(log_id, sth); |
| 158 } |
| 159 |
| 160 private: |
| 161 net::ct::SignedTreeHead known_sth_; |
| 162 }; |
| 163 |
| 164 class LogProofFetcherTest : public ::testing::Test { |
| 165 public: |
| 166 LogProofFetcherTest() |
| 167 : context_(true), |
| 168 log_url_(std::string(kLogSchema) + "://" + std::string(kLogURL) + "/") { |
| 169 context_.Init(); |
| 170 } |
| 171 |
| 172 void SetUp() override { |
| 173 scoped_ptr<GetSTHResponseHandler> handler(new GetSTHResponseHandler()); |
| 174 handler_ = handler.get(); |
| 175 |
| 176 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( |
| 177 kLogSchema, kLogURL, handler.Pass()); |
| 178 |
| 179 fetcher_.reset(new LogProofFetcher(&context_)); |
| 180 } |
| 181 |
| 182 void TearDown() override { |
| 183 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(kLogSchema, |
| 184 kLogURL); |
| 185 } |
| 186 |
| 187 protected: |
| 188 void SetValidSTHJSONResponse() { |
| 189 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); |
| 190 handler_->SetResponseData(sth_json_reply_data); |
| 191 } |
| 192 |
| 193 void RunFetcherWithCallback(RecordFetchCallbackInvocations* callback) { |
| 194 fetcher_->FetchSignedTreeHead( |
| 195 log_url_, GetLogID(), |
| 196 base::Bind(&RecordFetchCallbackInvocations::STHFetched, |
| 197 base::Unretained(callback)), |
| 198 base::Bind(&RecordFetchCallbackInvocations::FetchingFailed, |
| 199 base::Unretained(callback))); |
| 200 message_loop_.RunUntilIdle(); |
| 201 } |
| 202 |
| 203 base::MessageLoopForIO message_loop_; |
| 204 net::TestURLRequestContext context_; |
| 205 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_; |
| 206 scoped_ptr<LogProofFetcher> fetcher_; |
| 207 GURL log_url_; |
| 208 GetSTHResponseHandler* handler_; |
| 209 }; |
| 210 |
| 211 TEST_F(LogProofFetcherTest, TestValidGetSTHReply) { |
| 212 SetValidSTHJSONResponse(); |
| 213 |
| 214 ExpectedSuccessCallback cb; |
| 215 RunFetcherWithCallback(&cb); |
| 216 |
| 217 ASSERT_TRUE(cb.invoked()); |
| 218 ASSERT_FALSE(cb.failed()); |
| 219 } |
| 220 |
| 221 TEST_F(LogProofFetcherTest, TestValidGetSTHReplyAsyncIO) { |
| 222 SetValidSTHJSONResponse(); |
| 223 handler_->SetAsyncIO(true); |
| 224 |
| 225 ExpectedSuccessCallback cb; |
| 226 RunFetcherWithCallback(&cb); |
| 227 |
| 228 ASSERT_TRUE(cb.invoked()); |
| 229 ASSERT_FALSE(cb.failed()); |
| 230 } |
| 231 |
| 232 TEST_F(LogProofFetcherTest, TestInvalidGetSTHReplyIncompleteSTH) { |
| 233 std::string sth_json_reply_data = net::ct::CreateSignedTreeHeadJsonString( |
| 234 21 /* tree_size */, 123456u /* timestamp */, std::string(""), |
| 235 std::string("")); |
| 236 handler_->SetResponseData(sth_json_reply_data); |
| 237 |
| 238 RecordFetchCallbackInvocations cb; |
| 239 RunFetcherWithCallback(&cb); |
| 240 |
| 241 ASSERT_TRUE(cb.invoked()); |
| 242 ASSERT_TRUE(cb.failed()); |
| 243 ASSERT_EQ(net::ERR_CT_STH_INCOMPLETE, cb.net_error()); |
| 244 } |
| 245 |
| 246 TEST_F(LogProofFetcherTest, TestInvalidGetSTHReplyInvalidJSON) { |
| 247 std::string sth_json_reply_data = "{\"tree_size\":21,\"timestamp\":}"; |
| 248 handler_->SetResponseData(sth_json_reply_data); |
| 249 |
| 250 RecordFetchCallbackInvocations cb; |
| 251 RunFetcherWithCallback(&cb); |
| 252 |
| 253 ASSERT_TRUE(cb.invoked()); |
| 254 ASSERT_TRUE(cb.failed()); |
| 255 ASSERT_EQ(net::ERR_CT_STH_PARSING_FAILED, cb.net_error()); |
| 256 } |
| 257 |
| 258 TEST_F(LogProofFetcherTest, TestLogReplyIsTooLong) { |
| 259 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); |
| 260 // kMaxLogResponseSizeInBytes is 600 - add that much to make sure the response |
| 261 // is too big. |
| 262 sth_json_reply_data.append(std::string(600, ' ')); |
| 263 handler_->SetResponseData(sth_json_reply_data); |
| 264 |
| 265 RecordFetchCallbackInvocations cb; |
| 266 RunFetcherWithCallback(&cb); |
| 267 |
| 268 ASSERT_TRUE(cb.invoked()); |
| 269 ASSERT_TRUE(cb.failed()); |
| 270 ASSERT_EQ(net::ERR_FILE_TOO_BIG, cb.net_error()); |
| 271 ASSERT_EQ(net::HTTP_OK, cb.http_response_code()); |
| 272 } |
| 273 |
| 274 TEST_F(LogProofFetcherTest, TestLogReplyIsExactlyMaxSize) { |
| 275 std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson(); |
| 276 // Extend the reply to be exactly kMaxLogResponseSizeInBytes. |
| 277 sth_json_reply_data.append( |
| 278 std::string(600 - sth_json_reply_data.size(), ' ')); |
| 279 handler_->SetResponseData(sth_json_reply_data); |
| 280 |
| 281 ExpectedSuccessCallback cb; |
| 282 RunFetcherWithCallback(&cb); |
| 283 |
| 284 ASSERT_TRUE(cb.invoked()); |
| 285 ASSERT_FALSE(cb.failed()); |
| 286 } |
| 287 |
| 288 TEST_F(LogProofFetcherTest, TestLogRepliesWithHttpError) { |
| 289 handler_->SetResponseCode(net::HTTP_NOT_FOUND); |
| 290 |
| 291 RecordFetchCallbackInvocations cb; |
| 292 RunFetcherWithCallback(&cb); |
| 293 |
| 294 ASSERT_TRUE(cb.invoked()); |
| 295 ASSERT_TRUE(cb.failed()); |
| 296 ASSERT_EQ(net::OK, cb.net_error()); |
| 297 ASSERT_EQ(net::HTTP_NOT_FOUND, cb.http_response_code()); |
| 298 } |
| 299 |
| 300 } // namespace |
| 301 |
| 302 } // namespace certificate_transparency |
| OLD | NEW |