cookies_unittest.cc revision 3551c9c881056c480085172ff9840cab31610854
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.
4
5// Tests common functionality used by the Chrome Extensions Cookies API
6// implementation.
7
8#include "testing/gtest/include/gtest/gtest.h"
9
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 "net/cookies/canonical_cookie.h"
16#include "net/cookies/cookie_constants.h"
17#include "url/gurl.h"
18
19using extensions::api::cookies::Cookie;
20using extensions::api::cookies::CookieStore;
21
22namespace GetAll = extensions::api::cookies::GetAll;
23
24namespace extensions {
25
26namespace keys = cookies_api_constants;
27
28namespace {
29
30struct DomainMatchCase {
31  const char* filter;
32  const char* domain;
33  const bool matches;
34};
35
36}  // namespace
37
38class ExtensionCookiesTest : public testing::Test {
39};
40
41TEST_F(ExtensionCookiesTest, StoreIdProfileConversion) {
42  TestingProfile::Builder profile_builder;
43  TestingProfile::Builder otr_profile_builder;
44  otr_profile_builder.SetIncognito();
45  scoped_ptr<TestingProfile> profile = profile_builder.Build();
46  scoped_ptr<TestingProfile> otr_profile = otr_profile_builder.Build();
47  otr_profile->SetOriginalProfile(profile.get());
48  profile->SetOffTheRecordProfile(otr_profile.PassAs<Profile>());
49
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));
61  EXPECT_EQ(NULL,
62            cookies_helpers::ChooseProfileFromStoreId(
63                "1", profile.get(), false));
64
65  EXPECT_EQ(std::string("1"),
66            cookies_helpers::GetStoreIdFromProfile(
67                profile->GetOffTheRecordProfile()));
68  EXPECT_EQ(NULL,
69            cookies_helpers::ChooseProfileFromStoreId(
70                "0", profile->GetOffTheRecordProfile(), true));
71  EXPECT_EQ(NULL,
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));
80}
81
82TEST_F(ExtensionCookiesTest, ExtensionTypeCreation) {
83  net::CanonicalCookie canonical_cookie1(
84      GURL(), "ABC", "DEF", "www.foobar.com", "/",
85      base::Time(), base::Time(), base::Time(),
86      false, false, net::COOKIE_PRIORITY_DEFAULT);
87  scoped_ptr<Cookie> cookie1(
88      cookies_helpers::CreateCookie(
89          canonical_cookie1, "some cookie store"));
90  EXPECT_EQ("ABC", cookie1->name);
91  EXPECT_EQ("DEF", cookie1->value);
92  EXPECT_EQ("www.foobar.com", cookie1->domain);
93  EXPECT_TRUE(cookie1->host_only);
94  EXPECT_EQ("/", cookie1->path);
95  EXPECT_FALSE(cookie1->secure);
96  EXPECT_FALSE(cookie1->http_only);
97  EXPECT_TRUE(cookie1->session);
98  EXPECT_FALSE(cookie1->expiration_date.get());
99  EXPECT_EQ("some cookie store", cookie1->store_id);
100
101  net::CanonicalCookie canonical_cookie2(
102      GURL(), "ABC", "DEF", ".foobar.com", "/",
103      base::Time(), base::Time::FromDoubleT(10000), base::Time(),
104      false, false, net::COOKIE_PRIORITY_DEFAULT);
105  scoped_ptr<Cookie> cookie2(
106      cookies_helpers::CreateCookie(
107          canonical_cookie2, "some cookie store"));
108  EXPECT_FALSE(cookie2->host_only);
109  EXPECT_FALSE(cookie2->session);
110  ASSERT_TRUE(cookie2->expiration_date.get());
111  EXPECT_EQ(10000, *cookie2->expiration_date);
112
113  TestingProfile profile;
114  base::ListValue* tab_ids_list = new base::ListValue();
115  std::vector<int> tab_ids;
116  scoped_ptr<CookieStore> cookie_store(
117      cookies_helpers::CreateCookieStore(&profile, tab_ids_list));
118  EXPECT_EQ("0", cookie_store->id);
119  EXPECT_EQ(tab_ids, cookie_store->tab_ids);
120}
121
122TEST_F(ExtensionCookiesTest, GetURLFromCanonicalCookie) {
123  net::CanonicalCookie cookie1(
124      GURL(), "ABC", "DEF", "www.foobar.com", "/", base::Time(), base::Time(),
125      base::Time(), false, false, net::COOKIE_PRIORITY_DEFAULT);
126  EXPECT_EQ("http://www.foobar.com/",
127            cookies_helpers::GetURLFromCanonicalCookie(
128                cookie1).spec());
129
130  net::CanonicalCookie cookie2(
131      GURL(), "ABC", "DEF", ".helloworld.com", "/", base::Time(), base::Time(),
132      base::Time(), true, false, net::COOKIE_PRIORITY_DEFAULT);
133  EXPECT_EQ("https://helloworld.com/",
134            cookies_helpers::GetURLFromCanonicalCookie(
135                cookie2).spec());
136}
137
138TEST_F(ExtensionCookiesTest, EmptyDictionary) {
139  base::DictionaryValue dict;
140  GetAll::Params::Details details;
141  bool rv = GetAll::Params::Details::Populate(dict, &details);
142  ASSERT_TRUE(rv);
143  cookies_helpers::MatchFilter filter(&details);
144  net::CanonicalCookie cookie;
145  EXPECT_TRUE(filter.MatchesCookie(cookie));
146}
147
148TEST_F(ExtensionCookiesTest, DomainMatching) {
149  const DomainMatchCase tests[] = {
150    { "bar.com", "bar.com", true },
151    { ".bar.com", "bar.com", true },
152    { "bar.com", "foo.bar.com", true },
153    { "bar.com", "bar.foo.com", false },
154    { ".bar.com", ".foo.bar.com", true },
155    { ".bar.com", "baz.foo.bar.com", true },
156    { "foo.bar.com", ".bar.com", false }
157  };
158
159  for (size_t i = 0; i < arraysize(tests); ++i) {
160    // Build up the Params struct.
161    base::ListValue args;
162    base::DictionaryValue* dict = new base::DictionaryValue();
163    dict->SetString(keys::kDomainKey, std::string(tests[i].filter));
164    args.Set(0, dict);
165    scoped_ptr<GetAll::Params> params(GetAll::Params::Create(args));
166
167    cookies_helpers::MatchFilter filter(&params->details);
168    net::CanonicalCookie cookie(GURL(),
169                                std::string(),
170                                std::string(),
171                                tests[i].domain,
172                                std::string(),
173                                base::Time(),
174                                base::Time(),
175                                base::Time(),
176                                false,
177                                false,
178                                net::COOKIE_PRIORITY_DEFAULT);
179    EXPECT_EQ(tests[i].matches, filter.MatchesCookie(cookie));
180  }
181}
182
183TEST_F(ExtensionCookiesTest, DecodeUTF8WithErrorHandling) {
184  net::CanonicalCookie canonical_cookie(GURL(),
185                                        std::string(),
186                                        "011Q255bNX_1!yd\203e+",
187                                        "test.com",
188                                        "/path\203",
189                                        base::Time(),
190                                        base::Time(),
191                                        base::Time(),
192                                        false,
193                                        false,
194                                        net::COOKIE_PRIORITY_DEFAULT);
195  scoped_ptr<Cookie> cookie(
196      cookies_helpers::CreateCookie(
197          canonical_cookie, "some cookie store"));
198  EXPECT_EQ(std::string("011Q255bNX_1!yd\xEF\xBF\xBD" "e+"), cookie->value);
199  EXPECT_EQ(std::string(), cookie->path);
200}
201
202}  // namespace extensions
203