1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ios/net/cookies/cookie_cache.h"
7 #include "net/cookies/canonical_cookie.h"
8 #include "net/cookies/cookie_constants.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 using net::CanonicalCookie
;
17 CanonicalCookie
MakeCookie(const GURL
& url
,
18 const std::string
& name
,
19 const std::string
& value
) {
20 return CanonicalCookie(url
, name
, value
, url
.host(), url
.path(), base::Time(),
21 base::Time(), base::Time(), false, false, false,
22 net::COOKIE_PRIORITY_DEFAULT
);
27 TEST(CookieCacheTest
, UpdateAddsCookieAllowsnullptr
) {
29 const GURL
test_url("http://www.google.com");
30 std::vector
<CanonicalCookie
> cookies
;
31 cookies
.push_back(MakeCookie(test_url
, "abc", "def"));
32 EXPECT_TRUE(cache
.Update(test_url
, "abc", cookies
, nullptr, nullptr));
33 EXPECT_FALSE(cache
.Update(test_url
, "abc", cookies
, nullptr, nullptr));
36 TEST(CookieCacheTest
, UpdateAddsCookie
) {
38 const GURL
test_url("http://www.google.com");
39 std::vector
<CanonicalCookie
> cookies
;
40 cookies
.push_back(MakeCookie(test_url
, "abc", "def"));
41 std::vector
<net::CanonicalCookie
> removed
;
42 std::vector
<net::CanonicalCookie
> changed
;
44 EXPECT_TRUE(cache
.Update(test_url
, "abc", cookies
, &removed
, &changed
));
45 EXPECT_TRUE(removed
.empty());
46 EXPECT_EQ(1U, changed
.size());
47 EXPECT_EQ("abc", changed
[0].Name());
48 EXPECT_EQ("def", changed
[0].Value());
52 EXPECT_FALSE(cache
.Update(test_url
, "abc", cookies
, &removed
, &changed
));
53 EXPECT_TRUE(removed
.empty());
54 EXPECT_TRUE(changed
.empty());
57 TEST(CookieCacheTest
, UpdateAddsDistinctCookie
) {
59 const GURL
test_url("http://www.google.com");
60 const GURL
test_url_path("http://www.google.com/foo");
61 const GURL
test_url_path_long("http://www.google.com/foo/bar");
62 std::vector
<CanonicalCookie
> cookies
;
63 std::vector
<net::CanonicalCookie
> removed
;
64 std::vector
<net::CanonicalCookie
> changed
;
66 cookies
.push_back(MakeCookie(test_url
, "abc", "def"));
68 cache
.Update(test_url_path_long
, "abc", cookies
, &removed
, &changed
));
69 EXPECT_TRUE(removed
.empty());
70 EXPECT_EQ(1U, changed
.size());
74 cookies
.push_back(MakeCookie(test_url_path
, "abc", "def"));
76 cache
.Update(test_url_path_long
, "abc", cookies
, &removed
, &changed
));
77 EXPECT_TRUE(removed
.empty());
78 EXPECT_EQ(1U, changed
.size());
82 cookies
.push_back(MakeCookie(test_url_path_long
, "abc", "def"));
84 cache
.Update(test_url_path_long
, "abc", cookies
, &removed
, &changed
));
85 EXPECT_TRUE(removed
.empty());
86 EXPECT_EQ(1U, changed
.size());
89 TEST(CookieCacheTest
, UpdateValueChanged
) {
91 const GURL
test_url("http://www.google.com");
92 std::vector
<CanonicalCookie
> cookies
;
94 cookies
.push_back(MakeCookie(test_url
, "abc", "def"));
95 EXPECT_TRUE(cache
.Update(test_url
, "abc", cookies
, nullptr, nullptr));
97 std::vector
<net::CanonicalCookie
> removed
;
98 std::vector
<net::CanonicalCookie
> changed
;
99 cookies
[0] = MakeCookie(test_url
, "abc", "ghi");
100 EXPECT_TRUE(cache
.Update(test_url
, "abc", cookies
, &removed
, &changed
));
101 EXPECT_EQ(1U, removed
.size());
102 EXPECT_EQ("abc", removed
[0].Name());
103 EXPECT_EQ("def", removed
[0].Value());
105 EXPECT_EQ(1U, changed
.size());
106 EXPECT_EQ("abc", changed
[0].Name());
107 EXPECT_EQ("ghi", changed
[0].Value());
110 TEST(CookieCacheTest
, UpdateDeletedCookie
) {
112 const GURL
test_url("http://www.google.com");
113 std::vector
<CanonicalCookie
> cookies
;
114 cookies
.push_back(MakeCookie(test_url
, "abc", "def"));
115 EXPECT_TRUE(cache
.Update(test_url
, "abc", cookies
, nullptr, nullptr));
118 std::vector
<net::CanonicalCookie
> removed
;
119 std::vector
<net::CanonicalCookie
> changed
;
120 EXPECT_TRUE(cache
.Update(test_url
, "abc", cookies
, &removed
, &changed
));
121 EXPECT_EQ(1U, removed
.size());
122 EXPECT_TRUE(changed
.empty());
125 TEST(CookieCacheTest
, UpdatePathChanged
) {
127 const GURL
test_url("http://www.google.com");
128 const GURL
test_url_path("http://www.google.com/foo");
129 std::vector
<CanonicalCookie
> cookies
;
130 cookies
.push_back(MakeCookie(test_url
, "abc", "def"));
131 EXPECT_TRUE(cache
.Update(test_url
, "abc", cookies
, nullptr, nullptr));
133 std::vector
<net::CanonicalCookie
> removed
;
134 std::vector
<net::CanonicalCookie
> changed
;
135 cookies
[0] = MakeCookie(test_url_path
, "abc", "def");
136 EXPECT_TRUE(cache
.Update(test_url
, "abc", cookies
, &removed
, &changed
));
137 EXPECT_EQ(1U, removed
.size());
138 EXPECT_EQ(1U, changed
.size());
141 TEST(CookieCacheTest
, MultipleDomains
) {
143 const GURL
test_url_a("http://www.google.com");
144 const GURL
test_url_b("http://test.google.com");
145 const GURL
cookieurl("http://google.com");
146 std::vector
<CanonicalCookie
> cookies
;
147 cookies
.push_back(MakeCookie(cookieurl
, "abc", "def"));
148 EXPECT_TRUE(cache
.Update(test_url_a
, "abc", cookies
, nullptr, nullptr));
149 EXPECT_FALSE(cache
.Update(test_url_a
, "abc", cookies
, nullptr, nullptr));
150 EXPECT_TRUE(cache
.Update(test_url_b
, "abc", cookies
, nullptr, nullptr));
151 EXPECT_FALSE(cache
.Update(test_url_b
, "abc", cookies
, nullptr, nullptr));
154 TEST(CookieCacheTest
, MultipleNames
) {
156 const GURL
cookieurl("http://google.com");
157 std::vector
<CanonicalCookie
> cookies
;
158 cookies
.push_back(MakeCookie(cookieurl
, "abc", "def"));
159 EXPECT_TRUE(cache
.Update(cookieurl
, "abc", cookies
, nullptr, nullptr));
160 EXPECT_FALSE(cache
.Update(cookieurl
, "abc", cookies
, nullptr, nullptr));
161 cookies
[0] = MakeCookie(cookieurl
, "def", "def");
162 EXPECT_TRUE(cache
.Update(cookieurl
, "def", cookies
, nullptr, nullptr));
163 EXPECT_FALSE(cache
.Update(cookieurl
, "def", cookies
, nullptr, nullptr));
164 cookies
[0] = MakeCookie(cookieurl
, "abc", "def");
165 EXPECT_FALSE(cache
.Update(cookieurl
, "abc", cookies
, nullptr, nullptr));