OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "net/cert/ct_log_response_parser.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/base64.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/time/time.h" |
| 12 #include "net/cert/ct_serialization.h" |
| 13 #include "net/cert/signed_tree_head.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace net { |
| 17 |
| 18 namespace ct { |
| 19 |
| 20 // Basic fields, sha256_root_hash, tree_head_signature. |
| 21 const char kSTHFormatString[] = |
| 22 "{%s\"%s\",\"tree_head_signature\":\"%s\"}"; |
| 23 const char kBasicSTHFields[] = |
| 24 "\"tree_size\":2903698,\"timestamp\":1395761621447,\"sha256_root_hash\":"; |
| 25 |
| 26 const char kSHA256RootHash[] = |
| 27 "/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoAo="; |
| 28 |
| 29 const char kTreeHeadSignature[] = |
| 30 "BAMARzBFAiAB+IIYrkRsZDW0/6TzPgR+aJ26twCQ1JDTwq/" \ |
| 31 "mpinCjAIhAKDXdXMtqbvQ42r9dBIwV5RM/KpEzNQdIhXHesd9HPv3"; |
| 32 |
| 33 class CTLogResponseParserTest : public ::testing::Test { |
| 34 public: |
| 35 CTLogResponseParserTest() {} |
| 36 |
| 37 virtual void SetUp() OVERRIDE { |
| 38 sample_sth_ = base::StringPrintf( |
| 39 kSTHFormatString, kBasicSTHFields, kSHA256RootHash, kTreeHeadSignature); |
| 40 base::Base64Decode(kSHA256RootHash, &expected_sha256_root_hash_); |
| 41 |
| 42 std::string tree_head_signature; |
| 43 base::Base64Decode(kTreeHeadSignature, &tree_head_signature); |
| 44 base::StringPiece sp(tree_head_signature); |
| 45 ASSERT_TRUE(DecodeDigitallySigned(&sp, &expected_signature_)); |
| 46 } |
| 47 |
| 48 protected: |
| 49 std::string sample_sth_; |
| 50 std::string expected_sha256_root_hash_; |
| 51 DigitallySigned expected_signature_; |
| 52 }; |
| 53 |
| 54 TEST_F(CTLogResponseParserTest, ParsesValidJsonSTH) { |
| 55 SignedTreeHead tree_head; |
| 56 EXPECT_TRUE(CTLogResponseParser::FillSignedTreeHead(sample_sth_, &tree_head)); |
| 57 |
| 58 base::Time expected_timestamp = |
| 59 base::Time::UnixEpoch() + |
| 60 base::TimeDelta::FromMilliseconds(1395761621447); |
| 61 |
| 62 ASSERT_EQ(SignedTreeHead::V1, tree_head.version); |
| 63 ASSERT_EQ(expected_timestamp, tree_head.timestamp); |
| 64 ASSERT_EQ(2903698u, tree_head.tree_size); |
| 65 |
| 66 // Copy the field from the SignedTreeHead because it's not null terminated |
| 67 // there and ASSERT_STREQ expects null-terminated strings. |
| 68 char actual_hash[kSthRootHashLength + 1]; |
| 69 memcpy(actual_hash, tree_head.sha256_root_hash, kSthRootHashLength); |
| 70 actual_hash[kSthRootHashLength] = '\0'; |
| 71 ASSERT_STREQ(expected_sha256_root_hash_.c_str(), actual_hash); |
| 72 ASSERT_EQ(tree_head.signature.hash_algorithm, |
| 73 expected_signature_.hash_algorithm); |
| 74 ASSERT_EQ(tree_head.signature.signature_algorithm, |
| 75 expected_signature_.signature_algorithm); |
| 76 ASSERT_EQ(tree_head.signature.signature_data, |
| 77 expected_signature_.signature_data); |
| 78 } |
| 79 |
| 80 TEST_F(CTLogResponseParserTest, FailsToParseMissingFields) { |
| 81 std::string missing_fields_sth = base::StringPrintf( |
| 82 "{%s\"%s\"}", kBasicSTHFields, kSHA256RootHash); |
| 83 |
| 84 SignedTreeHead tree_head; |
| 85 ASSERT_FALSE( |
| 86 CTLogResponseParser::FillSignedTreeHead(missing_fields_sth, &tree_head)); |
| 87 } |
| 88 |
| 89 TEST_F(CTLogResponseParserTest, FailsToParseIncorrectLengthRootHash) { |
| 90 SignedTreeHead tree_head; |
| 91 |
| 92 std::string too_long_hash = base::StringPrintf( |
| 93 kSTHFormatString, |
| 94 kBasicSTHFields, |
| 95 kSHA256RootHash, |
| 96 "/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoArK\n"); |
| 97 ASSERT_FALSE( |
| 98 CTLogResponseParser::FillSignedTreeHead(too_long_hash, &tree_head)); |
| 99 |
| 100 std::string too_short_hash = base::StringPrintf( |
| 101 kSTHFormatString, |
| 102 kBasicSTHFields, |
| 103 kSHA256RootHash, |
| 104 "/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoA==\n"); |
| 105 ASSERT_FALSE( |
| 106 CTLogResponseParser::FillSignedTreeHead(too_short_hash, &tree_head)); |
| 107 } |
| 108 |
| 109 } // namespace ct |
| 110 |
| 111 } // namespace net |
OLD | NEW |