Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2011 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/extension_cookies_api_constants.h"
     12 #include "chrome/browser/extensions/extension_cookies_helpers.h"
     13 #include "chrome/test/testing_profile.h"
     14 #include "googleurl/src/gurl.h"
     15 
     16 namespace keys = extension_cookies_api_constants;
     17 
     18 namespace {
     19 
     20 struct DomainMatchCase {
     21   const char* filter;
     22   const char* domain;
     23   const bool matches;
     24 };
     25 
     26 // A test profile that supports linking with another profile for incognito support.
     27 class OtrTestingProfile : public TestingProfile {
     28  public:
     29   OtrTestingProfile() : linked_profile_(NULL) {}
     30   virtual Profile* GetOriginalProfile() {
     31     if (IsOffTheRecord())
     32       return linked_profile_;
     33     else
     34       return this;
     35   }
     36 
     37   virtual Profile* GetOffTheRecordProfile() {
     38     if (IsOffTheRecord())
     39       return this;
     40     else
     41       return linked_profile_;
     42   }
     43 
     44   virtual bool HasOffTheRecordProfile() {
     45     return (!IsOffTheRecord() && linked_profile_);
     46   }
     47 
     48   static void LinkProfiles(OtrTestingProfile* profile1,
     49                            OtrTestingProfile* profile2) {
     50     profile1->set_linked_profile(profile2);
     51     profile2->set_linked_profile(profile1);
     52   }
     53 
     54   void set_linked_profile(OtrTestingProfile* profile) {
     55     linked_profile_ = profile;
     56   }
     57 
     58  private:
     59   OtrTestingProfile* linked_profile_;
     60 };
     61 
     62 }  // namespace
     63 
     64 class ExtensionCookiesTest : public testing::Test {
     65 };
     66 
     67 TEST_F(ExtensionCookiesTest, StoreIdProfileConversion) {
     68   OtrTestingProfile profile, otrProfile;
     69   otrProfile.set_incognito(true);
     70   OtrTestingProfile::LinkProfiles(&profile, &otrProfile);
     71 
     72   EXPECT_EQ(std::string("0"),
     73             extension_cookies_helpers::GetStoreIdFromProfile(&profile));
     74   EXPECT_EQ(&profile,
     75             extension_cookies_helpers::ChooseProfileFromStoreId(
     76                 "0", &profile, true));
     77   EXPECT_EQ(&profile,
     78             extension_cookies_helpers::ChooseProfileFromStoreId(
     79                 "0", &profile, false));
     80   EXPECT_EQ(&otrProfile,
     81             extension_cookies_helpers::ChooseProfileFromStoreId(
     82                 "1", &profile, true));
     83   EXPECT_EQ(NULL,
     84             extension_cookies_helpers::ChooseProfileFromStoreId(
     85                 "1", &profile, false));
     86 
     87   EXPECT_EQ(std::string("1"),
     88             extension_cookies_helpers::GetStoreIdFromProfile(&otrProfile));
     89   EXPECT_EQ(NULL,
     90             extension_cookies_helpers::ChooseProfileFromStoreId(
     91                 "0", &otrProfile, true));
     92   EXPECT_EQ(NULL,
     93             extension_cookies_helpers::ChooseProfileFromStoreId(
     94                 "0", &otrProfile, false));
     95   EXPECT_EQ(&otrProfile,
     96             extension_cookies_helpers::ChooseProfileFromStoreId(
     97                 "1", &otrProfile, true));
     98   EXPECT_EQ(&otrProfile,
     99             extension_cookies_helpers::ChooseProfileFromStoreId(
    100                 "1", &otrProfile, false));
    101 }
    102 
    103 TEST_F(ExtensionCookiesTest, ExtensionTypeCreation) {
    104   std::string string_value;
    105   bool boolean_value;
    106   double double_value;
    107   Value* value;
    108 
    109   net::CookieMonster::CanonicalCookie cookie1(
    110       GURL(), "ABC", "DEF", "www.foobar.com", "/", base::Time(), base::Time(),
    111       base::Time(), false, false, false);
    112   scoped_ptr<DictionaryValue> cookie_value1(
    113       extension_cookies_helpers::CreateCookieValue(
    114           cookie1, "some cookie store"));
    115   EXPECT_TRUE(cookie_value1->GetString(keys::kNameKey, &string_value));
    116   EXPECT_EQ("ABC", string_value);
    117   EXPECT_TRUE(cookie_value1->GetString(keys::kValueKey, &string_value));
    118   EXPECT_EQ("DEF", string_value);
    119   EXPECT_TRUE(cookie_value1->GetString(keys::kDomainKey, &string_value));
    120   EXPECT_EQ("www.foobar.com", string_value);
    121   EXPECT_TRUE(cookie_value1->GetBoolean(keys::kHostOnlyKey, &boolean_value));
    122   EXPECT_TRUE(boolean_value);
    123   EXPECT_TRUE(cookie_value1->GetString(keys::kPathKey, &string_value));
    124   EXPECT_EQ("/", string_value);
    125   EXPECT_TRUE(cookie_value1->GetBoolean(keys::kSecureKey, &boolean_value));
    126   EXPECT_FALSE(boolean_value);
    127   EXPECT_TRUE(cookie_value1->GetBoolean(keys::kHttpOnlyKey, &boolean_value));
    128   EXPECT_FALSE(boolean_value);
    129   EXPECT_TRUE(cookie_value1->GetBoolean(keys::kSessionKey, &boolean_value));
    130   EXPECT_TRUE(boolean_value);
    131   EXPECT_FALSE(
    132       cookie_value1->GetDouble(keys::kExpirationDateKey, &double_value));
    133   EXPECT_TRUE(cookie_value1->GetString(keys::kStoreIdKey, &string_value));
    134   EXPECT_EQ("some cookie store", string_value);
    135 
    136   net::CookieMonster::CanonicalCookie cookie2(
    137       GURL(), "ABC", "DEF", ".foobar.com", "/",
    138       base::Time(), base::Time::FromDoubleT(10000), base::Time(),
    139       false, false, true);
    140   scoped_ptr<DictionaryValue> cookie_value2(
    141       extension_cookies_helpers::CreateCookieValue(
    142           cookie2, "some cookie store"));
    143   EXPECT_TRUE(cookie_value2->GetBoolean(keys::kHostOnlyKey, &boolean_value));
    144   EXPECT_FALSE(boolean_value);
    145   EXPECT_TRUE(cookie_value2->GetBoolean(keys::kSessionKey, &boolean_value));
    146   EXPECT_FALSE(boolean_value);
    147   EXPECT_TRUE(
    148       cookie_value2->GetDouble(keys::kExpirationDateKey, &double_value));
    149   EXPECT_EQ(10000, double_value);
    150 
    151   TestingProfile profile;
    152   ListValue* tab_ids = new ListValue();
    153   scoped_ptr<DictionaryValue> cookie_store_value(
    154       extension_cookies_helpers::CreateCookieStoreValue(&profile, tab_ids));
    155   EXPECT_TRUE(cookie_store_value->GetString(keys::kIdKey, &string_value));
    156   EXPECT_EQ("0", string_value);
    157   EXPECT_TRUE(cookie_store_value->Get(keys::kTabIdsKey, &value));
    158   EXPECT_EQ(tab_ids, value);
    159 }
    160 
    161 TEST_F(ExtensionCookiesTest, GetURLFromCanonicalCookie) {
    162   net::CookieMonster::CanonicalCookie cookie1(
    163       GURL(), "ABC", "DEF", "www.foobar.com", "/", base::Time(), base::Time(),
    164       base::Time(), false, false, false);
    165   EXPECT_EQ("http://www.foobar.com/",
    166             extension_cookies_helpers::GetURLFromCanonicalCookie(
    167                 cookie1).spec());
    168 
    169   net::CookieMonster::CanonicalCookie cookie2(
    170       GURL(), "ABC", "DEF", ".helloworld.com", "/", base::Time(), base::Time(),
    171       base::Time(), true, false, false);
    172   EXPECT_EQ("https://helloworld.com/",
    173             extension_cookies_helpers::GetURLFromCanonicalCookie(
    174                 cookie2).spec());
    175 }
    176 
    177 TEST_F(ExtensionCookiesTest, EmptyDictionary) {
    178   scoped_ptr<DictionaryValue> details(new DictionaryValue());
    179   extension_cookies_helpers::MatchFilter filter(details.get());
    180   std::string domain;
    181   net::CookieMonster::CanonicalCookie cookie;
    182 
    183   EXPECT_TRUE(filter.MatchesCookie(cookie));
    184 }
    185 
    186 TEST_F(ExtensionCookiesTest, DomainMatching) {
    187   const DomainMatchCase tests[] = {
    188     { "bar.com", "bar.com", true },
    189     { ".bar.com", "bar.com", true },
    190     { "bar.com", "foo.bar.com", true },
    191     { "bar.com", "bar.foo.com", false },
    192     { ".bar.com", ".foo.bar.com", true },
    193     { ".bar.com", "baz.foo.bar.com", true },
    194     { "foo.bar.com", ".bar.com", false }
    195   };
    196 
    197   scoped_ptr<DictionaryValue> details(new DictionaryValue());
    198   for (size_t i = 0; i < arraysize(tests); ++i) {
    199     details->SetString(keys::kDomainKey, std::string(tests[i].filter));
    200     extension_cookies_helpers::MatchFilter filter(details.get());
    201     net::CookieMonster::CanonicalCookie cookie(GURL(), "", "", tests[i].domain,
    202                                                "", base::Time(), base::Time(),
    203                                                base::Time(), false, false,
    204                                                false);
    205     EXPECT_EQ(tests[i].matches, filter.MatchesCookie(cookie));
    206   }
    207 }
    208