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