summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorshess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-18 23:53:11 +0000
committershess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-18 23:53:11 +0000
commiteef5337c72da6d28a00b45df020ac9054d58e5f2 (patch)
treec3453463c683f504aebd3b38d6011af897aeb1f2 /chrome
parent07203723a0165189c821fa5f2bb54e4bff122a6e (diff)
downloadchromium_src-eef5337c72da6d28a00b45df020ac9054d58e5f2.zip
chromium_src-eef5337c72da6d28a00b45df020ac9054d58e5f2.tar.gz
chromium_src-eef5337c72da6d28a00b45df020ac9054d58e5f2.tar.bz2
Remove dead code from safe_browsing_util.h/cc.
BUG=none TEST=none Review URL: http://codereview.chromium.org/630012 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@39398 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/safe_browsing/protocol_parser_unittest.cc36
-rw-r--r--chrome/browser/safe_browsing/safe_browsing_database_bloom.cc6
-rw-r--r--chrome/browser/safe_browsing/safe_browsing_util.cc342
-rw-r--r--chrome/browser/safe_browsing/safe_browsing_util.h106
-rw-r--r--chrome/browser/safe_browsing/safe_browsing_util_unittest.cc230
5 files changed, 73 insertions, 647 deletions
diff --git a/chrome/browser/safe_browsing/protocol_parser_unittest.cc b/chrome/browser/safe_browsing/protocol_parser_unittest.cc
index f20cb29..1796bd3 100644
--- a/chrome/browser/safe_browsing/protocol_parser_unittest.cc
+++ b/chrome/browser/safe_browsing/protocol_parser_unittest.cc
@@ -29,12 +29,14 @@ TEST(SafeBrowsingProtocolParsingTest, TestAddChunk) {
EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161);
SBEntry* entry = chunks[0].hosts[0].entry;
- EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX);
+ EXPECT_TRUE(entry->IsAdd());
+ EXPECT_TRUE(entry->IsPrefix());
EXPECT_EQ(entry->prefix_count(), 0);
EXPECT_EQ(chunks[0].hosts[1].host, 0x31313131);
entry = chunks[0].hosts[1].entry;
- EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX);
+ EXPECT_TRUE(entry->IsAdd());
+ EXPECT_TRUE(entry->IsPrefix());
EXPECT_EQ(entry->prefix_count(), 3);
EXPECT_EQ(entry->PrefixAt(0), 0x32323232);
EXPECT_EQ(entry->PrefixAt(1), 0x33333333);
@@ -42,7 +44,8 @@ TEST(SafeBrowsingProtocolParsingTest, TestAddChunk) {
EXPECT_EQ(chunks[0].hosts[2].host, 0x37373737);
entry = chunks[0].hosts[2].entry;
- EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX);
+ EXPECT_TRUE(entry->IsAdd());
+ EXPECT_TRUE(entry->IsPrefix());
EXPECT_EQ(entry->prefix_count(), 2);
EXPECT_EQ(entry->PrefixAt(0), 0x38383838);
EXPECT_EQ(entry->PrefixAt(1), 0x39393939);
@@ -79,7 +82,8 @@ TEST(SafeBrowsingProtocolParsingTest, TestAddFullChunk) {
EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161);
SBEntry* entry = chunks[0].hosts[0].entry;
- EXPECT_EQ(entry->type(), SBEntry::ADD_FULL_HASH);
+ EXPECT_TRUE(entry->IsAdd());
+ EXPECT_FALSE(entry->IsPrefix());
EXPECT_EQ(entry->prefix_count(), 2);
EXPECT_TRUE(entry->FullHashAt(0) == full_hash1);
EXPECT_TRUE(entry->FullHashAt(1) == full_hash2);
@@ -109,12 +113,14 @@ TEST(SafeBrowsingProtocolParsingTest, TestAddChunks) {
EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161);
SBEntry* entry = chunks[0].hosts[0].entry;
- EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX);
+ EXPECT_TRUE(entry->IsAdd());
+ EXPECT_TRUE(entry->IsPrefix());
EXPECT_EQ(entry->prefix_count(), 0);
EXPECT_EQ(chunks[0].hosts[1].host, 0x31313131);
entry = chunks[0].hosts[1].entry;
- EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX);
+ EXPECT_TRUE(entry->IsAdd());
+ EXPECT_TRUE(entry->IsPrefix());
EXPECT_EQ(entry->prefix_count(), 3);
EXPECT_EQ(entry->PrefixAt(0), 0x32323232);
EXPECT_EQ(entry->PrefixAt(1), 0x33333333);
@@ -122,7 +128,8 @@ TEST(SafeBrowsingProtocolParsingTest, TestAddChunks) {
EXPECT_EQ(chunks[0].hosts[2].host, 0x37373737);
entry = chunks[0].hosts[2].entry;
- EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX);
+ EXPECT_TRUE(entry->IsAdd());
+ EXPECT_TRUE(entry->IsPrefix());
EXPECT_EQ(entry->prefix_count(), 2);
EXPECT_EQ(entry->PrefixAt(0), 0x38383838);
EXPECT_EQ(entry->PrefixAt(1), 0x39393939);
@@ -133,7 +140,8 @@ TEST(SafeBrowsingProtocolParsingTest, TestAddChunks) {
EXPECT_EQ(chunks[1].hosts[0].host, 0x35353535);
entry = chunks[1].hosts[0].entry;
- EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX);
+ EXPECT_TRUE(entry->IsAdd());
+ EXPECT_TRUE(entry->IsPrefix());
EXPECT_EQ(entry->prefix_count(), 2);
EXPECT_EQ(entry->PrefixAt(0), 0x70707070);
EXPECT_EQ(entry->PrefixAt(1), 0x67676767);
@@ -195,13 +203,15 @@ TEST(SafeBrowsingProtocolParsingTest, TestSubChunk) {
EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161);
SBEntry* entry = chunks[0].hosts[0].entry;
- EXPECT_EQ(entry->type(), SBEntry::SUB_PREFIX);
+ EXPECT_TRUE(entry->IsSub());
+ EXPECT_TRUE(entry->IsPrefix());
EXPECT_EQ(entry->chunk_id(), 0x6b6b6b6b);
EXPECT_EQ(entry->prefix_count(), 0);
EXPECT_EQ(chunks[0].hosts[1].host, 0x31313131);
entry = chunks[0].hosts[1].entry;
- EXPECT_EQ(entry->type(), SBEntry::SUB_PREFIX);
+ EXPECT_TRUE(entry->IsSub());
+ EXPECT_TRUE(entry->IsPrefix());
EXPECT_EQ(entry->prefix_count(), 3);
EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x7a7a7a7a);
EXPECT_EQ(entry->PrefixAt(0), 0x32323232);
@@ -212,7 +222,8 @@ TEST(SafeBrowsingProtocolParsingTest, TestSubChunk) {
EXPECT_EQ(chunks[0].hosts[2].host, 0x37373737);
entry = chunks[0].hosts[2].entry;
- EXPECT_EQ(entry->type(), SBEntry::SUB_PREFIX);
+ EXPECT_TRUE(entry->IsSub());
+ EXPECT_TRUE(entry->IsPrefix());
EXPECT_EQ(entry->prefix_count(), 2);
EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x79797979);
EXPECT_EQ(entry->PrefixAt(0), 0x38383838);
@@ -253,7 +264,8 @@ TEST(SafeBrowsingProtocolParsingTest, TestSubFullChunk) {
EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161);
SBEntry* entry = chunks[0].hosts[0].entry;
- EXPECT_EQ(entry->type(), SBEntry::SUB_FULL_HASH);
+ EXPECT_TRUE(entry->IsSub());
+ EXPECT_FALSE(entry->IsPrefix());
EXPECT_EQ(entry->prefix_count(), 2);
EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x79797979);
EXPECT_TRUE(entry->FullHashAt(0) == full_hash1);
diff --git a/chrome/browser/safe_browsing/safe_browsing_database_bloom.cc b/chrome/browser/safe_browsing/safe_browsing_database_bloom.cc
index 6a40167..d818881 100644
--- a/chrome/browser/safe_browsing/safe_browsing_database_bloom.cc
+++ b/chrome/browser/safe_browsing/safe_browsing_database_bloom.cc
@@ -1103,7 +1103,8 @@ void SafeBrowsingDatabaseBloom::InsertAdd(SBPrefix host, SBEntry* entry) {
STATS_COUNTER("SB.HostInsert", 1);
int encoded = EncodeChunkId(entry->chunk_id(), entry->list_id());
- if (entry->type() == SBEntry::ADD_FULL_HASH) {
+ DCHECK(entry->IsAdd());
+ if (!entry->IsPrefix()) {
base::Time receive_time = base::Time::Now();
for (int i = 0; i < entry->prefix_count(); ++i) {
SBFullHash full_hash = entry->FullHashAt(i);
@@ -1180,7 +1181,8 @@ void SafeBrowsingDatabaseBloom::InsertSub(
int encoded = EncodeChunkId(chunk_id, entry->list_id());
int encoded_add;
- if (entry->type() == SBEntry::SUB_FULL_HASH) {
+ DCHECK(entry->IsSub());
+ if (!entry->IsPrefix()) {
for (int i = 0; i < entry->prefix_count(); ++i) {
SBFullHash full_hash = entry->FullHashAt(i);
SBPrefix prefix = full_hash.prefix;
diff --git a/chrome/browser/safe_browsing/safe_browsing_util.cc b/chrome/browser/safe_browsing/safe_browsing_util.cc
index 2d6cd9c..5386f27 100644
--- a/chrome/browser/safe_browsing/safe_browsing_util.cc
+++ b/chrome/browser/safe_browsing/safe_browsing_util.cc
@@ -43,8 +43,21 @@ void SBEntry::Destroy() {
free(this);
}
-bool SBEntry::IsValid() const {
- return IsAdd() || IsSub();
+// static
+int SBEntry::PrefixSize(Type type) {
+ switch (type) {
+ case ADD_PREFIX:
+ return sizeof(SBPrefix);
+ case ADD_FULL_HASH:
+ return sizeof(SBFullHash);
+ case SUB_PREFIX:
+ return sizeof(SBSubPrefix);
+ case SUB_FULL_HASH:
+ return sizeof(SBSubFullHash);
+ default:
+ NOTREACHED();
+ return 0;
+ }
}
int SBEntry::Size() const {
@@ -66,88 +79,6 @@ SBEntry* SBEntry::Enlarge(int extra_prefixes) {
return rv;
}
-void SBEntry::RemovePrefix(int index) {
- DCHECK(index < prefix_count());
- int bytes_to_copy = PrefixSize(type()) * (prefix_count() - index - 1);
- void* to;
- switch (type()) {
- case ADD_PREFIX:
- to = &add_prefixes_[index];
- break;
- case ADD_FULL_HASH:
- to = &add_full_hashes_[index];
- break;
- case SUB_PREFIX:
- to = &sub_prefixes_[index];
- break;
- case SUB_FULL_HASH:
- to = &sub_full_hashes_[index];
- break;
- default:
- NOTREACHED();
- return;
- }
-
- char* from = reinterpret_cast<char*>(to) + PrefixSize(type());
- memmove(to, from, bytes_to_copy);
- set_prefix_count(prefix_count() - 1);
-}
-
-bool SBEntry::PrefixesMatch(int index,
- const SBEntry* other,
- int other_index) const {
- if (IsPrefix() != other->IsPrefix() || IsAdd() == other->IsAdd() ||
- ChunkIdAtPrefix(index) != other->ChunkIdAtPrefix(other_index))
- return false;
-
- return IsPrefix() ? (PrefixAt(index) == other->PrefixAt(other_index)) :
- (FullHashAt(index) == other->FullHashAt(other_index));
-}
-
-bool SBEntry::AddPrefixMatches(int index, const SBFullHash& full_hash) const {
- DCHECK(IsAdd());
-
- if (!IsPrefix())
- return full_hash == add_full_hashes_[index];
-
- SBPrefix prefix;
- memcpy(&prefix, &full_hash, sizeof(SBPrefix));
- return prefix == add_prefixes_[index];
-}
-
-bool SBEntry::IsPrefix() const {
- return type() == ADD_PREFIX || type() == SUB_PREFIX;
-}
-
-bool SBEntry::IsAdd() const {
- return type() == ADD_PREFIX || type() == ADD_FULL_HASH;
-}
-
-bool SBEntry::IsSub() const {
- return type() == SUB_PREFIX || type() == SUB_FULL_HASH;
-}
-
-int SBEntry::HashLen() const {
- return IsPrefix() ? sizeof(SBPrefix) : sizeof(SBFullHash);
-}
-
-// static
-int SBEntry::PrefixSize(Type type) {
- switch (type) {
- case ADD_PREFIX:
- return sizeof(SBPrefix);
- case ADD_FULL_HASH:
- return sizeof(SBFullHash);
- case SUB_PREFIX:
- return sizeof(SBSubPrefix);
- case SUB_FULL_HASH:
- return sizeof(SBSubFullHash);
- default:
- NOTREACHED();
- return 0;
- }
-}
-
int SBEntry::ChunkIdAtPrefix(int index) const {
if (type() == SUB_PREFIX)
return sub_prefixes_[index].add_chunk;
@@ -195,249 +126,6 @@ void SBEntry::SetFullHashAt(int index, const SBFullHash& full_hash) {
}
-// SBHostInfo ------------------------------------------------------------------
-
-SBHostInfo::SBHostInfo() : size_(0) {
-}
-
-bool SBHostInfo::Initialize(const void* data, int size) {
- size_ = size;
- if (!size_)
- return true;
-
- data_.reset(new char[size_]);
- memcpy(data_.get(), data, size_);
- if (!IsValid()) {
- size_ = 0;
- data_.reset();
- return false;
- }
-
- return true;
-}
-
-void SBHostInfo::AddPrefixes(SBEntry* entry) {
- DCHECK(entry->IsAdd());
- bool insert_entry = true;
- const SBEntry* sub_entry = NULL;
- // Remove any prefixes for which a sub already came.
- while (GetNextEntry(&sub_entry)) {
- if (sub_entry->IsAdd() || entry->list_id() != sub_entry->list_id())
- continue;
-
- if (!sub_entry->prefix_count()) {
- if (entry->chunk_id() != sub_entry->chunk_id())
- continue;
-
- // We don't want to add any of these prefixes so just return. Also no
- // more need to store the sub chunk data around for this chunk_id so
- // remove it.
- RemoveSubEntry(entry->list_id(), entry->chunk_id());
- return;
- }
-
- // Remove any matching prefixes.
- for (int i = 0; i < sub_entry->prefix_count(); ++i) {
- for (int j = 0; j < entry->prefix_count(); ++j) {
- if (entry->PrefixesMatch(j, sub_entry, i)) {
- entry->RemovePrefix(j--);
- if (!entry->prefix_count()) {
- // The add entry used to have prefixes, but they were all removed
- // because of matching sub entries. We don't want to add this
- // empty add entry, because it would block that entire host.
- insert_entry = false;
- }
- }
- }
- }
-
- RemoveSubEntry(entry->list_id(), entry->chunk_id());
- break;
- }
-
- if (insert_entry)
- Add(entry);
- DCHECK(IsValid());
-}
-
-void SBHostInfo::RemovePrefixes(SBEntry* sub_entry, bool persist) {
- DCHECK(sub_entry->IsSub());
- scoped_array<char> new_data(new char[size_]);
- char* write_ptr = new_data.get();
- int new_size = 0;
- const SBEntry* add_entry = NULL;
- // Remove any of the prefixes that are in the database.
- while (GetNextEntry(&add_entry)) {
- SBEntry* new_add_entry = const_cast<SBEntry*>(add_entry);
- scoped_array<char> data;
- if (add_entry->IsAdd() && add_entry->list_id() == sub_entry->list_id()) {
- if (!sub_entry->prefix_count() &&
- add_entry->chunk_id() == sub_entry->chunk_id()) {
- // When prefixes are empty, that means we want to remove the entry for
- // that host key completely. No need to add this sub chunk to the db.
- persist = false;
- continue;
- }
-
- if (sub_entry->prefix_count() && add_entry->prefix_count()) {
- // Remove any of the sub prefixes from these add prefixes.
- data.reset(new char[add_entry->Size()]);
- new_add_entry = reinterpret_cast<SBEntry*>(data.get());
- memcpy(new_add_entry, add_entry, add_entry->Size());
-
- for (int i = 0; i < new_add_entry->prefix_count(); ++i) {
- for (int j = 0; j < sub_entry->prefix_count(); ++j) {
- if (!sub_entry->PrefixesMatch(j, new_add_entry, i))
- continue;
-
- new_add_entry->RemovePrefix(i--);
- sub_entry->RemovePrefix(j--);
- if (!sub_entry->prefix_count())
- persist = false; // Sub entry is all used up.
-
- break;
- }
- }
- }
- }
-
- // If we didn't modify the entry, then add it. Else if we modified it,
- // then only add it if there are prefixes left. Otherwise, it it had n
- // prefixes and now it has 0, if we were to add it that would mean all
- // prefixes from that host are in the database.
- if (new_add_entry == add_entry || new_add_entry->prefix_count()) {
- memcpy(write_ptr, new_add_entry, new_add_entry->Size());
- new_size += new_add_entry->Size();
- write_ptr += new_add_entry->Size();
- }
- }
-
- if (persist && new_size == size_) {
- // We didn't find any matches because the sub came before the add, so save
- // it for later.
- Add(sub_entry);
- return;
- }
-
- size_ = new_size;
- data_.reset(new_data.release());
- DCHECK(IsValid());
-}
-
-bool SBHostInfo::Contains(const std::vector<SBFullHash>& prefixes,
- int* list_id,
- std::vector<SBPrefix>* prefix_hits) {
- prefix_hits->clear();
- *list_id = -1;
- bool hits = false;
- const SBEntry* add_entry = NULL;
- while (GetNextEntry(&add_entry)) {
- if (add_entry->IsSub())
- continue;
-
- if (!add_entry->prefix_count()) {
- // This means all paths for this url are blacklisted.
- return true;
- }
-
- for (int i = 0; i < add_entry->prefix_count(); ++i) {
- for (size_t j = 0; j < prefixes.size(); ++j) {
- if (!add_entry->AddPrefixMatches(i, prefixes[j]))
- continue;
-
- hits = true;
- if (!add_entry->IsPrefix())
- *list_id = add_entry->list_id();
- else
- prefix_hits->push_back(add_entry->PrefixAt(i));
- }
- }
- }
-
- return hits;
-}
-
-bool SBHostInfo::IsValid() {
- const SBEntry* entry = NULL;
- while (GetNextEntry(&entry)) {
- if (!entry->IsValid())
- return false;
- }
- return true;
-}
-
-bool SBHostInfo::GetNextEntry(const SBEntry** entry) {
- // It is an error to call this function with a |*entry| outside of |data_|.
- const char* current = reinterpret_cast<const char*>(*entry);
- DCHECK(!current || current >= data_.get());
-
- // Compute the address of the next entry.
- const char* next = current ? (current + (*entry)->Size()) : data_.get();
- const char* end = data_.get() + size_;
- DCHECK(next <= end);
- const SBEntry* next_entry = reinterpret_cast<const SBEntry*>(next);
-
- // Validate that there is a next entry and it's wholly contained inside of
- // |data_|.
- //
- // NOTE: The short-circuit is important, as |next_entry| may be NULL or
- // garbage when we're at the end.
- if (next == end || (end - next) < next_entry->Size())
- return false;
-
- *entry = next_entry;
- return true;
-}
-
-void SBHostInfo::Add(const SBEntry* entry) {
- int new_size = size_ + entry->Size();
- char* new_data = new char[new_size];
- memcpy(new_data, data_.get(), size_);
- memcpy(new_data + size_, entry, entry->Size());
- data_.reset(new_data);
- size_ = new_size;
- DCHECK(IsValid());
-}
-
-void SBHostInfo::RemoveSubEntry(int list_id, int chunk_id) {
- scoped_array<char> new_data(new char[size_]);
- char* write_ptr = new_data.get();
- int new_size = 0;
- const SBEntry* entry = NULL;
- while (GetNextEntry(&entry)) {
- SBEntry* new_sub_entry = const_cast<SBEntry*>(entry);
- scoped_array<char> data;
- if (entry->list_id() == list_id && entry->IsSub()) {
- if (entry->prefix_count()) {
- // Make a copy of the entry so that we can modify it.
- data.reset(new char[entry->Size()]);
- new_sub_entry = reinterpret_cast<SBEntry*>(data.get());
- memcpy(new_sub_entry, entry, entry->Size());
-
- // Remove any matching prefixes.
- for (int i = 0; i < new_sub_entry->prefix_count(); ++i) {
- if (new_sub_entry->ChunkIdAtPrefix(i) == chunk_id)
- new_sub_entry->RemovePrefix(i--);
- }
-
- if (!new_sub_entry->prefix_count())
- continue; // We removed the last prefix in the entry, so remove it.
- } else if (entry->chunk_id() == chunk_id) {
- continue;
- }
- }
-
- memcpy(write_ptr, new_sub_entry, new_sub_entry->Size());
- new_size += new_sub_entry->Size();
- write_ptr += new_sub_entry->Size();
- }
-
- size_ = new_size;
- data_.reset(new_data.release());
- DCHECK(IsValid());
-}
-
-
// Utility functions -----------------------------------------------------------
namespace safe_browsing_util {
diff --git a/chrome/browser/safe_browsing/safe_browsing_util.h b/chrome/browser/safe_browsing/safe_browsing_util.h
index 4e9104d..21fd7e3 100644
--- a/chrome/browser/safe_browsing/safe_browsing_util.h
+++ b/chrome/browser/safe_browsing/safe_browsing_util.h
@@ -108,53 +108,35 @@ class SBEntry {
// Frees the entry's memory.
void Destroy();
- // Returns whether this entry is internally consistent.
- bool IsValid() const;
-
- // Returns how many bytes this entry is.
- int Size() const;
-
- // Helper to return how much memory a given Entry would require.
- static int Size(Type type, int prefix_count);
-
void set_list_id(int list_id) { data_.list_id = list_id; }
int list_id() const { return data_.list_id; }
void set_chunk_id(int chunk_id) { data_.chunk_id = chunk_id; }
int chunk_id() const { return data_.chunk_id; }
int prefix_count() const { return data_.prefix_count; }
- Type type() const { return data_.type; }
// Returns a new entry that is larger by the given number of prefixes, with
// all the existing data already copied over. The old entry is destroyed.
SBEntry* Enlarge(int extra_prefixes);
- // Removes the prefix at the given index.
- void RemovePrefix(int index);
-
- // Returns true if the prefix/hash at the given index is equal to a
- // prefix/hash at another entry's index. Works with all combinations of
- // add/subs as long as they're the same size. Also checks chunk_ids.
- bool PrefixesMatch(int index, const SBEntry* other, int other_index) const;
-
- // Returns true if the add prefix/hash at the given index is equal to the
- // given full hash.
- bool AddPrefixMatches(int index, const SBFullHash& full_hash) const;
-
// Returns true if this is a prefix as opposed to a full hash.
- bool IsPrefix() const;
+ bool IsPrefix() const {
+ return type() == ADD_PREFIX || type() == SUB_PREFIX;
+ }
// Returns true if this is an add entry.
- bool IsAdd() const;
+ bool IsAdd() const {
+ return type() == ADD_PREFIX || type() == ADD_FULL_HASH;
+ }
// Returns true if this is a sub entry.
- bool IsSub() const;
+ bool IsSub() const {
+ return type() == SUB_PREFIX || type() == SUB_FULL_HASH;
+ }
// Helper to return the size of the prefixes.
- int HashLen() const;
-
- // Helper to return the size of each prefix entry (i.e. for subs this
- // includes an add chunk id).
- static int PrefixSize(Type type);
+ int HashLen() const {
+ return IsPrefix() ? sizeof(SBPrefix) : sizeof(SBFullHash);
+ }
// For add entries, returns the add chunk id. For sub entries, returns the
// add_chunk id for the prefix at the given index.
@@ -202,6 +184,18 @@ class SBEntry {
SBEntry();
~SBEntry();
+ // Helper to return the size of each prefix entry (i.e. for subs this
+ // includes an add chunk id).
+ static int PrefixSize(Type type);
+
+ // Helper to return how much memory a given Entry would require.
+ static int Size(Type type, int prefix_count);
+
+ // Returns how many bytes this entry is.
+ int Size() const;
+
+ Type type() const { return data_.type; }
+
void set_prefix_count(int count) { data_.prefix_count = count; }
void set_type(Type type) { data_.type = type; }
@@ -217,58 +211,6 @@ class SBEntry {
};
-// SBHostInfo ------------------------------------------------------------------
-
-// Holds the hostkey specific information in the database. This is basically a
-// collection of SBEntry objects.
-class SBHostInfo {
- public:
- SBHostInfo();
- // By default, an empty SBHostInfo is created. Call this to deserialize from
- // the database. Returns false if |data| is not internally consistent.
- bool Initialize(const void* data, int size);
-
- // Adds the given prefixes to the unsafe list. Note that the prefixes array
- // might be modified internally.
- void AddPrefixes(SBEntry* entry);
-
- // Remove the given prefixes. If prefixes is empty, then all entries from
- // sub.add_chunk_number are removed. Otherwise sub. add_chunk_id is ignored
- // and the chunk_id from each element in sub.prefixes is checked. If persist
- // is true and no matches are found, then the sub information will be stored
- // and checked in case a future add comes in with that chunk_id.
- void RemovePrefixes(SBEntry* entry, bool persist);
-
- // Returns true if the host entry contains any of the prefixes. If a full
- // hash matched, then list_id contains the list id. Otherwise list_id is -1
- // and prefix_hits contains the matching prefixes if any are matched, or is
- // empty if the entire host is blacklisted.
- bool Contains(const std::vector<SBFullHash>& prefixes,
- int* list_id,
- std::vector<SBPrefix>* prefix_hits);
-
- // Used for serialization.
- const void* data() const { return data_.get(); }
- int size() const { return size_; }
-
- private:
- // Checks data_ for internal consistency.
- bool IsValid();
-
- // Allows enumeration of Entry structs. To start off, pass NULL for *entry,
- // and then afterwards return the previous pointer.
- bool GetNextEntry(const SBEntry** entry);
-
- void Add(const SBEntry* entry);
-
- void RemoveSubEntry(int list_id, int chunk_id);
-
- // Collection of SBEntry objects.
- scoped_array<char> data_;
- int size_;
-};
-
-
// Utility functions -----------------------------------------------------------
namespace safe_browsing_util {
diff --git a/chrome/browser/safe_browsing/safe_browsing_util_unittest.cc b/chrome/browser/safe_browsing/safe_browsing_util_unittest.cc
index c53ff17..8e37b9c 100644
--- a/chrome/browser/safe_browsing/safe_browsing_util_unittest.cc
+++ b/chrome/browser/safe_browsing/safe_browsing_util_unittest.cc
@@ -2,28 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <algorithm>
+
#include "base/sha2.h"
#include "chrome/browser/safe_browsing/safe_browsing_util.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
- bool VectorContains(const std::vector<std::string>& data,
- const std::string& str) {
- for (size_t i = 0; i < data.size(); ++i) {
- if (data[i] == str)
- return true;
- }
-
- return false;
- }
-SBFullHash CreateFullHash(SBPrefix prefix) {
- SBFullHash result;
- memset(&result, 0, sizeof(result));
- memcpy(&result, &prefix, sizeof(result));
- return result;
+bool VectorContains(const std::vector<std::string>& data,
+ const std::string& str) {
+ return std::find(data.begin(), data.end(), str) != data.end();
}
+
}
// Tests that we generate the required host/path combinations for testing
@@ -82,213 +74,3 @@ TEST(SafeBrowsingUtilTest, FullHashCompare) {
url = GURL("http://www.evil.com/okay_path.html");
EXPECT_EQ(safe_browsing_util::CompareFullHashes(url, full_hashes), -1);
}
-
-// Checks the reading/writing code of the database information for a hostkey.
-TEST(SafeBrowsing, HostInfo) {
- // Test a simple case of adding a prefix from scratch.
- SBEntry* entry = SBEntry::Create(SBEntry::ADD_PREFIX, 1);
- entry->SetPrefixAt(0, 0x01000000);
- entry->set_list_id(1);
- entry->set_chunk_id(1);
-
- SBHostInfo info;
- info.AddPrefixes(entry);
- entry->Destroy();
-
- int list_id;
- std::vector<SBFullHash> full_hashes;
- full_hashes.push_back(CreateFullHash(0x01000000));
- std::vector<SBPrefix> prefix_hits;
- EXPECT_TRUE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- // Test appending prefixes to an existing entry.
- entry = SBEntry::Create(SBEntry::ADD_PREFIX, 2);
- entry->SetPrefixAt(0, 0x02000000);
- entry->SetPrefixAt(1, 0x02000001);
- entry->set_list_id(1);
- entry->set_chunk_id(2);
- info.AddPrefixes(entry);
- entry->Destroy();
-
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x01000000));
- EXPECT_TRUE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x02000000));
- EXPECT_TRUE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x02000001));
- EXPECT_TRUE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
-
- // Test removing the entire first entry.
- entry = SBEntry::Create(SBEntry::SUB_PREFIX, 0);
- entry->set_list_id(1);
- entry->set_chunk_id(1);
- info.RemovePrefixes(entry, false);
- entry->Destroy();
-
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x01000000));
- EXPECT_FALSE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x02000000));
- EXPECT_TRUE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x02000001));
- EXPECT_TRUE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- // Test removing one prefix from the second entry.
- entry = SBEntry::Create(SBEntry::SUB_PREFIX, 1);
- entry->SetPrefixAt(0,0x02000000);
- entry->SetChunkIdAtPrefix(0, 2);
- entry->set_list_id(1);
- info.RemovePrefixes(entry, false);
- entry->Destroy();
-
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x02000000));
- EXPECT_FALSE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x02000001));
- EXPECT_TRUE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- // Test adding a sub that specifies a prefix before the add.
- entry = SBEntry::Create(SBEntry::SUB_PREFIX, 1);
- entry->SetPrefixAt(0, 0x1000);
- entry->SetChunkIdAtPrefix(0, 100);
- entry->set_list_id(1);
- info.RemovePrefixes(entry, true);
- entry->Destroy();
-
- // Make sure we don't get a match from a sub.
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x1000));
- EXPECT_FALSE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- // Now add the prefixes.
- entry = SBEntry::Create(SBEntry::ADD_PREFIX, 3);
- entry->SetPrefixAt(0, 0x10000);
- entry->SetPrefixAt(1, 0x1000);
- entry->SetPrefixAt(2, 0x100000);
- entry->set_list_id(1);
- entry->set_chunk_id(100);
- info.AddPrefixes(entry);
- entry->Destroy();
-
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x10000));
- EXPECT_TRUE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x1000));
- EXPECT_FALSE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x100000));
- EXPECT_TRUE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- // Now try adding a sub that deletes all prefixes from the chunk.
- entry = SBEntry::Create(SBEntry::SUB_PREFIX, 0);
- entry->set_list_id(1);
- entry->set_chunk_id(100);
- info.RemovePrefixes(entry, true);
- entry->Destroy();
-
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x10000));
- EXPECT_FALSE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x100000));
- EXPECT_FALSE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- // Add a sub for all prefixes before the add comes.
- entry = SBEntry::Create(SBEntry::SUB_PREFIX, 0);
- entry->set_list_id(1);
- entry->set_chunk_id(200);
- info.RemovePrefixes(entry, true);
- entry->Destroy();
-
- // Now add the prefixes.
- entry = SBEntry::Create(SBEntry::ADD_PREFIX, 3);
- entry->SetPrefixAt(0, 0x2000);
- entry->SetPrefixAt(1, 0x20000);
- entry->SetPrefixAt(2, 0x200000);
- entry->set_list_id(1);
- entry->set_chunk_id(200);
- info.AddPrefixes(entry);
- entry->Destroy();
-
- // None of the prefixes should be found.
- full_hashes.clear();
- full_hashes.push_back(CreateFullHash(0x2000));
- full_hashes.push_back(CreateFullHash(0x20000));
- full_hashes.push_back(CreateFullHash(0x200000));
- EXPECT_FALSE(info.Contains(full_hashes, &list_id, &prefix_hits));
-}
-
-// Checks that if we have a hostname blacklisted and we get a sub prefix, the
-// hostname remains blacklisted.
-TEST(SafeBrowsing, HostInfo2) {
- // Blacklist the entire hostname.
- SBEntry* entry = SBEntry::Create(SBEntry::ADD_PREFIX, 0);
- entry->set_list_id(1);
- entry->set_chunk_id(1);
-
- SBHostInfo info;
- info.AddPrefixes(entry);
- entry->Destroy();
-
- int list_id;
- std::vector<SBFullHash> full_hashes;
- full_hashes.push_back(CreateFullHash(0x01000000));
- std::vector<SBPrefix> prefix_hits;
- EXPECT_TRUE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- // Now add a sub prefix.
- entry = SBEntry::Create(SBEntry::SUB_PREFIX, 1);
- entry->SetPrefixAt(0, 0x02000000);
- entry->SetChunkIdAtPrefix(0, 2);
- entry->set_list_id(1);
- info.RemovePrefixes(entry, true);
- entry->Destroy();
-
- // Any prefix except the one removed should still be blocked.
- EXPECT_TRUE(info.Contains(full_hashes, &list_id, &prefix_hits));
-}
-
-// Checks that if we get a sub chunk with one prefix, then get the add chunk
-// for that same prefix afterwards, the entry becomes empty.
-TEST(SafeBrowsing, HostInfo3) {
- SBHostInfo info;
-
- // Add a sub prefix.
- SBEntry* entry = SBEntry::Create(SBEntry::SUB_PREFIX, 1);
- entry->SetPrefixAt(0, 0x01000000);
- entry->SetChunkIdAtPrefix(0, 1);
- entry->set_list_id(1);
- info.RemovePrefixes(entry, true);
- entry->Destroy();
-
- int list_id;
- std::vector<SBFullHash> full_hashes;
- full_hashes.push_back(CreateFullHash(0x01000000));
- std::vector<SBPrefix> prefix_hits;
- EXPECT_FALSE(info.Contains(full_hashes, &list_id, &prefix_hits));
-
- // Now add the prefix.
- entry = SBEntry::Create(SBEntry::ADD_PREFIX, 1);
- entry->SetPrefixAt(0, 0x01000000);
- entry->set_list_id(1);
- entry->set_chunk_id(1);
- info.AddPrefixes(entry);
- entry->Destroy();
-
- EXPECT_FALSE(info.Contains(full_hashes, &list_id, &prefix_hits));
-}