File: log_proof_fetcher_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (441 lines) | stat: -rw-r--r-- 15,341 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/certificate_transparency/log_proof_fetcher.h"

#include <memory>
#include <string>
#include <utility>

#include "base/format_macros.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "components/safe_json/testing_json_parser.h"
#include "net/base/net_errors.h"
#include "net/base/network_delegate.h"
#include "net/cert/signed_tree_head.h"
#include "net/http/http_status_code.h"
#include "net/test/ct_test_util.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_filter.h"
#include "net/url_request/url_request_interceptor.h"
#include "net/url_request/url_request_job.h"
#include "net/url_request/url_request_test_job.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace certificate_transparency {

namespace {

const char kGetResponseHeaders[] =
    "HTTP/1.1 200 OK\n"
    "Content-Type: application/json; charset=ISO-8859-1\n";

const char kGetResponseNotFoundHeaders[] =
    "HTTP/1.1 404 Not Found\n"
    "Content-Type: text/html; charset=iso-8859-1\n";

const char kLogSchema[] = "https";
const char kLogHost[] = "ct.log.example.com";
const char kLogPathPrefix[] = "somelog";
const char kLogID[] = "some_id";

// Gets a dummy consistency proof for the given |node_id|.
std::string GetDummyConsistencyProofNode(uint64_t node_id) {
  // Take the low 8 bits and repeat them as a string. This
  // has no special meaning, other than making it easier to
  // debug which consistency proof was used.
  return std::string(32, static_cast<char>(node_id));
}

// Number of nodes in a dummy consistency proof.
const size_t kDummyConsistencyProofNumNodes = 4;

class LogFetchTestJob : public net::URLRequestTestJob {
 public:
  LogFetchTestJob(const std::string& get_log_data,
                  const std::string& get_log_headers,
                  net::URLRequest* request,
                  net::NetworkDelegate* network_delegate)
      : URLRequestTestJob(request,
                          network_delegate,
                          get_log_headers,
                          get_log_data,
                          true),
        async_io_(false) {}

  void set_async_io(bool async_io) { async_io_ = async_io; }

 private:
  ~LogFetchTestJob() override {}

  bool NextReadAsync() override {
    // Response with indication of async IO only once, otherwise the final
    // Read would (incorrectly) be classified as async, causing the
    // URLRequestJob to try reading another time and failing on a CHECK
    // that the raw_read_buffer_ is not null.
    // According to mmenke@, this is a bug in the URLRequestTestJob code.
    // TODO(eranm): Once said bug is fixed, switch most tests to using async
    // IO.
    if (async_io_) {
      async_io_ = false;
      return true;
    }
    return false;
  }

  bool async_io_;

  DISALLOW_COPY_AND_ASSIGN(LogFetchTestJob);
};

class LogGetResponseHandler : public net::URLRequestInterceptor {
 public:
  LogGetResponseHandler()
      : async_io_(false),
        response_headers_(
            std::string(kGetResponseHeaders, arraysize(kGetResponseHeaders))) {}
  ~LogGetResponseHandler() override {}

  // URLRequestInterceptor implementation:
  net::URLRequestJob* MaybeInterceptRequest(
      net::URLRequest* request,
      net::NetworkDelegate* network_delegate) const override {
    EXPECT_EQ(expected_url_, request->url());

    LogFetchTestJob* job = new LogFetchTestJob(
        response_body_, response_headers_, request, network_delegate);
    job->set_async_io(async_io_);
    return job;
  }

  void set_response_body(const std::string& response_body) {
    response_body_ = response_body;
  }

  void set_response_headers(const std::string& response_headers) {
    response_headers_ = response_headers;
  }

  void set_async_io(bool async_io) { async_io_ = async_io; }

  void set_expected_url(const GURL& url) { expected_url_ = url; }

 private:
  bool async_io_;
  std::string response_body_;
  std::string response_headers_;

  // Stored for test body to assert on
  GURL expected_url_;

