OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/core/crypto/cert_compressor.h" | 5 #include "net/quic/core/crypto/cert_compressor.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "net/quic/core/quic_utils.h" | 9 #include "net/quic/core/quic_utils.h" |
| 10 #include "net/quic/platform/api/quic_text_utils.h" |
10 #include "net/quic/test_tools/crypto_test_utils.h" | 11 #include "net/quic/test_tools/crypto_test_utils.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 using base::StringPiece; | 14 using base::StringPiece; |
14 using std::string; | 15 using std::string; |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 namespace test { | 18 namespace test { |
18 | 19 |
19 TEST(CertCompressor, EmptyChain) { | 20 TEST(CertCompressor, EmptyChain) { |
20 std::vector<string> chain; | 21 std::vector<string> chain; |
21 const string compressed = CertCompressor::CompressChain( | 22 const string compressed = CertCompressor::CompressChain( |
22 chain, StringPiece(), StringPiece(), nullptr); | 23 chain, StringPiece(), StringPiece(), nullptr); |
23 EXPECT_EQ("00", QuicUtils::HexEncode(compressed)); | 24 EXPECT_EQ("00", QuicTextUtils::HexEncode(compressed)); |
24 | 25 |
25 std::vector<string> chain2, cached_certs; | 26 std::vector<string> chain2, cached_certs; |
26 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, | 27 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, |
27 &chain2)); | 28 &chain2)); |
28 EXPECT_EQ(chain.size(), chain2.size()); | 29 EXPECT_EQ(chain.size(), chain2.size()); |
29 } | 30 } |
30 | 31 |
31 TEST(CertCompressor, Compressed) { | 32 TEST(CertCompressor, Compressed) { |
32 std::vector<string> chain; | 33 std::vector<string> chain; |
33 chain.push_back("testcert"); | 34 chain.push_back("testcert"); |
34 const string compressed = CertCompressor::CompressChain( | 35 const string compressed = CertCompressor::CompressChain( |
35 chain, StringPiece(), StringPiece(), nullptr); | 36 chain, StringPiece(), StringPiece(), nullptr); |
36 ASSERT_GE(compressed.size(), 2u); | 37 ASSERT_GE(compressed.size(), 2u); |
37 EXPECT_EQ("0100", QuicUtils::HexEncode(compressed.substr(0, 2))); | 38 EXPECT_EQ("0100", QuicTextUtils::HexEncode(compressed.substr(0, 2))); |
38 | 39 |
39 std::vector<string> chain2, cached_certs; | 40 std::vector<string> chain2, cached_certs; |
40 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, | 41 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, |
41 &chain2)); | 42 &chain2)); |
42 EXPECT_EQ(chain.size(), chain2.size()); | 43 EXPECT_EQ(chain.size(), chain2.size()); |
43 EXPECT_EQ(chain[0], chain2[0]); | 44 EXPECT_EQ(chain[0], chain2[0]); |
44 } | 45 } |
45 | 46 |
46 TEST(CertCompressor, Common) { | 47 TEST(CertCompressor, Common) { |
47 std::vector<string> chain; | 48 std::vector<string> chain; |
48 chain.push_back("testcert"); | 49 chain.push_back("testcert"); |
49 static const uint64_t set_hash = 42; | 50 static const uint64_t set_hash = 42; |
50 std::unique_ptr<CommonCertSets> common_sets( | 51 std::unique_ptr<CommonCertSets> common_sets( |
51 CryptoTestUtils::MockCommonCertSets(chain[0], set_hash, 1)); | 52 CryptoTestUtils::MockCommonCertSets(chain[0], set_hash, 1)); |
52 const string compressed = CertCompressor::CompressChain( | 53 const string compressed = CertCompressor::CompressChain( |
53 chain, | 54 chain, |
54 StringPiece(reinterpret_cast<const char*>(&set_hash), sizeof(set_hash)), | 55 StringPiece(reinterpret_cast<const char*>(&set_hash), sizeof(set_hash)), |
55 StringPiece(), common_sets.get()); | 56 StringPiece(), common_sets.get()); |
56 EXPECT_EQ( | 57 EXPECT_EQ( |
57 "03" /* common */ | 58 "03" /* common */ |
58 "2A00000000000000" /* set hash 42 */ | 59 "2A00000000000000" /* set hash 42 */ |
59 "01000000" /* index 1 */ | 60 "01000000" /* index 1 */ |
60 "00" /* end of list */, | 61 "00" /* end of list */, |
61 QuicUtils::HexEncode(compressed)); | 62 QuicTextUtils::HexEncode(compressed)); |
62 | 63 |
63 std::vector<string> chain2, cached_certs; | 64 std::vector<string> chain2, cached_certs; |
64 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, | 65 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, |
65 common_sets.get(), &chain2)); | 66 common_sets.get(), &chain2)); |
66 EXPECT_EQ(chain.size(), chain2.size()); | 67 EXPECT_EQ(chain.size(), chain2.size()); |
67 EXPECT_EQ(chain[0], chain2[0]); | 68 EXPECT_EQ(chain[0], chain2[0]); |
68 } | 69 } |
69 | 70 |
70 TEST(CertCompressor, Cached) { | 71 TEST(CertCompressor, Cached) { |
71 std::vector<string> chain; | 72 std::vector<string> chain; |
72 chain.push_back("testcert"); | 73 chain.push_back("testcert"); |
73 uint64_t hash = QuicUtils::FNV1a_64_Hash(chain[0]); | 74 uint64_t hash = QuicUtils::FNV1a_64_Hash(chain[0]); |
74 StringPiece hash_bytes(reinterpret_cast<char*>(&hash), sizeof(hash)); | 75 StringPiece hash_bytes(reinterpret_cast<char*>(&hash), sizeof(hash)); |
75 const string compressed = | 76 const string compressed = |
76 CertCompressor::CompressChain(chain, StringPiece(), hash_bytes, nullptr); | 77 CertCompressor::CompressChain(chain, StringPiece(), hash_bytes, nullptr); |
77 | 78 |
78 EXPECT_EQ("02" /* cached */ + QuicUtils::HexEncode(hash_bytes) + | 79 EXPECT_EQ("02" /* cached */ + QuicTextUtils::HexEncode(hash_bytes) + |
79 "00" /* end of list */, | 80 "00" /* end of list */, |
80 QuicUtils::HexEncode(compressed)); | 81 QuicTextUtils::HexEncode(compressed)); |
81 | 82 |
82 std::vector<string> cached_certs, chain2; | 83 std::vector<string> cached_certs, chain2; |
83 cached_certs.push_back(chain[0]); | 84 cached_certs.push_back(chain[0]); |
84 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, | 85 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, |
85 &chain2)); | 86 &chain2)); |
86 EXPECT_EQ(chain.size(), chain2.size()); | 87 EXPECT_EQ(chain.size(), chain2.size()); |
87 EXPECT_EQ(chain[0], chain2[0]); | 88 EXPECT_EQ(chain[0], chain2[0]); |
88 } | 89 } |
89 | 90 |
90 TEST(CertCompressor, BadInputs) { | 91 TEST(CertCompressor, BadInputs) { |
91 std::vector<string> cached_certs, chain; | 92 std::vector<string> cached_certs, chain; |
92 | 93 |
93 EXPECT_FALSE(CertCompressor::DecompressChain( | 94 EXPECT_FALSE(CertCompressor::DecompressChain( |
94 QuicUtils::HexEncode("04") /* bad entry type */, cached_certs, nullptr, | 95 QuicTextUtils::HexEncode("04") /* bad entry type */, cached_certs, |
| 96 nullptr, &chain)); |
| 97 |
| 98 EXPECT_FALSE(CertCompressor::DecompressChain( |
| 99 QuicTextUtils::HexEncode("01") /* no terminator */, cached_certs, nullptr, |
95 &chain)); | 100 &chain)); |
96 | 101 |
97 EXPECT_FALSE(CertCompressor::DecompressChain( | 102 EXPECT_FALSE(CertCompressor::DecompressChain( |
98 QuicUtils::HexEncode("01") /* no terminator */, cached_certs, nullptr, | 103 QuicTextUtils::HexEncode("0200") /* hash truncated */, cached_certs, |
99 &chain)); | 104 nullptr, &chain)); |
100 | 105 |
101 EXPECT_FALSE(CertCompressor::DecompressChain( | 106 EXPECT_FALSE(CertCompressor::DecompressChain( |
102 QuicUtils::HexEncode("0200") /* hash truncated */, cached_certs, nullptr, | 107 QuicTextUtils::HexEncode("0300") /* hash and index truncated */, |
103 &chain)); | 108 cached_certs, nullptr, &chain)); |
104 | |
105 EXPECT_FALSE(CertCompressor::DecompressChain( | |
106 QuicUtils::HexEncode("0300") /* hash and index truncated */, cached_certs, | |
107 nullptr, &chain)); | |
108 | 109 |
109 /* without a CommonCertSets */ | 110 /* without a CommonCertSets */ |
110 EXPECT_FALSE( | 111 EXPECT_FALSE(CertCompressor::DecompressChain( |
111 CertCompressor::DecompressChain(QuicUtils::HexEncode("03" | 112 QuicTextUtils::HexEncode("03" |
112 "0000000000000000" | 113 "0000000000000000" |
113 "00000000"), | 114 "00000000"), |
114 cached_certs, nullptr, &chain)); | 115 cached_certs, nullptr, &chain)); |
115 | 116 |
116 std::unique_ptr<CommonCertSets> common_sets( | 117 std::unique_ptr<CommonCertSets> common_sets( |
117 CryptoTestUtils::MockCommonCertSets("foo", 42, 1)); | 118 CryptoTestUtils::MockCommonCertSets("foo", 42, 1)); |
118 | 119 |
119 /* incorrect hash and index */ | 120 /* incorrect hash and index */ |
120 EXPECT_FALSE( | 121 EXPECT_FALSE(CertCompressor::DecompressChain( |
121 CertCompressor::DecompressChain(QuicUtils::HexEncode("03" | 122 QuicTextUtils::HexEncode("03" |
122 "a200000000000000" | 123 "a200000000000000" |
123 "00000000"), | 124 "00000000"), |
124 cached_certs, nullptr, &chain)); | 125 cached_certs, nullptr, &chain)); |
125 } | 126 } |
126 | 127 |
127 } // namespace test | 128 } // namespace test |
128 } // namespace net | 129 } // namespace net |
OLD | NEW |