summaryrefslogtreecommitdiffstats
path: root/net/base/cookie_monster_store_test.cc
diff options
context:
space:
mode:
authorerg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-26 20:23:44 +0000
committererg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-26 20:23:44 +0000
commitd2db029ab5a3ee3d103d71447467f7f0074869dc (patch)
tree38d2cc560189c0d79493460051764e5a3a5bbd4c /net/base/cookie_monster_store_test.cc
parent48f1e5cb62b299a83d7ae5f897eca0a587abf3f0 (diff)
downloadchromium_src-d2db029ab5a3ee3d103d71447467f7f0074869dc.zip
chromium_src-d2db029ab5a3ee3d103d71447467f7f0074869dc.tar.gz
chromium_src-d2db029ab5a3ee3d103d71447467f7f0074869dc.tar.bz2
Clean up net unit testing code.
(Reapply r72562 with willchan's nits + locally tested shlib fixes + removing duplicate code that was partially responsible for the failure.) - Move code included from blah_unittest.h (where blah_unittest.cc has actual unittests) into their own files, often completely out-of-lining the definitions. - Remove anonymous namespaces from headers. - Reorder method declarations. - Make other unit test link against net_test_support instead of reaching in and including .h files in their targets directly. BUG=68682 TEST=compiles First Review URL: http://codereview.chromium.org/6264013 Review URL: http://codereview.chromium.org/6248021 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72682 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/cookie_monster_store_test.cc')
-rw-r--r--net/base/cookie_monster_store_test.cc176
1 files changed, 176 insertions, 0 deletions
diff --git a/net/base/cookie_monster_store_test.cc b/net/base/cookie_monster_store_test.cc
new file mode 100644
index 0000000..e785a38
--- /dev/null
+++ b/net/base/cookie_monster_store_test.cc
@@ -0,0 +1,176 @@
+// Copyright (c) 2011 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/base/cookie_monster_store_test.h"
+
+#include "base/message_loop.h"
+#include "base/stringprintf.h"
+#include "base/time.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace net {
+
+MockPersistentCookieStore::MockPersistentCookieStore()
+ : load_return_value_(true) {
+}
+
+MockPersistentCookieStore::~MockPersistentCookieStore() {}
+
+void MockPersistentCookieStore::SetLoadExpectation(
+ bool return_value,
+ const std::vector<net::CookieMonster::CanonicalCookie*>& result) {
+ load_return_value_ = return_value;
+ load_result_ = result;
+}
+
+bool MockPersistentCookieStore::Load(
+ std::vector<net::CookieMonster::CanonicalCookie*>* out_cookies) {
+ bool ok = load_return_value_;
+ if (ok)
+ *out_cookies = load_result_;
+ return ok;
+}
+
+void MockPersistentCookieStore::AddCookie(
+ const net::CookieMonster::CanonicalCookie& cookie) {
+ commands_.push_back(
+ CookieStoreCommand(CookieStoreCommand::ADD, cookie));
+}
+
+void MockPersistentCookieStore::UpdateCookieAccessTime(
+ const net::CookieMonster::CanonicalCookie& cookie) {
+ commands_.push_back(CookieStoreCommand(
+ CookieStoreCommand::UPDATE_ACCESS_TIME, cookie));
+}
+
+void MockPersistentCookieStore::DeleteCookie(
+ const net::CookieMonster::CanonicalCookie& cookie) {
+ commands_.push_back(
+ CookieStoreCommand(CookieStoreCommand::REMOVE, cookie));
+}
+
+void MockPersistentCookieStore::Flush(Task* completion_task) {
+ if (completion_task)
+ MessageLoop::current()->PostTask(FROM_HERE, completion_task);
+}
+
+// No files are created so nothing to clear either
+void
+MockPersistentCookieStore::SetClearLocalStateOnExit(bool clear_local_state) {
+}
+
+MockCookieMonsterDelegate::MockCookieMonsterDelegate() {}
+
+void MockCookieMonsterDelegate::OnCookieChanged(
+ const net::CookieMonster::CanonicalCookie& cookie,
+ bool removed) {
+ CookieNotification notification(cookie, removed);
+ changes_.push_back(notification);
+ }
+
+MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {}
+
+void AddCookieToList(
+ const std::string& key,
+ const std::string& cookie_line,
+ const base::Time& creation_time,
+ std::vector<net::CookieMonster::CanonicalCookie*>* out_list) {
+
+ // Parse the cookie line.
+ net::CookieMonster::ParsedCookie pc(cookie_line);
+ EXPECT_TRUE(pc.IsValid());
+
+ // This helper is simplistic in interpreting a parsed cookie, in order to
+ // avoid duplicated CookieMonster's CanonPath() and CanonExpiration()
+ // functions. Would be nice to export them, and re-use here.
+ EXPECT_FALSE(pc.HasMaxAge());
+ EXPECT_TRUE(pc.HasPath());
+ base::Time cookie_expires = pc.HasExpires() ?
+ net::CookieMonster::ParseCookieTime(pc.Expires()) : base::Time();
+ std::string cookie_path = pc.Path();
+
+ scoped_ptr<net::CookieMonster::CanonicalCookie> cookie(
+ new net::CookieMonster::CanonicalCookie(
+ pc.Name(), pc.Value(), key, cookie_path,
+ pc.IsSecure(), pc.IsHttpOnly(),
+ creation_time, creation_time,
+ !cookie_expires.is_null(),
+ cookie_expires));
+
+ out_list->push_back(cookie.release());
+}
+
+MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() {}
+
+MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {}
+
+bool MockSimplePersistentCookieStore::Load(
+ std::vector<net::CookieMonster::CanonicalCookie*>* out_cookies) {
+ for (CanonicalCookieMap::const_iterator it = cookies_.begin();
+ it != cookies_.end(); it++)
+ out_cookies->push_back(
+ new net::CookieMonster::CanonicalCookie(it->second));
+ return true;
+}
+
+void MockSimplePersistentCookieStore::AddCookie(
+ const net::CookieMonster::CanonicalCookie& cookie) {
+ int64 creation_time = cookie.CreationDate().ToInternalValue();
+ EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end());
+ cookies_[creation_time] = cookie;
+}
+
+void MockSimplePersistentCookieStore::UpdateCookieAccessTime(
+ const net::CookieMonster::CanonicalCookie& cookie) {
+ int64 creation_time = cookie.CreationDate().ToInternalValue();
+ ASSERT_TRUE(cookies_.find(creation_time) != cookies_.end());
+ cookies_[creation_time].SetLastAccessDate(base::Time::Now());
+}
+
+void MockSimplePersistentCookieStore::DeleteCookie(
+ const net::CookieMonster::CanonicalCookie& cookie) {
+ int64 creation_time = cookie.CreationDate().ToInternalValue();
+ CanonicalCookieMap::iterator it = cookies_.find(creation_time);
+ ASSERT_TRUE(it != cookies_.end());
+ cookies_.erase(it);
+}
+
+void MockSimplePersistentCookieStore::Flush(Task* completion_task) {
+ if (completion_task)
+ MessageLoop::current()->PostTask(FROM_HERE, completion_task);
+}
+
+void MockSimplePersistentCookieStore::SetClearLocalStateOnExit(
+ bool clear_local_state) {
+}
+
+net::CookieMonster* CreateMonsterFromStoreForGC(
+ int num_cookies,
+ int num_old_cookies,
+ int days_old) {
+ base::Time current(base::Time::Now());
+ base::Time past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000));
+ scoped_refptr<MockSimplePersistentCookieStore> store(
+ new MockSimplePersistentCookieStore);
+ // Must expire to be persistent
+ for (int i = 0; i < num_old_cookies; i++) {
+ net::CookieMonster::CanonicalCookie cc(
+ "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", false, false,
+ past_creation + base::TimeDelta::FromMicroseconds(i),
+ current - base::TimeDelta::FromDays(days_old),
+ true, current + base::TimeDelta::FromDays(30));
+ store->AddCookie(cc);
+ }
+ for (int i = num_old_cookies; i < num_cookies; i++) {
+ net::CookieMonster::CanonicalCookie cc(
+ "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", false, false,
+ past_creation + base::TimeDelta::FromMicroseconds(i), current,
+ true, current + base::TimeDelta::FromDays(30));
+ store->AddCookie(cc);
+ }
+
+ return new net::CookieMonster(store, NULL);
+}
+
+} // namespace net