1 // Copyright (c) 2012 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.
5 // Tests common functionality used by the Chrome Extensions Cookies API
8 #include "testing/gtest/include/gtest/gtest.h"
10 #include "base/values.h"
11 #include "chrome/browser/extensions/api/cookies/cookies_api_constants.h"
12 #include "chrome/browser/extensions/api/cookies/cookies_helpers.h"
13 #include "chrome/common/extensions/api/cookies.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "net/cookies/canonical_cookie.h"
17 #include "net/cookies/cookie_constants.h"
20 using extensions::api::cookies::Cookie
;
21 using extensions::api::cookies::CookieStore
;
23 namespace GetAll
= extensions::api::cookies::GetAll
;
25 namespace extensions
{
27 namespace keys
= cookies_api_constants
;
31 struct DomainMatchCase
{
39 class ExtensionCookiesTest
: public testing::Test
{
41 content::TestBrowserThreadBundle thread_bundle_
;
44 TEST_F(ExtensionCookiesTest
, StoreIdProfileConversion
) {
45 TestingProfile::Builder profile_builder
;
46 scoped_ptr
<TestingProfile
> profile
= profile_builder
.Build();
47 // Trigger early creation of off-the-record profile.
48 EXPECT_TRUE(profile
->GetOffTheRecordProfile());
50 EXPECT_EQ(std::string("0"),
51 cookies_helpers::GetStoreIdFromProfile(profile
.get()));
52 EXPECT_EQ(profile
.get(),
53 cookies_helpers::ChooseProfileFromStoreId(
54 "0", profile
.get(), true));
55 EXPECT_EQ(profile
.get(),
56 cookies_helpers::ChooseProfileFromStoreId(
57 "0", profile
.get(), false));
58 EXPECT_EQ(profile
->GetOffTheRecordProfile(),
59 cookies_helpers::ChooseProfileFromStoreId(
60 "1", profile
.get(), true));
62 cookies_helpers::ChooseProfileFromStoreId(
63 "1", profile
.get(), false));
65 EXPECT_EQ(std::string("1"),
66 cookies_helpers::GetStoreIdFromProfile(
67 profile
->GetOffTheRecordProfile()));
69 cookies_helpers::ChooseProfileFromStoreId(
70 "0", profile
->GetOffTheRecordProfile(), true));
72 cookies_helpers::ChooseProfileFromStoreId(
73 "0", profile
->GetOffTheRecordProfile(), false));
74 EXPECT_EQ(profile
->GetOffTheRecordProfile(),
75 cookies_helpers::ChooseProfileFromStoreId(
76 "1", profile
->GetOffTheRecordProfile(), true));
77 EXPECT_EQ(profile
->GetOffTheRecordProfile(),
78 cookies_helpers::ChooseProfileFromStoreId(
79 "1", profile
->GetOffTheRecordProfile(), false));
82 TEST_F(ExtensionCookiesTest
, ExtensionTypeCreation
) {
83 net::CanonicalCookie
canonical_cookie1(
84 GURL(), "ABC", "DEF", "www.foobar.com", "/", base::Time(), base::Time(),
85 base::Time(), false, false, false, net::COOKIE_PRIORITY_DEFAULT
);
86 scoped_ptr
<Cookie
> cookie1(
87 cookies_helpers::CreateCookie(
88 canonical_cookie1
, "some cookie store"));
89 EXPECT_EQ("ABC", cookie1
->name
);
90 EXPECT_EQ("DEF", cookie1
->value
);
91 EXPECT_EQ("www.foobar.com", cookie1
->domain
);
92 EXPECT_TRUE(cookie1
->host_only
);
93 EXPECT_EQ("/", cookie1
->path
);
94 EXPECT_FALSE(cookie1
->secure
);
95 EXPECT_FALSE(cookie1
->http_only
);
96 EXPECT_TRUE(cookie1
->session
);
97 EXPECT_FALSE(cookie1
->expiration_date
.get());
98 EXPECT_EQ("some cookie store", cookie1
->store_id
);
100 net::CanonicalCookie
canonical_cookie2(
101 GURL(), "ABC", "DEF", ".foobar.com", "/", base::Time(),
102 base::Time::FromDoubleT(10000), base::Time(), false, false, false,
103 net::COOKIE_PRIORITY_DEFAULT
);
104 scoped_ptr
<Cookie
> cookie2(
105 cookies_helpers::CreateCookie(
106 canonical_cookie2
, "some cookie store"));
107 EXPECT_FALSE(cookie2
->host_only
);
108 EXPECT_FALSE(cookie2
->session
);
109 ASSERT_TRUE(cookie2
->expiration_date
.get());
110 EXPECT_EQ(10000, *cookie2
->expiration_date
);
112 TestingProfile profile
;
113 base::ListValue
* tab_ids_list
= new base::ListValue();
114 std::vector
<int> tab_ids
;
115 scoped_ptr
<CookieStore
> cookie_store(
116 cookies_helpers::CreateCookieStore(&profile
, tab_ids_list
));
117 EXPECT_EQ("0", cookie_store
->id
);
118 EXPECT_EQ(tab_ids
, cookie_store
->tab_ids
);
121 TEST_F(ExtensionCookiesTest
, GetURLFromCanonicalCookie
) {
122 net::CanonicalCookie
cookie1(GURL(), "ABC", "DEF", "www.foobar.com", "/",
123 base::Time(), base::Time(), base::Time(), false,
124 false, false, net::COOKIE_PRIORITY_DEFAULT
);
125 EXPECT_EQ("http://www.foobar.com/",
126 cookies_helpers::GetURLFromCanonicalCookie(
129 net::CanonicalCookie
cookie2(GURL(), "ABC", "DEF", ".helloworld.com", "/",
130 base::Time(), base::Time(), base::Time(), true,
131 false, false, net::COOKIE_PRIORITY_DEFAULT
);
132 EXPECT_EQ("https://helloworld.com/",
133 cookies_helpers::GetURLFromCanonicalCookie(
137 TEST_F(ExtensionCookiesTest
, EmptyDictionary
) {
138 base::DictionaryValue dict
;
139 GetAll::Params::Details details
;
140 bool rv
= GetAll::Params::Details::Populate(dict
, &details
);
142 cookies_helpers::MatchFilter
filter(&details
);
143 net::CanonicalCookie cookie
;
144 EXPECT_TRUE(filter
.MatchesCookie(cookie
));
147 TEST_F(ExtensionCookiesTest
, DomainMatching
) {
148 const DomainMatchCase tests
[] = {
149 { "bar.com", "bar.com", true },
150 { ".bar.com", "bar.com", true },
151 { "bar.com", "foo.bar.com", true },
152 { "bar.com", "bar.foo.com", false },
153 { ".bar.com", ".foo.bar.com", true },
154 { ".bar.com", "baz.foo.bar.com", true },
155 { "foo.bar.com", ".bar.com", false }
158 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
159 // Build up the Params struct.
160 base::ListValue args
;
161 base::DictionaryValue
* dict
= new base::DictionaryValue();
162 dict
->SetString(keys::kDomainKey
, std::string(tests
[i
].filter
));
164 scoped_ptr
<GetAll::Params
> params(GetAll::Params::Create(args
));
166 cookies_helpers::MatchFilter
filter(¶ms
->details
);
167 net::CanonicalCookie
cookie(GURL(), std::string(), std::string(),
168 tests
[i
].domain
, std::string(), base::Time(),
169 base::Time(), base::Time(), false, false, false,
170 net::COOKIE_PRIORITY_DEFAULT
);
171 EXPECT_EQ(tests
[i
].matches
, filter
.MatchesCookie(cookie
));
175 TEST_F(ExtensionCookiesTest
, DecodeUTF8WithErrorHandling
) {
176 net::CanonicalCookie
canonical_cookie(
177 GURL(), std::string(), "011Q255bNX_1!yd\203e+", "test.com", "/path\203",
178 base::Time(), base::Time(), base::Time(), false, false, false,
179 net::COOKIE_PRIORITY_DEFAULT
);
180 scoped_ptr
<Cookie
> cookie(
181 cookies_helpers::CreateCookie(
182 canonical_cookie
, "some cookie store"));
183 EXPECT_EQ(std::string("011Q255bNX_1!yd\xEF\xBF\xBD" "e+"), cookie
->value
);
184 EXPECT_EQ(std::string(), cookie
->path
);
187 } // namespace extensions