summaryrefslogtreecommitdiffstats
path: root/net/cert/ct_log_response_parser_unittest.cc
blob: 7d6140a60e98652331f13404c07d0c12af3213f7 (plain)
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
// Copyright 2014 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 "net/cert/ct_log_response_parser.h"

#include <string>

#include "base/base64.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "net/cert/ct_serialization.h"
#include "net/cert/signed_tree_head.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace ct {

std::string CreateSignedTreeHeadJsonString(std::string sha256_root_hash,
                                           std::string tree_head_signature) {
  std::string sth_json = "{\"tree_size\":2903698,\"timestamp\":1395761621447";

  if (!sha256_root_hash.empty()) {
    sth_json += base::StringPrintf(",\"sha256_root_hash\":\"%s\"",
                                   sha256_root_hash.c_str());
  }
  if (!tree_head_signature.empty()) {
    sth_json += base::StringPrintf(",\"tree_head_signature\":\"%s\"",
                                   tree_head_signature.c_str());
  }

  sth_json += "}";
  return sth_json;
}

const char kSHA256RootHash[] = "/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoAo=";

const char kTreeHeadSignature[] =
    "BAMARzBFAiAB+IIYrkRsZDW0/6TzPgR+aJ26twCQ1JDTwq/"
    "mpinCjAIhAKDXdXMtqbvQ42r9dBIwV5RM/KpEzNQdIhXHesd9HPv3";

TEST(CTLogResponseParserTest, ParsesValidJsonSTH) {
  std::string sample_sth =
      CreateSignedTreeHeadJsonString(kSHA256RootHash, kTreeHeadSignature);
  SignedTreeHead tree_head;
  EXPECT_TRUE(FillSignedTreeHead(sample_sth, &tree_head));

  base::Time expected_timestamp =
      base::Time::UnixEpoch() +
      base::TimeDelta::FromMilliseconds(1395761621447);

  ASSERT_EQ(SignedTreeHead::V1, tree_head.version);
  ASSERT_EQ(expected_timestamp, tree_head.timestamp);
  ASSERT_EQ(2903698u, tree_head.tree_size);

  // Copy the field from the SignedTreeHead because it's not null terminated
  // there and ASSERT_STREQ expects null-terminated strings.
  char actual_hash[kSthRootHashLength + 1];
  memcpy(actual_hash, tree_head.sha256_root_hash, kSthRootHashLength);
  actual_hash[kSthRootHashLength] = '\0';
  std::string expected_sha256_root_hash;
  base::Base64Decode(kSHA256RootHash, &expected_sha256_root_hash);
  ASSERT_STREQ(expected_sha256_root_hash.c_str(), actual_hash);

  std::string tree_head_signature;
  base::Base64Decode(kTreeHeadSignature, &tree_head_signature);
  base::StringPiece sp(tree_head_signature);
  DigitallySigned expected_signature;
  ASSERT_TRUE(DecodeDigitallySigned(&sp, &expected_signature));

  ASSERT_EQ(tree_head.signature.hash_algorithm,
            expected_signature.hash_algorithm);
  ASSERT_EQ(tree_head.signature.signature_algorithm,
            expected_signature.signature_algorithm);
  ASSERT_EQ(tree_head.signature.signature_data,
            expected_signature.signature_data);
}

TEST(CTLogResponseParserTest, FailsToParseMissingFields) {
  std::string missing_signature_sth =
      CreateSignedTreeHeadJsonString(kSHA256RootHash, "");

  SignedTreeHead tree_head;
  ASSERT_FALSE(FillSignedTreeHead(missing_signature_sth, &tree_head));

  std::string missing_root_hash_sth =
      CreateSignedTreeHeadJsonString("", kTreeHeadSignature);
  ASSERT_FALSE(FillSignedTreeHead(missing_root_hash_sth, &tree_head));
}

TEST(CTLogResponseParserTest, FailsToParseIncorrectLengthRootHash) {
  SignedTreeHead tree_head;

  std::string too_long_hash = CreateSignedTreeHeadJsonString(
      kSHA256RootHash, "/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoArK\n");
  ASSERT_FALSE(FillSignedTreeHead(too_long_hash, &tree_head));

  std::string too_short_hash = CreateSignedTreeHeadJsonString(
      kSHA256RootHash, "/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoA==\n");
  ASSERT_FALSE(FillSignedTreeHead(too_short_hash, &tree_head));
}

}  // namespace ct

}  // namespace net