  DISALLOW_COPY_AND_ASSIGN(LogGetResponseHandler);
};

enum InterceptedResultType {
  NOTHING,
  FAILURE,
  STH_FETCH,
  CONSISTENCY_PROOF_FETCH
};

class RecordFetchCallbackInvocations {
 public:
  RecordFetchCallbackInvocations(bool expect_success)
      : expect_success_(expect_success),
        net_error_(net::OK),
        http_response_code_(-1),
        request_type_(NOTHING) {}

  void STHFetched(base::Closure quit_closure,
                  const std::string& log_id,
                  const net::ct::SignedTreeHead& sth) {
    ASSERT_TRUE(expect_success_);
    ASSERT_EQ(NOTHING, request_type_);
    request_type_ = STH_FETCH;
    sth_ = sth;
    log_id_ = log_id;
    quit_closure.Run();
  }

  void ConsistencyProofFetched(
      base::Closure quit_closure,
      const std::string& log_id,
      const std::vector<std::string>& consistency_proof) {
    ASSERT_TRUE(expect_success_);
    ASSERT_EQ(NOTHING, request_type_);
    request_type_ = CONSISTENCY_PROOF_FETCH;
    consistency_proof_.assign(consistency_proof.begin(),
                              consistency_proof.end());
    log_id_ = log_id;
    quit_closure.Run();
  }

  void FetchingFailed(base::Closure quit_closure,
                      const std::string& log_id,
                      int net_error,
                      int http_response_code) {
    ASSERT_FALSE(expect_success_);
    ASSERT_EQ(NOTHING, request_type_);
    request_type_ = FAILURE;
    net_error_ = net_error;
    http_response_code_ = http_response_code;
    if (net_error_ == net::OK) {
      EXPECT_NE(net::HTTP_OK, http_response_code_);
    }

    quit_closure.Run();
  }

  InterceptedResultType intercepted_result_type() const {
    return request_type_;
  }

  int net_error() const { return net_error_; }

  int http_response_code() const { return http_response_code_; }

  const net::ct::SignedTreeHead& intercepted_sth() const { return sth_; }

  const std::string& intercepted_log_id() const { return log_id_; }

  const std::vector<std::string>& intercepted_proof() const {
    return consistency_proof_;
  }

 private:
  const bool expect_success_;
  int net_error_;
  int http_response_code_;
  InterceptedResultType request_type_;
  net::ct::SignedTreeHead sth_;
  std::string log_id_;
  std::vector<std::string> consistency_proof_;
};

class LogProofFetcherTest : public ::testing::Test {
 public:
  LogProofFetcherTest()
      : log_url_(base::StringPrintf("%s://%s/%s/",
                                    kLogSchema,
                                    kLogHost,
                                    kLogPathPrefix)) {
    std::unique_ptr<LogGetResponseHandler> handler(new LogGetResponseHandler());
    handler_ = handler.get();

    net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
        kLogSchema, kLogHost, std::move(handler));

    fetcher_.reset(new LogProofFetcher(&context_));
  }

  ~LogProofFetcherTest() override {
    net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(kLogSchema,
                                                                kLogHost);
  }

 protected:
  void SetValidSTHJSONResponse() {
    std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson();
    handler_->set_response_body(sth_json_reply_data);
    handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth"));
  }

  void RunFetcherWithCallback(RecordFetchCallbackInvocations* callback) {
    fetcher_->FetchSignedTreeHead(
        log_url_, kLogID,
        base::Bind(&RecordFetchCallbackInvocations::STHFetched,
                   base::Unretained(callback), run_loop_.QuitClosure()),
        base::Bind(&RecordFetchCallbackInvocations::FetchingFailed,
                   base::Unretained(callback), run_loop_.QuitClosure()));
    run_loop_.Run();
  }

  void RunGetConsistencyFetcherWithCallback(
      RecordFetchCallbackInvocations* callback) {
    const uint64_t kOldTree = 5;
    const uint64_t kNewTree = 8;
    handler_->set_expected_url(log_url_.Resolve(base::StringPrintf(
        "ct/v1/get-sth-consistency?first=%" PRIu64 "&second=%" PRIu64, kOldTree,
        kNewTree)));
    fetcher_->FetchConsistencyProof(
        log_url_, kLogID, kOldTree, kNewTree,
        base::Bind(&RecordFetchCallbackInvocations::ConsistencyProofFetched,
                   base::Unretained(callback), run_loop_.QuitClosure()),
        base::Bind(&RecordFetchCallbackInvocations::FetchingFailed,
                   base::Unretained(callback), run_loop_.QuitClosure()));
    run_loop_.Run();
  }

