ct_log_response_parser_unittest.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2014 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. #include "net/cert/ct_log_response_parser.h"
  5. #include <memory>
  6. #include <string>
  7. #include "base/base64.h"
  8. #include "base/json/json_reader.h"
  9. #include "base/time/time.h"
  10. #include "base/values.h"
  11. #include "net/cert/ct_serialization.h"
  12. #include "net/cert/signed_tree_head.h"
  13. #include "net/test/ct_test_util.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. namespace net::ct {
  16. TEST(CTLogResponseParserTest, ParsesValidJsonSTH) {
  17. absl::optional<base::Value> sample_sth_json =
  18. base::JSONReader::Read(GetSampleSTHAsJson());
  19. SignedTreeHead tree_head;
  20. EXPECT_TRUE(FillSignedTreeHead(*sample_sth_json, &tree_head));
  21. SignedTreeHead sample_sth;
  22. ASSERT_TRUE(GetSampleSignedTreeHead(&sample_sth));
  23. ASSERT_EQ(SignedTreeHead::V1, tree_head.version);
  24. ASSERT_EQ(sample_sth.timestamp, tree_head.timestamp);
  25. ASSERT_EQ(sample_sth.tree_size, tree_head.tree_size);
  26. // Copy the field from the SignedTreeHead because it's not null terminated
  27. // there and ASSERT_STREQ expects null-terminated strings.
  28. char actual_hash[kSthRootHashLength + 1];
  29. memcpy(actual_hash, tree_head.sha256_root_hash, kSthRootHashLength);
  30. actual_hash[kSthRootHashLength] = '\0';
  31. std::string expected_sha256_root_hash = GetSampleSTHSHA256RootHash();
  32. ASSERT_STREQ(expected_sha256_root_hash.c_str(), actual_hash);
  33. const DigitallySigned& expected_signature(sample_sth.signature);
  34. ASSERT_EQ(tree_head.signature.hash_algorithm,
  35. expected_signature.hash_algorithm);
  36. ASSERT_EQ(tree_head.signature.signature_algorithm,
  37. expected_signature.signature_algorithm);
  38. ASSERT_EQ(tree_head.signature.signature_data,
  39. expected_signature.signature_data);
  40. }
  41. TEST(CTLogResponseParserTest, FailsToParseMissingFields) {
  42. absl::optional<base::Value> missing_signature_sth = base::JSONReader::Read(
  43. CreateSignedTreeHeadJsonString(1 /* tree_size */, 123456u /* timestamp */,
  44. GetSampleSTHSHA256RootHash(), ""));
  45. SignedTreeHead tree_head;
  46. ASSERT_FALSE(FillSignedTreeHead(*missing_signature_sth, &tree_head));
  47. absl::optional<base::Value> missing_root_hash_sth = base::JSONReader::Read(
  48. CreateSignedTreeHeadJsonString(1 /* tree_size */, 123456u /* timestamp */,
  49. "", GetSampleSTHTreeHeadSignature()));
  50. ASSERT_FALSE(FillSignedTreeHead(*missing_root_hash_sth, &tree_head));
  51. }
  52. TEST(CTLogResponseParserTest, FailsToParseIncorrectLengthRootHash) {
  53. SignedTreeHead tree_head;
  54. std::string too_long_hash;
  55. base::Base64Decode(
  56. base::StringPiece("/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoArK\n"),
  57. &too_long_hash);
  58. absl::optional<base::Value> too_long_hash_json =
  59. base::JSONReader::Read(CreateSignedTreeHeadJsonString(
  60. 1 /* tree_size */, 123456u /* timestamp */,
  61. GetSampleSTHSHA256RootHash(), too_long_hash));
  62. ASSERT_FALSE(FillSignedTreeHead(*too_long_hash_json, &tree_head));
  63. std::string too_short_hash;
  64. base::Base64Decode(
  65. base::StringPiece("/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoA==\n"),
  66. &too_short_hash);
  67. absl::optional<base::Value> too_short_hash_json =
  68. base::JSONReader::Read(CreateSignedTreeHeadJsonString(
  69. 1 /* tree_size */, 123456u /* timestamp */,
  70. GetSampleSTHSHA256RootHash(), too_short_hash));
  71. ASSERT_FALSE(FillSignedTreeHead(*too_short_hash_json, &tree_head));
  72. }
  73. TEST(CTLogResponseParserTest, ParsesJsonSTHWithLargeTimestamp) {
  74. SignedTreeHead tree_head;
  75. absl::optional<base::Value> large_timestamp_json =
  76. base::JSONReader::Read(CreateSignedTreeHeadJsonString(
  77. 100, INT64_C(1) << 34, GetSampleSTHSHA256RootHash(),
  78. GetSampleSTHTreeHeadSignature()));
  79. ASSERT_TRUE(FillSignedTreeHead(*large_timestamp_json, &tree_head));
  80. base::Time expected_time =
  81. base::Time::UnixEpoch() + base::Milliseconds(INT64_C(1) << 34);
  82. EXPECT_EQ(tree_head.timestamp, expected_time);
  83. }
  84. TEST(CTLogResponseParserTest, ParsesConsistencyProofSuccessfully) {
  85. std::string first(32, 'a');
  86. std::string second(32, 'b');
  87. std::string third(32, 'c');
  88. std::vector<std::string> raw_nodes;
  89. raw_nodes.push_back(first);
  90. raw_nodes.push_back(second);
  91. raw_nodes.push_back(third);
  92. absl::optional<base::Value> sample_consistency_proof =
  93. base::JSONReader::Read(CreateConsistencyProofJsonString(raw_nodes));
  94. std::vector<std::string> output;
  95. ASSERT_TRUE(FillConsistencyProof(*sample_consistency_proof, &output));
  96. EXPECT_EQ(output[0], first);
  97. EXPECT_EQ(output[1], second);
  98. EXPECT_EQ(output[2], third);
  99. }
  100. TEST(CTLogResponseParserTest, FailsOnInvalidProofJson) {
  101. std::vector<std::string> output;
  102. absl::optional<base::Value> badly_encoded =
  103. base::JSONReader::Read(std::string("{\"consistency\": [\"notbase64\"]}"));
  104. EXPECT_FALSE(FillConsistencyProof(*badly_encoded, &output));
  105. absl::optional<base::Value> not_a_string =
  106. base::JSONReader::Read(std::string("{\"consistency\": [42, 16]}"));
  107. EXPECT_FALSE(FillConsistencyProof(*badly_encoded, &output));
  108. absl::optional<base::Value> missing_consistency =
  109. base::JSONReader::Read(std::string("{}"));
  110. EXPECT_FALSE(FillConsistencyProof(*missing_consistency, &output));
  111. absl::optional<base::Value> not_a_dict =
  112. base::JSONReader::Read(std::string("[]"));
  113. EXPECT_FALSE(FillConsistencyProof(*not_a_dict, &output));
  114. }
  115. TEST(CTLogResponseParserTest, ParsesProofJsonWithExtraFields) {
  116. std::vector<std::string> output;
  117. absl::optional<base::Value> badly_encoded = base::JSONReader::Read(
  118. std::string("{\"consistency\": [], \"somethingelse\": 3}"));
  119. EXPECT_TRUE(FillConsistencyProof(*badly_encoded, &output));
  120. }
  121. } // namespace net::ct