1 // Copyright 2014 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_tests/extension_manifest_test.h"
9 #include "extensions/common/error_utils.h"
10 #include "extensions/common/manifest_constants.h"
11 #include "extensions/common/manifest_handlers/externally_connectable.h"
12 #include "extensions/common/permissions/permissions_data.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using testing::ElementsAre
;
18 namespace extensions
{
20 namespace errors
= externally_connectable_errors
;
22 class ExternallyConnectableTest
: public ExtensionManifestTest
{
24 ExternallyConnectableTest() : channel_(chrome::VersionInfo::CHANNEL_DEV
) {}
27 ExternallyConnectableInfo
* GetExternallyConnectableInfo(
28 scoped_refptr
<Extension
> extension
) {
29 return static_cast<ExternallyConnectableInfo
*>(
30 extension
->GetManifestData(manifest_keys::kExternallyConnectable
));
34 ScopedCurrentChannel channel_
;
37 TEST_F(ExternallyConnectableTest
, IDsAndMatches
) {
38 scoped_refptr
<Extension
> extension
=
39 LoadAndExpectSuccess("externally_connectable_ids_and_matches.json");
40 ASSERT_TRUE(extension
.get());
42 EXPECT_TRUE(extension
->permissions_data()->HasAPIPermission(
43 APIPermission::kWebConnectable
));
45 ExternallyConnectableInfo
* info
=
46 ExternallyConnectableInfo::Get(extension
.get());
49 EXPECT_THAT(info
->ids
,
50 ElementsAre("abcdefghijklmnopabcdefghijklmnop",
51 "ponmlkjihgfedcbaponmlkjihgfedcba"));
53 EXPECT_FALSE(info
->all_ids
);
55 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://example.com")));
56 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://example.com/")));
57 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://example.com/index.html")));
59 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://google.com")));
60 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://google.com/")));
61 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://google.com/index.html")));
62 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.google.com")));
63 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.google.com/")));
64 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://google.com")));
65 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://google.com/")));
67 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
68 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org/")));
70 info
->matches
.MatchesURL(GURL("http://build.chromium.org/index.html")));
71 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("https://build.chromium.org")));
72 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("https://build.chromium.org/")));
74 info
->matches
.MatchesURL(GURL("http://foo.chromium.org/index.html")));
76 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://yahoo.com")));
77 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://yahoo.com/")));
79 // TLD-style patterns should match just the TLD.
80 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://appspot.com/foo.html")));
81 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://com")));
82 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://go/here")));
84 // TLD-style patterns should *not* match any subdomains of the TLD.
86 info
->matches
.MatchesURL(GURL("http://codereview.appspot.com/foo.html")));
88 info
->matches
.MatchesURL(GURL("http://chromium.com/index.html")));
89 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://here.go/somewhere")));
91 // Paths that don't have any wildcards should match the exact domain, but
92 // ignore the trailing slash. This is kind of a corner case, so let's test it.
93 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://no.wildcard.path")));
94 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://no.wildcard.path/")));
96 info
->matches
.MatchesURL(GURL("http://no.wildcard.path/index.html")));
99 TEST_F(ExternallyConnectableTest
, IDs
) {
100 scoped_refptr
<Extension
> extension
=
101 LoadAndExpectSuccess("externally_connectable_ids.json");
102 ASSERT_TRUE(extension
.get());
104 EXPECT_FALSE(extension
->permissions_data()->HasAPIPermission(
105 APIPermission::kWebConnectable
));
107 ExternallyConnectableInfo
* info
=
108 ExternallyConnectableInfo::Get(extension
.get());
111 EXPECT_THAT(info
->ids
,
112 ElementsAre("abcdefghijklmnopabcdefghijklmnop",
113 "ponmlkjihgfedcbaponmlkjihgfedcba"));
115 EXPECT_FALSE(info
->all_ids
);
117 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://google.com/index.html")));
120 TEST_F(ExternallyConnectableTest
, Matches
) {
121 scoped_refptr
<Extension
> extension
=
122 LoadAndExpectSuccess("externally_connectable_matches.json");
123 ASSERT_TRUE(extension
.get());
125 EXPECT_TRUE(extension
->permissions_data()->HasAPIPermission(
126 APIPermission::kWebConnectable
));
128 ExternallyConnectableInfo
* info
=
129 ExternallyConnectableInfo::Get(extension
.get());
132 EXPECT_THAT(info
->ids
, ElementsAre());
134 EXPECT_FALSE(info
->all_ids
);
136 EXPECT_FALSE(info
->accepts_tls_channel_id
);
138 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://example.com")));
139 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://example.com/")));
140 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://example.com/index.html")));
142 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://google.com")));
143 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://google.com/")));
144 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://google.com/index.html")));
145 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.google.com")));
146 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.google.com/")));
147 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://google.com")));
148 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://google.com/")));
150 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
151 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org/")));
153 info
->matches
.MatchesURL(GURL("http://build.chromium.org/index.html")));
154 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("https://build.chromium.org")));
155 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("https://build.chromium.org/")));
157 info
->matches
.MatchesURL(GURL("http://foo.chromium.org/index.html")));
159 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://yahoo.com")));
160 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://yahoo.com/")));
163 TEST_F(ExternallyConnectableTest
, MatchesWithTlsChannelId
) {
164 scoped_refptr
<Extension
> extension
= LoadAndExpectSuccess(
165 "externally_connectable_matches_tls_channel_id.json");
166 ASSERT_TRUE(extension
.get());
168 EXPECT_TRUE(extension
->permissions_data()->HasAPIPermission(
169 APIPermission::kWebConnectable
));
171 ExternallyConnectableInfo
* info
=
172 ExternallyConnectableInfo::Get(extension
.get());
175 EXPECT_THAT(info
->ids
, ElementsAre());
177 EXPECT_FALSE(info
->all_ids
);
179 EXPECT_TRUE(info
->accepts_tls_channel_id
);
181 // The matches portion of the manifest is identical to those in
182 // externally_connectable_matches, so only a subset of the Matches tests is
184 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://example.com")));
185 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://example.com/index.html")));
188 TEST_F(ExternallyConnectableTest
, AllIDs
) {
189 scoped_refptr
<Extension
> extension
=
190 LoadAndExpectSuccess("externally_connectable_all_ids.json");
191 ASSERT_TRUE(extension
.get());
193 EXPECT_FALSE(extension
->permissions_data()->HasAPIPermission(
194 APIPermission::kWebConnectable
));
196 ExternallyConnectableInfo
* info
=
197 ExternallyConnectableInfo::Get(extension
.get());
200 EXPECT_THAT(info
->ids
,
201 ElementsAre("abcdefghijklmnopabcdefghijklmnop",
202 "ponmlkjihgfedcbaponmlkjihgfedcba"));
204 EXPECT_TRUE(info
->all_ids
);
206 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://google.com/index.html")));
209 TEST_F(ExternallyConnectableTest
, IdCanConnect
) {
210 // Not in order to test that ExternallyConnectableInfo sorts it.
211 std::string matches_ids_array
[] = {"g", "h", "c", "i", "a", "z", "b"};
212 std::vector
<std::string
> matches_ids(
213 matches_ids_array
, matches_ids_array
+ arraysize(matches_ids_array
));
215 std::string nomatches_ids_array
[] = {"2", "3", "1"};
219 ExternallyConnectableInfo
info(URLPatternSet(), matches_ids
, false, false);
220 for (size_t i
= 0; i
< matches_ids
.size(); ++i
)
221 EXPECT_TRUE(info
.IdCanConnect(matches_ids
[i
]));
222 for (size_t i
= 0; i
< arraysize(nomatches_ids_array
); ++i
)
223 EXPECT_FALSE(info
.IdCanConnect(nomatches_ids_array
[i
]));
228 ExternallyConnectableInfo
info(URLPatternSet(), matches_ids
, true, false);
229 for (size_t i
= 0; i
< matches_ids
.size(); ++i
)
230 EXPECT_TRUE(info
.IdCanConnect(matches_ids
[i
]));
231 for (size_t i
= 0; i
< arraysize(nomatches_ids_array
); ++i
)
232 EXPECT_TRUE(info
.IdCanConnect(nomatches_ids_array
[i
]));
236 TEST_F(ExternallyConnectableTest
, ErrorWrongFormat
) {
237 LoadAndExpectError("externally_connectable_error_wrong_format.json",
238 "expected dictionary, got string");
241 TEST_F(ExternallyConnectableTest
, ErrorBadID
) {
243 "externally_connectable_bad_id.json",
244 ErrorUtils::FormatErrorMessage(errors::kErrorInvalidId
, "badid"));
247 TEST_F(ExternallyConnectableTest
, ErrorBadMatches
) {
248 LoadAndExpectError("externally_connectable_error_bad_matches.json",
249 ErrorUtils::FormatErrorMessage(
250 errors::kErrorInvalidMatchPattern
, "www.yahoo.com"));
253 TEST_F(ExternallyConnectableTest
, WarningNoAllURLs
) {
254 scoped_refptr
<Extension
> extension
= LoadAndExpectWarning(
255 "externally_connectable_error_all_urls.json",
256 ErrorUtils::FormatErrorMessage(errors::kErrorWildcardHostsNotAllowed
,
258 ExternallyConnectableInfo
* info
= GetExternallyConnectableInfo(extension
);
259 EXPECT_FALSE(info
->matches
.ContainsPattern(
260 URLPattern(URLPattern::SCHEME_ALL
, "<all_urls>")));
261 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://example.com")));
262 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
265 TEST_F(ExternallyConnectableTest
, WarningWildcardHost
) {
266 scoped_refptr
<Extension
> extension
= LoadAndExpectWarning(
267 "externally_connectable_error_wildcard_host.json",
268 ErrorUtils::FormatErrorMessage(errors::kErrorWildcardHostsNotAllowed
,
270 ExternallyConnectableInfo
* info
= GetExternallyConnectableInfo(extension
);
271 EXPECT_FALSE(info
->matches
.ContainsPattern(
272 URLPattern(URLPattern::SCHEME_ALL
, "http://*/*")));
273 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://example.com")));
274 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
277 TEST_F(ExternallyConnectableTest
, WarningNoTLD
) {
278 scoped_refptr
<Extension
> extension
= LoadAndExpectWarning(
279 "externally_connectable_error_tld.json",
280 ErrorUtils::FormatErrorMessage(errors::kErrorTopLevelDomainsNotAllowed
,
282 "http://*.co.uk/*"));
283 ExternallyConnectableInfo
* info
= GetExternallyConnectableInfo(extension
);
284 EXPECT_FALSE(info
->matches
.ContainsPattern(
285 URLPattern(URLPattern::SCHEME_ALL
, "http://*.co.uk/*")));
286 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://example.com")));
287 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
290 TEST_F(ExternallyConnectableTest
, WarningNoEffectiveTLD
) {
291 scoped_refptr
<Extension
> extension
= LoadAndExpectWarning(
292 "externally_connectable_error_effective_tld.json",
293 ErrorUtils::FormatErrorMessage(errors::kErrorTopLevelDomainsNotAllowed
,
295 "http://*.appspot.com/*"));
296 ExternallyConnectableInfo
* info
= GetExternallyConnectableInfo(extension
);
297 EXPECT_FALSE(info
->matches
.ContainsPattern(
298 URLPattern(URLPattern::SCHEME_ALL
, "http://*.appspot.com/*")));
299 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://example.com")));
300 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
303 TEST_F(ExternallyConnectableTest
, WarningUnknownTLD
) {
304 scoped_refptr
<Extension
> extension
= LoadAndExpectWarning(
305 "externally_connectable_error_unknown_tld.json",
306 ErrorUtils::FormatErrorMessage(errors::kErrorTopLevelDomainsNotAllowed
,
308 "http://*.notatld/*"));
309 ExternallyConnectableInfo
* info
= GetExternallyConnectableInfo(extension
);
310 EXPECT_FALSE(info
->matches
.ContainsPattern(
311 URLPattern(URLPattern::SCHEME_ALL
, "http://*.notatld/*")));
312 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("https://example.com")));
313 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://build.chromium.org")));
316 TEST_F(ExternallyConnectableTest
, WarningNothingSpecified
) {
317 LoadAndExpectWarning("externally_connectable_nothing_specified.json",
318 errors::kErrorNothingSpecified
);
321 } // namespace extensions