Chromium Code Reviews| 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 "net/cert/ct_ev_whitelist.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace net { | |
| 12 | |
| 13 namespace ct { | |
| 14 | |
| 15 namespace internal { | |
| 16 | |
| 17 const uint8 kSomeData[] = {0xd5, 0xe2, 0xaf, 0xe5, 0xbb, 0x10, 0x7c, 0xd1}; | |
|
wtc
2014/09/03 14:29:48
A solution I used before is to initialize the char
Eran Messeri
2014/09/03 20:27:09
Acknowledged - since I only use the data once (to
| |
| 18 | |
| 19 TEST(BitStreamReaderTest, CanReadSingleByte) { | |
| 20 BitStreamReader reader(reinterpret_cast<const char*>(kSomeData), 1); | |
| 21 uint64 v(0); | |
| 22 | |
| 23 EXPECT_EQ(8u, reader.BitsLeft()); | |
| 24 EXPECT_TRUE(reader.ReadBits(8, &v)); | |
| 25 EXPECT_EQ(UINT64_C(0xd5), v); | |
| 26 | |
| 27 EXPECT_FALSE(reader.ReadBits(1, &v)); | |
| 28 EXPECT_EQ(0u, reader.BitsLeft()); | |
| 29 } | |
| 30 | |
| 31 TEST(BitStreamReaderTest, CanReadSingleBits) { | |
| 32 const uint64 expected_bits[] = {1, 1, 0, 1, 0, 1, 0, 1, | |
| 33 1, 1, 1, 0, 0, 0, 1, 0}; | |
| 34 BitStreamReader reader(reinterpret_cast<const char*>(kSomeData), 2); | |
| 35 EXPECT_EQ(16u, reader.BitsLeft()); | |
| 36 uint64 v(0); | |
| 37 | |
| 38 for (int i = 0; i < 16; ++i) { | |
| 39 EXPECT_TRUE(reader.ReadBits(1, &v)); | |
| 40 EXPECT_EQ(expected_bits[i], v); | |
| 41 } | |
| 42 EXPECT_EQ(0u, reader.BitsLeft()); | |
| 43 } | |
| 44 | |
| 45 TEST(BitStreamReaderTest, CanReadBitGroups) { | |
| 46 BitStreamReader reader(reinterpret_cast<const char*>(kSomeData), 3); | |
| 47 EXPECT_EQ(24u, reader.BitsLeft()); | |
| 48 uint64 v(0); | |
| 49 uint64 res(0); | |
| 50 | |
| 51 EXPECT_TRUE(reader.ReadBits(5, &v)); | |
| 52 res |= v << 19; | |
| 53 EXPECT_EQ(19u, reader.BitsLeft()); | |
| 54 EXPECT_TRUE(reader.ReadBits(13, &v)); | |
| 55 res |= v << 6; | |
| 56 EXPECT_EQ(6u, reader.BitsLeft()); | |
| 57 EXPECT_TRUE(reader.ReadBits(6, &v)); | |
| 58 res |= v; | |
| 59 EXPECT_EQ(UINT64_C(0xd5e2af), res); | |
| 60 | |
| 61 EXPECT_FALSE(reader.ReadBits(1, &v)); | |
| 62 } | |
| 63 | |
| 64 TEST(BitStreamReaderTest, CanRead64Bit) { | |
| 65 BitStreamReader reader(reinterpret_cast<const char*>(kSomeData), 8); | |
| 66 EXPECT_EQ(64u, reader.BitsLeft()); | |
| 67 uint64 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(reinterpret_cast<const char*>(kSomeData), 3); | |
| 75 const uint64 expected_values[] = {2, 1, 1, 4, 0, 0, 1, 1, 1, 4}; | |
| 76 uint64 v(0); | |
| 77 for (int i = 0; i < 10; ++i) { | |
| 78 EXPECT_TRUE(reader.ReadUnaryEncoding(&v)); | |
| 79 EXPECT_EQ(expected_values[i], v) << "Values differ at position " << i; | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 } // namespace internal | |
| 84 | |
| 85 const uint8 kFirstHashRaw[] = {0x00, 0x00, 0x03, 0xd7, 0xfc, 0x18, 0x02, 0xcb}; | |
| 86 const std::string kFirstHash(reinterpret_cast<const char*>(kFirstHashRaw), 8); | |
|
wtc
2014/09/03 14:29:48
IMPORTANT: Chromium doesn't allow static objects.
Eran Messeri
2014/09/03 20:27:09
Changed to use getter methods for the string repre
| |
| 87 | |
| 88 // Second hash: Diff from first hash is > 2^47 | |
| 89 const uint8 kSecondHashRaw[] = {0x00, 0x01, 0x05, 0xd2, 0x58, 0x47, 0xa7, 0xbf}; | |
| 90 const std::string kSecondHash(reinterpret_cast<const char*>(kSecondHashRaw), 8); | |
| 91 | |
| 92 // Third hash: Diff from 2nd hash is < 2^47 | |
| 93 const uint8 kThirdHashRaw[] = {0x00, 0x01, 0x48, 0x45, 0x8c, 0x53, 0x03, 0x94}; | |
| 94 const std::string kThirdHash(reinterpret_cast<const char*>(kThirdHashRaw), 8); | |
| 95 | |
| 96 const uint8 kWhitelistData[] = { | |
| 97 0x00, 0x00, 0x03, 0xd7, 0xfc, 0x18, 0x02, 0xcb, // First hash | |
| 98 0xc0, 0x7e, 0x97, 0x0b, 0xe9, 0x3d, 0x10, 0x9c, | |
| 99 0xcd, 0x02, 0xd6, 0xf5, 0x40, | |
| 100 }; | |
| 101 | |
| 102 TEST(CTEVWhitelistTest, UncompressFailsForTooShortList) { | |
| 103 // This list does not contain enough bytes even for the first hash. | |
| 104 std::set<std::string> res; | |
| 105 EXPECT_FALSE(internal::UncompressEVWhitelist( | |
| 106 std::string(reinterpret_cast<const char*>(kWhitelistData), 7), &res)); | |
| 107 } | |
| 108 | |
| 109 TEST(CTEVWhitelistTest, UncompressFailsForTruncatedList) { | |
| 110 // This list is missing bits for the second part of the diff. | |
| 111 std::set<std::string> res; | |
| 112 EXPECT_FALSE(internal::UncompressEVWhitelist( | |
| 113 std::string(reinterpret_cast<const char*>(kWhitelistData), 14), &res)); | |
| 114 } | |
| 115 | |
| 116 TEST(CTEVWhitelistTest, UncompressesWhitelistCorrectly) { | |
| 117 std::set<std::string> res; | |
| 118 ASSERT_TRUE(internal::UncompressEVWhitelist( | |
| 119 std::string(reinterpret_cast<const char*>(kWhitelistData), | |
| 120 arraysize(kWhitelistData)), | |
| 121 &res)); | |
| 122 | |
| 123 // Ensure first hash is found | |
| 124 EXPECT_TRUE(res.find(kFirstHash) != res.end()); | |
| 125 // Ensure second hash is found | |
| 126 EXPECT_TRUE(res.find(kSecondHash) != res.end()); | |
| 127 // Ensure last hash is found | |
| 128 EXPECT_TRUE(res.find(kThirdHash) != res.end()); | |
| 129 // Ensure that there are exactly 3 hashes. | |
| 130 EXPECT_EQ(3u, res.size()); | |
| 131 } | |
| 132 | |
| 133 TEST(CTEVWhitelistTest, CanFindHashInSetList) { | |
| 134 std::set<std::string> whitelist_data; | |
| 135 whitelist_data.insert(kFirstHash); | |
|
wtc
2014/09/03 14:29:48
IMPORTANT: you also need to fix this test to const
Eran Messeri
2014/09/03 20:27:09
Done.
| |
| 136 internal::SetEVWhitelistData(whitelist_data); | |
| 137 | |
| 138 EXPECT_TRUE(IsCertificateHashInWhitelist(kFirstHash)); | |
| 139 } | |
| 140 | |
| 141 TEST(CTEVWhitelistTest, CannotFindOldHashAfterSetList) { | |
| 142 std::set<std::string> whitelist_data; | |
| 143 whitelist_data.insert(kFirstHash); | |
| 144 internal::SetEVWhitelistData(whitelist_data); | |
| 145 EXPECT_TRUE(IsCertificateHashInWhitelist(kFirstHash)); | |
| 146 | |
| 147 std::set<std::string> new_whitelist_data; | |
| 148 new_whitelist_data.insert(kSecondHash); | |
| 149 internal::SetEVWhitelistData(new_whitelist_data); | |
| 150 EXPECT_TRUE(IsCertificateHashInWhitelist(kSecondHash)); | |
| 151 EXPECT_FALSE(IsCertificateHashInWhitelist(kFirstHash)); | |
| 152 } | |
| 153 | |
| 154 } // namespace ct | |
| 155 | |
| 156 } // namespace net | |
| OLD | NEW |