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 "base/base64.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace net { |
| 14 |
| 15 namespace ct { |
| 16 |
| 17 namespace internal { |
| 18 |
| 19 const char kSomeData[] = {0xd5, 0xe2, 0xaf, 0xe5, 0xbb, 0x10, 0x7c, 0xd1}; |
| 20 |
| 21 TEST(BitStreamReaderTest, CanReadSingleByte) { |
| 22 BitStreamReader reader(kSomeData, 1); |
| 23 uint64 v(0); |
| 24 |
| 25 ASSERT_EQ(8u, reader.BitsLeft()); |
| 26 ASSERT_TRUE(reader.ReadBits(8, &v)); |
| 27 ASSERT_EQ(static_cast<uint64>(0xd5), v); |
| 28 |
| 29 ASSERT_FALSE(reader.ReadBits(1, &v)); |
| 30 ASSERT_EQ(0u, reader.BitsLeft()); |
| 31 } |
| 32 |
| 33 TEST(BitStreamReaderTest, CanReadSingleBits) { |
| 34 const uint64 expected_bits[] = { |
| 35 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0}; |
| 36 BitStreamReader reader(kSomeData, 2); |
| 37 ASSERT_EQ(16u, reader.BitsLeft()); |
| 38 uint64 v(0); |
| 39 |
| 40 for (int i = 0; i < 16; ++i) { |
| 41 ASSERT_TRUE(reader.ReadBits(1, &v)); |
| 42 ASSERT_EQ(expected_bits[i], v); |
| 43 } |
| 44 ASSERT_EQ(0u, reader.BitsLeft()); |
| 45 } |
| 46 |
| 47 TEST(BitStreamReaderTest, CanReadBitGroups) { |
| 48 BitStreamReader reader(kSomeData, 3); |
| 49 ASSERT_EQ(24u, reader.BitsLeft()); |
| 50 uint64 v(0); |
| 51 uint64 res(0); |
| 52 |
| 53 ASSERT_TRUE(reader.ReadBits(5, &v)); |
| 54 res |= v << 19; |
| 55 ASSERT_EQ(19u, reader.BitsLeft()); |
| 56 ASSERT_TRUE(reader.ReadBits(13, &v)); |
| 57 res |= v << 6; |
| 58 ASSERT_EQ(6u, reader.BitsLeft()); |
| 59 ASSERT_TRUE(reader.ReadBits(6, &v)); |
| 60 res |= v; |
| 61 ASSERT_EQ(static_cast<uint64>(0xd5e2af), res); |
| 62 |
| 63 ASSERT_FALSE(reader.ReadBits(1, &v)); |
| 64 } |
| 65 |
| 66 TEST(BitStreamReaderTest, CanRead64Bit) { |
| 67 BitStreamReader reader(kSomeData, 8); |
| 68 ASSERT_EQ(64u, reader.BitsLeft()); |
| 69 uint64 v(0); |
| 70 |
| 71 ASSERT_TRUE(reader.ReadBits(64, &v)); |
| 72 ASSERT_EQ(static_cast<uint64>(0xd5e2afe5bb107cd1), v); |
| 73 } |
| 74 |
| 75 TEST(BitStreamReaderTest, CanReadUnaryEncodedNumbers) { |
| 76 BitStreamReader reader(kSomeData, 3); |
| 77 const uint64 expected_values[] = { |
| 78 2, 1, 1, 4, 0, 0, 1, 1, 1}; |
| 79 uint64 v(0); |
| 80 for (int i = 0; i < 9; ++i) { |
| 81 ASSERT_TRUE(reader.ReadUnaryEncoding(&v)); |
| 82 ASSERT_EQ(expected_values[i], v) |
| 83 << "Expected " << expected_values[i] |
| 84 << " but was " << v << " at position " << i; |
| 85 } |
| 86 } |
| 87 |
| 88 } // namespace internal |
| 89 |
| 90 const char kFirstHash[] = {0x00, 0x00, 0x03, 0xd7, 0xfc, 0x18, 0x02, 0xcb}; |
| 91 // Second hash: Diff from first hash is > 2^47 |
| 92 const char kSecondHash[] = {0x00, 0x01, 0x05, 0xd2, 0x58, 0x47, 0xa7, 0xbf}; |
| 93 // Third hash: Diff from 2nd hash is < 2^47 |
| 94 const char kThirdHash[] = {0x00, 0x01, 0x48, 0x45, 0x8c, 0x53, 0x03, 0x94}; |
| 95 |
| 96 const char kWhitelistData[] = { |
| 97 0x00, 0x00, 0x03, 0xd7, 0xfc, 0x18, 0x02, 0xcb, // First hash |
| 98 0xc0, 0x7e, 0x97, 0x0b, 0xe9, 0x3d, |
| 99 0x10, 0x9c, 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 ASSERT_FALSE( |
| 106 internal::UncompressEVWhitelist(std::string(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 ASSERT_FALSE( |
| 113 internal::UncompressEVWhitelist(std::string(kWhitelistData, 14), &res)); |
| 114 } |
| 115 |
| 116 TEST(CTEVWhitelistTest, UncompressesWhitelistCorrectly) { |
| 117 std::set<std::string> res; |
| 118 ASSERT_TRUE( |
| 119 internal::UncompressEVWhitelist(std::string(kWhitelistData, 21), &res)); |
| 120 |
| 121 // Ensure first hash is found |
| 122 ASSERT_TRUE(res.find(std::string(kFirstHash, 8)) != res.end()); |
| 123 // Ensure second hash is found |
| 124 ASSERT_TRUE(res.find(std::string(kSecondHash, 8)) != res.end()); |
| 125 // Ensure last hash is found |
| 126 ASSERT_TRUE(res.find(std::string(kThirdHash, 8)) != res.end()); |
| 127 } |
| 128 |
| 129 TEST(CTEVWhitelistTest, CanFindHashInSetList) { |
| 130 } |
| 131 |
| 132 TEST(CTEVWhitelistTest, CannotFindOldHashAfterSetList) { |
| 133 } |
| 134 |
| 135 } // namespace ct |
| 136 |
| 137 } // namespace net |
OLD | NEW |