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 <algorithm> |
| 8 #include <string> |
| 9 |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 const uint8_t kFirstHashRaw[] = |
| 15 {0x00, 0x00, 0x03, 0xd7, 0xfc, 0x18, 0x02, 0xcb}; |
| 16 std::string GetFirstHash() { |
| 17 return std::string(reinterpret_cast<const char*>(kFirstHashRaw), 8); |
| 18 } |
| 19 |
| 20 // Second hash: Diff from first hash is > 2^47 |
| 21 const uint8_t kSecondHashRaw[] = |
| 22 {0x00, 0x01, 0x05, 0xd2, 0x58, 0x47, 0xa7, 0xbf}; |
| 23 std::string GetSecondHash() { |
| 24 return std::string(reinterpret_cast<const char*>(kSecondHashRaw), 8); |
| 25 } |
| 26 |
| 27 // Third hash: Diff from 2nd hash is < 2^47 |
| 28 const uint8_t kThirdHashRaw[] = |
| 29 {0x00, 0x01, 0x48, 0x45, 0x8c, 0x53, 0x03, 0x94}; |
| 30 std::string GetThirdHash() { |
| 31 return std::string(reinterpret_cast<const char*>(kThirdHashRaw), 8); |
| 32 } |
| 33 |
| 34 const uint8_t kWhitelistData[] = { |
| 35 0x00, 0x00, 0x03, 0xd7, 0xfc, 0x18, 0x02, 0xcb, // First hash |
| 36 0xc0, 0x7e, 0x97, 0x0b, 0xe9, 0x3d, 0x10, 0x9c, |
| 37 0xcd, 0x02, 0xd6, 0xf5, 0x40, |
| 38 }; |
| 39 |
| 40 std::string GetPartialWhitelistData(uint8_t num_bytes) { |
| 41 return std::string(reinterpret_cast<const char*>(kWhitelistData), num_bytes); |
| 42 } |
| 43 |
| 44 std::string GetAllWhitelistData() { |
| 45 return GetPartialWhitelistData(arraysize(kWhitelistData)); |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 TEST(PackedEVCertsWhitelistTest, UncompressFailsForTooShortList) { |
| 51 // This list does not contain enough bytes even for the first hash. |
| 52 std::vector<std::string> res; |
| 53 EXPECT_FALSE(PackedEVCertsWhitelist::UncompressEVWhitelist( |
| 54 std::string(reinterpret_cast<const char*>(kWhitelistData), 7), &res)); |
| 55 } |
| 56 |
| 57 TEST(PackedEVCertsWhitelistTest, UncompressFailsForTruncatedList) { |
| 58 // This list is missing bits for the second part of the diff. |
| 59 std::vector<std::string> res; |
| 60 EXPECT_FALSE(PackedEVCertsWhitelist::UncompressEVWhitelist( |
| 61 std::string(reinterpret_cast<const char*>(kWhitelistData), 14), &res)); |
| 62 } |
| 63 |
| 64 TEST(PackedEVCertsWhitelistTest, UncompressesWhitelistCorrectly) { |
| 65 std::vector<std::string> res; |
| 66 ASSERT_TRUE(PackedEVCertsWhitelist::UncompressEVWhitelist( |
| 67 std::string(reinterpret_cast<const char*>(kWhitelistData), |
| 68 arraysize(kWhitelistData)), |
| 69 &res)); |
| 70 |
| 71 // Ensure first hash is found |
| 72 EXPECT_TRUE(std::find(res.begin(), res.end(), GetFirstHash()) != res.end()); |
| 73 // Ensure second hash is found |
| 74 EXPECT_TRUE(std::find(res.begin(), res.end(), GetSecondHash()) != res.end()); |
| 75 // Ensure last hash is found |
| 76 EXPECT_TRUE(std::find(res.begin(), res.end(), GetThirdHash()) != res.end()); |
| 77 // Ensure that there are exactly 3 hashes. |
| 78 EXPECT_EQ(3u, res.size()); |
| 79 } |
| 80 |
| 81 TEST(PackedEVCertsWhitelistTest, CanFindHashInSetList) { |
| 82 scoped_refptr<PackedEVCertsWhitelist> whitelist( |
| 83 new PackedEVCertsWhitelist(GetAllWhitelistData())); |
| 84 |
| 85 EXPECT_TRUE(whitelist->IsValid()); |
| 86 EXPECT_TRUE(whitelist->ContainsCertificateHash(GetFirstHash())); |
| 87 EXPECT_TRUE(whitelist->ContainsCertificateHash(GetSecondHash())); |
| 88 EXPECT_TRUE(whitelist->ContainsCertificateHash(GetThirdHash())); |
| 89 } |
| 90 |
| 91 TEST(PackedEVCertsWhitelistTest, CorrectlyIdentifiesEmptyWhitelistIsInvalid) { |
| 92 scoped_refptr<PackedEVCertsWhitelist> whitelist( |
| 93 new PackedEVCertsWhitelist("")); |
| 94 |
| 95 EXPECT_FALSE(whitelist->IsValid()); |
| 96 } |
| 97 |
| 98 TEST(PackedEVCertsWhitelistTest, CorrectlyIdentifiesPartialWhitelistIsInvalid) { |
| 99 scoped_refptr<PackedEVCertsWhitelist> whitelist( |
| 100 new PackedEVCertsWhitelist(GetPartialWhitelistData(14))); |
| 101 |
| 102 EXPECT_FALSE(whitelist->IsValid()); |
| 103 } |
| 104 |
| 105 TEST(PackedEVCertsWhitelistTest, CorrectlyIdentifiesWhitelistIsValid) { |
| 106 scoped_refptr<PackedEVCertsWhitelist> whitelist( |
| 107 new PackedEVCertsWhitelist(GetAllWhitelistData())); |
| 108 |
| 109 EXPECT_TRUE(whitelist->IsValid()); |
| 110 } |
OLD | NEW |