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/test_tools/crypto_test_utils.h" | 10 #include "net/quic/test_tools/crypto_test_utils.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 using base::StringPiece; | 13 using base::StringPiece; |
14 using std::string; | 14 using std::string; |
15 using std::vector; | |
16 | 15 |
17 namespace net { | 16 namespace net { |
18 namespace test { | 17 namespace test { |
19 | 18 |
20 TEST(CertCompressor, EmptyChain) { | 19 TEST(CertCompressor, EmptyChain) { |
21 vector<string> chain; | 20 std::vector<string> chain; |
22 const string compressed = CertCompressor::CompressChain( | 21 const string compressed = CertCompressor::CompressChain( |
23 chain, StringPiece(), StringPiece(), nullptr); | 22 chain, StringPiece(), StringPiece(), nullptr); |
24 EXPECT_EQ("00", QuicUtils::HexEncode(compressed)); | 23 EXPECT_EQ("00", QuicUtils::HexEncode(compressed)); |
25 | 24 |
26 vector<string> chain2, cached_certs; | 25 std::vector<string> chain2, cached_certs; |
27 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, | 26 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, |
28 &chain2)); | 27 &chain2)); |
29 EXPECT_EQ(chain.size(), chain2.size()); | 28 EXPECT_EQ(chain.size(), chain2.size()); |
30 } | 29 } |
31 | 30 |
32 TEST(CertCompressor, Compressed) { | 31 TEST(CertCompressor, Compressed) { |
33 vector<string> chain; | 32 std::vector<string> chain; |
34 chain.push_back("testcert"); | 33 chain.push_back("testcert"); |
35 const string compressed = CertCompressor::CompressChain( | 34 const string compressed = CertCompressor::CompressChain( |
36 chain, StringPiece(), StringPiece(), nullptr); | 35 chain, StringPiece(), StringPiece(), nullptr); |
37 ASSERT_GE(compressed.size(), 2u); | 36 ASSERT_GE(compressed.size(), 2u); |
38 EXPECT_EQ("0100", QuicUtils::HexEncode(compressed.substr(0, 2))); | 37 EXPECT_EQ("0100", QuicUtils::HexEncode(compressed.substr(0, 2))); |
39 | 38 |
40 vector<string> chain2, cached_certs; | 39 std::vector<string> chain2, cached_certs; |
41 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, | 40 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, |
42 &chain2)); | 41 &chain2)); |
43 EXPECT_EQ(chain.size(), chain2.size()); | 42 EXPECT_EQ(chain.size(), chain2.size()); |
44 EXPECT_EQ(chain[0], chain2[0]); | 43 EXPECT_EQ(chain[0], chain2[0]); |
45 } | 44 } |
46 | 45 |
47 TEST(CertCompressor, Common) { | 46 TEST(CertCompressor, Common) { |
48 vector<string> chain; | 47 std::vector<string> chain; |
49 chain.push_back("testcert"); | 48 chain.push_back("testcert"); |
50 static const uint64_t set_hash = 42; | 49 static const uint64_t set_hash = 42; |
51 std::unique_ptr<CommonCertSets> common_sets( | 50 std::unique_ptr<CommonCertSets> common_sets( |
52 CryptoTestUtils::MockCommonCertSets(chain[0], set_hash, 1)); | 51 CryptoTestUtils::MockCommonCertSets(chain[0], set_hash, 1)); |
53 const string compressed = CertCompressor::CompressChain( | 52 const string compressed = CertCompressor::CompressChain( |
54 chain, | 53 chain, |
55 StringPiece(reinterpret_cast<const char*>(&set_hash), sizeof(set_hash)), | 54 StringPiece(reinterpret_cast<const char*>(&set_hash), sizeof(set_hash)), |
56 StringPiece(), common_sets.get()); | 55 StringPiece(), common_sets.get()); |
57 EXPECT_EQ( | 56 EXPECT_EQ( |
58 "03" /* common */ | 57 "03" /* common */ |
59 "2A00000000000000" /* set hash 42 */ | 58 "2A00000000000000" /* set hash 42 */ |
60 "01000000" /* index 1 */ | 59 "01000000" /* index 1 */ |
61 "00" /* end of list */, | 60 "00" /* end of list */, |
62 QuicUtils::HexEncode(compressed)); | 61 QuicUtils::HexEncode(compressed)); |
63 | 62 |
64 vector<string> chain2, cached_certs; | 63 std::vector<string> chain2, cached_certs; |
65 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, | 64 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, |
66 common_sets.get(), &chain2)); | 65 common_sets.get(), &chain2)); |
67 EXPECT_EQ(chain.size(), chain2.size()); | 66 EXPECT_EQ(chain.size(), chain2.size()); |
68 EXPECT_EQ(chain[0], chain2[0]); | 67 EXPECT_EQ(chain[0], chain2[0]); |
69 } | 68 } |
70 | 69 |
71 TEST(CertCompressor, Cached) { | 70 TEST(CertCompressor, Cached) { |
72 vector<string> chain; | 71 std::vector<string> chain; |
73 chain.push_back("testcert"); | 72 chain.push_back("testcert"); |
74 uint64_t hash = QuicUtils::FNV1a_64_Hash(chain[0].data(), chain[0].size()); | 73 uint64_t hash = QuicUtils::FNV1a_64_Hash(chain[0].data(), chain[0].size()); |
75 StringPiece hash_bytes(reinterpret_cast<char*>(&hash), sizeof(hash)); | 74 StringPiece hash_bytes(reinterpret_cast<char*>(&hash), sizeof(hash)); |
76 const string compressed = | 75 const string compressed = |
77 CertCompressor::CompressChain(chain, StringPiece(), hash_bytes, nullptr); | 76 CertCompressor::CompressChain(chain, StringPiece(), hash_bytes, nullptr); |
78 | 77 |
79 EXPECT_EQ("02" /* cached */ + QuicUtils::HexEncode(hash_bytes) + | 78 EXPECT_EQ("02" /* cached */ + QuicUtils::HexEncode(hash_bytes) + |
80 "00" /* end of list */, | 79 "00" /* end of list */, |
81 QuicUtils::HexEncode(compressed)); | 80 QuicUtils::HexEncode(compressed)); |
82 | 81 |
83 vector<string> cached_certs, chain2; | 82 std::vector<string> cached_certs, chain2; |
84 cached_certs.push_back(chain[0]); | 83 cached_certs.push_back(chain[0]); |
85 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, | 84 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, |
86 &chain2)); | 85 &chain2)); |
87 EXPECT_EQ(chain.size(), chain2.size()); | 86 EXPECT_EQ(chain.size(), chain2.size()); |
88 EXPECT_EQ(chain[0], chain2[0]); | 87 EXPECT_EQ(chain[0], chain2[0]); |
89 } | 88 } |
90 | 89 |
91 TEST(CertCompressor, BadInputs) { | 90 TEST(CertCompressor, BadInputs) { |
92 vector<string> cached_certs, chain; | 91 std::vector<string> cached_certs, chain; |
93 | 92 |
94 EXPECT_FALSE(CertCompressor::DecompressChain( | 93 EXPECT_FALSE(CertCompressor::DecompressChain( |
95 QuicUtils::HexEncode("04") /* bad entry type */, cached_certs, nullptr, | 94 QuicUtils::HexEncode("04") /* bad entry type */, cached_certs, nullptr, |
96 &chain)); | 95 &chain)); |
97 | 96 |
98 EXPECT_FALSE(CertCompressor::DecompressChain( | 97 EXPECT_FALSE(CertCompressor::DecompressChain( |
99 QuicUtils::HexEncode("01") /* no terminator */, cached_certs, nullptr, | 98 QuicUtils::HexEncode("01") /* no terminator */, cached_certs, nullptr, |
100 &chain)); | 99 &chain)); |
101 | 100 |
102 EXPECT_FALSE(CertCompressor::DecompressChain( | 101 EXPECT_FALSE(CertCompressor::DecompressChain( |
(...skipping 17 matching lines...) Expand all Loading... |
120 /* incorrect hash and index */ | 119 /* incorrect hash and index */ |
121 EXPECT_FALSE( | 120 EXPECT_FALSE( |
122 CertCompressor::DecompressChain(QuicUtils::HexEncode("03" | 121 CertCompressor::DecompressChain(QuicUtils::HexEncode("03" |
123 "a200000000000000" | 122 "a200000000000000" |
124 "00000000"), | 123 "00000000"), |
125 cached_certs, nullptr, &chain)); | 124 cached_certs, nullptr, &chain)); |
126 } | 125 } |
127 | 126 |
128 } // namespace test | 127 } // namespace test |
129 } // namespace net | 128 } // namespace net |
OLD | NEW |