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.
4
5 #include "ios/net/cookies/cookie_cache.h"
6
7 #include "net/cookies/canonical_cookie.h"
8 #include "net/cookies/cookie_constants.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
11
12 namespace net {
13
14 using net::CanonicalCookie;
15
16 namespace {
17
MakeCookie(const GURL & url,const std::string & name,const std::string & value)18 CanonicalCookie MakeCookie(const GURL& url,
19 const std::string& name,
20 const std::string& value) {
21 return CanonicalCookie(name, value, url.host(), url.path(), base::Time(),
22 base::Time(), base::Time(), false, false,
23 net::CookieSameSite::NO_RESTRICTION,
24 net::COOKIE_PRIORITY_DEFAULT, false);
25 }
26
27 } // namespace
28
29 using CookieCacheTest = PlatformTest;
30
TEST_F(CookieCacheTest,UpdateAddsCookieAllowsnullptr)31 TEST_F(CookieCacheTest, UpdateAddsCookieAllowsnullptr) {
32 CookieCache cache;
33 const GURL test_url("http://www.google.com");
34 std::vector<CanonicalCookie> cookies;
35 cookies.push_back(MakeCookie(test_url, "abc", "def"));
36 EXPECT_TRUE(cache.Update(test_url, "abc", cookies, nullptr, nullptr));
37 EXPECT_FALSE(cache.Update(test_url, "abc", cookies, nullptr, nullptr));
38 }
39
TEST_F(CookieCacheTest,UpdateAddsCookie)40 TEST_F(CookieCacheTest, UpdateAddsCookie) {
41 CookieCache cache;
42 const GURL test_url("http://www.google.com");
43 std::vector<CanonicalCookie> cookies;
44 cookies.push_back(MakeCookie(test_url, "abc", "def"));
45 std::vector<net::CanonicalCookie> removed;
46 std::vector<net::CanonicalCookie> changed;
47
48 EXPECT_TRUE(cache.Update(test_url, "abc", cookies, &removed, &changed));
49 EXPECT_TRUE(removed.empty());
50 EXPECT_EQ(1U, changed.size());
51 EXPECT_EQ("abc", changed[0].Name());
52 EXPECT_EQ("def", changed[0].Value());
53 removed.clear();
54 changed.clear();
55
56 EXPECT_FALSE(cache.Update(test_url, "abc", cookies, &removed, &changed));
57 EXPECT_TRUE(removed.empty());
58 EXPECT_TRUE(changed.empty());
59 }
60
TEST_F(CookieCacheTest,UpdateAddsDistinctCookie)61 TEST_F(CookieCacheTest, UpdateAddsDistinctCookie) {
62 CookieCache cache;
63 const GURL test_url("http://www.google.com");
64 const GURL test_url_path("http://www.google.com/foo");
65 const GURL test_url_path_long("http://www.google.com/foo/bar");
66 std::vector<CanonicalCookie> cookies;
67 std::vector<net::CanonicalCookie> removed;
68 std::vector<net::CanonicalCookie> changed;
69
70 cookies.push_back(MakeCookie(test_url, "abc", "def"));
71 EXPECT_TRUE(
72 cache.Update(test_url_path_long, "abc", cookies, &removed, &changed));
73 EXPECT_TRUE(removed.empty());
74 EXPECT_EQ(1U, changed.size());
75 removed.clear();
76 changed.clear();
77
78 cookies.push_back(MakeCookie(test_url_path, "abc", "def"));
79 EXPECT_TRUE(
80 cache.Update(test_url_path_long, "abc", cookies, &removed, &changed));
81 EXPECT_TRUE(removed.empty());
82 EXPECT_EQ(1U, changed.size());
83 removed.clear();
84 changed.clear();
85
86 cookies.push_back(MakeCookie(test_url_path_long, "abc", "def"));
87 EXPECT_TRUE(
88 cache.Update(test_url_path_long, "abc", cookies, &removed, &changed));
89 EXPECT_TRUE(removed.empty());
90 EXPECT_EQ(1U, changed.size());
91 }
92
TEST_F(CookieCacheTest,UpdateValueChanged)93 TEST_F(CookieCacheTest, UpdateValueChanged) {
94 CookieCache cache;
95 const GURL test_url("http://www.google.com");
96 std::vector<CanonicalCookie> cookies;
97
98 cookies.push_back(MakeCookie(test_url, "abc", "def"));
99 EXPECT_TRUE(cache.Update(test_url, "abc", cookies, nullptr, nullptr));
100
101 std::vector<net::CanonicalCookie> removed;
102 std::vector<net::CanonicalCookie> changed;
103 cookies[0] = MakeCookie(test_url, "abc", "ghi");
104 EXPECT_TRUE(cache.Update(test_url, "abc", cookies, &removed, &changed));
105 EXPECT_EQ(1U, removed.size());
106 EXPECT_EQ("abc", removed[0].Name());
107 EXPECT_EQ("def", removed[0].Value());
108
109 EXPECT_EQ(1U, changed.size());
110 EXPECT_EQ("abc", changed[0].Name());
111 EXPECT_EQ("ghi", changed[0].Value());
112 }
113
TEST_F(CookieCacheTest,UpdateDeletedCookie)114 TEST_F(CookieCacheTest, UpdateDeletedCookie) {
115 CookieCache cache;
116 const GURL test_url("http://www.google.com");
117 std::vector<CanonicalCookie> cookies;
118 cookies.push_back(MakeCookie(test_url, "abc", "def"));
119 EXPECT_TRUE(cache.Update(test_url, "abc", cookies, nullptr, nullptr));
120 cookies.clear();
121
122 std::vector<net::CanonicalCookie> removed;
123 std::vector<net::CanonicalCookie> changed;
124 EXPECT_TRUE(cache.Update(test_url, "abc", cookies, &removed, &changed));
125 EXPECT_EQ(1U, removed.size());
126 EXPECT_TRUE(changed.empty());
127 }
128
TEST_F(CookieCacheTest,UpdatePathChanged)129 TEST_F(CookieCacheTest, UpdatePathChanged) {
130 CookieCache cache;
131 const GURL test_url("http://www.google.com");
132 const GURL test_url_path("http://www.google.com/foo");
133 std::vector<CanonicalCookie> cookies;
134 cookies.push_back(MakeCookie(test_url, "abc", "def"));
135 EXPECT_TRUE(cache.Update(test_url, "abc", cookies, nullptr, nullptr));
136
137 std::vector<net::CanonicalCookie> removed;
138 std::vector<net::CanonicalCookie> changed;
139 cookies[0] = MakeCookie(test_url_path, "abc", "def");
140 EXPECT_TRUE(cache.Update(test_url, "abc", cookies, &removed, &changed));
141 EXPECT_EQ(1U, removed.size());
142 EXPECT_EQ(1U, changed.size());
143 }
144
TEST_F(CookieCacheTest,MultipleDomains)145 TEST_F(CookieCacheTest, MultipleDomains) {
146 CookieCache cache;
147 const GURL test_url_a("http://www.google.com");
148 const GURL test_url_b("http://test.google.com");
149 const GURL cookieurl("http://google.com");
150 std::vector<CanonicalCookie> cookies;
151 cookies.push_back(MakeCookie(cookieurl, "abc", "def"));
152 EXPECT_TRUE(cache.Update(test_url_a, "abc", cookies, nullptr, nullptr));
153 EXPECT_FALSE(cache.Update(test_url_a, "abc", cookies, nullptr, nullptr));
154 EXPECT_TRUE(cache.Update(test_url_b, "abc", cookies, nullptr, nullptr));
155 EXPECT_FALSE(cache.Update(test_url_b, "abc", cookies, nullptr, nullptr));
156 }
157
TEST_F(CookieCacheTest,MultipleNames)158 TEST_F(CookieCacheTest, MultipleNames) {
159 CookieCache cache;
160 const GURL cookieurl("http://google.com");
161 std::vector<CanonicalCookie> cookies;
162 cookies.push_back(MakeCookie(cookieurl, "abc", "def"));
163 EXPECT_TRUE(cache.Update(cookieurl, "abc", cookies, nullptr, nullptr));
164 EXPECT_FALSE(cache.Update(cookieurl, "abc", cookies, nullptr, nullptr));
165 cookies[0] = MakeCookie(cookieurl, "def", "def");
166 EXPECT_TRUE(cache.Update(cookieurl, "def", cookies, nullptr, nullptr));
167 EXPECT_FALSE(cache.Update(cookieurl, "def", cookies, nullptr, nullptr));
168 cookies[0] = MakeCookie(cookieurl, "abc", "def");
169 EXPECT_FALSE(cache.Update(cookieurl, "abc", cookies, nullptr, nullptr));
170 }
171
172 } // namespace net
173