1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
// Copyright (c) 2009 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 "chrome/browser/cookies_tree_model.h"
#include <string>
#include "app/l10n_util.h"
#include "chrome/browser/net/url_request_context_getter.h"
#include "chrome/test/testing_profile.h"
#include "net/url_request/url_request_context.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class TestURLRequestContext : public URLRequestContext {
public:
TestURLRequestContext() {
cookie_store_ = new net::CookieMonster();
}
};
class TestURLRequestContextGetter : public URLRequestContextGetter {
public:
virtual URLRequestContext* GetURLRequestContext() {
if (!context_)
context_ = new TestURLRequestContext();
return context_.get();
}
private:
scoped_refptr<URLRequestContext> context_;
};
class CookieTestingProfile : public TestingProfile {
public:
virtual URLRequestContextGetter* GetRequestContext() {
if (!url_request_context_getter_.get())
url_request_context_getter_ = new TestURLRequestContextGetter;
return url_request_context_getter_.get();
}
virtual ~CookieTestingProfile() {}
net::CookieMonster* GetCookieMonster() {
return GetRequestContext()->GetCookieStore()->GetCookieMonster();
}
private:
scoped_refptr<URLRequestContextGetter> url_request_context_getter_;
};
class CookiesTreeModelTest : public testing::Test {
public:
CookiesTreeModelTest() : io_thread_(ChromeThread::IO, &message_loop_) {
}
virtual ~CookiesTreeModelTest() {
}
virtual void SetUp() {
profile_.reset(new CookieTestingProfile());
}
// Get the cookie names in the cookie list, as a comma seperated string.
// (Note that the CookieMonster cookie list is sorted by domain.)
// Ex:
// monster->SetCookie(GURL("http://b"), "X=1")
// monster->SetCookie(GURL("http://a"), "Y=1")
// EXPECT_STREQ("Y,X", GetMonsterCookies(monster).c_str());
std::string GetMonsterCookies(net::CookieMonster* monster) {
std::vector<std::string> parts;
net::CookieMonster::CookieList cookie_list = monster->GetAllCookies();
for (size_t i = 0; i < cookie_list.size(); ++i)
parts.push_back(cookie_list[i].second.Name());
return JoinString(parts, ',');
}
std::string GetCookiesOfChildren(const CookieTreeNode* node) {
if (node->GetChildCount()) {
std::string retval;
for (int i = 0; i < node->GetChildCount(); ++i) {
retval += GetCookiesOfChildren(node->GetChild(i));
}
return retval;
} else {
if (node->GetDetailedInfo().node_type ==
CookieTreeNode::DetailedInfo::TYPE_COOKIE)
return node->GetDetailedInfo().cookie->second.Name() + ",";
else
return "";
}
}
// Get the cookie names displayed in the view (if we had one) in the order
// they are displayed, as a comma seperated string.
// Ex: EXPECT_STREQ("X,Y", GetDisplayedCookies(cookies_view).c_str());
std::string GetDisplayedCookies(CookiesTreeModel* cookies_model) {
CookieTreeRootNode* root = static_cast<CookieTreeRootNode*>(
cookies_model->GetRoot());
std::string retval = GetCookiesOfChildren(root);
if (retval.length() && retval[retval.length() - 1] == ',')
retval.erase(retval.length() - 1);
return retval;
}
// do not call on the root
void DeleteCookie(CookieTreeNode* node) {
node->DeleteStoredObjects();
// find the parent and index
CookieTreeNode* parent_node = node->GetParent();
DCHECK(parent_node);
int ct_node_index = parent_node->IndexOfChild(node);
delete parent_node->GetModel()->Remove(parent_node, ct_node_index);
}
protected:
MessageLoop message_loop_;
ChromeThread io_thread_;
scoped_ptr<CookieTestingProfile> profile_;
};
TEST_F(CookiesTreeModelTest, RemoveAll) {
net::CookieMonster* monster = profile_->GetCookieMonster();
monster->SetCookie(GURL("http://foo"), "A=1");
monster->SetCookie(GURL("http://foo2"), "B=1");
CookiesTreeModel cookies_model(profile_.get());
// Reset the selection of the first row.
{
SCOPED_TRACE("Before removing");
EXPECT_EQ(GetMonsterCookies(monster), GetDisplayedCookies(&cookies_model));
}
cookies_model.DeleteAllCookies();
{
SCOPED_TRACE("After removing");
EXPECT_EQ(1, cookies_model.GetRoot()->GetTotalNodeCount());
EXPECT_EQ(0, cookies_model.GetRoot()->GetChildCount());
EXPECT_EQ(std::string(""), GetMonsterCookies(monster));
EXPECT_EQ(GetMonsterCookies(monster), GetDisplayedCookies(&cookies_model));
}
}
TEST_F(CookiesTreeModelTest, Remove) {
net::CookieMonster* monster = profile_->GetCookieMonster();
monster->SetCookie(GURL("http://foo1"), "A=1");
monster->SetCookie(GURL("http://foo2"), "B=1");
monster->SetCookie(GURL("http://foo3"), "C=1");
CookiesTreeModel cookies_model(profile_.get());
{
SCOPED_TRACE("Initial State 3 cookies");
// 10 because there's the root, then foo1 -> cookies -> a,
// foo2 -> cookies -> b, foo3 -> cookies -> c
EXPECT_EQ(10, cookies_model.GetRoot()->GetTotalNodeCount());
}
DeleteCookie(cookies_model.GetRoot()->GetChild(0));
{
SCOPED_TRACE("First origin removed");
EXPECT_STREQ("B,C", GetMonsterCookies(monster).c_str());
EXPECT_STREQ("B,C", GetDisplayedCookies(&cookies_model).c_str());
EXPECT_EQ(7, cookies_model.GetRoot()->GetTotalNodeCount());
}
}
TEST_F(CookiesTreeModelTest, RemoveCookiesNode) {
net::CookieMonster* monster = profile_->GetCookieMonster();
monster->SetCookie(GURL("http://foo1"), "A=1");
monster->SetCookie(GURL("http://foo2"), "B=1");
monster->SetCookie(GURL("http://foo3"), "C=1");
CookiesTreeModel cookies_model(profile_.get());
{
SCOPED_TRACE("Initial State 3 cookies");
// 10 because there's the root, then foo1 -> cookies -> a,
// foo2 -> cookies -> b, foo3 -> cookies -> c
EXPECT_EQ(10, cookies_model.GetRoot()->GetTotalNodeCount());
}
DeleteCookie(cookies_model.GetRoot()->GetChild(0)->GetChild(0));
{
SCOPED_TRACE("First origin removed");
EXPECT_STREQ("B,C", GetMonsterCookies(monster).c_str());
EXPECT_STREQ("B,C", GetDisplayedCookies(&cookies_model).c_str());
// 8 because in this case, the origin remains, although the COOKIES
// node beneath it has been deleted. So, we have
// root -> foo1 -> cookies -> a, foo2, foo3 -> cookies -> c
EXPECT_EQ(8, cookies_model.GetRoot()->GetTotalNodeCount());
}
}
TEST_F(CookiesTreeModelTest, RemoveCookieNode) {
net::CookieMonster* monster = profile_->GetCookieMonster();
monster->SetCookie(GURL("http://foo1"), "A=1");
monster->SetCookie(GURL("http://foo2"), "B=1");
monster->SetCookie(GURL("http://foo3"), "C=1");
CookiesTreeModel cookies_model(profile_.get());
{
SCOPED_TRACE("Initial State 3 cookies");
// 10 because there's the root, then foo1 -> cookies -> a,
// foo2 -> cookies -> b, foo3 -> cookies -> c
EXPECT_EQ(10, cookies_model.GetRoot()->GetTotalNodeCount());
}
DeleteCookie(cookies_model.GetRoot()->GetChild(1)->GetChild(0));
{
SCOPED_TRACE("Second origin COOKIES node removed");
EXPECT_STREQ("A,C", GetMonsterCookies(monster).c_str());
EXPECT_STREQ("A,C", GetDisplayedCookies(&cookies_model).c_str());
// 8 because in this case, the origin remains, although the COOKIES
// node beneath it has been deleted. So, we have
// root -> foo1 -> cookies -> a, foo2, foo3 -> cookies -> c
EXPECT_EQ(8, cookies_model.GetRoot()->GetTotalNodeCount());
}
}
TEST_F(CookiesTreeModelTest, RemoveSingleCookieNode) {
net::CookieMonster* monster = profile_->GetCookieMonster();
monster->SetCookie(GURL("http://foo1"), "A=1");
monster->SetCookie(GURL("http://foo2"), "B=1");
monster->SetCookie(GURL("http://foo3"), "C=1");
monster->SetCookie(GURL("http://foo3"), "D=1");
CookiesTreeModel cookies_model(profile_.get());
{
SCOPED_TRACE("Initial State 4 cookies");
// 11 because there's the root, then foo1 -> cookies -> a,
// foo2 -> cookies -> b, foo3 -> cookies -> c,d
EXPECT_EQ(11, cookies_model.GetRoot()->GetTotalNodeCount());
EXPECT_STREQ("A,B,C,D", GetMonsterCookies(monster).c_str());
EXPECT_STREQ("A,B,C,D", GetDisplayedCookies(&cookies_model).c_str());
}
DeleteCookie(cookies_model.GetRoot()->GetChild(2));
{
SCOPED_TRACE("Third origin removed");
EXPECT_STREQ("A,B", GetMonsterCookies(monster).c_str());
EXPECT_STREQ("A,B", GetDisplayedCookies(&cookies_model).c_str());
EXPECT_EQ(7, cookies_model.GetRoot()->GetTotalNodeCount());
}
}
TEST_F(CookiesTreeModelTest, RemoveSingleCookieNodeOf3) {
net::CookieMonster* monster = profile_->GetCookieMonster();
monster->SetCookie(GURL("http://foo1"), "A=1");
monster->SetCookie(GURL("http://foo2"), "B=1");
monster->SetCookie(GURL("http://foo3"), "C=1");
monster->SetCookie(GURL("http://foo3"), "D=1");
monster->SetCookie(GURL("http://foo3"), "E=1");
CookiesTreeModel cookies_model(profile_.get());
{
SCOPED_TRACE("Initial State 5 cookies");
// 11 because there's the root, then foo1 -> cookies -> a,
// foo2 -> cookies -> b, foo3 -> cookies -> c,d,e
EXPECT_EQ(12, cookies_model.GetRoot()->GetTotalNodeCount());
EXPECT_STREQ("A,B,C,D,E", GetMonsterCookies(monster).c_str());
EXPECT_STREQ("A,B,C,D,E", GetDisplayedCookies(&cookies_model).c_str());
}
DeleteCookie(cookies_model.GetRoot()->GetChild(2)->GetChild(0)->
GetChild(1));
{
SCOPED_TRACE("Middle cookie in third origin removed");
EXPECT_STREQ("A,B,C,E", GetMonsterCookies(monster).c_str());
EXPECT_STREQ("A,B,C,E", GetDisplayedCookies(&cookies_model).c_str());
EXPECT_EQ(11, cookies_model.GetRoot()->GetTotalNodeCount());
}
}
TEST_F(CookiesTreeModelTest, RemoveSecondOrigin) {
net::CookieMonster* monster = profile_->GetCookieMonster();
monster->SetCookie(GURL("http://foo1"), "A=1");
monster->SetCookie(GURL("http://foo2"), "B=1");
monster->SetCookie(GURL("http://foo3"), "C=1");
monster->SetCookie(GURL("http://foo3"), "D=1");
monster->SetCookie(GURL("http://foo3"), "E=1");
CookiesTreeModel cookies_model(profile_.get());
{
SCOPED_TRACE("Initial State 5 cookies");
// 11 because there's the root, then foo1 -> cookies -> a,
// foo2 -> cookies -> b, foo3 -> cookies -> c,d,e
EXPECT_EQ(12, cookies_model.GetRoot()->GetTotalNodeCount());
EXPECT_STREQ("A,B,C,D,E", GetMonsterCookies(monster).c_str());
EXPECT_STREQ("A,B,C,D,E", GetDisplayedCookies(&cookies_model).c_str());
}
DeleteCookie(cookies_model.GetRoot()->GetChild(1));
{
SCOPED_TRACE("Second origin removed");
EXPECT_STREQ("A,C,D,E", GetMonsterCookies(monster).c_str());
EXPECT_STREQ("A,C,D,E", GetDisplayedCookies(&cookies_model).c_str());
// Left with root -> foo1 -> cookies -> a, foo3 -> cookies -> c,d,e
EXPECT_EQ(9, cookies_model.GetRoot()->GetTotalNodeCount());
}
}
TEST_F(CookiesTreeModelTest, OriginOrdering) {
net::CookieMonster* monster = profile_->GetCookieMonster();
monster->SetCookie(GURL("http://a.foo2.com"), "A=1");
monster->SetCookie(GURL("http://foo2.com"), "B=1");
monster->SetCookie(GURL("http://b.foo1.com"), "C=1");
monster->SetCookie(GURL("http://foo4.com"), "D=1; domain=.foo4.com;"
" path=/;"); // Leading dot on the foo4
monster->SetCookie(GURL("http://a.foo1.com"), "E=1");
monster->SetCookie(GURL("http://foo1.com"), "F=1");
monster->SetCookie(GURL("http://foo3.com"), "G=1");
monster->SetCookie(GURL("http://foo4.com"), "H=1");
CookiesTreeModel cookies_model(profile_.get());
{
SCOPED_TRACE("Initial State 8 cookies");
// D starts with a ., CookieMonster orders that lexicographically first
EXPECT_STREQ("D,E,A,C,F,B,G,H", GetMonsterCookies(monster).c_str());
EXPECT_STREQ("F,E,C,B,A,G,D,H",
GetDisplayedCookies(&cookies_model).c_str());
}
DeleteCookie(cookies_model.GetRoot()->GetChild(1)); // Delete "E"
{
SCOPED_TRACE("Second origin removed");
EXPECT_STREQ("D,A,C,F,B,G,H", GetMonsterCookies(monster).c_str());
EXPECT_STREQ("F,C,B,A,G,D,H", GetDisplayedCookies(&cookies_model).c_str());
}
}
} // namespace
|