Resubmit: Add real file for AdNetworks DB

This is a resubmit of https://codereview.chromium.org/268063004/.

The previous version was reverted because it caused a memory leak. This was a
problem with RefCounting from ui::ResourceBundle::LoadDataResourceBytes(), which
has been fixed.

Details:
Even though the method returns a non-refcounted ptr to a RefCountedStaticMemory
(which according to the class comment, "the ref counting does not matter" [1]),
the ref-counting _does_ matter. The fix for this went in as part of a refactor
in https://codereview.chromium.org/263953003/.

[1] http://src.chromium.org/viewvc/chrome/trunk/src/base/memory/ref_counted_memory.h?revision=267321 line 44

BUG=357204

TBR=jhawkins@chromium.org (previously approved)
TBR=felt@chromium.org (very minor changes)

Review URL: https://codereview.chromium.org/274563003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269494 0039d316-1c4b-4281-b951-d872f2087c98
parent 0e8e792c
......@@ -31,7 +31,8 @@ const size_t kChecksumHashSize = 32u;
} // namespace
HashedAdNetworkDatabase::HashedAdNetworkDatabase(
scoped_refptr<base::RefCountedStaticMemory> entries_memory) {
scoped_refptr<base::RefCountedStaticMemory> entries_memory)
: is_valid_(false) {
// This can legitimately happen in unit tests.
if (!entries_memory)
return;
......@@ -67,6 +68,8 @@ HashedAdNetworkDatabase::HashedAdNetworkDatabase(
memcpy(&value, index, kUrlHashSize);
entries_.insert(value);
}
is_valid_ = true;
}
HashedAdNetworkDatabase::~HashedAdNetworkDatabase() {}
......
......@@ -23,10 +23,17 @@ class HashedAdNetworkDatabase : public AdNetworkDatabase {
scoped_refptr<base::RefCountedStaticMemory> memory);
virtual ~HashedAdNetworkDatabase();
bool is_valid() const { return is_valid_; }
private:
// AdNetworkDatabase implementation.
virtual bool IsAdNetwork(const GURL& url) const OVERRIDE;
// Whether or not the database is valid, i.e., has correctly parsed the hash
// file. If it is not, it will always return false for IsAdNetwork(), since
// it cannot match against known hashes.
bool is_valid_;
// The set of partial hashes for known ad networks.
base::hash_set<int64> entries_;
};
......
......@@ -49,6 +49,7 @@ const size_t kDataResourceSize = kChecksumSize + kAdNetworkHostHashesTotalSize;
class HashedAdNetworkDatabaseUnitTest : public testing::Test {
protected:
virtual void SetUp() OVERRIDE;
virtual void TearDown() OVERRIDE;
private:
// Generate a piece of memory with a hash structure identical to the real one,
......@@ -68,6 +69,11 @@ void HashedAdNetworkDatabaseUnitTest::SetUp() {
scoped_ptr<AdNetworkDatabase>(new HashedAdNetworkDatabase(memory_)));
}
void HashedAdNetworkDatabaseUnitTest::TearDown() {
// Reset the database.
AdNetworkDatabase::SetForTesting(scoped_ptr<AdNetworkDatabase>());
}
void HashedAdNetworkDatabaseUnitTest::GenerateMockMemory() {
int64 host_hashes[kNumAdNetworkHosts];
......@@ -94,6 +100,9 @@ void HashedAdNetworkDatabaseUnitTest::GenerateMockMemory() {
memory_ = new base::RefCountedStaticMemory(raw_data_, kDataResourceSize);
};
// Test that the logic for the Ad Network Database works. That is, the hashing
// scheme works, correctly reports when URLs are present in the database,
// treats hosts and sumdomains correctly, etc.
TEST_F(HashedAdNetworkDatabaseUnitTest, HashedAdNetworkDatabaseTest) {
const AdNetworkDatabase* database = AdNetworkDatabase::Get();
ASSERT_TRUE(database);
......@@ -130,4 +139,23 @@ TEST_F(HashedAdNetworkDatabaseUnitTest, HashedAdNetworkDatabaseTest) {
GURL("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")));
}
// Test that the HashAdNetworkDatabse test works with the real file. For
// security and privacy purposes, we cannot verify that real URLs are
// recognized. However, we can at least verify that the file is recognized and
// parsed.
TEST(HashedAdNetworkDatabaseWithRealFileUnitTest,
HashedAdNetworkDatabaseRealFileTest) {
// This constructs the database, and, since we didn't mock up any memory, it
// uses the real file.
const AdNetworkDatabase* database = AdNetworkDatabase::Get();
ASSERT_TRUE(database);
const HashedAdNetworkDatabase* hashed_database =
static_cast<const HashedAdNetworkDatabase*>(database);
EXPECT_TRUE(hashed_database->is_valid());
// We can also safely assume that a made-up url is not in the database.
EXPECT_FALSE(database->IsAdNetwork(
GURL("http://www.this-is-definitely-not-a-real-site.orarealtld")));
}
} // namespace extensions
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment