1 // Copyright 2013 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.
7 #include "chrome/common/extensions/features/feature_channel.h"
8 #include "chrome/common/extensions/manifest_handlers/externally_connectable.h"
9 #include "chrome/common/extensions/manifest_tests/extension_manifest_test.h"
10 #include "extensions/common/error_utils.h"
11 #include "extensions/common/manifest_constants.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using testing::ElementsAre
;
17 namespace extensions
{
19 namespace errors
= externally_connectable_errors
;
21 class ExternallyConnectableTest
: public ExtensionManifestTest
{
23 ExternallyConnectableTest() : channel_(chrome::VersionInfo::CHANNEL_DEV
) {}
26 ExternallyConnectableInfo
* GetExternallyConnectableInfo(
27 scoped_refptr
<Extension
> extension
) {
28 return static_cast<ExternallyConnectableInfo
*>(extension
->GetManifestData(
29 manifest_keys::kExternallyConnectable
));
33 ScopedCurrentChannel channel_
;
36 TEST_F(ExternallyConnectableTest
, IDsAndMatches
) {
37 scoped_refptr
<Extension
> extension
=
38 LoadAndExpectSuccess("externally_connectable_ids_and_matches.json");
39 ASSERT_TRUE(extension
.get());
41 EXPECT_TRUE(extension
->HasAPIPermission(APIPermission::kWebConnectable
));
43 ExternallyConnectableInfo
* info
=
44 ExternallyConnectableInfo::Get(extension
.get());
47 EXPECT_THAT(info
->ids
, ElementsAre("abcdefghijklmnopabcdefghijklmnop",
48 "ponmlkjihgfedcbaponmlkjihgfedcba"));
50 EXPECT_FALSE(info
->all_ids
);
52 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://example.com")));
53 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://example.com/")));
54 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://example.com/index.html")));
56 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://google.com")));
57 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://google.com/")));
58 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://google.com/index.html")));
59 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.google.com")));
60 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.google.com/")));
61 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://google.com")));
62 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://google.com/")));
64 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
65 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org/")));
67 info
->matches
.MatchesURL(GURL("http://build.chromium.org/index.html")));
68 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("https://build.chromium.org")));
69 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("https://build.chromium.org/")));
71 info
->matches
.MatchesURL(GURL("http://foo.chromium.org/index.html")));
73 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://yahoo.com")));
74 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://yahoo.com/")));
76 // TLD-style patterns should match just the TLD.
77 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://appspot.com/foo.html")));
78 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://com")));
79 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://go/here")));
81 // TLD-style patterns should *not* match any subdomains of the TLD.
83 info
->matches
.MatchesURL(GURL("http://codereview.appspot.com/foo.html")));
85 info
->matches
.MatchesURL(GURL("http://chromium.com/index.html")));
86 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://here.go/somewhere")));
88 // Paths that don't have any wildcards should match the exact domain, but
89 // ignore the trailing slash. This is kind of a corner case, so let's test it.
90 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://no.wildcard.path")));
91 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://no.wildcard.path/")));
92 EXPECT_FALSE(info
->matches
.MatchesURL(
93 GURL("http://no.wildcard.path/index.html")));
96 TEST_F(ExternallyConnectableTest
, IDs
) {
97 scoped_refptr
<Extension
> extension
=
98 LoadAndExpectSuccess("externally_connectable_ids.json");
99 ASSERT_TRUE(extension
.get());
101 EXPECT_FALSE(extension
->HasAPIPermission(APIPermission::kWebConnectable
));
103 ExternallyConnectableInfo
* info
=
104 ExternallyConnectableInfo::Get(extension
.get());
107 EXPECT_THAT(info
->ids
, ElementsAre("abcdefghijklmnopabcdefghijklmnop",
108 "ponmlkjihgfedcbaponmlkjihgfedcba"));
110 EXPECT_FALSE(info
->all_ids
);
112 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://google.com/index.html")));
115 TEST_F(ExternallyConnectableTest
, Matches
) {
116 scoped_refptr
<Extension
> extension
=
117 LoadAndExpectSuccess("externally_connectable_matches.json");
118 ASSERT_TRUE(extension
.get());
120 EXPECT_TRUE(extension
->HasAPIPermission(APIPermission::kWebConnectable
));
122 ExternallyConnectableInfo
* info
=
123 ExternallyConnectableInfo::Get(extension
.get());
126 EXPECT_THAT(info
->ids
, ElementsAre());
128 EXPECT_FALSE(info
->all_ids
);
130 EXPECT_FALSE(info
->accepts_tls_channel_id
);
132 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://example.com")));
133 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://example.com/")));
134 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://example.com/index.html")));
136 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://google.com")));
137 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://google.com/")));
138 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://google.com/index.html")));
139 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.google.com")));
140 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.google.com/")));
141 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://google.com")));
142 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://google.com/")));
144 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
145 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org/")));
147 info
->matches
.MatchesURL(GURL("http://build.chromium.org/index.html")));
148 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("https://build.chromium.org")));
149 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("https://build.chromium.org/")));
151 info
->matches
.MatchesURL(GURL("http://foo.chromium.org/index.html")));
153 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://yahoo.com")));
154 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://yahoo.com/")));
157 TEST_F(ExternallyConnectableTest
, MatchesWithTlsChannelId
) {
158 scoped_refptr
<Extension
> extension
=
159 LoadAndExpectSuccess(
160 "externally_connectable_matches_tls_channel_id.json");
161 ASSERT_TRUE(extension
.get());
163 EXPECT_TRUE(extension
->HasAPIPermission(APIPermission::kWebConnectable
));
165 ExternallyConnectableInfo
* info
=
166 ExternallyConnectableInfo::Get(extension
.get());
169 EXPECT_THAT(info
->ids
, ElementsAre());
171 EXPECT_FALSE(info
->all_ids
);
173 EXPECT_TRUE(info
->accepts_tls_channel_id
);
175 // The matches portion of the manifest is identical to those in
176 // externally_connectable_matches, so only a subset of the Matches tests is
178 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://example.com")));
179 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://example.com/index.html")));
182 TEST_F(ExternallyConnectableTest
, AllIDs
) {
183 scoped_refptr
<Extension
> extension
=
184 LoadAndExpectSuccess("externally_connectable_all_ids.json");
185 ASSERT_TRUE(extension
.get());
187 EXPECT_FALSE(extension
->HasAPIPermission(APIPermission::kWebConnectable
));
189 ExternallyConnectableInfo
* info
=
190 ExternallyConnectableInfo::Get(extension
.get());
193 EXPECT_THAT(info
->ids
, ElementsAre("abcdefghijklmnopabcdefghijklmnop",
194 "ponmlkjihgfedcbaponmlkjihgfedcba"));
196 EXPECT_TRUE(info
->all_ids
);
198 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://google.com/index.html")));
201 TEST_F(ExternallyConnectableTest
, IdCanConnect
) {
202 // Not in order to test that ExternallyConnectableInfo sorts it.
203 std::string matches_ids_array
[] = { "g", "h", "c", "i", "a", "z", "b" };
204 std::vector
<std::string
> matches_ids(
205 matches_ids_array
, matches_ids_array
+ arraysize(matches_ids_array
));
207 std::string nomatches_ids_array
[] = { "2", "3", "1" };
211 ExternallyConnectableInfo
info(URLPatternSet(), matches_ids
, false, false);
212 for (size_t i
= 0; i
< matches_ids
.size(); ++i
)
213 EXPECT_TRUE(info
.IdCanConnect(matches_ids
[i
]));
214 for (size_t i
= 0; i
< arraysize(nomatches_ids_array
); ++i
)
215 EXPECT_FALSE(info
.IdCanConnect(nomatches_ids_array
[i
]));
220 ExternallyConnectableInfo
info(URLPatternSet(), matches_ids
, true, false);
221 for (size_t i
= 0; i
< matches_ids
.size(); ++i
)
222 EXPECT_TRUE(info
.IdCanConnect(matches_ids
[i
]));
223 for (size_t i
= 0; i
< arraysize(nomatches_ids_array
); ++i
)
224 EXPECT_TRUE(info
.IdCanConnect(nomatches_ids_array
[i
]));
228 TEST_F(ExternallyConnectableTest
, ErrorWrongFormat
) {
229 LoadAndExpectError("externally_connectable_error_wrong_format.json",
230 "expected dictionary, got string");
233 TEST_F(ExternallyConnectableTest
, ErrorBadID
) {
235 "externally_connectable_bad_id.json",
236 ErrorUtils::FormatErrorMessage(errors::kErrorInvalidId
, "badid"));
239 TEST_F(ExternallyConnectableTest
, ErrorBadMatches
) {
241 "externally_connectable_error_bad_matches.json",
242 ErrorUtils::FormatErrorMessage(errors::kErrorInvalidMatchPattern
,
246 TEST_F(ExternallyConnectableTest
, WarningNoAllURLs
) {
247 scoped_refptr
<Extension
> extension
= LoadAndExpectWarning(
248 "externally_connectable_error_all_urls.json",
249 ErrorUtils::FormatErrorMessage(errors::kErrorWildcardHostsNotAllowed
,
251 ExternallyConnectableInfo
* info
= GetExternallyConnectableInfo(extension
);
252 EXPECT_FALSE(info
->matches
.ContainsPattern(
253 URLPattern(URLPattern::SCHEME_ALL
, "<all_urls>")));
254 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://example.com")));
255 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
258 TEST_F(ExternallyConnectableTest
, WarningWildcardHost
) {
259 scoped_refptr
<Extension
> extension
= LoadAndExpectWarning(
260 "externally_connectable_error_wildcard_host.json",
261 ErrorUtils::FormatErrorMessage(errors::kErrorWildcardHostsNotAllowed
,
263 ExternallyConnectableInfo
* info
= GetExternallyConnectableInfo(extension
);
264 EXPECT_FALSE(info
->matches
.ContainsPattern(
265 URLPattern(URLPattern::SCHEME_ALL
, "http://*/*")));
266 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://example.com")));
267 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
270 TEST_F(ExternallyConnectableTest
, WarningNoTLD
) {
271 scoped_refptr
<Extension
> extension
= LoadAndExpectWarning(
272 "externally_connectable_error_tld.json",
273 ErrorUtils::FormatErrorMessage(
274 errors::kErrorTopLevelDomainsNotAllowed
,
276 "http://*.co.uk/*"));
277 ExternallyConnectableInfo
* info
= GetExternallyConnectableInfo(extension
);
278 EXPECT_FALSE(info
->matches
.ContainsPattern(
279 URLPattern(URLPattern::SCHEME_ALL
, "http://*.co.uk/*")));
280 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://example.com")));
281 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
284 TEST_F(ExternallyConnectableTest
, WarningNoEffectiveTLD
) {
285 scoped_refptr
<Extension
> extension
= LoadAndExpectWarning(
286 "externally_connectable_error_effective_tld.json",
287 ErrorUtils::FormatErrorMessage(
288 errors::kErrorTopLevelDomainsNotAllowed
,
290 "http://*.appspot.com/*"));
291 ExternallyConnectableInfo
* info
= GetExternallyConnectableInfo(extension
);
292 EXPECT_FALSE(info
->matches
.ContainsPattern(
293 URLPattern(URLPattern::SCHEME_ALL
, "http://*.appspot.com/*")));
294 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://example.com")));
295 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
298 TEST_F(ExternallyConnectableTest
, WarningUnknownTLD
) {
299 scoped_refptr
<Extension
> extension
= LoadAndExpectWarning(
300 "externally_connectable_error_unknown_tld.json",
301 ErrorUtils::FormatErrorMessage(
302 errors::kErrorTopLevelDomainsNotAllowed
,
304 "http://*.notatld/*"));
305 ExternallyConnectableInfo
* info
= GetExternallyConnectableInfo(extension
);
306 EXPECT_FALSE(info
->matches
.ContainsPattern(
307 URLPattern(URLPattern::SCHEME_ALL
, "http://*.notatld/*")));
308 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://example.com")));
309 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
312 TEST_F(ExternallyConnectableTest
, WarningNothingSpecified
) {
313 LoadAndExpectWarning("externally_connectable_nothing_specified.json",
314 errors::kErrorNothingSpecified
);
317 } // namespace extensions