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  scoped_ptr<TestingProfile> profile = profile_builder.Build();
44  // Trigger early creation of off-the-record profile.
45  EXPECT_TRUE(profile->GetOffTheRecordProfile());
46
47  EXPECT_EQ(std::string("0"),
48            cookies_helpers::GetStoreIdFromProfile(profile.get()));
49  EXPECT_EQ(profile.get(),
50            cookies_helpers::ChooseProfileFromStoreId(
51                "0", profile.get(), true));
52  EXPECT_EQ(profile.get(),
53            cookies_helpers::ChooseProfileFromStoreId(
54                "0", profile.get(), false));
55  EXPECT_EQ(profile->GetOffTheRecordProfile(),
56            cookies_helpers::ChooseProfileFromStoreId(
57                "1", profile.get(), true));
58  EXPECT_EQ(NULL,
59            cookies_helpers::ChooseProfileFromStoreId(
60                "1", profile.get(), false));
61
62  EXPECT_EQ(std::string("1"),
63            cookies_helpers::GetStoreIdFromProfile(
64                profile->GetOffTheRecordProfile()));
65  EXPECT_EQ(NULL,
66            cookies_helpers::ChooseProfileFromStoreId(
67                "0", profile->GetOffTheRecordProfile(), true));
68  EXPECT_EQ(NULL,
69            cookies_helpers::ChooseProfileFromStoreId(
70                "0", profile->GetOffTheRecordProfile(), false));
71  EXPECT_EQ(profile->GetOffTheRecordProfile(),
72            cookies_helpers::ChooseProfileFromStoreId(
73                "1", profile->GetOffTheRecordProfile(), true));
74  EXPECT_EQ(profile->GetOffTheRecordProfile(),
75            cookies_helpers::ChooseProfileFromStoreId(
76                "1", profile->GetOffTheRecordProfile(), false));
77}
78
79TEST_F(ExtensionCookiesTest, ExtensionTypeCreation) {
80  net::CanonicalCookie canonical_cookie1(
81      GURL(), "ABC", "DEF", "www.foobar.com", "/",
82      base::Time(), base::Time(), base::Time(),
83      false, false, net::COOKIE_PRIORITY_DEFAULT);
84  scoped_ptr<Cookie> cookie1(
85      cookies_helpers::CreateCookie(
86          canonical_cookie1, "some cookie store"));
87  EXPECT_EQ("ABC", cookie1->name);
88  EXPECT_EQ("DEF", cookie1->value);
89  EXPECT_EQ("www.foobar.com", cookie1->domain);
90  EXPECT_TRUE(cookie1->host_only);
91  EXPECT_EQ("/", cookie1->path);
92  EXPECT_FALSE(cookie1->secure);
93  EXPECT_FALSE(cookie1->http_only);
94  EXPECT_TRUE(cookie1->session);
95  EXPECT_FALSE(cookie1->expiration_date.get());
96  EXPECT_EQ("some cookie store", cookie1->store_id);
97
98  net::CanonicalCookie canonical_cookie2(
99      GURL(), "ABC", "DEF", ".foobar.com", "/",
100      base::Time(), base::Time::FromDoubleT(10000), base::Time(),
101      false, false, net::COOKIE_PRIORITY_DEFAULT);
102  scoped_ptr<Cookie> cookie2(
103      cookies_helpers::CreateCookie(
104          canonical_cookie2, "some cookie store"));
105  EXPECT_FALSE(cookie2->host_only);
106  EXPECT_FALSE(cookie2->session);
107  ASSERT_TRUE(cookie2->expiration_date.get());
108  EXPECT_EQ(10000, *cookie2->expiration_date);
109
110  TestingProfile profile;
111  base::ListValue* tab_ids_list = new base::ListValue();
112  std::vector<int> tab_ids;
113  scoped_ptr<CookieStore> cookie_store(
114      cookies_helpers::CreateCookieStore(&profile, tab_ids_list));
115  EXPECT_EQ("0", cookie_store->id);
116  EXPECT_EQ(tab_ids, cookie_store->tab_ids);
117}
118
119TEST_F(ExtensionCookiesTest, GetURLFromCanonicalCookie) {
120  net::CanonicalCookie cookie1(
121      GURL(), "ABC", "DEF", "www.foobar.com", "/", base::Time(), base::Time(),
122      base::Time(), false, false, net::COOKIE_PRIORITY_DEFAULT);
123  EXPECT_EQ("http://www.foobar.com/",
124            cookies_helpers::GetURLFromCanonicalCookie(
125                cookie1).spec());
126
127  net::CanonicalCookie cookie2(
128      GURL(), "ABC", "DEF", ".helloworld.com", "/", base::Time(), base::Time(),
129      base::Time(), true, false, net::COOKIE_PRIORITY_DEFAULT);
130  EXPECT_EQ("https://helloworld.com/",
131            cookies_helpers::GetURLFromCanonicalCookie(
132                cookie2).spec());
133}
134
135TEST_F(ExtensionCookiesTest, EmptyDictionary) {
136  base::DictionaryValue dict;
137  GetAll::Params::Details details;
138  bool rv = GetAll::Params::Details::Populate(dict, &details);
139  ASSERT_TRUE(rv);
140  cookies_helpers::MatchFilter filter(&details);
141  net::CanonicalCookie cookie;
142  EXPECT_TRUE(filter.MatchesCookie(cookie));
143}
144
145TEST_F(ExtensionCookiesTest, DomainMatching) {
146  const DomainMatchCase tests[] = {
147    { "bar.com", "bar.com", true },
148    { ".bar.com", "bar.com", true },
149    { "bar.com", "foo.bar.com", true },
150    { "bar.com", "bar.foo.com", false },
151    { ".bar.com", ".foo.bar.com", true },
152    { ".bar.com", "baz.foo.bar.com", true },
153    { "foo.bar.com", ".bar.com", false }
154  };
155
156  for (size_t i = 0; i < arraysize(tests); ++i) {
157    // Build up the Params struct.
158    base::ListValue args;
159    base::DictionaryValue* dict = new base::DictionaryValue();
160    dict->SetString(keys::kDomainKey, std::string(tests[i].filter));
161    args.Set(0, dict);
162    scoped_ptr<GetAll::Params> params(GetAll::Params::Create(args));
163
164    cookies_helpers::MatchFilter filter(&params->details);
165    net::CanonicalCookie cookie(GURL(),
166                                std::string(),
167                                std::string(),
168                                tests[i].domain,
169                                std::string(),
170                                base::Time(),
171                                base::Time(),
172                                base::Time(),
173                                false,
174                                false,
175                                net::COOKIE_PRIORITY_DEFAULT);
176    EXPECT_EQ(tests[i].matches, filter.MatchesCookie(cookie));
177  }
178}
179
180TEST_F(ExtensionCookiesTest, DecodeUTF8WithErrorHandling) {
181  net::CanonicalCookie canonical_cookie(GURL(),
182                                        std::string(),
183                                        "011Q255bNX_1!yd\203e+",
184                                        "test.com",
185                                        "/path\203",
186                                        base::Time(),
187                                        base::Time(),
188                                        base::Time(),
189                                        false,
190                                        false,
191                                        net::COOKIE_PRIORITY_DEFAULT);
192  scoped_ptr<Cookie> cookie(
193      cookies_helpers::CreateCookie(
194          canonical_cookie, "some cookie store"));
195  EXPECT_EQ(std::string("011Q255bNX_1!yd\xEF\xBF\xBD" "e+"), cookie->value);
196  EXPECT_EQ(std::string(), cookie->path);
197}
198
199}  // namespace extensions
200