| Index: net/cert/ct_ev_whitelist_unittest.cc
|
| diff --git a/net/cert/ct_ev_whitelist_unittest.cc b/net/cert/ct_ev_whitelist_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9a278bdf0249c035b9d6223192ab8fb4dec5c653
|
| --- /dev/null
|
| +++ b/net/cert/ct_ev_whitelist_unittest.cc
|
| @@ -0,0 +1,137 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "net/cert/ct_ev_whitelist.h"
|
| +
|
| +#include <string>
|
| +
|
| +#include "base/base64.h"
|
| +#include "base/strings/stringprintf.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace net {
|
| +
|
| +namespace ct {
|
| +
|
| +namespace internal {
|
| +
|
| +const char kSomeData[] = {0xd5, 0xe2, 0xaf, 0xe5, 0xbb, 0x10, 0x7c, 0xd1};
|
| +
|
| +TEST(BitStreamReaderTest, CanReadSingleByte) {
|
| + BitStreamReader reader(kSomeData, 1);
|
| + uint64 v(0);
|
| +
|
| + ASSERT_EQ(8u, reader.BitsLeft());
|
| + ASSERT_TRUE(reader.ReadBits(8, &v));
|
| + ASSERT_EQ(static_cast<uint64>(0xd5), v);
|
| +
|
| + ASSERT_FALSE(reader.ReadBits(1, &v));
|
| + ASSERT_EQ(0u, reader.BitsLeft());
|
| +}
|
| +
|
| +TEST(BitStreamReaderTest, CanReadSingleBits) {
|
| + const uint64 expected_bits[] = {
|
| + 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0};
|
| + BitStreamReader reader(kSomeData, 2);
|
| + ASSERT_EQ(16u, reader.BitsLeft());
|
| + uint64 v(0);
|
| +
|
| + for (int i = 0; i < 16; ++i) {
|
| + ASSERT_TRUE(reader.ReadBits(1, &v));
|
| + ASSERT_EQ(expected_bits[i], v);
|
| + }
|
| + ASSERT_EQ(0u, reader.BitsLeft());
|
| +}
|
| +
|
| +TEST(BitStreamReaderTest, CanReadBitGroups) {
|
| + BitStreamReader reader(kSomeData, 3);
|
| + ASSERT_EQ(24u, reader.BitsLeft());
|
| + uint64 v(0);
|
| + uint64 res(0);
|
| +
|
| + ASSERT_TRUE(reader.ReadBits(5, &v));
|
| + res |= v << 19;
|
| + ASSERT_EQ(19u, reader.BitsLeft());
|
| + ASSERT_TRUE(reader.ReadBits(13, &v));
|
| + res |= v << 6;
|
| + ASSERT_EQ(6u, reader.BitsLeft());
|
| + ASSERT_TRUE(reader.ReadBits(6, &v));
|
| + res |= v;
|
| + ASSERT_EQ(static_cast<uint64>(0xd5e2af), res);
|
| +
|
| + ASSERT_FALSE(reader.ReadBits(1, &v));
|
| +}
|
| +
|
| +TEST(BitStreamReaderTest, CanRead64Bit) {
|
| + BitStreamReader reader(kSomeData, 8);
|
| + ASSERT_EQ(64u, reader.BitsLeft());
|
| + uint64 v(0);
|
| +
|
| + ASSERT_TRUE(reader.ReadBits(64, &v));
|
| + ASSERT_EQ(static_cast<uint64>(0xd5e2afe5bb107cd1), v);
|
| +}
|
| +
|
| +TEST(BitStreamReaderTest, CanReadUnaryEncodedNumbers) {
|
| + BitStreamReader reader(kSomeData, 3);
|
| + const uint64 expected_values[] = {
|
| + 2, 1, 1, 4, 0, 0, 1, 1, 1};
|
| + uint64 v(0);
|
| + for (int i = 0; i < 9; ++i) {
|
| + ASSERT_TRUE(reader.ReadUnaryEncoding(&v));
|
| + ASSERT_EQ(expected_values[i], v)
|
| + << "Expected " << expected_values[i]
|
| + << " but was " << v << " at position " << i;
|
| + }
|
| +}
|
| +
|
| +} // namespace internal
|
| +
|
| +const char kFirstHash[] = {0x00, 0x00, 0x03, 0xd7, 0xfc, 0x18, 0x02, 0xcb};
|
| + // Second hash: Diff from first hash is > 2^47
|
| +const char kSecondHash[] = {0x00, 0x01, 0x05, 0xd2, 0x58, 0x47, 0xa7, 0xbf};
|
| + // Third hash: Diff from 2nd hash is < 2^47
|
| +const char kThirdHash[] = {0x00, 0x01, 0x48, 0x45, 0x8c, 0x53, 0x03, 0x94};
|
| +
|
| +const char kWhitelistData[] = {
|
| + 0x00, 0x00, 0x03, 0xd7, 0xfc, 0x18, 0x02, 0xcb, // First hash
|
| + 0xc0, 0x7e, 0x97, 0x0b, 0xe9, 0x3d,
|
| + 0x10, 0x9c, 0xcd, 0x02, 0xd6, 0xf5, 0x40,
|
| +};
|
| +
|
| +TEST(CTEVWhitelistTest, UncompressFailsForTooShortList) {
|
| + // This list does not contain enough bytes even for the first hash.
|
| + std::set<std::string> res;
|
| + ASSERT_FALSE(
|
| + internal::UncompressEVWhitelist(std::string(kWhitelistData, 7), &res));
|
| +}
|
| +
|
| +TEST(CTEVWhitelistTest, UncompressFailsForTruncatedList) {
|
| + // This list is missing bits for the second part of the diff.
|
| + std::set<std::string> res;
|
| + ASSERT_FALSE(
|
| + internal::UncompressEVWhitelist(std::string(kWhitelistData, 14), &res));
|
| +}
|
| +
|
| +TEST(CTEVWhitelistTest, UncompressesWhitelistCorrectly) {
|
| + std::set<std::string> res;
|
| + ASSERT_TRUE(
|
| + internal::UncompressEVWhitelist(std::string(kWhitelistData, 21), &res));
|
| +
|
| + // Ensure first hash is found
|
| + ASSERT_TRUE(res.find(std::string(kFirstHash, 8)) != res.end());
|
| + // Ensure second hash is found
|
| + ASSERT_TRUE(res.find(std::string(kSecondHash, 8)) != res.end());
|
| + // Ensure last hash is found
|
| + ASSERT_TRUE(res.find(std::string(kThirdHash, 8)) != res.end());
|
| +}
|
| +
|
| +TEST(CTEVWhitelistTest, CanFindHashInSetList) {
|
| +}
|
| +
|
| +TEST(CTEVWhitelistTest, CannotFindOldHashAfterSetList) {
|
| +}
|
| +
|
| +} // namespace ct
|
| +
|
| +} // namespace net
|
|
|