2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework 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 <vector>
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2014-07-15 20:10:40 +02:00
|
|
|
#include "include/base/cef_bind.h"
|
2014-07-15 00:18:51 +02:00
|
|
|
#include "include/base/cef_logging.h"
|
|
|
|
#include "include/base/cef_ref_counted.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "include/cef_cookie.h"
|
|
|
|
#include "include/cef_scheme.h"
|
2016-11-15 22:18:41 +01:00
|
|
|
#include "include/cef_waitable_event.h"
|
2014-07-15 20:10:40 +02:00
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2016-11-15 18:56:02 +01:00
|
|
|
#include "include/wrapper/cef_scoped_temp_dir.h"
|
2016-11-18 18:31:21 +01:00
|
|
|
#include "tests/ceftests/test_handler.h"
|
|
|
|
#include "tests/ceftests/test_suite.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
const int kCacheDeleteDelay = 50;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
const char* kTestUrl = "http://www.test.com/path/to/cookietest/foo.html";
|
|
|
|
const char* kTestDomain = "www.test.com";
|
|
|
|
const char* kTestPath = "/path/to/cookietest";
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
const int kIgnoreNumDeleted = -2;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
typedef std::vector<CefCookie> CookieVector;
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
class TestCompletionCallback : public CefCompletionCallback {
|
|
|
|
public:
|
2016-11-15 22:18:41 +01:00
|
|
|
explicit TestCompletionCallback(CefRefPtr<CefWaitableEvent> event)
|
2017-05-17 11:29:28 +02:00
|
|
|
: event_(event) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void OnComplete() override { event_->Signal(); }
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
private:
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event_;
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(TestCompletionCallback);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback);
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestSetCookieCallback : public CefSetCookieCallback {
|
|
|
|
public:
|
|
|
|
TestSetCookieCallback(bool expected_success,
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefWaitableEvent> event)
|
|
|
|
: expected_success_(expected_success), event_(event) {}
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
void OnComplete(bool success) override {
|
|
|
|
EXPECT_EQ(expected_success_, success);
|
|
|
|
event_->Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool expected_success_;
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event_;
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(TestSetCookieCallback);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestSetCookieCallback);
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestDeleteCookiesCallback : public CefDeleteCookiesCallback {
|
|
|
|
public:
|
|
|
|
TestDeleteCookiesCallback(int expected_num_deleted,
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event)
|
2017-05-17 11:29:28 +02:00
|
|
|
: expected_num_deleted_(expected_num_deleted), event_(event) {}
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
void OnComplete(int num_deleted) override {
|
2017-08-11 18:56:52 +02:00
|
|
|
if (expected_num_deleted_ != kIgnoreNumDeleted) {
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_EQ(expected_num_deleted_, num_deleted);
|
2017-08-11 18:56:52 +02:00
|
|
|
}
|
2015-03-02 21:25:14 +01:00
|
|
|
event_->Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int expected_num_deleted_;
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event_;
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(TestDeleteCookiesCallback);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestDeleteCookiesCallback);
|
|
|
|
};
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
class TestVisitor : public CefCookieVisitor {
|
|
|
|
public:
|
2017-05-17 11:29:28 +02:00
|
|
|
TestVisitor(CookieVector* cookies,
|
|
|
|
bool deleteCookies,
|
2018-03-20 21:15:08 +01:00
|
|
|
const base::Closure& callback)
|
|
|
|
: cookies_(cookies), delete_cookies_(deleteCookies), callback_(callback) {
|
|
|
|
EXPECT_TRUE(cookies_);
|
|
|
|
EXPECT_FALSE(callback_.is_null());
|
|
|
|
}
|
|
|
|
~TestVisitor() override { callback_.Run(); }
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool Visit(const CefCookie& cookie,
|
|
|
|
int count,
|
|
|
|
int total,
|
2014-11-12 20:25:15 +01:00
|
|
|
bool& deleteCookie) override {
|
2012-04-03 03:34:16 +02:00
|
|
|
cookies_->push_back(cookie);
|
|
|
|
if (delete_cookies_)
|
|
|
|
deleteCookie = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
private:
|
2012-04-03 03:34:16 +02:00
|
|
|
CookieVector* cookies_;
|
|
|
|
bool delete_cookies_;
|
2018-03-20 21:15:08 +01:00
|
|
|
base::Closure callback_;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(TestVisitor);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set the cookies.
|
|
|
|
void SetCookies(CefRefPtr<CefCookieManager> manager,
|
2017-05-17 11:29:28 +02:00
|
|
|
const CefString& url,
|
|
|
|
const CookieVector& cookies,
|
2015-03-02 21:25:14 +01:00
|
|
|
bool expected_success,
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event) {
|
2015-03-02 21:25:14 +01:00
|
|
|
CookieVector::const_iterator it = cookies.begin();
|
|
|
|
for (; it != cookies.end(); ++it) {
|
|
|
|
EXPECT_TRUE(manager->SetCookie(
|
2016-11-15 22:18:41 +01:00
|
|
|
url, *it, new TestSetCookieCallback(expected_success, event)));
|
|
|
|
event->Wait();
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the cookie.
|
|
|
|
void DeleteCookies(CefRefPtr<CefCookieManager> manager,
|
2017-05-17 11:29:28 +02:00
|
|
|
const CefString& url,
|
|
|
|
const CefString& cookie_name,
|
2015-03-02 21:25:14 +01:00
|
|
|
int expected_num_deleted,
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event) {
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_TRUE(manager->DeleteCookies(
|
|
|
|
url, cookie_name,
|
2016-11-15 22:18:41 +01:00
|
|
|
new TestDeleteCookiesCallback(expected_num_deleted, event)));
|
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a test cookie. If |withDomain| is true a domain cookie will be
|
|
|
|
// created, otherwise a host cookie will be created.
|
|
|
|
void CreateCookie(CefRefPtr<CefCookieManager> manager,
|
2017-05-17 11:29:28 +02:00
|
|
|
CefCookie& cookie,
|
|
|
|
bool withDomain,
|
2013-02-13 20:53:41 +01:00
|
|
|
bool sessionCookie,
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event) {
|
2012-04-03 03:34:16 +02:00
|
|
|
CefString(&cookie.name).FromASCII("my_cookie");
|
|
|
|
CefString(&cookie.value).FromASCII("My Value");
|
|
|
|
if (withDomain)
|
|
|
|
CefString(&cookie.domain).FromASCII(kTestDomain);
|
|
|
|
CefString(&cookie.path).FromASCII(kTestPath);
|
2013-02-13 20:53:41 +01:00
|
|
|
if (!sessionCookie) {
|
|
|
|
cookie.has_expires = true;
|
|
|
|
cookie.expires.year = 2200;
|
|
|
|
cookie.expires.month = 4;
|
|
|
|
cookie.expires.day_of_week = 5;
|
|
|
|
cookie.expires.day_of_month = 11;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CookieVector cookies;
|
|
|
|
cookies.push_back(cookie);
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
SetCookies(manager, kTestUrl, cookies, true, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
// Visit URL cookies. Execute |callback| on completion.
|
|
|
|
void VisitUrlCookies(CefRefPtr<CefCookieManager> manager,
|
|
|
|
const CefString& url,
|
|
|
|
bool includeHttpOnly,
|
|
|
|
CookieVector& cookies,
|
|
|
|
bool deleteCookies,
|
|
|
|
const base::Closure& callback) {
|
|
|
|
EXPECT_TRUE(manager->VisitUrlCookies(
|
|
|
|
url, includeHttpOnly,
|
|
|
|
new TestVisitor(&cookies, deleteCookies, callback)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit URL cookies. Block on |event|.
|
|
|
|
void VisitUrlCookies(CefRefPtr<CefCookieManager> manager,
|
|
|
|
const CefString& url,
|
|
|
|
bool includeHttpOnly,
|
|
|
|
CookieVector& cookies,
|
|
|
|
bool deleteCookies,
|
|
|
|
CefRefPtr<CefWaitableEvent> event) {
|
|
|
|
VisitUrlCookies(manager, url, includeHttpOnly, cookies, deleteCookies,
|
|
|
|
base::Bind(&CefWaitableEvent::Signal, event));
|
|
|
|
event->Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit all cookies. Execute |callback| on completion.
|
|
|
|
void VisitAllCookies(CefRefPtr<CefCookieManager> manager,
|
|
|
|
CookieVector& cookies,
|
|
|
|
bool deleteCookies,
|
|
|
|
const base::Closure& callback) {
|
|
|
|
EXPECT_TRUE(manager->VisitAllCookies(
|
|
|
|
new TestVisitor(&cookies, deleteCookies, callback)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit all cookies. Block on |event|.
|
|
|
|
void VisitAllCookies(CefRefPtr<CefCookieManager> manager,
|
|
|
|
CookieVector& cookies,
|
|
|
|
bool deleteCookies,
|
|
|
|
CefRefPtr<CefWaitableEvent> event) {
|
|
|
|
VisitAllCookies(manager, cookies, deleteCookies,
|
|
|
|
base::Bind(&CefWaitableEvent::Signal, event));
|
|
|
|
event->Wait();
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Retrieve the test cookie. If |withDomain| is true check that the cookie
|
|
|
|
// is a domain cookie, otherwise a host cookie. if |deleteCookies| is true
|
|
|
|
// the cookie will be deleted when it's retrieved.
|
|
|
|
void GetCookie(CefRefPtr<CefCookieManager> manager,
|
2017-05-17 11:29:28 +02:00
|
|
|
const CefCookie& cookie,
|
|
|
|
bool withDomain,
|
|
|
|
CefRefPtr<CefWaitableEvent> event,
|
|
|
|
bool deleteCookies) {
|
2012-04-03 03:34:16 +02:00
|
|
|
CookieVector cookies;
|
|
|
|
|
|
|
|
// Get the cookie and delete it.
|
2018-03-20 21:15:08 +01:00
|
|
|
VisitUrlCookies(manager, kTestUrl, false, cookies, deleteCookies, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_EQ(1U, cookies.size());
|
|
|
|
if (cookies.size() != 1U)
|
|
|
|
return;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
const CefCookie& cookie_read = cookies[0];
|
|
|
|
EXPECT_EQ(CefString(&cookie_read.name), "my_cookie");
|
|
|
|
EXPECT_EQ(CefString(&cookie_read.value), "My Value");
|
|
|
|
if (withDomain)
|
|
|
|
EXPECT_EQ(CefString(&cookie_read.domain), ".www.test.com");
|
|
|
|
else
|
|
|
|
EXPECT_EQ(CefString(&cookie_read.domain), kTestDomain);
|
|
|
|
EXPECT_EQ(CefString(&cookie_read.path), kTestPath);
|
2013-02-13 20:53:41 +01:00
|
|
|
EXPECT_EQ(cookie.has_expires, cookie_read.has_expires);
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_EQ(cookie.expires.year, cookie_read.expires.year);
|
|
|
|
EXPECT_EQ(cookie.expires.month, cookie_read.expires.month);
|
|
|
|
EXPECT_EQ(cookie.expires.day_of_week, cookie_read.expires.day_of_week);
|
|
|
|
EXPECT_EQ(cookie.expires.day_of_month, cookie_read.expires.day_of_month);
|
|
|
|
EXPECT_EQ(cookie.expires.hour, cookie_read.expires.hour);
|
|
|
|
EXPECT_EQ(cookie.expires.minute, cookie_read.expires.minute);
|
|
|
|
EXPECT_EQ(cookie.expires.second, cookie_read.expires.second);
|
|
|
|
EXPECT_EQ(cookie.expires.millisecond, cookie_read.expires.millisecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that no cookies exist. If |withUrl| is true it will only check for
|
|
|
|
// cookies matching the URL.
|
|
|
|
void VerifyNoCookies(CefRefPtr<CefCookieManager> manager,
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefWaitableEvent> event,
|
|
|
|
bool withUrl) {
|
2012-04-03 03:34:16 +02:00
|
|
|
CookieVector cookies;
|
|
|
|
|
|
|
|
// Verify that the cookie has been deleted.
|
|
|
|
if (withUrl) {
|
2018-03-20 21:15:08 +01:00
|
|
|
VisitUrlCookies(manager, kTestUrl, false, cookies, false, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
} else {
|
2018-03-20 21:15:08 +01:00
|
|
|
VisitAllCookies(manager, cookies, false, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_EQ(0U, cookies.size());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all system cookies.
|
|
|
|
void DeleteAllCookies(CefRefPtr<CefCookieManager> manager,
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event) {
|
2015-03-02 21:25:14 +01:00
|
|
|
DeleteCookies(manager, CefString(), CefString(), kIgnoreNumDeleted, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
void TestDomainCookie(CefRefPtr<CefCookieManager> manager,
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event) {
|
2012-04-03 03:34:16 +02:00
|
|
|
CefCookie cookie;
|
|
|
|
|
|
|
|
// Create a domain cookie.
|
2013-02-13 20:53:41 +01:00
|
|
|
CreateCookie(manager, cookie, true, false, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Retrieve, verify and delete the domain cookie.
|
|
|
|
GetCookie(manager, cookie, true, event, true);
|
|
|
|
|
|
|
|
// Verify that the cookie was deleted.
|
|
|
|
VerifyNoCookies(manager, event, true);
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
void TestHostCookie(CefRefPtr<CefCookieManager> manager,
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event) {
|
2012-04-03 03:34:16 +02:00
|
|
|
CefCookie cookie;
|
|
|
|
|
|
|
|
// Create a host cookie.
|
2013-02-13 20:53:41 +01:00
|
|
|
CreateCookie(manager, cookie, false, false, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Retrieve, verify and delete the host cookie.
|
|
|
|
GetCookie(manager, cookie, false, event, true);
|
|
|
|
|
|
|
|
// Verify that the cookie was deleted.
|
|
|
|
VerifyNoCookies(manager, event, true);
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
void TestMultipleCookies(CefRefPtr<CefCookieManager> manager,
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event) {
|
2012-04-03 03:34:16 +02:00
|
|
|
std::stringstream ss;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
CookieVector cookies;
|
|
|
|
|
|
|
|
const int kNumCookies = 4;
|
|
|
|
|
|
|
|
// Create the cookies.
|
|
|
|
for (i = 0; i < kNumCookies; i++) {
|
|
|
|
CefCookie cookie;
|
|
|
|
|
|
|
|
ss << "my_cookie" << i;
|
|
|
|
CefString(&cookie.name).FromASCII(ss.str().c_str());
|
|
|
|
ss.str("");
|
|
|
|
ss << "My Value " << i;
|
|
|
|
CefString(&cookie.value).FromASCII(ss.str().c_str());
|
|
|
|
ss.str("");
|
|
|
|
|
|
|
|
cookies.push_back(cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the cookies.
|
2015-03-02 21:25:14 +01:00
|
|
|
SetCookies(manager, kTestUrl, cookies, true, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
cookies.clear();
|
|
|
|
|
|
|
|
// Get the cookies without deleting them.
|
|
|
|
VisitUrlCookies(manager, kTestUrl, false, cookies, false, event);
|
|
|
|
|
|
|
|
EXPECT_EQ((CookieVector::size_type)kNumCookies, cookies.size());
|
|
|
|
|
|
|
|
CookieVector::const_iterator it = cookies.begin();
|
|
|
|
for (i = 0; it != cookies.end(); ++it, ++i) {
|
|
|
|
const CefCookie& cookie = *it;
|
|
|
|
|
|
|
|
ss << "my_cookie" << i;
|
|
|
|
EXPECT_EQ(CefString(&cookie.name), ss.str());
|
|
|
|
ss.str("");
|
|
|
|
ss << "My Value " << i;
|
|
|
|
EXPECT_EQ(CefString(&cookie.value), ss.str());
|
|
|
|
ss.str("");
|
|
|
|
}
|
|
|
|
|
|
|
|
cookies.clear();
|
|
|
|
|
|
|
|
// Delete the 2nd cookie.
|
2019-02-21 01:42:36 +01:00
|
|
|
DeleteCookies(manager, kTestUrl, CefString("my_cookie1"), 1, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Verify that the cookie has been deleted.
|
|
|
|
VisitUrlCookies(manager, kTestUrl, false, cookies, false, event);
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_EQ(3U, cookies.size());
|
|
|
|
if (cookies.size() != 3U)
|
|
|
|
return;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_EQ(CefString(&cookies[0].name), "my_cookie0");
|
|
|
|
EXPECT_EQ(CefString(&cookies[1].name), "my_cookie2");
|
|
|
|
EXPECT_EQ(CefString(&cookies[2].name), "my_cookie3");
|
|
|
|
|
|
|
|
cookies.clear();
|
|
|
|
|
|
|
|
// Delete the rest of the cookies.
|
2015-03-02 21:25:14 +01:00
|
|
|
DeleteCookies(manager, kTestUrl, CefString(), 3, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Verify that the cookies have been deleted.
|
|
|
|
VisitUrlCookies(manager, kTestUrl, false, cookies, false, event);
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_EQ(0U, cookies.size());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Create the cookies.
|
|
|
|
for (i = 0; i < kNumCookies; i++) {
|
|
|
|
CefCookie cookie;
|
|
|
|
|
|
|
|
ss << "my_cookie" << i;
|
|
|
|
CefString(&cookie.name).FromASCII(ss.str().c_str());
|
|
|
|
ss.str("");
|
|
|
|
ss << "My Value " << i;
|
|
|
|
CefString(&cookie.value).FromASCII(ss.str().c_str());
|
|
|
|
ss.str("");
|
|
|
|
|
|
|
|
cookies.push_back(cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all of the cookies using the visitor.
|
|
|
|
VisitUrlCookies(manager, kTestUrl, false, cookies, true, event);
|
|
|
|
|
|
|
|
cookies.clear();
|
|
|
|
|
|
|
|
// Verify that the cookies have been deleted.
|
|
|
|
VisitUrlCookies(manager, kTestUrl, false, cookies, false, event);
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_EQ(0U, cookies.size());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
void TestAllCookies(CefRefPtr<CefCookieManager> manager,
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event) {
|
2012-04-03 03:34:16 +02:00
|
|
|
CookieVector cookies;
|
|
|
|
|
|
|
|
// Delete all system cookies just in case something is left over from a
|
|
|
|
// different test.
|
2015-03-02 21:25:14 +01:00
|
|
|
DeleteAllCookies(manager, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Verify that all system cookies have been deleted.
|
|
|
|
VisitAllCookies(manager, cookies, false, event);
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_EQ(0U, cookies.size());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Create cookies with 2 separate hosts.
|
|
|
|
CefCookie cookie1;
|
|
|
|
const char* kUrl1 = "http://www.foo.com";
|
|
|
|
CefString(&cookie1.name).FromASCII("my_cookie1");
|
|
|
|
CefString(&cookie1.value).FromASCII("My Value 1");
|
|
|
|
|
|
|
|
cookies.push_back(cookie1);
|
2017-05-17 11:29:28 +02:00
|
|
|
SetCookies(manager, kUrl1, cookies, true, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
cookies.clear();
|
|
|
|
|
|
|
|
CefCookie cookie2;
|
|
|
|
const char* kUrl2 = "http://www.bar.com";
|
|
|
|
CefString(&cookie2.name).FromASCII("my_cookie2");
|
|
|
|
CefString(&cookie2.value).FromASCII("My Value 2");
|
|
|
|
|
|
|
|
cookies.push_back(cookie2);
|
2017-05-17 11:29:28 +02:00
|
|
|
SetCookies(manager, kUrl2, cookies, true, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
cookies.clear();
|
|
|
|
|
|
|
|
// Verify that all system cookies can be retrieved.
|
|
|
|
VisitAllCookies(manager, cookies, false, event);
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_EQ(2U, cookies.size());
|
|
|
|
if (cookies.size() != 2U)
|
|
|
|
return;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_EQ(CefString(&cookies[0].name), "my_cookie1");
|
|
|
|
EXPECT_EQ(CefString(&cookies[0].value), "My Value 1");
|
|
|
|
EXPECT_EQ(CefString(&cookies[0].domain), "www.foo.com");
|
|
|
|
EXPECT_EQ(CefString(&cookies[1].name), "my_cookie2");
|
|
|
|
EXPECT_EQ(CefString(&cookies[1].value), "My Value 2");
|
|
|
|
EXPECT_EQ(CefString(&cookies[1].domain), "www.bar.com");
|
|
|
|
cookies.clear();
|
|
|
|
|
|
|
|
// Verify that the cookies can be retrieved separately.
|
|
|
|
VisitUrlCookies(manager, kUrl1, false, cookies, false, event);
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_EQ(1U, cookies.size());
|
|
|
|
if (cookies.size() != 1U)
|
|
|
|
return;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_EQ(CefString(&cookies[0].name), "my_cookie1");
|
|
|
|
EXPECT_EQ(CefString(&cookies[0].value), "My Value 1");
|
|
|
|
EXPECT_EQ(CefString(&cookies[0].domain), "www.foo.com");
|
|
|
|
cookies.clear();
|
|
|
|
|
|
|
|
VisitUrlCookies(manager, kUrl2, false, cookies, false, event);
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_EQ(1U, cookies.size());
|
|
|
|
if (cookies.size() != 1U)
|
|
|
|
return;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_EQ(CefString(&cookies[0].name), "my_cookie2");
|
|
|
|
EXPECT_EQ(CefString(&cookies[0].value), "My Value 2");
|
|
|
|
EXPECT_EQ(CefString(&cookies[0].domain), "www.bar.com");
|
|
|
|
cookies.clear();
|
|
|
|
|
|
|
|
// Delete all of the system cookies.
|
|
|
|
DeleteAllCookies(manager, event);
|
|
|
|
|
|
|
|
// Verify that all system cookies have been deleted.
|
|
|
|
VerifyNoCookies(manager, event, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestChangeDirectory(CefRefPtr<CefCookieManager> manager,
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event,
|
2012-04-03 03:34:16 +02:00
|
|
|
const CefString& original_dir) {
|
|
|
|
CefCookie cookie;
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
CefScopedTempDir temp_dir;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Create a new temporary directory.
|
|
|
|
EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
|
|
|
|
|
|
|
|
// Delete all of the system cookies.
|
|
|
|
DeleteAllCookies(manager, event);
|
|
|
|
|
|
|
|
// Set the new temporary directory as the storage location.
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_TRUE(manager->SetStoragePath(temp_dir.GetPath(), false, NULL));
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Wait for the storage location change to complete on the IO thread.
|
|
|
|
WaitForIOThread();
|
|
|
|
|
|
|
|
// Verify that no cookies exist.
|
|
|
|
VerifyNoCookies(manager, event, true);
|
|
|
|
|
|
|
|
// Create a domain cookie.
|
2013-02-13 20:53:41 +01:00
|
|
|
CreateCookie(manager, cookie, true, false, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Retrieve and verify the domain cookie.
|
|
|
|
GetCookie(manager, cookie, true, event, false);
|
|
|
|
|
|
|
|
// Restore the original storage location.
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_TRUE(manager->SetStoragePath(original_dir, false, NULL));
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Wait for the storage location change to complete on the IO thread.
|
|
|
|
WaitForIOThread();
|
|
|
|
|
|
|
|
// Verify that no cookies exist.
|
|
|
|
VerifyNoCookies(manager, event, true);
|
|
|
|
|
|
|
|
// Set the new temporary directory as the storage location.
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_TRUE(manager->SetStoragePath(temp_dir.GetPath(), false, NULL));
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Wait for the storage location change to complete on the IO thread.
|
|
|
|
WaitForIOThread();
|
|
|
|
|
|
|
|
// Retrieve and verify the domain cookie that was set previously.
|
|
|
|
GetCookie(manager, cookie, true, event, false);
|
|
|
|
|
|
|
|
// Restore the original storage location.
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_TRUE(manager->SetStoragePath(original_dir, false, NULL));
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Wait for the storage location change to complete on the IO thread.
|
|
|
|
WaitForIOThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Test creation of a domain cookie.
|
|
|
|
TEST(CookieTest, DomainCookieGlobal) {
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
CefRefPtr<CefCookieManager> manager =
|
2016-11-15 22:18:41 +01:00
|
|
|
CefCookieManager::GetGlobalManager(new TestCompletionCallback(event));
|
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestDomainCookie(manager, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test creation of a domain cookie.
|
|
|
|
TEST(CookieTest, DomainCookieInMemory) {
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefCookieManager> manager = CefCookieManager::CreateManager(
|
|
|
|
CefString(), false, new TestCompletionCallback(event));
|
2016-11-15 22:18:41 +01:00
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestDomainCookie(manager, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test creation of a domain cookie.
|
|
|
|
TEST(CookieTest, DomainCookieOnDisk) {
|
2016-11-15 18:56:02 +01:00
|
|
|
CefScopedTempDir temp_dir;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Create a new temporary directory.
|
|
|
|
EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefCookieManager> manager = CefCookieManager::CreateManager(
|
|
|
|
temp_dir.GetPath(), false, new TestCompletionCallback(event));
|
2016-11-15 22:18:41 +01:00
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestDomainCookie(manager, event);
|
2015-11-10 21:18:16 +01:00
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
// The backing store will be closed on the FILE thread after the CookieManager
|
2015-11-10 21:18:16 +01:00
|
|
|
// is destroyed.
|
|
|
|
manager = NULL;
|
2018-03-20 21:15:08 +01:00
|
|
|
WaitForFILEThreadWithDelay(kCacheDeleteDelay);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test creation of a host cookie.
|
|
|
|
TEST(CookieTest, HostCookieGlobal) {
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
CefRefPtr<CefCookieManager> manager =
|
2016-11-15 22:18:41 +01:00
|
|
|
CefCookieManager::GetGlobalManager(new TestCompletionCallback(event));
|
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestHostCookie(manager, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test creation of a host cookie.
|
|
|
|
TEST(CookieTest, HostCookieInMemory) {
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefCookieManager> manager = CefCookieManager::CreateManager(
|
|
|
|
CefString(), false, new TestCompletionCallback(event));
|
2016-11-15 22:18:41 +01:00
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestHostCookie(manager, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test creation of a host cookie.
|
|
|
|
TEST(CookieTest, HostCookieOnDisk) {
|
2016-11-15 18:56:02 +01:00
|
|
|
CefScopedTempDir temp_dir;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Create a new temporary directory.
|
|
|
|
EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefCookieManager> manager = CefCookieManager::CreateManager(
|
|
|
|
temp_dir.GetPath(), false, new TestCompletionCallback(event));
|
2016-11-15 22:18:41 +01:00
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestHostCookie(manager, event);
|
2015-11-10 21:18:16 +01:00
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
// The backing store will be closed on the FILE thread after the CookieManager
|
2015-11-10 21:18:16 +01:00
|
|
|
// is destroyed.
|
|
|
|
manager = NULL;
|
2018-03-20 21:15:08 +01:00
|
|
|
WaitForFILEThreadWithDelay(kCacheDeleteDelay);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test creation of multiple cookies.
|
|
|
|
TEST(CookieTest, MultipleCookiesGlobal) {
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
CefRefPtr<CefCookieManager> manager =
|
2016-11-15 22:18:41 +01:00
|
|
|
CefCookieManager::GetGlobalManager(new TestCompletionCallback(event));
|
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestMultipleCookies(manager, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test creation of multiple cookies.
|
|
|
|
TEST(CookieTest, MultipleCookiesInMemory) {
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefCookieManager> manager = CefCookieManager::CreateManager(
|
|
|
|
CefString(), false, new TestCompletionCallback(event));
|
2016-11-15 22:18:41 +01:00
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestMultipleCookies(manager, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test creation of multiple cookies.
|
|
|
|
TEST(CookieTest, MultipleCookiesOnDisk) {
|
2016-11-15 18:56:02 +01:00
|
|
|
CefScopedTempDir temp_dir;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Create a new temporary directory.
|
|
|
|
EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefCookieManager> manager = CefCookieManager::CreateManager(
|
|
|
|
temp_dir.GetPath(), false, new TestCompletionCallback(event));
|
2016-11-15 22:18:41 +01:00
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestMultipleCookies(manager, event);
|
2015-11-10 21:18:16 +01:00
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
// The backing store will be closed on the FILE thread after the CookieManager
|
2015-11-10 21:18:16 +01:00
|
|
|
// is destroyed.
|
|
|
|
manager = NULL;
|
2018-03-20 21:15:08 +01:00
|
|
|
WaitForFILEThreadWithDelay(kCacheDeleteDelay);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CookieTest, AllCookiesGlobal) {
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
CefRefPtr<CefCookieManager> manager =
|
2016-11-15 22:18:41 +01:00
|
|
|
CefCookieManager::GetGlobalManager(new TestCompletionCallback(event));
|
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestAllCookies(manager, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CookieTest, AllCookiesInMemory) {
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefCookieManager> manager = CefCookieManager::CreateManager(
|
|
|
|
CefString(), false, new TestCompletionCallback(event));
|
2016-11-15 22:18:41 +01:00
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestAllCookies(manager, event);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CookieTest, AllCookiesOnDisk) {
|
2016-11-15 18:56:02 +01:00
|
|
|
CefScopedTempDir temp_dir;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Create a new temporary directory.
|
|
|
|
EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefCookieManager> manager = CefCookieManager::CreateManager(
|
|
|
|
temp_dir.GetPath(), false, new TestCompletionCallback(event));
|
2016-11-15 22:18:41 +01:00
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestAllCookies(manager, event);
|
2015-11-10 21:18:16 +01:00
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
// The backing store will be closed on the FILE thread after the CookieManager
|
2015-11-10 21:18:16 +01:00
|
|
|
// is destroyed.
|
|
|
|
manager = NULL;
|
2018-03-20 21:15:08 +01:00
|
|
|
WaitForFILEThreadWithDelay(kCacheDeleteDelay);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CookieTest, ChangeDirectoryGlobal) {
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
CefRefPtr<CefCookieManager> manager =
|
2016-11-15 22:18:41 +01:00
|
|
|
CefCookieManager::GetGlobalManager(new TestCompletionCallback(event));
|
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
|
|
|
std::string cache_path;
|
2016-11-16 21:24:13 +01:00
|
|
|
CefTestSuite::GetInstance()->GetCachePath(cache_path);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestChangeDirectory(manager, event, cache_path);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CookieTest, ChangeDirectoryCreated) {
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefCookieManager> manager = CefCookieManager::CreateManager(
|
|
|
|
CefString(), false, new TestCompletionCallback(event));
|
2016-11-15 22:18:41 +01:00
|
|
|
event->Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
TestChangeDirectory(manager, event, CefString());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-02-13 20:53:41 +01:00
|
|
|
TEST(CookieTest, SessionCookieNoPersist) {
|
2016-11-15 18:56:02 +01:00
|
|
|
CefScopedTempDir temp_dir;
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2013-02-13 20:53:41 +01:00
|
|
|
CefCookie cookie;
|
|
|
|
|
|
|
|
// Create a new temporary directory.
|
|
|
|
EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefCookieManager> manager = CefCookieManager::CreateManager(
|
|
|
|
temp_dir.GetPath(), false, new TestCompletionCallback(event));
|
2016-11-15 22:18:41 +01:00
|
|
|
event->Wait();
|
2013-02-13 20:53:41 +01:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
|
|
|
// Create a session cookie.
|
|
|
|
CreateCookie(manager, cookie, true, true, event);
|
|
|
|
|
|
|
|
// Retrieve and verify the cookie.
|
|
|
|
GetCookie(manager, cookie, true, event, false);
|
|
|
|
|
|
|
|
// Flush the cookie store to disk.
|
2016-11-15 22:18:41 +01:00
|
|
|
manager->FlushStore(new TestCompletionCallback(event));
|
|
|
|
event->Wait();
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2013-02-13 20:53:41 +01:00
|
|
|
// Create a new manager to read the same cookie store.
|
2016-11-15 18:56:02 +01:00
|
|
|
manager = CefCookieManager::CreateManager(temp_dir.GetPath(), false,
|
2016-11-15 22:18:41 +01:00
|
|
|
new TestCompletionCallback(event));
|
|
|
|
event->Wait();
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_TRUE(manager.get());
|
2013-02-13 20:53:41 +01:00
|
|
|
|
|
|
|
// Verify that the cookie doesn't exist.
|
|
|
|
VerifyNoCookies(manager, event, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CookieTest, SessionCookieWillPersist) {
|
2016-11-15 18:56:02 +01:00
|
|
|
CefScopedTempDir temp_dir;
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
2013-02-13 20:53:41 +01:00
|
|
|
CefCookie cookie;
|
|
|
|
|
|
|
|
// Create a new temporary directory.
|
|
|
|
EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefCookieManager> manager = CefCookieManager::CreateManager(
|
|
|
|
temp_dir.GetPath(), true, new TestCompletionCallback(event));
|
2016-11-15 22:18:41 +01:00
|
|
|
event->Wait();
|
2013-02-13 20:53:41 +01:00
|
|
|
EXPECT_TRUE(manager.get());
|
|
|
|
|
|
|
|
// Create a session cookie.
|
|
|
|
CreateCookie(manager, cookie, true, true, event);
|
|
|
|
|
|
|
|
// Retrieve and verify the cookie.
|
|
|
|
GetCookie(manager, cookie, true, event, false);
|
|
|
|
|
|
|
|
// Flush the cookie store to disk.
|
2016-11-15 22:18:41 +01:00
|
|
|
manager->FlushStore(new TestCompletionCallback(event));
|
|
|
|
event->Wait();
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2013-02-13 20:53:41 +01:00
|
|
|
// Create a new manager to read the same cookie store.
|
2016-11-15 18:56:02 +01:00
|
|
|
manager = CefCookieManager::CreateManager(temp_dir.GetPath(), true,
|
2016-11-15 22:18:41 +01:00
|
|
|
new TestCompletionCallback(event));
|
|
|
|
event->Wait();
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_TRUE(manager.get());
|
2013-02-13 20:53:41 +01:00
|
|
|
|
|
|
|
// Verify that the cookie exists.
|
|
|
|
GetCookie(manager, cookie, true, event, false);
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char* kCookieJSUrl1 = "http://tests/cookie1.html";
|
|
|
|
const char* kCookieJSUrl2 = "http://tests/cookie2.html";
|
|
|
|
|
|
|
|
class CookieTestJSHandler : public TestHandler {
|
|
|
|
public:
|
2013-09-03 18:43:31 +02:00
|
|
|
class RequestContextHandler : public CefRequestContextHandler {
|
|
|
|
public:
|
|
|
|
explicit RequestContextHandler(CookieTestJSHandler* handler)
|
|
|
|
: handler_(handler) {}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefCookieManager> GetCookieManager() override {
|
2013-09-03 18:43:31 +02:00
|
|
|
EXPECT_TRUE(handler_);
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_IO));
|
|
|
|
|
|
|
|
if (url_ == kCookieJSUrl1) {
|
|
|
|
// Return the first cookie manager.
|
|
|
|
handler_->got_cookie_manager1_.yes();
|
|
|
|
return handler_->manager1_;
|
|
|
|
} else {
|
|
|
|
// Return the second cookie manager.
|
|
|
|
handler_->got_cookie_manager2_.yes();
|
|
|
|
return handler_->manager2_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void SetURL(const std::string& url) { url_ = url; }
|
2013-09-03 18:43:31 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void Detach() { handler_ = NULL; }
|
2013-09-03 18:43:31 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string url_;
|
|
|
|
CookieTestJSHandler* handler_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(RequestContextHandler);
|
|
|
|
};
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
CookieTestJSHandler() {}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void RunTest() override {
|
2015-03-02 21:25:14 +01:00
|
|
|
// Create new in-memory managers.
|
|
|
|
manager1_ = CefCookieManager::CreateManager(CefString(), false, NULL);
|
|
|
|
manager2_ = CefCookieManager::CreateManager(CefString(), false, NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
std::string page =
|
|
|
|
"<html><head>"
|
|
|
|
"<script>"
|
|
|
|
"document.cookie='name1=value1';"
|
|
|
|
"</script>"
|
|
|
|
"</head><body>COOKIE TEST1</body></html>";
|
|
|
|
AddResource(kCookieJSUrl1, page, "text/html");
|
|
|
|
|
|
|
|
page =
|
|
|
|
"<html><head>"
|
|
|
|
"<script>"
|
|
|
|
"document.cookie='name2=value2';"
|
|
|
|
"</script>"
|
|
|
|
"</head><body>COOKIE TEST2</body></html>";
|
|
|
|
AddResource(kCookieJSUrl2, page, "text/html");
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
context_handler_ = new RequestContextHandler(this);
|
|
|
|
context_handler_->SetURL(kCookieJSUrl1);
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
// Create the request context that will use an in-memory cache.
|
|
|
|
CefRequestContextSettings settings;
|
|
|
|
CefRefPtr<CefRequestContext> request_context =
|
|
|
|
CefRequestContext::CreateContext(settings, context_handler_.get());
|
|
|
|
|
|
|
|
// Create the browser.
|
|
|
|
CreateBrowser(kCookieJSUrl1, request_context);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
// Time out the test after a reasonable period of time.
|
|
|
|
SetTestTimeout();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
// Go to the next URL.
|
|
|
|
void LoadNextURL(CefRefPtr<CefFrame> frame) {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
|
|
|
CefPostTask(TID_UI,
|
2017-05-17 11:29:28 +02:00
|
|
|
base::Bind(&CookieTestJSHandler::LoadNextURL, this, frame));
|
2016-11-15 22:18:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
context_handler_->SetURL(kCookieJSUrl2);
|
|
|
|
frame->LoadURL(kCookieJSUrl2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompleteTest() {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2017-05-17 11:29:28 +02:00
|
|
|
CefPostTask(TID_UI, base::Bind(&CookieTestJSHandler::CompleteTest, this));
|
2016-11-15 22:18:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DestroyTest();
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnLoadEnd(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
int httpStatusCode) override {
|
2012-04-03 03:34:16 +02:00
|
|
|
std::string url = frame->GetURL();
|
|
|
|
if (url == kCookieJSUrl1) {
|
|
|
|
got_load_end1_.yes();
|
2016-11-15 22:18:41 +01:00
|
|
|
VerifyCookie(manager1_, url, "name1", "value1", &got_cookie1_,
|
2017-05-17 11:29:28 +02:00
|
|
|
base::Bind(&CookieTestJSHandler::LoadNextURL, this, frame));
|
2012-04-03 03:34:16 +02:00
|
|
|
} else {
|
|
|
|
got_load_end2_.yes();
|
2016-11-15 22:18:41 +01:00
|
|
|
VerifyCookie(manager2_, url, "name2", "value2", &got_cookie2_,
|
2017-05-17 11:29:28 +02:00
|
|
|
base::Bind(&CookieTestJSHandler::CompleteTest, this));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void DestroyTest() override {
|
2013-09-03 18:43:31 +02:00
|
|
|
context_handler_->Detach();
|
|
|
|
context_handler_ = NULL;
|
|
|
|
|
|
|
|
TestHandler::DestroyTest();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the cookie was set successfully.
|
|
|
|
void VerifyCookie(CefRefPtr<CefCookieManager> manager,
|
|
|
|
const std::string& url,
|
|
|
|
const std::string& name,
|
|
|
|
const std::string& value,
|
2016-11-15 22:18:41 +01:00
|
|
|
TrackCallback* callback,
|
|
|
|
const base::Closure& continue_callback) {
|
2012-04-03 03:34:16 +02:00
|
|
|
// Get the cookie.
|
2018-03-20 21:15:08 +01:00
|
|
|
EXPECT_TRUE(cookies_.empty());
|
|
|
|
VisitUrlCookies(manager, url, false, cookies_, false,
|
|
|
|
base::Bind(&CookieTestJSHandler::VerifyCookieComplete, this,
|
|
|
|
name, value, callback, continue_callback));
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
void VerifyCookieComplete(const std::string& name,
|
|
|
|
const std::string& value,
|
|
|
|
TrackCallback* callback,
|
|
|
|
const base::Closure& continue_callback) {
|
|
|
|
if (cookies_.size() == 1U && CefString(&cookies_[0].name) == name &&
|
|
|
|
CefString(&cookies_[0].value) == value) {
|
2016-11-15 22:18:41 +01:00
|
|
|
callback->yes();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2016-11-15 22:18:41 +01:00
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
cookies_.clear();
|
2016-11-15 22:18:41 +01:00
|
|
|
continue_callback.Run();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
CefRefPtr<RequestContextHandler> context_handler_;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefCookieManager> manager1_;
|
|
|
|
CefRefPtr<CefCookieManager> manager2_;
|
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
CookieVector cookies_;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
TrackCallback got_cookie_manager1_;
|
|
|
|
TrackCallback got_cookie_manager2_;
|
|
|
|
TrackCallback got_load_end1_;
|
|
|
|
TrackCallback got_load_end2_;
|
|
|
|
TrackCallback got_cookie1_;
|
|
|
|
TrackCallback got_cookie2_;
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CookieTestJSHandler);
|
2012-04-03 03:34:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Verify use of multiple cookie managers vis JS.
|
|
|
|
TEST(CookieTest, GetCookieManagerJS) {
|
|
|
|
CefRefPtr<CookieTestJSHandler> handler = new CookieTestJSHandler();
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_cookie_manager1_);
|
|
|
|
EXPECT_TRUE(handler->got_cookie_manager2_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end1_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end2_);
|
|
|
|
EXPECT_TRUE(handler->got_cookie1_);
|
|
|
|
EXPECT_TRUE(handler->got_cookie2_);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-04-04 00:14:28 +02:00
|
|
|
class CookieTestSchemeHandler : public TestHandler {
|
2012-04-03 03:34:16 +02:00
|
|
|
public:
|
|
|
|
class SchemeHandler : public CefResourceHandler {
|
|
|
|
public:
|
2012-04-04 00:14:28 +02:00
|
|
|
explicit SchemeHandler(CookieTestSchemeHandler* handler)
|
2017-05-17 11:29:28 +02:00
|
|
|
: handler_(handler), offset_(0) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool ProcessRequest(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2012-04-03 03:34:16 +02:00
|
|
|
std::string url = request->GetURL();
|
2012-04-04 00:14:28 +02:00
|
|
|
if (url == handler_->url1_) {
|
2012-04-03 03:34:16 +02:00
|
|
|
content_ = "<html><body>COOKIE TEST1</body></html>";
|
|
|
|
cookie_ = "name1=value1";
|
|
|
|
handler_->got_process_request1_.yes();
|
2012-04-04 00:14:28 +02:00
|
|
|
} else if (url == handler_->url2_) {
|
2012-04-03 03:34:16 +02:00
|
|
|
content_ = "<html><body>COOKIE TEST2</body></html>";
|
|
|
|
cookie_ = "name2=value2";
|
|
|
|
handler_->got_process_request2_.yes();
|
2012-04-04 00:14:28 +02:00
|
|
|
} else if (url == handler_->url3_) {
|
2012-04-03 03:34:16 +02:00
|
|
|
content_ = "<html><body>COOKIE TEST3</body></html>";
|
|
|
|
handler_->got_process_request3_.yes();
|
|
|
|
|
|
|
|
// Verify that the cookie was passed in.
|
|
|
|
CefRequest::HeaderMap headerMap;
|
|
|
|
request->GetHeaderMap(headerMap);
|
|
|
|
CefRequest::HeaderMap::iterator it = headerMap.find("Cookie");
|
|
|
|
if (it != headerMap.end() && it->second == "name2=value2")
|
|
|
|
handler_->got_process_request_cookie_.yes();
|
|
|
|
}
|
|
|
|
callback->Continue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void GetResponseHeaders(CefRefPtr<CefResponse> response,
|
|
|
|
int64& response_length,
|
|
|
|
CefString& redirectUrl) override {
|
2012-04-03 03:34:16 +02:00
|
|
|
response_length = content_.size();
|
|
|
|
|
|
|
|
response->SetStatus(200);
|
|
|
|
response->SetMimeType("text/html");
|
|
|
|
|
|
|
|
if (!cookie_.empty()) {
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
response->GetHeaderMap(headerMap);
|
|
|
|
headerMap.insert(std::make_pair("Set-Cookie", cookie_));
|
|
|
|
response->SetHeaderMap(headerMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool ReadResponse(void* data_out,
|
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2012-04-03 03:34:16 +02:00
|
|
|
bool has_data = false;
|
|
|
|
bytes_read = 0;
|
|
|
|
|
|
|
|
size_t size = content_.size();
|
|
|
|
if (offset_ < size) {
|
|
|
|
int transfer_size =
|
|
|
|
std::min(bytes_to_read, static_cast<int>(size - offset_));
|
|
|
|
memcpy(data_out, content_.c_str() + offset_, transfer_size);
|
|
|
|
offset_ += transfer_size;
|
|
|
|
|
|
|
|
bytes_read = transfer_size;
|
|
|
|
has_data = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return has_data;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void Cancel() override {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
private:
|
2012-04-04 00:14:28 +02:00
|
|
|
CookieTestSchemeHandler* handler_;
|
2012-04-03 03:34:16 +02:00
|
|
|
std::string content_;
|
|
|
|
size_t offset_;
|
|
|
|
std::string cookie_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(SchemeHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
class SchemeHandlerFactory : public CefSchemeHandlerFactory {
|
|
|
|
public:
|
2012-04-04 00:14:28 +02:00
|
|
|
explicit SchemeHandlerFactory(CookieTestSchemeHandler* handler)
|
2012-04-03 03:34:16 +02:00
|
|
|
: handler_(handler) {}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefResourceHandler> Create(
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
const CefString& scheme_name,
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefRequest> request) override {
|
2012-04-03 03:34:16 +02:00
|
|
|
std::string url = request->GetURL();
|
2012-04-04 00:14:28 +02:00
|
|
|
if (url == handler_->url3_) {
|
2012-04-03 03:34:16 +02:00
|
|
|
// Verify that the cookie was not passed in.
|
|
|
|
CefRequest::HeaderMap headerMap;
|
|
|
|
request->GetHeaderMap(headerMap);
|
|
|
|
CefRequest::HeaderMap::iterator it = headerMap.find("Cookie");
|
|
|
|
if (it != headerMap.end() && it->second == "name2=value2")
|
|
|
|
handler_->got_create_cookie_.yes();
|
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
return new SchemeHandler(handler_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-04-04 00:14:28 +02:00
|
|
|
CookieTestSchemeHandler* handler_;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(SchemeHandlerFactory);
|
|
|
|
};
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
class RequestContextHandler : public CefRequestContextHandler {
|
|
|
|
public:
|
|
|
|
explicit RequestContextHandler(CookieTestSchemeHandler* handler)
|
|
|
|
: handler_(handler) {}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefCookieManager> GetCookieManager() override {
|
2013-09-03 18:43:31 +02:00
|
|
|
EXPECT_TRUE(handler_);
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_IO));
|
|
|
|
|
|
|
|
if (url_ == handler_->url1_) {
|
|
|
|
// Return the first cookie manager.
|
|
|
|
handler_->got_cookie_manager1_.yes();
|
|
|
|
return handler_->manager1_;
|
|
|
|
} else {
|
|
|
|
// Return the second cookie manager.
|
|
|
|
handler_->got_cookie_manager2_.yes();
|
|
|
|
return handler_->manager2_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void SetURL(const std::string& url) { url_ = url; }
|
2013-09-03 18:43:31 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void Detach() { handler_ = NULL; }
|
2013-09-03 18:43:31 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string url_;
|
|
|
|
CookieTestSchemeHandler* handler_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(RequestContextHandler);
|
|
|
|
};
|
|
|
|
|
2012-04-04 00:14:28 +02:00
|
|
|
CookieTestSchemeHandler(const std::string& scheme) : scheme_(scheme) {
|
|
|
|
url1_ = scheme + "://cookie-tests/cookie1.html";
|
|
|
|
url2_ = scheme + "://cookie-tests/cookie2.html";
|
|
|
|
url3_ = scheme + "://cookie-tests/cookie3.html";
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void RunTest() override {
|
2012-04-03 03:34:16 +02:00
|
|
|
// Create new in-memory managers.
|
2015-03-02 21:25:14 +01:00
|
|
|
manager1_ = CefCookieManager::CreateManager(CefString(), false, NULL);
|
|
|
|
manager2_ = CefCookieManager::CreateManager(CefString(), false, NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-04-04 00:14:28 +02:00
|
|
|
if (scheme_ != "http") {
|
|
|
|
std::vector<CefString> schemes;
|
|
|
|
schemes.push_back(scheme_);
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
manager1_->SetSupportedSchemes(schemes, NULL);
|
|
|
|
manager2_->SetSupportedSchemes(schemes, NULL);
|
2012-04-04 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
context_handler_ = new RequestContextHandler(this);
|
|
|
|
context_handler_->SetURL(url1_);
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
// Create the request context that will use an in-memory cache.
|
|
|
|
CefRequestContextSettings settings;
|
|
|
|
CefRefPtr<CefRequestContext> request_context =
|
|
|
|
CefRequestContext::CreateContext(settings, context_handler_.get());
|
|
|
|
|
|
|
|
// Register the scheme handler.
|
2017-05-17 11:29:28 +02:00
|
|
|
request_context->RegisterSchemeHandlerFactory(
|
|
|
|
scheme_, "cookie-tests", new SchemeHandlerFactory(this));
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
// Create the browser.
|
|
|
|
CreateBrowser(url1_, request_context);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
// Time out the test after a reasonable period of time.
|
|
|
|
SetTestTimeout();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
// Go to the next URL.
|
|
|
|
void LoadNextURL(CefRefPtr<CefFrame> frame, const std::string& url) {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2017-05-17 11:29:28 +02:00
|
|
|
CefPostTask(TID_UI, base::Bind(&CookieTestSchemeHandler::LoadNextURL,
|
|
|
|
this, frame, url));
|
2016-11-15 22:18:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
context_handler_->SetURL(url);
|
|
|
|
frame->LoadURL(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompleteTest(CefRefPtr<CefBrowser> browser) {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2017-05-17 11:29:28 +02:00
|
|
|
CefPostTask(TID_UI, base::Bind(&CookieTestSchemeHandler::CompleteTest,
|
|
|
|
this, browser));
|
2016-11-15 22:18:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unregister the scheme handler.
|
|
|
|
browser->GetHost()->GetRequestContext()->RegisterSchemeHandlerFactory(
|
|
|
|
scheme_, "cookie-tests", NULL);
|
|
|
|
|
|
|
|
DestroyTest();
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnLoadEnd(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
int httpStatusCode) override {
|
2012-04-03 03:34:16 +02:00
|
|
|
std::string url = frame->GetURL();
|
2012-04-04 00:14:28 +02:00
|
|
|
if (url == url1_) {
|
2012-04-03 03:34:16 +02:00
|
|
|
got_load_end1_.yes();
|
2016-11-15 22:18:41 +01:00
|
|
|
VerifyCookie(manager1_, url, "name1", "value1", &got_cookie1_,
|
2017-05-17 11:29:28 +02:00
|
|
|
base::Bind(&CookieTestSchemeHandler::LoadNextURL, this,
|
|
|
|
frame, url2_));
|
2012-04-04 00:14:28 +02:00
|
|
|
} else if (url == url2_) {
|
2012-04-03 03:34:16 +02:00
|
|
|
got_load_end2_.yes();
|
2016-11-15 22:18:41 +01:00
|
|
|
VerifyCookie(manager2_, url, "name2", "value2", &got_cookie2_,
|
2017-05-17 11:29:28 +02:00
|
|
|
base::Bind(&CookieTestSchemeHandler::LoadNextURL, this,
|
|
|
|
frame, url3_));
|
2012-04-03 03:34:16 +02:00
|
|
|
} else {
|
|
|
|
got_load_end3_.yes();
|
2017-05-17 11:29:28 +02:00
|
|
|
VerifyCookie(
|
|
|
|
manager2_, url, "name2", "value2", &got_cookie3_,
|
2016-11-15 22:18:41 +01:00
|
|
|
base::Bind(&CookieTestSchemeHandler::CompleteTest, this, browser));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void DestroyTest() override {
|
|
|
|
context_handler_->Detach();
|
|
|
|
context_handler_ = NULL;
|
2013-09-03 18:43:31 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
TestHandler::DestroyTest();
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Verify that the cookie was set successfully.
|
|
|
|
void VerifyCookie(CefRefPtr<CefCookieManager> manager,
|
|
|
|
const std::string& url,
|
|
|
|
const std::string& name,
|
|
|
|
const std::string& value,
|
2016-11-15 22:18:41 +01:00
|
|
|
TrackCallback* callback,
|
|
|
|
const base::Closure& continue_callback) {
|
2012-04-03 03:34:16 +02:00
|
|
|
// Get the cookie.
|
2018-03-20 21:15:08 +01:00
|
|
|
EXPECT_TRUE(cookies_.empty());
|
|
|
|
VisitUrlCookies(manager, url, false, cookies_, false,
|
|
|
|
base::Bind(&CookieTestSchemeHandler::VerifyCookieComplete,
|
|
|
|
this, name, value, callback, continue_callback));
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
void VerifyCookieComplete(const std::string& name,
|
|
|
|
const std::string& value,
|
|
|
|
TrackCallback* callback,
|
|
|
|
const base::Closure& continue_callback) {
|
|
|
|
if (cookies_.size() == 1U && CefString(&cookies_[0].name) == name &&
|
|
|
|
CefString(&cookies_[0].value) == value) {
|
2016-11-15 22:18:41 +01:00
|
|
|
callback->yes();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2016-11-15 22:18:41 +01:00
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
cookies_.clear();
|
2016-11-15 22:18:41 +01:00
|
|
|
continue_callback.Run();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2012-04-04 00:14:28 +02:00
|
|
|
std::string scheme_;
|
|
|
|
std::string url1_;
|
|
|
|
std::string url2_;
|
|
|
|
std::string url3_;
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
CefRefPtr<RequestContextHandler> context_handler_;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefCookieManager> manager1_;
|
|
|
|
CefRefPtr<CefCookieManager> manager2_;
|
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
CookieVector cookies_;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
TrackCallback got_process_request1_;
|
|
|
|
TrackCallback got_process_request2_;
|
|
|
|
TrackCallback got_process_request3_;
|
|
|
|
TrackCallback got_create_cookie_;
|
|
|
|
TrackCallback got_process_request_cookie_;
|
|
|
|
TrackCallback got_cookie_manager1_;
|
|
|
|
TrackCallback got_cookie_manager2_;
|
|
|
|
TrackCallback got_load_end1_;
|
|
|
|
TrackCallback got_load_end2_;
|
|
|
|
TrackCallback got_load_end3_;
|
|
|
|
TrackCallback got_cookie1_;
|
|
|
|
TrackCallback got_cookie2_;
|
|
|
|
TrackCallback got_cookie3_;
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CookieTestSchemeHandler);
|
2012-04-03 03:34:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2012-04-04 00:14:28 +02:00
|
|
|
// Verify use of multiple cookie managers via HTTP.
|
2012-04-03 03:34:16 +02:00
|
|
|
TEST(CookieTest, GetCookieManagerHttp) {
|
2012-04-04 00:14:28 +02:00
|
|
|
CefRefPtr<CookieTestSchemeHandler> handler =
|
|
|
|
new CookieTestSchemeHandler("http");
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_process_request1_);
|
|
|
|
EXPECT_TRUE(handler->got_process_request2_);
|
|
|
|
EXPECT_TRUE(handler->got_process_request3_);
|
|
|
|
EXPECT_FALSE(handler->got_create_cookie_);
|
|
|
|
EXPECT_TRUE(handler->got_process_request_cookie_);
|
|
|
|
EXPECT_TRUE(handler->got_cookie_manager1_);
|
|
|
|
EXPECT_TRUE(handler->got_cookie_manager2_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end1_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end2_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end3_);
|
|
|
|
EXPECT_TRUE(handler->got_cookie1_);
|
|
|
|
EXPECT_TRUE(handler->got_cookie2_);
|
|
|
|
EXPECT_TRUE(handler->got_cookie3_);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-04-04 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify use of multiple cookie managers via a custom scheme.
|
|
|
|
TEST(CookieTest, GetCookieManagerCustom) {
|
|
|
|
CefRefPtr<CookieTestSchemeHandler> handler =
|
|
|
|
new CookieTestSchemeHandler("ccustom");
|
2012-04-03 03:34:16 +02:00
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_process_request1_);
|
|
|
|
EXPECT_TRUE(handler->got_process_request2_);
|
|
|
|
EXPECT_TRUE(handler->got_process_request3_);
|
|
|
|
EXPECT_FALSE(handler->got_create_cookie_);
|
|
|
|
EXPECT_TRUE(handler->got_process_request_cookie_);
|
|
|
|
EXPECT_TRUE(handler->got_cookie_manager1_);
|
|
|
|
EXPECT_TRUE(handler->got_cookie_manager2_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end1_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end2_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end3_);
|
|
|
|
EXPECT_TRUE(handler->got_cookie1_);
|
|
|
|
EXPECT_TRUE(handler->got_cookie2_);
|
|
|
|
EXPECT_TRUE(handler->got_cookie3_);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2012-04-24 20:01:48 +02:00
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Entry point for registering custom schemes.
|
|
|
|
// Called from client_app_delegates.cc.
|
2017-05-17 11:29:28 +02:00
|
|
|
void RegisterCookieCustomSchemes(CefRawPtr<CefSchemeRegistrar> registrar,
|
|
|
|
std::vector<CefString>& cookiable_schemes) {
|
2012-04-24 20:01:48 +02:00
|
|
|
// Used by GetCookieManagerCustom test.
|
2019-02-04 23:31:31 +01:00
|
|
|
registrar->AddCustomScheme("ccustom", true, false, false, false, true, false,
|
|
|
|
false);
|
2012-04-24 20:01:48 +02:00
|
|
|
}
|