Home | History | Annotate | Download | only in cookies
      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 
     19 using extensions::api::cookies::Cookie;
     20 using extensions::api::cookies::CookieStore;
     21 
     22 namespace GetAll = extensions::api::cookies::GetAll;
     23 
     24 namespace extensions {
     25 
     26 namespace keys = cookies_api_constants;
     27 
     28 namespace {
     29 
     30 struct DomainMatchCase {
     31   const char* filter;
     32   const char* domain;
     33   const bool matches;
     34 };
     35 
     36 // A test profile that supports linking with another profile for incognito
     37 // support.
     38 class OtrTestingProfile : public TestingProfile {
     39  public:
     40   OtrTestingProfile() : linked_profile_(NULL) {}
     41   virtual Profile* GetOriginalProfile() OVERRIDE {
     42     if (IsOffTheRecord())
     43       return linked_profile_;
     44     else
     45       return this;
     46   }
     47 
     48   virtual Profile* GetOffTheRecordProfile() OVERRIDE {
     49     if (IsOffTheRecord())
     50       return this;
     51     else
     52       return linked_profile_;
     53   }
     54 
     55   virtual bool HasOffTheRecordProfile() OVERRIDE {
     56     return (!IsOffTheRecord() && linked_profile_);
     57   }
     58 
     59   static void LinkProfiles(OtrTestingProfile* profile1,
     60                            OtrTestingProfile* profile2) {
     61     profile1->set_linked_profile(profile2);
     62     profile2->set_linked_profile(profile1);
     63   }
     64 
     65   void set_linked_profile(OtrTestingProfile* profile) {
     66     linked_profile_ = profile;
     67   }
     68 
     69  private:
     70   OtrTestingProfile* linked_profile_;
     71 };
     72 
     73 }  // namespace
     74 
     75 class ExtensionCookiesTest : public testing::Test {
     76 };
     77 
     78 TEST_F(ExtensionCookiesTest, StoreIdProfileConversion) {
     79   OtrTestingProfile profile, otrProfile;
     80   otrProfile.set_incognito(true);
     81   OtrTestingProfile::LinkProfiles(&profile, &otrProfile);
     82 
     83   EXPECT_EQ(std::string("0"),
     84             cookies_helpers::GetStoreIdFromProfile(&profile));
     85   EXPECT_EQ(&profile,
     86             cookies_helpers::ChooseProfileFromStoreId(
     87                 "0", &profile, true));
     88   EXPECT_EQ(&profile,
     89             cookies_helpers::ChooseProfileFromStoreId(
     90                 "0", &profile, false));
     91   EXPECT_EQ(&otrProfile,
     92             cookies_helpers::ChooseProfileFromStoreId(
     93                 "1", &profile, true));
     94   EXPECT_EQ(NULL,
     95             cookies_helpers::ChooseProfileFromStoreId(
     96                 "1", &profile, false));
     97 
     98   EXPECT_EQ(std::string("1"),
     99             cookies_helpers::GetStoreIdFromProfile(&otrProfile));
    100   EXPECT_EQ(NULL,
    101             cookies_helpers::ChooseProfileFromStoreId(
    102                 "0", &otrProfile, true));
    103   EXPECT_EQ(NULL,
    104             cookies_helpers::ChooseProfileFromStoreId(
    105                 "0", &otrProfile, false));
    106   EXPECT_EQ(&otrProfile,
    107             cookies_helpers::ChooseProfileFromStoreId(
    108                 "1", &otrProfile, true));
    109   EXPECT_EQ(&otrProfile,
    110             cookies_helpers::ChooseProfileFromStoreId(
    111                 "1", &otrProfile, false));
    112 }
    113 
    114 TEST_F(ExtensionCookiesTest, ExtensionTypeCreation) {
    115   net::CanonicalCookie canonical_cookie1(
    116       GURL(), "ABC", "DEF", "www.foobar.com", "/",
    117       base::Time(), base::Time(), base::Time(),
    118       false, false, net::COOKIE_PRIORITY_DEFAULT);
    119   scoped_ptr<Cookie> cookie1(
    120       cookies_helpers::CreateCookie(
    121           canonical_cookie1, "some cookie store"));
    122   EXPECT_EQ("ABC", cookie1->name);
    123   EXPECT_EQ("DEF", cookie1->value);
    124   EXPECT_EQ("www.foobar.com", cookie1->domain);
    125   EXPECT_TRUE(cookie1->host_only);
    126   EXPECT_EQ("/", cookie1->path);
    127   EXPECT_FALSE(cookie1->secure);
    128   EXPECT_FALSE(cookie1->http_only);
    129   EXPECT_TRUE(cookie1->session);
    130   EXPECT_FALSE(cookie1->expiration_date.get());
    131   EXPECT_EQ("some cookie store", cookie1->store_id);
    132 
    133   net::CanonicalCookie canonical_cookie2(
    134       GURL(), "ABC", "DEF", ".foobar.com", "/",
    135       base::Time(), base::Time::FromDoubleT(10000), base::Time(),
    136       false, false, net::COOKIE_PRIORITY_DEFAULT);
    137   scoped_ptr<Cookie> cookie2(
    138       cookies_helpers::CreateCookie(
    139           canonical_cookie2, "some cookie store"));
    140   EXPECT_FALSE(cookie2->host_only);
    141   EXPECT_FALSE(cookie2->session);
    142   ASSERT_TRUE(cookie2->expiration_date.get());
    143   EXPECT_EQ(10000, *cookie2->expiration_date);
    144 
    145   TestingProfile profile;
    146   base::ListValue* tab_ids_list = new base::ListValue();
    147   std::vector<int> tab_ids;
    148   scoped_ptr<CookieStore> cookie_store(
    149       cookies_helpers::CreateCookieStore(&profile, tab_ids_list));
    150   EXPECT_EQ("0", cookie_store->id);
    151   EXPECT_EQ(tab_ids, cookie_store->tab_ids);
    152 }
    153 
    154 TEST_F(ExtensionCookiesTest, GetURLFromCanonicalCookie) {
    155   net::CanonicalCookie cookie1(
    156       GURL(), "ABC", "DEF", "www.foobar.com", "/", base::Time(), base::Time(),
    157       base::Time(), false, false, net::COOKIE_PRIORITY_DEFAULT);
    158   EXPECT_EQ("http://www.foobar.com/",
    159             cookies_helpers::GetURLFromCanonicalCookie(
    160                 cookie1).spec());
    161 
    162   net::CanonicalCookie cookie2(
    163       GURL(), "ABC", "DEF", ".helloworld.com", "/", base::Time(), base::Time(),
    164       base::Time(), true, false, net::COOKIE_PRIORITY_DEFAULT);
    165   EXPECT_EQ("https://helloworld.com/",
    166             cookies_helpers::GetURLFromCanonicalCookie(
    167                 cookie2).spec());
    168 }
    169 
    170 TEST_F(ExtensionCookiesTest, EmptyDictionary) {
    171   base::DictionaryValue dict;
    172   GetAll::Params::Details details;
    173   bool rv = GetAll::Params::Details::Populate(dict, &details);
    174   ASSERT_TRUE(rv);
    175   cookies_helpers::MatchFilter filter(&details);
    176   net::CanonicalCookie cookie;
    177   EXPECT_TRUE(filter.MatchesCookie(cookie));
    178 }
    179 
    180 TEST_F(ExtensionCookiesTest, DomainMatching) {
    181   const DomainMatchCase tests[] = {
    182     { "bar.com", "bar.com", true },
    183     { ".bar.com", "bar.com", true },
    184     { "bar.com", "foo.bar.com", true },
    185     { "bar.com", "bar.foo.com", false },
    186     { ".bar.com", ".foo.bar.com", true },
    187     { ".bar.com", "baz.foo.bar.com", true },
    188     { "foo.bar.com", ".bar.com", false }
    189   };
    190 
    191   for (size_t i = 0; i < arraysize(tests); ++i) {
    192     // Build up the Params struct.
    193     base::ListValue args;
    194     base::DictionaryValue* dict = new base::DictionaryValue();
    195     dict->SetString(keys::kDomainKey, std::string(tests[i].filter));
    196     args.Set(0, dict);
    197     scoped_ptr<GetAll::Params> params(GetAll::Params::Create(args));
    198 
    199     cookies_helpers::MatchFilter filter(&params->details);
    200     net::CanonicalCookie cookie(GURL(),
    201                                 std::string(),
    202                                 std::string(),
    203                                 tests[i].domain,
    204                                 std::string(),
    205                                 base::Time(),
    206                                 base::Time(),
    207                                 base::Time(),
    208                                 false,
    209                                 false,
    210                                 net::COOKIE_PRIORITY_DEFAULT);
    211     EXPECT_EQ(tests[i].matches, filter.MatchesCookie(cookie));
    212   }
    213 }
    214 
    215 TEST_F(ExtensionCookiesTest, DecodeUTF8WithErrorHandling) {
    216   net::CanonicalCookie canonical_cookie(GURL(),
    217                                         std::string(),
    218                                         "011Q255bNX_1!yd\203e+",
    219                                         "test.com",
    220                                         "/path\203",
    221                                         base::Time(),
    222                                         base::Time(),
    223                                         base::Time(),
    224                                         false,
    225                                         false,
    226                                         net::COOKIE_PRIORITY_DEFAULT);
    227   scoped_ptr<Cookie> cookie(
    228       cookies_helpers::CreateCookie(
    229           canonical_cookie, "some cookie store"));
    230   EXPECT_EQ(std::string("011Q255bNX_1!yd\xEF\xBF\xBD" "e+"), cookie->value);
    231   EXPECT_EQ(std::string(), cookie->path);
    232 }
    233 
    234 }  // namespace extensions
    235