blob: 3aba46d56002bdeb0aa319ec34d2c9951f2e21c9 [file] [log] [blame]
// Copyright 2014 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 "ios/net/cookies/cookie_cache.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
using net::CanonicalCookie;
namespace {
CanonicalCookie MakeCookie(const GURL& url,
const std::string& name,
const std::string& value) {
return CanonicalCookie(url, name, value, url.host(), url.path(), base::Time(),
base::Time(), base::Time(), false, false, false,
net::COOKIE_PRIORITY_DEFAULT);
}
} // namespace
TEST(CookieCacheTest, UpdateAddsCookieAllowsnullptr) {
CookieCache cache;
const GURL test_url("http://www.google.com");
std::vector<CanonicalCookie> cookies;
cookies.push_back(MakeCookie(test_url, "abc", "def"));
EXPECT_TRUE(cache.Update(test_url, "abc", cookies, nullptr, nullptr));
EXPECT_FALSE(cache.Update(test_url, "abc", cookies, nullptr, nullptr));
}
TEST(CookieCacheTest, UpdateAddsCookie) {
CookieCache cache;
const GURL test_url("http://www.google.com");
std::vector<CanonicalCookie> cookies;
cookies.push_back(MakeCookie(test_url, "abc", "def"));
std::vector<net::CanonicalCookie> removed;
std::vector<net::CanonicalCookie> changed;
EXPECT_TRUE(cache.Update(test_url, "abc", cookies, &removed, &changed));
EXPECT_TRUE(removed.empty());
EXPECT_EQ(1U, changed.size());
EXPECT_EQ("abc", changed[0].Name());
EXPECT_EQ("def", changed[0].Value());
removed.clear();
changed.clear();
EXPECT_FALSE(cache.Update(test_url, "abc", cookies, &removed, &changed));
EXPECT_TRUE(removed.empty());
EXPECT_TRUE(changed.empty());
}
TEST(CookieCacheTest, UpdateAddsDistinctCookie) {
CookieCache cache;
const GURL test_url("http://www.google.com");
const GURL test_url_path("http://www.google.com/foo");
const GURL test_url_path_long("http://www.google.com/foo/bar");
std::vector<CanonicalCookie> cookies;
std::vector<net::CanonicalCookie> removed;
std::vector<net::CanonicalCookie> changed;
cookies.push_back(MakeCookie(test_url, "abc", "def"));
EXPECT_TRUE(
cache.Update(test_url_path_long, "abc", cookies, &removed, &changed));
EXPECT_TRUE(removed.empty());
EXPECT_EQ(1U, changed.size());
removed.clear();
changed.clear();
cookies.push_back(MakeCookie(test_url_path, "abc", "def"));
EXPECT_TRUE(
cache.Update(test_url_path_long, "abc", cookies, &removed, &changed));
EXPECT_TRUE(removed.empty());
EXPECT_EQ(1U, changed.size());
removed.clear();
changed.clear();
cookies.push_back(MakeCookie(test_url_path_long, "abc", "def"));
EXPECT_TRUE(
cache.Update(test_url_path_long, "abc", cookies, &removed, &changed));
EXPECT_TRUE(removed.empty());
EXPECT_EQ(1U, changed.size());
}
TEST(CookieCacheTest, UpdateValueChanged) {
CookieCache cache;
const GURL test_url("http://www.google.com");
std::vector<CanonicalCookie> cookies;
cookies.push_back(MakeCookie(test_url, "abc", "def"));
EXPECT_TRUE(cache.Update(test_url, "abc", cookies, nullptr, nullptr));
std::vector<net::CanonicalCookie> removed;
std::vector<net::CanonicalCookie> changed;
cookies[0] = MakeCookie(test_url, "abc", "ghi");
EXPECT_TRUE(cache.Update(test_url, "abc", cookies, &removed, &changed));
EXPECT_EQ(1U, removed.size());
EXPECT_EQ("abc", removed[0].Name());
EXPECT_EQ("def", removed[0].Value());
EXPECT_EQ(1U, changed.size());
EXPECT_EQ("abc", changed[0].Name());
EXPECT_EQ("ghi", changed[0].Value());
}
TEST(CookieCacheTest, UpdateDeletedCookie) {
CookieCache cache;
const GURL test_url("http://www.google.com");
std::vector<CanonicalCookie> cookies;
cookies.push_back(MakeCookie(test_url, "abc", "def"));
EXPECT_TRUE(cache.Update(test_url, "abc", cookies, nullptr, nullptr));
cookies.clear();
std::vector<net::CanonicalCookie> removed;
std::vector<net::CanonicalCookie> changed;
EXPECT_TRUE(cache.Update(test_url, "abc", cookies, &removed, &changed));
EXPECT_EQ(1U, removed.size());
EXPECT_TRUE(changed.empty());
}
TEST(CookieCacheTest, UpdatePathChanged) {
CookieCache cache;
const GURL test_url("http://www.google.com");
const GURL test_url_path("http://www.google.com/foo");
std::vector<CanonicalCookie> cookies;
cookies.push_back(MakeCookie(test_url, "abc", "def"));
EXPECT_TRUE(cache.Update(test_url, "abc", cookies, nullptr, nullptr));
std::vector<net::CanonicalCookie> removed;
std::vector<net::CanonicalCookie> changed;
cookies[0] = MakeCookie(test_url_path, "abc", "def");
EXPECT_TRUE(cache.Update(test_url, "abc", cookies, &removed, &changed));
EXPECT_EQ(1U, removed.size());
EXPECT_EQ(1U, changed.size());
}
TEST(CookieCacheTest, MultipleDomains) {
CookieCache cache;
const GURL test_url_a("http://www.google.com");
const GURL test_url_b("http://test.google.com");
const GURL cookieurl("http://google.com");
std::vector<CanonicalCookie> cookies;
cookies.push_back(MakeCookie(cookieurl, "abc", "def"));
EXPECT_TRUE(cache.Update(test_url_a, "abc", cookies, nullptr, nullptr));
EXPECT_FALSE(cache.Update(test_url_a, "abc", cookies, nullptr, nullptr));
EXPECT_TRUE(cache.Update(test_url_b, "abc", cookies, nullptr, nullptr));
EXPECT_FALSE(cache.Update(test_url_b, "abc", cookies, nullptr, nullptr));
}
TEST(CookieCacheTest, MultipleNames) {
CookieCache cache;
const GURL cookieurl("http://google.com");
std::vector<CanonicalCookie> cookies;
cookies.push_back(MakeCookie(cookieurl, "abc", "def"));
EXPECT_TRUE(cache.Update(cookieurl, "abc", cookies, nullptr, nullptr));
EXPECT_FALSE(cache.Update(cookieurl, "abc", cookies, nullptr, nullptr));
cookies[0] = MakeCookie(cookieurl, "def", "def");
EXPECT_TRUE(cache.Update(cookieurl, "def", cookies, nullptr, nullptr));
EXPECT_FALSE(cache.Update(cookieurl, "def", cookies, nullptr, nullptr));
cookies[0] = MakeCookie(cookieurl, "abc", "def");
EXPECT_FALSE(cache.Update(cookieurl, "abc", cookies, nullptr, nullptr));
}
} // namespace net