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/crypto/cert_compressor.h" | 5 #include "net/quic/crypto/cert_compressor.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "net/quic/quic_utils.h" | 8 #include "net/quic/quic_utils.h" |
9 #include "net/quic/test_tools/crypto_test_utils.h" | 9 #include "net/quic/test_tools/crypto_test_utils.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 TEST(CertCompressor, Common) { | 46 TEST(CertCompressor, Common) { |
47 vector<string> chain; | 47 vector<string> chain; |
48 chain.push_back("testcert"); | 48 chain.push_back("testcert"); |
49 static const uint64 set_hash = 42; | 49 static const uint64 set_hash = 42; |
50 scoped_ptr<CommonCertSets> common_sets( | 50 scoped_ptr<CommonCertSets> common_sets( |
51 CryptoTestUtils::MockCommonCertSets(chain[0], set_hash, 1)); | 51 CryptoTestUtils::MockCommonCertSets(chain[0], set_hash, 1)); |
52 const string compressed = CertCompressor::CompressChain( | 52 const string compressed = CertCompressor::CompressChain( |
53 chain, | 53 chain, |
54 StringPiece(reinterpret_cast<const char*>(&set_hash), sizeof(set_hash)), | 54 StringPiece(reinterpret_cast<const char*>(&set_hash), sizeof(set_hash)), |
55 StringPiece(), common_sets.get()); | 55 StringPiece(), |
56 const string common("03" /* common */ | 56 common_sets.get()); |
57 "2A00000000000000" /* set hash 42 */ | 57 const string common( |
58 "01000000" /* index 1 */ | 58 "03" /* common */ |
59 "00" /* end of list */); | 59 "2A00000000000000" /* set hash 42 */ |
| 60 "01000000" /* index 1 */ |
| 61 "00" /* end of list */); |
60 EXPECT_EQ(common.data(), | 62 EXPECT_EQ(common.data(), |
61 base::HexEncode(compressed.data(), compressed.size())); | 63 base::HexEncode(compressed.data(), compressed.size())); |
62 | 64 |
63 vector<string> chain2, cached_certs; | 65 vector<string> chain2, cached_certs; |
64 ASSERT_TRUE(CertCompressor::DecompressChain(compressed, cached_certs, | 66 ASSERT_TRUE(CertCompressor::DecompressChain( |
65 common_sets.get(), &chain2)); | 67 compressed, cached_certs, common_sets.get(), &chain2)); |
66 EXPECT_EQ(chain.size(), chain2.size()); | 68 EXPECT_EQ(chain.size(), chain2.size()); |
67 EXPECT_EQ(chain[0], chain2[0]); | 69 EXPECT_EQ(chain[0], chain2[0]); |
68 } | 70 } |
69 | 71 |
70 TEST(CertCompressor, Cached) { | 72 TEST(CertCompressor, Cached) { |
71 vector<string> chain; | 73 vector<string> chain; |
72 chain.push_back("testcert"); | 74 chain.push_back("testcert"); |
73 uint64 hash = QuicUtils::FNV1a_64_Hash(chain[0].data(), chain[0].size()); | 75 uint64 hash = QuicUtils::FNV1a_64_Hash(chain[0].data(), chain[0].size()); |
74 StringPiece hash_bytes(reinterpret_cast<char*>(&hash), sizeof(hash)); | 76 StringPiece hash_bytes(reinterpret_cast<char*>(&hash), sizeof(hash)); |
75 const string compressed = | 77 const string compressed = |
76 CertCompressor::CompressChain(chain, StringPiece(), hash_bytes, NULL); | 78 CertCompressor::CompressChain(chain, StringPiece(), hash_bytes, NULL); |
77 | 79 |
78 EXPECT_EQ("02" /* cached */ + | 80 EXPECT_EQ("02" /* cached */ + |
79 base::HexEncode(hash_bytes.data(), hash_bytes.size()) + | 81 base::HexEncode(hash_bytes.data(), hash_bytes.size()) + |
80 "00" /* end of list */, | 82 "00" /* end of list */, |
81 base::HexEncode(compressed.data(), compressed.size())); | 83 base::HexEncode(compressed.data(), compressed.size())); |
82 | 84 |
83 vector<string> cached_certs, chain2; | 85 vector<string> cached_certs, chain2; |
84 cached_certs.push_back(chain[0]); | 86 cached_certs.push_back(chain[0]); |
85 ASSERT_TRUE( | 87 ASSERT_TRUE( |
86 CertCompressor::DecompressChain(compressed, cached_certs, NULL, &chain2)); | 88 CertCompressor::DecompressChain(compressed, cached_certs, NULL, &chain2)); |
87 EXPECT_EQ(chain.size(), chain2.size()); | 89 EXPECT_EQ(chain.size(), chain2.size()); |
88 EXPECT_EQ(chain[0], chain2[0]); | 90 EXPECT_EQ(chain[0], chain2[0]); |
89 } | 91 } |
90 | 92 |
91 TEST(CertCompressor, BadInputs) { | 93 TEST(CertCompressor, BadInputs) { |
92 vector<string> cached_certs, chain; | 94 vector<string> cached_certs, chain; |
93 | 95 |
94 /* bad entry type */ | 96 /* bad entry type */ |
95 const string bad_entry("04"); | 97 const string bad_entry("04"); |
96 EXPECT_FALSE(CertCompressor::DecompressChain( | 98 EXPECT_FALSE(CertCompressor::DecompressChain( |
97 base::HexEncode(bad_entry.data(), bad_entry.size()), | 99 base::HexEncode(bad_entry.data(), bad_entry.size()), |
98 cached_certs, NULL, &chain)); | 100 cached_certs, |
| 101 NULL, |
| 102 &chain)); |
99 | 103 |
100 /* no terminator */ | 104 /* no terminator */ |
101 const string no_terminator("01"); | 105 const string no_terminator("01"); |
102 EXPECT_FALSE(CertCompressor::DecompressChain( | 106 EXPECT_FALSE(CertCompressor::DecompressChain( |
103 base::HexEncode(no_terminator.data(), no_terminator.size()), | 107 base::HexEncode(no_terminator.data(), no_terminator.size()), |
104 cached_certs, NULL, &chain)); | 108 cached_certs, |
| 109 NULL, |
| 110 &chain)); |
105 | 111 |
106 /* hash truncated */ | 112 /* hash truncated */ |
107 const string hash_truncated("0200"); | 113 const string hash_truncated("0200"); |
108 EXPECT_FALSE(CertCompressor::DecompressChain( | 114 EXPECT_FALSE(CertCompressor::DecompressChain( |
109 base::HexEncode(hash_truncated.data(), hash_truncated.size()), | 115 base::HexEncode(hash_truncated.data(), hash_truncated.size()), |
110 cached_certs, NULL, &chain)); | 116 cached_certs, |
| 117 NULL, |
| 118 &chain)); |
111 | 119 |
112 /* hash and index truncated */ | 120 /* hash and index truncated */ |
113 const string hash_and_index_truncated("0300"); | 121 const string hash_and_index_truncated("0300"); |
114 EXPECT_FALSE(CertCompressor::DecompressChain( | 122 EXPECT_FALSE(CertCompressor::DecompressChain( |
115 base::HexEncode(hash_and_index_truncated.data(), | 123 base::HexEncode(hash_and_index_truncated.data(), |
116 hash_and_index_truncated.size()), | 124 hash_and_index_truncated.size()), |
117 cached_certs, NULL, &chain)); | 125 cached_certs, |
| 126 NULL, |
| 127 &chain)); |
118 | 128 |
119 /* without a CommonCertSets */ | 129 /* without a CommonCertSets */ |
120 const string without_a_common_cert_set( | 130 const string without_a_common_cert_set( |
121 "03" "0000000000000000" "00000000"); | 131 "03" |
| 132 "0000000000000000" |
| 133 "00000000"); |
122 EXPECT_FALSE(CertCompressor::DecompressChain( | 134 EXPECT_FALSE(CertCompressor::DecompressChain( |
123 base::HexEncode(without_a_common_cert_set.data(), | 135 base::HexEncode(without_a_common_cert_set.data(), |
124 without_a_common_cert_set.size()), | 136 without_a_common_cert_set.size()), |
125 cached_certs, NULL, &chain)); | 137 cached_certs, |
| 138 NULL, |
| 139 &chain)); |
126 | 140 |
127 scoped_ptr<CommonCertSets> common_sets( | 141 scoped_ptr<CommonCertSets> common_sets( |
128 CryptoTestUtils::MockCommonCertSets("foo", 42, 1)); | 142 CryptoTestUtils::MockCommonCertSets("foo", 42, 1)); |
129 | 143 |
130 /* incorrect hash and index */ | 144 /* incorrect hash and index */ |
131 const string incorrect_hash_and_index( | 145 const string incorrect_hash_and_index( |
132 "03" "a200000000000000" "00000000"); | 146 "03" |
| 147 "a200000000000000" |
| 148 "00000000"); |
133 EXPECT_FALSE(CertCompressor::DecompressChain( | 149 EXPECT_FALSE(CertCompressor::DecompressChain( |
134 base::HexEncode(incorrect_hash_and_index.data(), | 150 base::HexEncode(incorrect_hash_and_index.data(), |
135 incorrect_hash_and_index.size()), | 151 incorrect_hash_and_index.size()), |
136 cached_certs, NULL, &chain)); | 152 cached_certs, |
| 153 NULL, |
| 154 &chain)); |
137 } | 155 } |
138 | 156 |
139 } // namespace test | 157 } // namespace test |
140 } // namespace net | 158 } // namespace net |
OLD | NEW |