blob: a7517f4c1801377ff6a1553140c027d1355aa7dd [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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>
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -05009
10#include "net/third_party/quiche/src/quic/core/quic_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050011#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h"
dmcardle904ef182019-12-13 08:34:33 -080013#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 teama6ef0a62019-03-07 20:34:33 -050015
16namespace quic {
17namespace test {
18
19class CertCompressorTest : public QuicTest {};
20
21TEST_F(CertCompressorTest, EmptyChain) {
vasilvvc48c8712019-03-11 13:38:16 -070022 std::vector<std::string> chain;
23 const std::string compressed = CertCompressor::CompressChain(
dmcardle904ef182019-12-13 08:34:33 -080024 chain, quiche::QuicheStringPiece(), quiche::QuicheStringPiece(), nullptr);
25 EXPECT_EQ("00", quiche::QuicheTextUtils::HexEncode(compressed));
QUICHE teama6ef0a62019-03-07 20:34:33 -050026
vasilvvc48c8712019-03-11 13:38:16 -070027 std::vector<std::string> chain2, cached_certs;
QUICHE teama6ef0a62019-03-07 20:34:33 -050028 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr,
29 &chain2));
30 EXPECT_EQ(chain.size(), chain2.size());
31}
32
33TEST_F(CertCompressorTest, Compressed) {
vasilvvc48c8712019-03-11 13:38:16 -070034 std::vector<std::string> chain;
QUICHE teama6ef0a62019-03-07 20:34:33 -050035 chain.push_back("testcert");
vasilvvc48c8712019-03-11 13:38:16 -070036 const std::string compressed = CertCompressor::CompressChain(
dmcardle904ef182019-12-13 08:34:33 -080037 chain, quiche::QuicheStringPiece(), quiche::QuicheStringPiece(), nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -050038 ASSERT_GE(compressed.size(), 2u);
dmcardle904ef182019-12-13 08:34:33 -080039 EXPECT_EQ("0100",
40 quiche::QuicheTextUtils::HexEncode(compressed.substr(0, 2)));
QUICHE teama6ef0a62019-03-07 20:34:33 -050041
vasilvvc48c8712019-03-11 13:38:16 -070042 std::vector<std::string> chain2, cached_certs;
QUICHE teama6ef0a62019-03-07 20:34:33 -050043 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
49TEST_F(CertCompressorTest, Common) {
vasilvvc48c8712019-03-11 13:38:16 -070050 std::vector<std::string> chain;
QUICHE teama6ef0a62019-03-07 20:34:33 -050051 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));
vasilvvc48c8712019-03-11 13:38:16 -070055 const std::string compressed = CertCompressor::CompressChain(
QUICHE teama6ef0a62019-03-07 20:34:33 -050056 chain,
dmcardle904ef182019-12-13 08:34:33 -080057 quiche::QuicheStringPiece(reinterpret_cast<const char*>(&set_hash),
58 sizeof(set_hash)),
59 quiche::QuicheStringPiece(), common_sets.get());
QUICHE teama6ef0a62019-03-07 20:34:33 -050060 EXPECT_EQ(
61 "03" /* common */
62 "2a00000000000000" /* set hash 42 */
63 "01000000" /* index 1 */
64 "00" /* end of list */,
dmcardle904ef182019-12-13 08:34:33 -080065 quiche::QuicheTextUtils::HexEncode(compressed));
QUICHE teama6ef0a62019-03-07 20:34:33 -050066
vasilvvc48c8712019-03-11 13:38:16 -070067 std::vector<std::string> chain2, cached_certs;
QUICHE teama6ef0a62019-03-07 20:34:33 -050068 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
74TEST_F(CertCompressorTest, Cached) {
vasilvvc48c8712019-03-11 13:38:16 -070075 std::vector<std::string> chain;
QUICHE teama6ef0a62019-03-07 20:34:33 -050076 chain.push_back("testcert");
77 uint64_t hash = QuicUtils::FNV1a_64_Hash(chain[0]);
dmcardle904ef182019-12-13 08:34:33 -080078 quiche::QuicheStringPiece hash_bytes(reinterpret_cast<char*>(&hash),
79 sizeof(hash));
vasilvvc48c8712019-03-11 13:38:16 -070080 const std::string compressed = CertCompressor::CompressChain(
dmcardle904ef182019-12-13 08:34:33 -080081 chain, quiche::QuicheStringPiece(), hash_bytes, nullptr);
QUICHE teama6ef0a62019-03-07 20:34:33 -050082
dmcardle904ef182019-12-13 08:34:33 -080083 EXPECT_EQ("02" /* cached */ + quiche::QuicheTextUtils::HexEncode(hash_bytes) +
QUICHE teama6ef0a62019-03-07 20:34:33 -050084 "00" /* end of list */,
dmcardle904ef182019-12-13 08:34:33 -080085 quiche::QuicheTextUtils::HexEncode(compressed));
QUICHE teama6ef0a62019-03-07 20:34:33 -050086
vasilvvc48c8712019-03-11 13:38:16 -070087 std::vector<std::string> cached_certs, chain2;
QUICHE teama6ef0a62019-03-07 20:34:33 -050088 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
95TEST_F(CertCompressorTest, BadInputs) {
vasilvvc48c8712019-03-11 13:38:16 -070096 std::vector<std::string> cached_certs, chain;
QUICHE teama6ef0a62019-03-07 20:34:33 -050097
98 EXPECT_FALSE(CertCompressor::DecompressChain(
dmcardle904ef182019-12-13 08:34:33 -080099 quiche::QuicheTextUtils::HexEncode("04") /* bad entry type */,
100 cached_certs, nullptr, &chain));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500101
102 EXPECT_FALSE(CertCompressor::DecompressChain(
dmcardle904ef182019-12-13 08:34:33 -0800103 quiche::QuicheTextUtils::HexEncode("01") /* no terminator */,
104 cached_certs, nullptr, &chain));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500105
106 EXPECT_FALSE(CertCompressor::DecompressChain(
dmcardle904ef182019-12-13 08:34:33 -0800107 quiche::QuicheTextUtils::HexEncode("0200") /* hash truncated */,
108 cached_certs, nullptr, &chain));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500109
110 EXPECT_FALSE(CertCompressor::DecompressChain(
dmcardle904ef182019-12-13 08:34:33 -0800111 quiche::QuicheTextUtils::HexEncode("0300") /* hash and index truncated */,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500112 cached_certs, nullptr, &chain));
113
114 /* without a CommonCertSets */
115 EXPECT_FALSE(CertCompressor::DecompressChain(
dmcardle904ef182019-12-13 08:34:33 -0800116 quiche::QuicheTextUtils::HexEncode("03"
117 "0000000000000000"
118 "00000000"),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500119 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(
dmcardle904ef182019-12-13 08:34:33 -0800126 quiche::QuicheTextUtils::HexEncode("03"
127 "a200000000000000"
128 "00000000"),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500129 cached_certs, nullptr, &chain));
130}
131
132} // namespace test
133} // namespace quic