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