OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/net/packed_ct_ev_whitelist.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace internal { |
| 12 |
| 13 const uint8_t kSomeData[] = {0xd5, 0xe2, 0xaf, 0xe5, 0xbb, 0x10, 0x7c, 0xd1}; |
| 14 |
| 15 TEST(BitStreamReaderTest, CanReadSingleByte) { |
| 16 BitStreamReader reader( |
| 17 base::StringPiece(reinterpret_cast<const char*>(kSomeData), 1)); |
| 18 uint64_t v(0); |
| 19 |
| 20 EXPECT_EQ(8u, reader.BitsLeft()); |
| 21 EXPECT_TRUE(reader.ReadBits(8, &v)); |
| 22 EXPECT_EQ(UINT64_C(0xd5), v); |
| 23 |
| 24 EXPECT_FALSE(reader.ReadBits(1, &v)); |
| 25 EXPECT_EQ(0u, reader.BitsLeft()); |
| 26 } |
| 27 |
| 28 TEST(BitStreamReaderTest, CanReadSingleBits) { |
| 29 const uint64_t expected_bits[] = { |
| 30 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0}; |
| 31 BitStreamReader reader( |
| 32 base::StringPiece(reinterpret_cast<const char*>(kSomeData), 2)); |
| 33 EXPECT_EQ(16u, reader.BitsLeft()); |
| 34 uint64_t v(0); |
| 35 |
| 36 for (int i = 0; i < 16; ++i) { |
| 37 EXPECT_TRUE(reader.ReadBits(1, &v)); |
| 38 EXPECT_EQ(expected_bits[i], v); |
| 39 } |
| 40 EXPECT_EQ(0u, reader.BitsLeft()); |
| 41 } |
| 42 |
| 43 TEST(BitStreamReaderTest, CanReadBitGroups) { |
| 44 BitStreamReader reader( |
| 45 base::StringPiece(reinterpret_cast<const char*>(kSomeData), 3)); |
| 46 EXPECT_EQ(24u, reader.BitsLeft()); |
| 47 uint64_t v(0); |
| 48 uint64_t res(0); |
| 49 |
| 50 EXPECT_TRUE(reader.ReadBits(5, &v)); |
| 51 res |= v << 19; |
| 52 EXPECT_EQ(19u, reader.BitsLeft()); |
| 53 EXPECT_TRUE(reader.ReadBits(13, &v)); |
| 54 res |= v << 6; |
| 55 EXPECT_EQ(6u, reader.BitsLeft()); |
| 56 EXPECT_TRUE(reader.ReadBits(6, &v)); |
| 57 res |= v; |
| 58 EXPECT_EQ(UINT64_C(0xd5e2af), res); |
| 59 |
| 60 EXPECT_FALSE(reader.ReadBits(1, &v)); |
| 61 } |
| 62 |
| 63 TEST(BitStreamReaderTest, CanRead64Bit) { |
| 64 BitStreamReader reader( |
| 65 base::StringPiece(reinterpret_cast<const char*>(kSomeData), 8)); |
| 66 EXPECT_EQ(64u, reader.BitsLeft()); |
| 67 uint64_t v(0); |
| 68 |
| 69 EXPECT_TRUE(reader.ReadBits(64, &v)); |
| 70 EXPECT_EQ(UINT64_C(0xd5e2afe5bb107cd1), v); |
| 71 } |
| 72 |
| 73 TEST(BitStreamReaderTest, CanReadUnaryEncodedNumbers) { |
| 74 BitStreamReader reader( |
| 75 base::StringPiece(reinterpret_cast<const char*>(kSomeData), 3)); |
| 76 const uint64_t expected_values[] = {2, 1, 1, 4, 0, 0, 1, 1, 1, 4}; |
| 77 uint64_t v(0); |
| 78 for (int i = 0; i < 10; ++i) { |
| 79 EXPECT_TRUE(reader.ReadUnaryEncoding(&v)); |
| 80 EXPECT_EQ(expected_values[i], v) << "Values differ at position " << i; |
| 81 } |
| 82 } |
| 83 |
| 84 } // namespace internal |
| 85 |
| 86 namespace { |
| 87 |
| 88 const uint8_t kFirstHashRaw[] = |
| 89 {0x00, 0x00, 0x03, 0xd7, 0xfc, 0x18, 0x02, 0xcb}; |
| 90 std::string GetFirstHash() { |
| 91 return std::string(reinterpret_cast<const char*>(kFirstHashRaw), 8); |
| 92 } |
| 93 |
| 94 // Second hash: Diff from first hash is > 2^47 |
| 95 const uint8_t kSecondHashRaw[] = |
| 96 {0x00, 0x01, 0x05, 0xd2, 0x58, 0x47, 0xa7, 0xbf}; |
| 97 std::string GetSecondHash() { |
| 98 return std::string(reinterpret_cast<const char*>(kSecondHashRaw), 8); |
| 99 } |
| 100 |
| 101 // Third hash: Diff from 2nd hash is < 2^47 |
| 102 const uint8_t kThirdHashRaw[] = |
| 103 {0x00, 0x01, 0x48, 0x45, 0x8c, 0x53, 0x03, 0x94}; |
| 104 std::string GetThirdHash() { |
| 105 return std::string(reinterpret_cast<const char*>(kThirdHashRaw), 8); |
| 106 } |
| 107 |
| 108 const uint8_t kWhitelistData[] = { |
| 109 0x00, 0x00, 0x03, 0xd7, 0xfc, 0x18, 0x02, 0xcb, // First hash |
| 110 0xc0, 0x7e, 0x97, 0x0b, 0xe9, 0x3d, 0x10, 0x9c, |
| 111 0xcd, 0x02, 0xd6, 0xf5, 0x40, |
| 112 }; |
| 113 |
| 114 std::string GetPartialWhitelistData(uint8_t num_bytes) { |
| 115 return std::string(reinterpret_cast<const char*>(kWhitelistData), num_bytes); |
| 116 } |
| 117 |
| 118 std::string GetAllWhitelistData() { |
| 119 return GetPartialWhitelistData(arraysize(kWhitelistData)); |
| 120 } |
| 121 |
| 122 } // namespace |
| 123 |
| 124 TEST(PackedEVCertsWhitelistTest, UncompressFailsForTooShortList) { |
| 125 // This list does not contain enough bytes even for the first hash. |
| 126 std::set<std::string> res; |
| 127 EXPECT_FALSE(PackedEVCertsWhitelist::UncompressEVWhitelist( |
| 128 std::string(reinterpret_cast<const char*>(kWhitelistData), 7), &res)); |
| 129 } |
| 130 |
| 131 TEST(PackedEVCertsWhitelistTest, UncompressFailsForTruncatedList) { |
| 132 // This list is missing bits for the second part of the diff. |
| 133 std::set<std::string> res; |
| 134 EXPECT_FALSE(PackedEVCertsWhitelist::UncompressEVWhitelist( |
| 135 std::string(reinterpret_cast<const char*>(kWhitelistData), 14), &res)); |
| 136 } |
| 137 |
| 138 TEST(PackedEVCertsWhitelistTest, UncompressesWhitelistCorrectly) { |
| 139 std::set<std::string> res; |
| 140 ASSERT_TRUE(PackedEVCertsWhitelist::UncompressEVWhitelist( |
| 141 std::string(reinterpret_cast<const char*>(kWhitelistData), |
| 142 arraysize(kWhitelistData)), |
| 143 &res)); |
| 144 |
| 145 // Ensure first hash is found |
| 146 EXPECT_TRUE(res.find(GetFirstHash()) != res.end()); |
| 147 // Ensure second hash is found |
| 148 EXPECT_TRUE(res.find(GetSecondHash()) != res.end()); |
| 149 // Ensure last hash is found |
| 150 EXPECT_TRUE(res.find(GetThirdHash()) != res.end()); |
| 151 // Ensure that there are exactly 3 hashes. |
| 152 EXPECT_EQ(3u, res.size()); |
| 153 } |
| 154 |
| 155 TEST(PackedEVCertsWhitelistTest, CanFindHashInSetList) { |
| 156 scoped_refptr<PackedEVCertsWhitelist> whitelist( |
| 157 new PackedEVCertsWhitelist(GetAllWhitelistData())); |
| 158 |
| 159 EXPECT_TRUE(whitelist->IsValid()); |
| 160 EXPECT_TRUE(whitelist->ContainsCertificateHash(GetFirstHash())); |
| 161 EXPECT_TRUE(whitelist->ContainsCertificateHash(GetSecondHash())); |
| 162 EXPECT_TRUE(whitelist->ContainsCertificateHash(GetThirdHash())); |
| 163 } |
| 164 |
| 165 TEST(PackedEVCertsWhitelistTest, CorrectlyIdentifiesEmptyWhitelistIsInvalid) { |
| 166 scoped_refptr<PackedEVCertsWhitelist> whitelist( |
| 167 new PackedEVCertsWhitelist("")); |
| 168 |
| 169 EXPECT_FALSE(whitelist->IsValid()); |
| 170 } |
| 171 |
| 172 TEST(PackedEVCertsWhitelistTest, CorrectlyIdentifiesPartialWhitelistIsInvalid) { |
| 173 scoped_refptr<PackedEVCertsWhitelist> whitelist( |
| 174 new PackedEVCertsWhitelist(GetPartialWhitelistData(14))); |
| 175 |
| 176 EXPECT_FALSE(whitelist->IsValid()); |
| 177 } |
| 178 |
| 179 TEST(PackedEVCertsWhitelistTest, CorrectlyIdentifiesWhitelistIsValid) { |
| 180 scoped_refptr<PackedEVCertsWhitelist> whitelist( |
| 181 new PackedEVCertsWhitelist(GetAllWhitelistData())); |
| 182 |
| 183 EXPECT_TRUE(whitelist->IsValid()); |
| 184 } |
OLD | NEW |