forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcookie_manager_browsertest.cc
186 lines (156 loc) · 6.65 KB
/
cookie_manager_browsertest.cc
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
// Copyright 2020 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 "base/files/file_util.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "content/public/browser/browser_context.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "weblayer/browser/cookie_manager_impl.h"
#include "weblayer/browser/profile_impl.h"
#include "weblayer/public/cookie_manager.h"
#include "weblayer/test/weblayer_browser_test.h"
#include "weblayer/test/weblayer_browser_test_utils.h"
namespace weblayer {
class CookieManagerBrowserTest : public WebLayerBrowserTest {
public:
const std::vector<net::CookieChangeInfo>& WaitForChanges(size_t num) {
if (change_infos_.size() >= num)
return change_infos_;
num_to_wait_for_ = num;
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
return change_infos_;
}
void OnCookieChanged(const net::CookieChangeInfo& info) {
change_infos_.push_back(info);
if (run_loop_ && num_to_wait_for_ == change_infos_.size())
run_loop_->Quit();
}
void Reset() { change_infos_.clear(); }
bool SetCookie(const std::string& cookie) {
GURL base_url = embedded_test_server()->base_url();
base::RunLoop run_loop;
bool final_result = false;
GetProfile()->GetCookieManager()->SetCookie(
base_url, cookie,
base::BindLambdaForTesting([&run_loop, &final_result](bool result) {
final_result = result;
run_loop.Quit();
}));
run_loop.Run();
return final_result;
}
base::Time GetCookieDbModifiedTime() {
base::FilePath cookie_path =
GetBrowserContext()->GetPath().Append(FILE_PATH_LITERAL("Cookies"));
base::ScopedAllowBlockingForTesting scoped_allow_blocking;
base::File::Info info;
EXPECT_TRUE(base::GetFileInfo(cookie_path, &info));
return info.last_modified;
}
private:
size_t num_to_wait_for_ = 0;
std::vector<net::CookieChangeInfo> change_infos_;
std::unique_ptr<base::RunLoop> run_loop_;
};
IN_PROC_BROWSER_TEST_F(CookieManagerBrowserTest, CookieChanged) {
EXPECT_TRUE(embedded_test_server()->Start());
NavigateAndWaitForCompletion(
embedded_test_server()->GetURL("/simple_page.html"), shell());
GURL base_url = embedded_test_server()->base_url();
base::CallbackListSubscription subscription =
GetProfile()->GetCookieManager()->AddCookieChangedCallback(
base_url, nullptr,
base::BindRepeating(&CookieManagerBrowserTest::OnCookieChanged,
base::Unretained(this)));
ASSERT_TRUE(SetCookie("foo=bar"));
const auto& changes = WaitForChanges(1);
ASSERT_EQ(changes.size(), 1u);
const auto& cookie = changes[0].cookie;
EXPECT_EQ(cookie.Name(), "foo");
EXPECT_EQ(cookie.Value(), "bar");
}
IN_PROC_BROWSER_TEST_F(CookieManagerBrowserTest,
CookieChangedRemoveSubscription) {
EXPECT_TRUE(embedded_test_server()->Start());
NavigateAndWaitForCompletion(
embedded_test_server()->GetURL("/simple_page.html"), shell());
GURL base_url = embedded_test_server()->base_url();
std::string cookie1 = "cookie1";
base::CallbackListSubscription subscription1 =
GetProfile()->GetCookieManager()->AddCookieChangedCallback(
base_url, &cookie1,
base::BindRepeating(&CookieManagerBrowserTest::OnCookieChanged,
base::Unretained(this)));
std::string cookie2 = "cookie2";
base::CallbackListSubscription subscription2 =
GetProfile()->GetCookieManager()->AddCookieChangedCallback(
base_url, &cookie2,
base::BindRepeating(&CookieManagerBrowserTest::OnCookieChanged,
base::Unretained(this)));
ASSERT_TRUE(SetCookie("cookie1=something"));
{
const auto& changes = WaitForChanges(1);
ASSERT_EQ(changes.size(), 1u);
EXPECT_EQ(changes[0].cookie.Name(), cookie1);
}
Reset();
subscription1 = {};
// Set cookie1 first and then cookie2. We should only receive a cookie change
// event for cookie2.
ASSERT_TRUE(SetCookie("cookie1=other"));
ASSERT_TRUE(SetCookie("cookie2=something"));
{
const auto& changes = WaitForChanges(1);
ASSERT_EQ(changes.size(), 1u);
EXPECT_EQ(changes[0].cookie.Name(), cookie2);
}
}
#if defined(OS_WIN)
// TODO(crbug.com/1204901): Disabled due to flakiness on Windows.
#define MAYBE_FlushCookiesAfterSet DISABLED_FlushCookiesAfterSet
#else
#define MAYBE_FlushCookiesAfterSet FlushCookiesAfterSet
#endif
IN_PROC_BROWSER_TEST_F(CookieManagerBrowserTest, MAYBE_FlushCookiesAfterSet) {
EXPECT_TRUE(embedded_test_server()->Start());
NavigateAndWaitForCompletion(
embedded_test_server()->GetURL("/simple_page.html"), shell());
base::Time original_modified_time = GetCookieDbModifiedTime();
ASSERT_TRUE(SetCookie("a=b; expires=Fri, 01 Jan 2038 00:00:00 GMT"));
EXPECT_EQ(GetCookieDbModifiedTime(), original_modified_time);
EXPECT_TRUE(static_cast<CookieManagerImpl*>(GetProfile()->GetCookieManager())
->FireFlushTimerForTesting());
EXPECT_GT(GetCookieDbModifiedTime(), original_modified_time);
}
#if defined(OS_WIN)
// TODO(crbug.com/1204901): Disabled due to flakiness on Windows.
#define MAYBE_FlushCookiesAfterSetMultiple DISABLED_FlushCookiesAfterSetMultiple
#else
#define MAYBE_FlushCookiesAfterSetMultiple FlushCookiesAfterSetMultiple
#endif
IN_PROC_BROWSER_TEST_F(CookieManagerBrowserTest,
MAYBE_FlushCookiesAfterSetMultiple) {
EXPECT_TRUE(embedded_test_server()->Start());
NavigateAndWaitForCompletion(
embedded_test_server()->GetURL("/simple_page.html"), shell());
base::Time original_modified_time = GetCookieDbModifiedTime();
ASSERT_TRUE(SetCookie("a=b; expires=Fri, 01 Jan 2038 00:00:00 GMT"));
EXPECT_EQ(GetCookieDbModifiedTime(), original_modified_time);
ASSERT_TRUE(SetCookie("c=d; expires=Fri, 01 Jan 2038 00:00:00 GMT"));
EXPECT_EQ(GetCookieDbModifiedTime(), original_modified_time);
CookieManagerImpl* cookie_manager =
static_cast<CookieManagerImpl*>(GetProfile()->GetCookieManager());
EXPECT_TRUE(cookie_manager->FireFlushTimerForTesting());
EXPECT_GT(GetCookieDbModifiedTime(), original_modified_time);
// Flush timer should be gone now.
EXPECT_FALSE(cookie_manager->FireFlushTimerForTesting());
// Try again to make sure it works a second time.
original_modified_time = GetCookieDbModifiedTime();
ASSERT_TRUE(SetCookie("d=f; expires=Fri, 01 Jan 2038 00:00:00 GMT"));
EXPECT_EQ(GetCookieDbModifiedTime(), original_modified_time);
EXPECT_TRUE(cookie_manager->FireFlushTimerForTesting());
EXPECT_GT(GetCookieDbModifiedTime(), original_modified_time);
}
} // namespace weblayer