cookie_manager_browsertest.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2020 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. #include "base/files/file_util.h"
  5. #include "base/test/bind.h"
  6. #include "build/build_config.h"
  7. #include "content/public/browser/browser_context.h"
  8. #include "net/test/embedded_test_server/embedded_test_server.h"
  9. #include "weblayer/browser/cookie_manager_impl.h"
  10. #include "weblayer/browser/profile_impl.h"
  11. #include "weblayer/public/cookie_manager.h"
  12. #include "weblayer/test/weblayer_browser_test.h"
  13. #include "weblayer/test/weblayer_browser_test_utils.h"
  14. namespace weblayer {
  15. class CookieManagerBrowserTest : public WebLayerBrowserTest {
  16. public:
  17. const std::vector<net::CookieChangeInfo>& WaitForChanges(size_t num) {
  18. if (change_infos_.size() >= num)
  19. return change_infos_;
  20. num_to_wait_for_ = num;
  21. run_loop_ = std::make_unique<base::RunLoop>();
  22. run_loop_->Run();
  23. return change_infos_;
  24. }
  25. void OnCookieChanged(const net::CookieChangeInfo& info) {
  26. change_infos_.push_back(info);
  27. if (run_loop_ && num_to_wait_for_ == change_infos_.size())
  28. run_loop_->Quit();
  29. }
  30. void Reset() { change_infos_.clear(); }
  31. bool SetCookie(const std::string& cookie) {
  32. GURL base_url = embedded_test_server()->base_url();
  33. base::RunLoop run_loop;
  34. bool final_result = false;
  35. GetProfile()->GetCookieManager()->SetCookie(
  36. base_url, cookie,
  37. base::BindLambdaForTesting([&run_loop, &final_result](bool result) {
  38. final_result = result;
  39. run_loop.Quit();
  40. }));
  41. run_loop.Run();
  42. return final_result;
  43. }
  44. base::Time GetCookieDbModifiedTime() {
  45. base::FilePath cookie_path =
  46. GetBrowserContext()->GetPath().Append(FILE_PATH_LITERAL("Cookies"));
  47. base::ScopedAllowBlockingForTesting scoped_allow_blocking;
  48. base::File::Info info;
  49. EXPECT_TRUE(base::GetFileInfo(cookie_path, &info));
  50. return info.last_modified;
  51. }
  52. private:
  53. size_t num_to_wait_for_ = 0;
  54. std::vector<net::CookieChangeInfo> change_infos_;
  55. std::unique_ptr<base::RunLoop> run_loop_;
  56. };
  57. IN_PROC_BROWSER_TEST_F(CookieManagerBrowserTest, CookieChanged) {
  58. EXPECT_TRUE(embedded_test_server()->Start());
  59. NavigateAndWaitForCompletion(
  60. embedded_test_server()->GetURL("/simple_page.html"), shell());
  61. GURL base_url = embedded_test_server()->base_url();
  62. base::CallbackListSubscription subscription =
  63. GetProfile()->GetCookieManager()->AddCookieChangedCallback(
  64. base_url, nullptr,
  65. base::BindRepeating(&CookieManagerBrowserTest::OnCookieChanged,
  66. base::Unretained(this)));
  67. ASSERT_TRUE(SetCookie("foo=bar"));
  68. const auto& changes = WaitForChanges(1);
  69. ASSERT_EQ(changes.size(), 1u);
  70. const auto& cookie = changes[0].cookie;
  71. EXPECT_EQ(cookie.Name(), "foo");
  72. EXPECT_EQ(cookie.Value(), "bar");
  73. }
  74. IN_PROC_BROWSER_TEST_F(CookieManagerBrowserTest,
  75. CookieChangedRemoveSubscription) {
  76. EXPECT_TRUE(embedded_test_server()->Start());
  77. NavigateAndWaitForCompletion(
  78. embedded_test_server()->GetURL("/simple_page.html"), shell());
  79. GURL base_url = embedded_test_server()->base_url();
  80. std::string cookie1 = "cookie1";
  81. base::CallbackListSubscription subscription1 =
  82. GetProfile()->GetCookieManager()->AddCookieChangedCallback(
  83. base_url, &cookie1,
  84. base::BindRepeating(&CookieManagerBrowserTest::OnCookieChanged,
  85. base::Unretained(this)));
  86. std::string cookie2 = "cookie2";
  87. base::CallbackListSubscription subscription2 =
  88. GetProfile()->GetCookieManager()->AddCookieChangedCallback(
  89. base_url, &cookie2,
  90. base::BindRepeating(&CookieManagerBrowserTest::OnCookieChanged,
  91. base::Unretained(this)));
  92. ASSERT_TRUE(SetCookie("cookie1=something"));
  93. {
  94. const auto& changes = WaitForChanges(1);
  95. ASSERT_EQ(changes.size(), 1u);
  96. EXPECT_EQ(changes[0].cookie.Name(), cookie1);
  97. }
  98. Reset();
  99. subscription1 = {};
  100. // Set cookie1 first and then cookie2. We should only receive a cookie change
  101. // event for cookie2.
  102. ASSERT_TRUE(SetCookie("cookie1=other"));
  103. ASSERT_TRUE(SetCookie("cookie2=something"));
  104. {
  105. const auto& changes = WaitForChanges(1);
  106. ASSERT_EQ(changes.size(), 1u);
  107. EXPECT_EQ(changes[0].cookie.Name(), cookie2);
  108. }
  109. }
  110. #if BUILDFLAG(IS_WIN)
  111. // TODO(crbug.com/1204901): Disabled due to flakiness on Windows.
  112. #define MAYBE_FlushCookiesAfterSet DISABLED_FlushCookiesAfterSet
  113. #else
  114. #define MAYBE_FlushCookiesAfterSet FlushCookiesAfterSet
  115. #endif
  116. IN_PROC_BROWSER_TEST_F(CookieManagerBrowserTest, MAYBE_FlushCookiesAfterSet) {
  117. EXPECT_TRUE(embedded_test_server()->Start());
  118. NavigateAndWaitForCompletion(
  119. embedded_test_server()->GetURL("/simple_page.html"), shell());
  120. base::Time original_modified_time = GetCookieDbModifiedTime();
  121. ASSERT_TRUE(SetCookie("a=b; expires=Fri, 01 Jan 2038 00:00:00 GMT"));
  122. EXPECT_EQ(GetCookieDbModifiedTime(), original_modified_time);
  123. EXPECT_TRUE(static_cast<CookieManagerImpl*>(GetProfile()->GetCookieManager())
  124. ->FireFlushTimerForTesting());
  125. EXPECT_GT(GetCookieDbModifiedTime(), original_modified_time);
  126. }
  127. #if BUILDFLAG(IS_WIN)
  128. // TODO(crbug.com/1204901): Disabled due to flakiness on Windows.
  129. #define MAYBE_FlushCookiesAfterSetMultiple DISABLED_FlushCookiesAfterSetMultiple
  130. #else
  131. #define MAYBE_FlushCookiesAfterSetMultiple FlushCookiesAfterSetMultiple
  132. #endif
  133. IN_PROC_BROWSER_TEST_F(CookieManagerBrowserTest,
  134. MAYBE_FlushCookiesAfterSetMultiple) {
  135. EXPECT_TRUE(embedded_test_server()->Start());
  136. NavigateAndWaitForCompletion(
  137. embedded_test_server()->GetURL("/simple_page.html"), shell());
  138. base::Time original_modified_time = GetCookieDbModifiedTime();
  139. ASSERT_TRUE(SetCookie("a=b; expires=Fri, 01 Jan 2038 00:00:00 GMT"));
  140. EXPECT_EQ(GetCookieDbModifiedTime(), original_modified_time);
  141. ASSERT_TRUE(SetCookie("c=d; expires=Fri, 01 Jan 2038 00:00:00 GMT"));
  142. EXPECT_EQ(GetCookieDbModifiedTime(), original_modified_time);
  143. CookieManagerImpl* cookie_manager =
  144. static_cast<CookieManagerImpl*>(GetProfile()->GetCookieManager());
  145. EXPECT_TRUE(cookie_manager->FireFlushTimerForTesting());
  146. EXPECT_GT(GetCookieDbModifiedTime(), original_modified_time);
  147. // Flush timer should be gone now.
  148. EXPECT_FALSE(cookie_manager->FireFlushTimerForTesting());
  149. // Try again to make sure it works a second time.
  150. original_modified_time = GetCookieDbModifiedTime();
  151. ASSERT_TRUE(SetCookie("d=f; expires=Fri, 01 Jan 2038 00:00:00 GMT"));
  152. EXPECT_EQ(GetCookieDbModifiedTime(), original_modified_time);
  153. EXPECT_TRUE(cookie_manager->FireFlushTimerForTesting());
  154. EXPECT_GT(GetCookieDbModifiedTime(), original_modified_time);
  155. }
  156. } // namespace weblayer