  void VerifyReceivedSTH(const std::string& log_id,
                         const net::ct::SignedTreeHead& sth) {
    net::ct::SignedTreeHead expected_sth;
    net::ct::GetSampleSignedTreeHead(&expected_sth);

    EXPECT_EQ(kLogID, log_id);
    EXPECT_EQ(expected_sth.version, sth.version);
    EXPECT_EQ(expected_sth.timestamp, sth.timestamp);
    EXPECT_EQ(expected_sth.tree_size, sth.tree_size);
    EXPECT_STREQ(expected_sth.sha256_root_hash, sth.sha256_root_hash);
    EXPECT_EQ(expected_sth.signature.hash_algorithm,
              sth.signature.hash_algorithm);
    EXPECT_EQ(expected_sth.signature.signature_algorithm,
              sth.signature.signature_algorithm);
    EXPECT_EQ(expected_sth.signature.signature_data,
              sth.signature.signature_data);
    EXPECT_EQ(kLogID, sth.log_id);
  }

  void VerifyConsistencyProof(
      const std::string& log_id,
      const std::vector<std::string>& consistency_proof) {
    EXPECT_EQ(kLogID, log_id);
    EXPECT_EQ(kDummyConsistencyProofNumNodes, consistency_proof.size());
    for (uint64_t i = 0; i < kDummyConsistencyProofNumNodes; ++i) {
      EXPECT_EQ(GetDummyConsistencyProofNode(i), consistency_proof[i])
          << " node: " << i;
    }
  }

  // The |message_loop_|, while seemingly unused, is necessary
  // for URL request interception. That is the message loop that
  // will be used by the RunLoop.
  base::MessageLoopForIO message_loop_;
  base::RunLoop run_loop_;
  net::TestURLRequestContext context_;
  safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_;
  std::unique_ptr<LogProofFetcher> fetcher_;
  const GURL log_url_;
  LogGetResponseHandler* handler_;
};

TEST_F(LogProofFetcherTest, TestValidGetReply) {
  SetValidSTHJSONResponse();

  RecordFetchCallbackInvocations callback(true);

  RunFetcherWithCallback(&callback);

  ASSERT_EQ(STH_FETCH, callback.intercepted_result_type());
  VerifyReceivedSTH(callback.intercepted_log_id(), callback.intercepted_sth());
}

TEST_F(LogProofFetcherTest, TestValidGetReplyAsyncIO) {
  SetValidSTHJSONResponse();
  handler_->set_async_io(true);

  RecordFetchCallbackInvocations callback(true);
  RunFetcherWithCallback(&callback);

  ASSERT_EQ(STH_FETCH, callback.intercepted_result_type());
  VerifyReceivedSTH(callback.intercepted_log_id(), callback.intercepted_sth());
}

TEST_F(LogProofFetcherTest, TestInvalidGetReplyIncompleteJSON) {
  std::string sth_json_reply_data = net::ct::CreateSignedTreeHeadJsonString(
      21 /* tree_size */, 123456u /* timestamp */, std::string(),
      std::string());
  handler_->set_response_body(sth_json_reply_data);
  handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth"));

  RecordFetchCallbackInvocations callback(false);
  RunFetcherWithCallback(&callback);

  ASSERT_EQ(FAILURE, callback.intercepted_result_type());
  EXPECT_EQ(net::ERR_CT_STH_INCOMPLETE, callback.net_error());
  EXPECT_EQ(net::HTTP_OK, callback.http_response_code());
}

