This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "chrome/browser/safe_browsing/safe_browsing_store.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
TEST(SafeBrowsingStoreTest, SBAddPrefixLess) {
EXPECT_TRUE(SBAddPrefixLess(SBAddPrefix(11, 1), SBAddPrefix(10, 2)));
EXPECT_FALSE(SBAddPrefixLess(SBAddPrefix(10, 2), SBAddPrefix(11, 1)));
EXPECT_TRUE(SBAddPrefixLess(SBAddPrefix(10, 1), SBAddPrefix(11, 1)));
EXPECT_FALSE(SBAddPrefixLess(SBAddPrefix(11, 1), SBAddPrefix(10, 1)));
EXPECT_FALSE(SBAddPrefixLess(SBAddPrefix(10, 1), SBAddPrefix(10, 1)));
}
TEST(SafeBrowsingStoreTest, SBAddPrefixHashLess) {
SBFullHash one, onetwo, two;
memset(&one, 0, sizeof(one));
memset(&onetwo, 0, sizeof(onetwo));
memset(&two, 0, sizeof(two));
one.prefix = 1;
one.full_hash[sizeof(SBPrefix)] = 1;
onetwo.prefix = 1;
onetwo.full_hash[sizeof(SBPrefix)] = 2;
two.prefix = 2;
const base::Time now = base::Time::Now();
EXPECT_TRUE(SBAddPrefixHashLess(SBAddFullHash(11, now, one),
SBAddFullHash(10, now, two)));
EXPECT_FALSE(SBAddPrefixHashLess(SBAddFullHash(11, now, two),
SBAddFullHash(10, now, one)));
EXPECT_TRUE(SBAddPrefixHashLess(SBAddFullHash(10, now, one),
SBAddFullHash(11, now, onetwo)));
EXPECT_FALSE(SBAddPrefixHashLess(SBAddFullHash(11, now, one),
SBAddFullHash(10, now, onetwo)));
EXPECT_TRUE(SBAddPrefixHashLess(SBAddFullHash(10, now, one),
SBAddFullHash(10, now, onetwo)));
EXPECT_FALSE(SBAddPrefixHashLess(SBAddFullHash(10, now, onetwo),
SBAddFullHash(10, now, one)));
EXPECT_FALSE(SBAddPrefixHashLess(SBAddFullHash(10, now, one),
SBAddFullHash(10, now, one)));
}
TEST(SafeBrowsingStoreTest, SBSubPrefixLess) {
EXPECT_TRUE(SBAddPrefixLess(SBSubPrefix(12, 11, 1), SBSubPrefix(9, 10, 2)));
EXPECT_FALSE(SBAddPrefixLess(SBSubPrefix(12, 11, 2), SBSubPrefix(9, 10, 1)));
EXPECT_TRUE(SBAddPrefixLess(SBSubPrefix(12, 9, 1), SBSubPrefix(9, 10, 1)));
EXPECT_FALSE(SBAddPrefixLess(SBSubPrefix(12, 10, 1), SBSubPrefix(9, 9, 1)));
EXPECT_FALSE(SBAddPrefixLess(SBSubPrefix(12, 10, 1), SBSubPrefix(12, 10, 1)));
}
TEST(SafeBrowsingStoreTest, SBSubFullHashLess) {
SBFullHash one, onetwo, two;
memset(&one, 0, sizeof(one));
memset(&onetwo, 0, sizeof(onetwo));
memset(&two, 0, sizeof(two));
one.prefix = 1;
one.full_hash[sizeof(SBPrefix)] = 1;
onetwo.prefix = 1;
onetwo.full_hash[sizeof(SBPrefix)] = 2;
two.prefix = 2;
EXPECT_TRUE(SBAddPrefixHashLess(SBSubFullHash(12, 11, one),
SBSubFullHash(9, 10, two)));
EXPECT_FALSE(SBAddPrefixHashLess(SBSubFullHash(12, 11, two),
SBSubFullHash(9, 10, one)));
EXPECT_TRUE(SBAddPrefixHashLess(SBSubFullHash(12, 10, one),
SBSubFullHash(9, 11, onetwo)));
EXPECT_FALSE(SBAddPrefixHashLess(SBSubFullHash(12, 11, one),
SBSubFullHash(9, 10, onetwo)));
EXPECT_TRUE(SBAddPrefixHashLess(SBSubFullHash(12, 10, one),
SBSubFullHash(9, 10, onetwo)));
EXPECT_FALSE(SBAddPrefixHashLess(SBSubFullHash(12, 10, onetwo),
SBSubFullHash(9, 10, one)));
EXPECT_FALSE(SBAddPrefixHashLess(SBSubFullHash(12, 10, one),
SBSubFullHash(9, 10, one)));
}
TEST(SafeBrowsingStoreTest, SBProcessSubsEmpty) {
SBAddPrefixes add_prefixes;
std::vector<SBAddFullHash> add_hashes;
SBSubPrefixes sub_prefixes;
std::vector<SBSubFullHash> sub_hashes;
const base::hash_set<int32> no_deletions;
SBProcessSubs(&add_prefixes, &sub_prefixes, &add_hashes, &sub_hashes,
no_deletions, no_deletions);
EXPECT_TRUE(add_prefixes.empty());
EXPECT_TRUE(sub_prefixes.empty());
EXPECT_TRUE(add_hashes.empty());
EXPECT_TRUE(sub_hashes.empty());
}
TEST(SafeBrowsingStoreTest, SBProcessSubsKnockout) {
const base::Time kNow = base::Time::Now();
const SBFullHash kHash1(SBFullHashForString("one"));
const SBFullHash kHash2(SBFullHashForString("two"));
const SBFullHash kHash3(SBFullHashForString("three"));
const SBFullHash kHash4(SBFullHashForString("four"));
const int kAddChunk1 = 1;
const int kSubChunk1 = 2;
SBFullHash kHash1mod1 = kHash1;
kHash1mod1.full_hash[sizeof(kHash1mod1.full_hash) - 1] ++;
SBFullHash kHash1mod2 = kHash1mod1;
kHash1mod2.full_hash[sizeof(kHash1mod2.full_hash) - 1] ++;
SBFullHash kHash1mod3 = kHash1mod2;
kHash1mod3.full_hash[sizeof(kHash1mod3.full_hash) - 1] ++;
SBFullHash kHash4mod = kHash4;
kHash4mod.full_hash[sizeof(kHash4mod.full_hash) - 1] ++;
SBAddPrefixes add_prefixes;
std::vector<SBAddFullHash> add_hashes;
SBSubPrefixes sub_prefixes;
std::vector<SBSubFullHash> sub_hashes;
add_prefixes.push_back(SBAddPrefix(kAddChunk1, kHash1.prefix));
add_hashes.push_back(SBAddFullHash(kAddChunk1, kNow, kHash1));
add_hashes.push_back(SBAddFullHash(kAddChunk1, kNow, kHash1mod1));
sub_prefixes.push_back(SBSubPrefix(kSubChunk1, kAddChunk1, kHash1.prefix));
sub_hashes.push_back(SBSubFullHash(kSubChunk1, kAddChunk1, kHash1mod2));
sub_hashes.push_back(SBSubFullHash(kSubChunk1, kAddChunk1, kHash1mod3));
add_hashes.push_back(SBAddFullHash(kAddChunk1, kNow, kHash2));
add_prefixes.push_back(SBAddPrefix(kAddChunk1, kHash2.prefix));
sub_hashes.push_back(SBSubFullHash(kSubChunk1, kAddChunk1, kHash3));
sub_prefixes.push_back(SBSubPrefix(kSubChunk1, kAddChunk1, kHash3.prefix));
add_prefixes.push_back(SBAddPrefix(kAddChunk1, kHash4.prefix));
add_hashes.push_back(SBAddFullHash(kAddChunk1, kNow, kHash4));
add_hashes.push_back(SBAddFullHash(kAddChunk1, kNow, kHash4mod));
sub_hashes.push_back(SBSubFullHash(kSubChunk1, kAddChunk1, kHash4mod));
std::sort(add_prefixes.begin(), add_prefixes.end(),
SBAddPrefixLess<SBAddPrefix,SBAddPrefix>);
std::sort(sub_prefixes.begin(), sub_prefixes.end(),
SBAddPrefixLess<SBSubPrefix,SBSubPrefix>);
std::sort(add_hashes.begin(), add_hashes.end(),
SBAddPrefixHashLess<SBAddFullHash,SBAddFullHash>);
std::sort(sub_hashes.begin(), sub_hashes.end(),
SBAddPrefixHashLess<SBSubFullHash,SBSubFullHash>);
const base::hash_set<int32> no_deletions;
SBProcessSubs(&add_prefixes, &sub_prefixes, &add_hashes, &sub_hashes,
no_deletions, no_deletions);
ASSERT_LE(2U, add_prefixes.size());
EXPECT_EQ(2U, add_prefixes.size());
EXPECT_EQ(kAddChunk1, add_prefixes[0].chunk_id);
EXPECT_EQ(kHash4.prefix, add_prefixes[0].prefix);
EXPECT_EQ(kAddChunk1, add_prefixes[1].chunk_id);
EXPECT_EQ(kHash2.prefix, add_prefixes[1].prefix);
ASSERT_LE(2U, add_hashes.size());
EXPECT_EQ(2U, add_hashes.size());
EXPECT_EQ(kAddChunk1, add_hashes[0].chunk_id);
EXPECT_TRUE(SBFullHashEqual(kHash4, add_hashes[0].full_hash));
EXPECT_EQ(kAddChunk1, add_hashes[1].chunk_id);
EXPECT_TRUE(SBFullHashEqual(kHash2, add_hashes[1].full_hash));
ASSERT_LE(1U, sub_prefixes.size());
EXPECT_EQ(1U, sub_prefixes.size());
EXPECT_EQ(kSubChunk1, sub_prefixes[0].chunk_id);
EXPECT_EQ(kAddChunk1, sub_prefixes[0].add_chunk_id);
EXPECT_EQ(kHash3.prefix, sub_prefixes[0].add_prefix);
ASSERT_LE(1U, sub_hashes.size());
EXPECT_EQ(1U, sub_hashes.size());
EXPECT_EQ(kSubChunk1, sub_hashes[0].chunk_id);
EXPECT_EQ(kAddChunk1, sub_hashes[0].add_chunk_id);
EXPECT_TRUE(SBFullHashEqual(kHash3, sub_hashes[0].full_hash));
}
TEST(SafeBrowsingStoreTest, SBProcessSubsDeleteChunk) {
const base::Time kNow = base::Time::Now();
const SBFullHash kHash1(SBFullHashForString("one"));
const SBFullHash kHash2(SBFullHashForString("two"));
const SBFullHash kHash3(SBFullHashForString("three"));
const int kAddChunk1 = 1;
const int kSubChunk1 = 2;
SBFullHash kHash1mod1 = kHash1;
kHash1mod1.full_hash[sizeof(kHash1mod1.full_hash) - 1] ++;
SBFullHash kHash1mod2 = kHash1mod1;
kHash1mod2.full_hash[sizeof(kHash1mod2.full_hash) - 1] ++;
SBFullHash kHash1mod3 = kHash1mod2;
kHash1mod3.full_hash[sizeof(kHash1mod3.full_hash) - 1] ++;
SBAddPrefixes add_prefixes;
std::vector<SBAddFullHash> add_hashes;
SBSubPrefixes sub_prefixes;
std::vector<SBSubFullHash> sub_hashes;
add_prefixes.push_back(SBAddPrefix(kAddChunk1, kHash1.prefix));
add_hashes.push_back(SBAddFullHash(kAddChunk1, kNow, kHash1));
add_hashes.push_back(SBAddFullHash(kAddChunk1, kNow, kHash1mod1));
sub_prefixes.push_back(SBSubPrefix(kSubChunk1, kAddChunk1, kHash1.prefix));
sub_hashes.push_back(SBSubFullHash(kSubChunk1, kAddChunk1, kHash1mod2));
sub_hashes.push_back(SBSubFullHash(kSubChunk1, kAddChunk1, kHash1mod3));
add_hashes.push_back(SBAddFullHash(kAddChunk1, kNow, kHash2));
add_prefixes.push_back(SBAddPrefix(kAddChunk1, kHash2.prefix));
sub_hashes.push_back(SBSubFullHash(kSubChunk1, kAddChunk1, kHash3));
sub_prefixes.push_back(SBSubPrefix(kSubChunk1, kAddChunk1, kHash3.prefix));
std::sort(add_prefixes.begin(), add_prefixes.end(),
SBAddPrefixLess<SBAddPrefix,SBAddPrefix>);
std::sort(sub_prefixes.begin(), sub_prefixes.end(),
SBAddPrefixLess<SBSubPrefix,SBSubPrefix>);
std::sort(add_hashes.begin(), add_hashes.end(),
SBAddPrefixHashLess<SBAddFullHash,SBAddFullHash>);
std::sort(sub_hashes.begin(), sub_hashes.end(),
SBAddPrefixHashLess<SBSubFullHash,SBSubFullHash>);
const base::hash_set<int32> no_deletions;
base::hash_set<int32> add_deletions;
add_deletions.insert(kAddChunk1);
SBProcessSubs(&add_prefixes, &sub_prefixes, &add_hashes, &sub_hashes,
add_deletions, no_deletions);
EXPECT_TRUE(add_prefixes.empty());
EXPECT_TRUE(add_hashes.empty());
EXPECT_EQ(1U, sub_prefixes.size());
EXPECT_EQ(kSubChunk1, sub_prefixes[0].chunk_id);
EXPECT_EQ(kAddChunk1, sub_prefixes[0].add_chunk_id);
EXPECT_EQ(kHash3.prefix, sub_prefixes[0].add_prefix);
EXPECT_EQ(1U, sub_hashes.size());
EXPECT_EQ(kSubChunk1, sub_hashes[0].chunk_id);
EXPECT_EQ(kAddChunk1, sub_hashes[0].add_chunk_id);
EXPECT_TRUE(SBFullHashEqual(kHash3, sub_hashes[0].full_hash));
std::sort(add_prefixes.begin(), add_prefixes.end(),
SBAddPrefixLess<SBAddPrefix,SBAddPrefix>);
std::sort(sub_prefixes.begin(), sub_prefixes.end(),
SBAddPrefixLess<SBSubPrefix,SBSubPrefix>);
std::sort(add_hashes.begin(), add_hashes.end(),
SBAddPrefixHashLess<SBAddFullHash,SBAddFullHash>);
std::sort(sub_hashes.begin(), sub_hashes.end(),
SBAddPrefixHashLess<SBSubFullHash,SBSubFullHash>);
base::hash_set<int32> sub_deletions;
sub_deletions.insert(kSubChunk1);
SBProcessSubs(&add_prefixes, &sub_prefixes, &add_hashes, &sub_hashes,
no_deletions, sub_deletions);
EXPECT_TRUE(add_prefixes.empty());
EXPECT_TRUE(add_hashes.empty());
EXPECT_TRUE(sub_prefixes.empty());
EXPECT_TRUE(sub_hashes.empty());
}
TEST(SafeBrowsingStoreTest, Y2K38) {
const base::Time now = base::Time::Now();
const base::Time future = now + base::TimeDelta::FromDays(3*365);
EXPECT_GT(static_cast<int32>(future.ToTimeT()), 0)
<< " (int32)time_t is running out.";
}
}