QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2013 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/third_party/quiche/src/quic/core/crypto/cert_compressor.h" |
| 6 | |
| 7 | #include <memory> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | |
| 10 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h" |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 13 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
| 14 | #include "net/third_party/quiche/src/common/platform/api/quiche_text_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 15 | |
| 16 | namespace quic { |
| 17 | namespace test { |
| 18 | |
| 19 | class CertCompressorTest : public QuicTest {}; |
| 20 | |
| 21 | TEST_F(CertCompressorTest, EmptyChain) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 22 | std::vector<std::string> chain; |
| 23 | const std::string compressed = CertCompressor::CompressChain( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 24 | chain, quiche::QuicheStringPiece(), quiche::QuicheStringPiece(), nullptr); |
| 25 | EXPECT_EQ("00", quiche::QuicheTextUtils::HexEncode(compressed)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 26 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 27 | std::vector<std::string> chain2, cached_certs; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 28 | ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, |
| 29 | &chain2)); |
| 30 | EXPECT_EQ(chain.size(), chain2.size()); |
| 31 | } |
| 32 | |
| 33 | TEST_F(CertCompressorTest, Compressed) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 34 | std::vector<std::string> chain; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 35 | chain.push_back("testcert"); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 36 | const std::string compressed = CertCompressor::CompressChain( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 37 | chain, quiche::QuicheStringPiece(), quiche::QuicheStringPiece(), nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 38 | ASSERT_GE(compressed.size(), 2u); |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 39 | EXPECT_EQ("0100", |
| 40 | quiche::QuicheTextUtils::HexEncode(compressed.substr(0, 2))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 41 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 42 | std::vector<std::string> chain2, cached_certs; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 43 | ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, |
| 44 | &chain2)); |
| 45 | EXPECT_EQ(chain.size(), chain2.size()); |
| 46 | EXPECT_EQ(chain[0], chain2[0]); |
| 47 | } |
| 48 | |
| 49 | TEST_F(CertCompressorTest, Common) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 50 | std::vector<std::string> chain; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 51 | chain.push_back("testcert"); |
| 52 | static const uint64_t set_hash = 42; |
| 53 | std::unique_ptr<CommonCertSets> common_sets( |
| 54 | crypto_test_utils::MockCommonCertSets(chain[0], set_hash, 1)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 55 | const std::string compressed = CertCompressor::CompressChain( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 56 | chain, |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 57 | quiche::QuicheStringPiece(reinterpret_cast<const char*>(&set_hash), |
| 58 | sizeof(set_hash)), |
| 59 | quiche::QuicheStringPiece(), common_sets.get()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 60 | EXPECT_EQ( |
| 61 | "03" /* common */ |
| 62 | "2a00000000000000" /* set hash 42 */ |
| 63 | "01000000" /* index 1 */ |
| 64 | "00" /* end of list */, |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 65 | quiche::QuicheTextUtils::HexEncode(compressed)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 66 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 67 | std::vector<std::string> chain2, cached_certs; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 68 | ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, |
| 69 | common_sets.get(), &chain2)); |
| 70 | EXPECT_EQ(chain.size(), chain2.size()); |
| 71 | EXPECT_EQ(chain[0], chain2[0]); |
| 72 | } |
| 73 | |
| 74 | TEST_F(CertCompressorTest, Cached) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 75 | std::vector<std::string> chain; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 76 | chain.push_back("testcert"); |
| 77 | uint64_t hash = QuicUtils::FNV1a_64_Hash(chain[0]); |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 78 | quiche::QuicheStringPiece hash_bytes(reinterpret_cast<char*>(&hash), |
| 79 | sizeof(hash)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 80 | const std::string compressed = CertCompressor::CompressChain( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 81 | chain, quiche::QuicheStringPiece(), hash_bytes, nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 82 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 83 | EXPECT_EQ("02" /* cached */ + quiche::QuicheTextUtils::HexEncode(hash_bytes) + |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 84 | "00" /* end of list */, |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 85 | quiche::QuicheTextUtils::HexEncode(compressed)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 86 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 87 | std::vector<std::string> cached_certs, chain2; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 88 | cached_certs.push_back(chain[0]); |
| 89 | ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, |
| 90 | &chain2)); |
| 91 | EXPECT_EQ(chain.size(), chain2.size()); |
| 92 | EXPECT_EQ(chain[0], chain2[0]); |
| 93 | } |
| 94 | |
| 95 | TEST_F(CertCompressorTest, BadInputs) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 96 | std::vector<std::string> cached_certs, chain; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 97 | |
| 98 | EXPECT_FALSE(CertCompressor::DecompressChain( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 99 | quiche::QuicheTextUtils::HexEncode("04") /* bad entry type */, |
| 100 | cached_certs, nullptr, &chain)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 101 | |
| 102 | EXPECT_FALSE(CertCompressor::DecompressChain( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 103 | quiche::QuicheTextUtils::HexEncode("01") /* no terminator */, |
| 104 | cached_certs, nullptr, &chain)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 105 | |
| 106 | EXPECT_FALSE(CertCompressor::DecompressChain( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 107 | quiche::QuicheTextUtils::HexEncode("0200") /* hash truncated */, |
| 108 | cached_certs, nullptr, &chain)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 109 | |
| 110 | EXPECT_FALSE(CertCompressor::DecompressChain( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 111 | quiche::QuicheTextUtils::HexEncode("0300") /* hash and index truncated */, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 112 | cached_certs, nullptr, &chain)); |
| 113 | |
| 114 | /* without a CommonCertSets */ |
| 115 | EXPECT_FALSE(CertCompressor::DecompressChain( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 116 | quiche::QuicheTextUtils::HexEncode("03" |
| 117 | "0000000000000000" |
| 118 | "00000000"), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 119 | cached_certs, nullptr, &chain)); |
| 120 | |
| 121 | std::unique_ptr<CommonCertSets> common_sets( |
| 122 | crypto_test_utils::MockCommonCertSets("foo", 42, 1)); |
| 123 | |
| 124 | /* incorrect hash and index */ |
| 125 | EXPECT_FALSE(CertCompressor::DecompressChain( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 126 | quiche::QuicheTextUtils::HexEncode("03" |
| 127 | "a200000000000000" |
| 128 | "00000000"), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 129 | cached_certs, nullptr, &chain)); |
| 130 | } |
| 131 | |
| 132 | } // namespace test |
| 133 | } // namespace quic |