TEST_F(LogProofFetcherTest, TestInvalidGetReplyInvalidJSON) {
  std::string sth_json_reply_data = "{\"tree_size\":21,\"timestamp\":}";
  handler_->set_response_body(sth_json_reply_data);
  handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth"));

  RecordFetchCallbackInvocations callback(false);
  RunFetcherWithCallback(&callback);

  ASSERT_EQ(FAILURE, callback.intercepted_result_type());
  EXPECT_EQ(net::ERR_CT_STH_PARSING_FAILED, callback.net_error());
  EXPECT_EQ(net::HTTP_OK, callback.http_response_code());
}

TEST_F(LogProofFetcherTest, TestLogReplyIsTooLong) {
  std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson();
  // Add kMaxLogResponseSizeInBytes to make sure the response is too big.
  sth_json_reply_data.append(
      std::string(LogProofFetcher::kMaxLogResponseSizeInBytes, ' '));
  handler_->set_response_body(sth_json_reply_data);
  handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth"));

  RecordFetchCallbackInvocations callback(false);
  RunFetcherWithCallback(&callback);

  ASSERT_EQ(FAILURE, callback.intercepted_result_type());
  EXPECT_EQ(net::ERR_FILE_TOO_BIG, callback.net_error());
  EXPECT_EQ(net::HTTP_OK, callback.http_response_code());
}

TEST_F(LogProofFetcherTest, TestLogReplyIsExactlyMaxSize) {
  std::string sth_json_reply_data = net::ct::GetSampleSTHAsJson();
  // Extend the reply to be exactly kMaxLogResponseSizeInBytes.
  sth_json_reply_data.append(std::string(
      LogProofFetcher::kMaxLogResponseSizeInBytes - sth_json_reply_data.size(),
      ' '));
  handler_->set_response_body(sth_json_reply_data);
  handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth"));

  RecordFetchCallbackInvocations callback(true);
  RunFetcherWithCallback(&callback);

  ASSERT_EQ(STH_FETCH, callback.intercepted_result_type());
  VerifyReceivedSTH(callback.intercepted_log_id(), callback.intercepted_sth());
}

TEST_F(LogProofFetcherTest, TestLogRepliesWithHttpError) {
  handler_->set_response_headers(std::string(
      kGetResponseNotFoundHeaders, arraysize(kGetResponseNotFoundHeaders)));
  handler_->set_expected_url(log_url_.Resolve("ct/v1/get-sth"));

  RecordFetchCallbackInvocations callback(false);
  RunFetcherWithCallback(&callback);

  ASSERT_EQ(FAILURE, callback.intercepted_result_type());
  EXPECT_EQ(net::OK, callback.net_error());
  EXPECT_EQ(net::HTTP_NOT_FOUND, callback.http_response_code());
}

TEST_F(LogProofFetcherTest, TestValidGetConsistencyValidReply) {
  std::vector<std::string> proof;
  for (uint64_t i = 0; i < kDummyConsistencyProofNumNodes; ++i)
    proof.push_back(GetDummyConsistencyProofNode(i));

  std::string consistency_proof_reply_data =
      net::ct::CreateConsistencyProofJsonString(proof);
  handler_->set_response_body(consistency_proof_reply_data);

  RecordFetchCallbackInvocations callback(true);
  RunGetConsistencyFetcherWithCallback(&callback);

  ASSERT_EQ(CONSISTENCY_PROOF_FETCH, callback.intercepted_result_type());
  VerifyConsistencyProof(callback.intercepted_log_id(),
                         callback.intercepted_proof());
}

TEST_F(LogProofFetcherTest, TestInvalidGetConsistencyReplyInvalidJSON) {
  std::string consistency_proof_reply_data = "{\"consistency\": [1,2]}";
  handler_->set_response_body(consistency_proof_reply_data);

  RecordFetchCallbackInvocations callback(false);
  RunGetConsistencyFetcherWithCallback(&callback);

  ASSERT_EQ(FAILURE, callback.intercepted_result_type());
  EXPECT_EQ(net::ERR_CT_CONSISTENCY_PROOF_PARSING_FAILED, callback.net_error());
  EXPECT_EQ(net::HTTP_OK, callback.http_response_code());
}

}  // namespace

}  // namespace certificate_transparency