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/platform/api/quic_text_utils.h" |
11 #include "net/quic/test_tools/crypto_test_utils.h" | 11 #include "net/quic/test_tools/crypto_test_utils.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 using base::StringPiece; | |
15 using std::string; | 14 using std::string; |
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 std::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, QuicStringPiece(), QuicStringPiece(), nullptr); |
24 EXPECT_EQ("00", QuicTextUtils::HexEncode(compressed)); | 23 EXPECT_EQ("00", QuicTextUtils::HexEncode(compressed)); |
25 | 24 |
26 std::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 std::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, QuicStringPiece(), QuicStringPiece(), nullptr); |
37 ASSERT_GE(compressed.size(), 2u); | 36 ASSERT_GE(compressed.size(), 2u); |
38 EXPECT_EQ("0100", QuicTextUtils::HexEncode(compressed.substr(0, 2))); | 37 EXPECT_EQ("0100", QuicTextUtils::HexEncode(compressed.substr(0, 2))); |
39 | 38 |
40 std::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 std::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 crypto_test_utils::MockCommonCertSets(chain[0], set_hash, 1)); | 51 crypto_test_utils::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 QuicStringPiece(reinterpret_cast<const char*>(&set_hash), |
56 StringPiece(), common_sets.get()); | 55 sizeof(set_hash)), |
| 56 QuicStringPiece(), common_sets.get()); |
57 EXPECT_EQ( | 57 EXPECT_EQ( |
58 "03" /* common */ | 58 "03" /* common */ |
59 "2a00000000000000" /* set hash 42 */ | 59 "2a00000000000000" /* set hash 42 */ |
60 "01000000" /* index 1 */ | 60 "01000000" /* index 1 */ |
61 "00" /* end of list */, | 61 "00" /* end of list */, |
62 QuicTextUtils::HexEncode(compressed)); | 62 QuicTextUtils::HexEncode(compressed)); |
63 | 63 |
64 std::vector<string> chain2, cached_certs; | 64 std::vector<string> chain2, cached_certs; |
65 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, | 65 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, |
66 common_sets.get(), &chain2)); | 66 common_sets.get(), &chain2)); |
67 EXPECT_EQ(chain.size(), chain2.size()); | 67 EXPECT_EQ(chain.size(), chain2.size()); |
68 EXPECT_EQ(chain[0], chain2[0]); | 68 EXPECT_EQ(chain[0], chain2[0]); |
69 } | 69 } |
70 | 70 |
71 TEST(CertCompressor, Cached) { | 71 TEST(CertCompressor, Cached) { |
72 std::vector<string> chain; | 72 std::vector<string> chain; |
73 chain.push_back("testcert"); | 73 chain.push_back("testcert"); |
74 uint64_t hash = QuicUtils::FNV1a_64_Hash(chain[0]); | 74 uint64_t hash = QuicUtils::FNV1a_64_Hash(chain[0]); |
75 StringPiece hash_bytes(reinterpret_cast<char*>(&hash), sizeof(hash)); | 75 QuicStringPiece hash_bytes(reinterpret_cast<char*>(&hash), sizeof(hash)); |
76 const string compressed = | 76 const string compressed = CertCompressor::CompressChain( |
77 CertCompressor::CompressChain(chain, StringPiece(), hash_bytes, nullptr); | 77 chain, QuicStringPiece(), hash_bytes, nullptr); |
78 | 78 |
79 EXPECT_EQ("02" /* cached */ + QuicTextUtils::HexEncode(hash_bytes) + | 79 EXPECT_EQ("02" /* cached */ + QuicTextUtils::HexEncode(hash_bytes) + |
80 "00" /* end of list */, | 80 "00" /* end of list */, |
81 QuicTextUtils::HexEncode(compressed)); | 81 QuicTextUtils::HexEncode(compressed)); |
82 | 82 |
83 std::vector<string> cached_certs, chain2; | 83 std::vector<string> cached_certs, chain2; |
84 cached_certs.push_back(chain[0]); | 84 cached_certs.push_back(chain[0]); |
85 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, | 85 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, nullptr, |
86 &chain2)); | 86 &chain2)); |
87 EXPECT_EQ(chain.size(), chain2.size()); | 87 EXPECT_EQ(chain.size(), chain2.size()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 /* incorrect hash and index */ | 120 /* incorrect hash and index */ |
121 EXPECT_FALSE(CertCompressor::DecompressChain( | 121 EXPECT_FALSE(CertCompressor::DecompressChain( |
122 QuicTextUtils::HexEncode("03" | 122 QuicTextUtils::HexEncode("03" |
123 "a200000000000000" | 123 "a200000000000000" |
124 "00000000"), | 124 "00000000"), |
125 cached_certs, nullptr, &chain)); | 125 cached_certs, nullptr, &chain)); |
126 } | 126 } |
127 | 127 |
128 } // namespace test | 128 } // namespace test |
129 } // namespace net | 129 } // namespace net |
OLD | NEW |