- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / manifest_handlers / externally_connectable_unittest.cc
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.
4
5 #include <algorithm>
6
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"
14
15 using testing::ElementsAre;
16
17 namespace extensions {
18
19 namespace errors = externally_connectable_errors;
20
21 class ExternallyConnectableTest : public ExtensionManifestTest {
22  public:
23   ExternallyConnectableTest() : channel_(chrome::VersionInfo::CHANNEL_DEV) {}
24
25  protected:
26   ExternallyConnectableInfo* GetExternallyConnectableInfo(
27       scoped_refptr<Extension> extension) {
28     return static_cast<ExternallyConnectableInfo*>(extension->GetManifestData(
29         manifest_keys::kExternallyConnectable));
30   }
31
32  private:
33   ScopedCurrentChannel channel_;
34 };
35
36 TEST_F(ExternallyConnectableTest, IDsAndMatches) {
37   scoped_refptr<Extension> extension =
38       LoadAndExpectSuccess("externally_connectable_ids_and_matches.json");
39   ASSERT_TRUE(extension.get());
40
41   EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kWebConnectable));
42
43   ExternallyConnectableInfo* info =
44       ExternallyConnectableInfo::Get(extension.get());
45   ASSERT_TRUE(info);
46
47   EXPECT_THAT(info->ids, ElementsAre("abcdefghijklmnopabcdefghijklmnop",
48                                      "ponmlkjihgfedcbaponmlkjihgfedcba"));
49
50   EXPECT_FALSE(info->all_ids);
51
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")));
55
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/")));
63
64   EXPECT_TRUE(info->matches.MatchesURL(GURL("http://build.chromium.org")));
65   EXPECT_TRUE(info->matches.MatchesURL(GURL("http://build.chromium.org/")));
66   EXPECT_TRUE(
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/")));
70   EXPECT_FALSE(
71       info->matches.MatchesURL(GURL("http://foo.chromium.org/index.html")));
72
73   EXPECT_FALSE(info->matches.MatchesURL(GURL("http://yahoo.com")));
74   EXPECT_FALSE(info->matches.MatchesURL(GURL("http://yahoo.com/")));
75
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")));
80
81   // TLD-style patterns should *not* match any subdomains of the TLD.
82   EXPECT_FALSE(
83       info->matches.MatchesURL(GURL("http://codereview.appspot.com/foo.html")));
84   EXPECT_FALSE(
85       info->matches.MatchesURL(GURL("http://chromium.com/index.html")));
86   EXPECT_FALSE(info->matches.MatchesURL(GURL("http://here.go/somewhere")));
87
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")));
94 }
95
96 TEST_F(ExternallyConnectableTest, IDs) {
97   scoped_refptr<Extension> extension =
98       LoadAndExpectSuccess("externally_connectable_ids.json");
99   ASSERT_TRUE(extension.get());
100
101   EXPECT_FALSE(extension->HasAPIPermission(APIPermission::kWebConnectable));
102
103   ExternallyConnectableInfo* info =
104       ExternallyConnectableInfo::Get(extension.get());
105   ASSERT_TRUE(info);
106
107   EXPECT_THAT(info->ids, ElementsAre("abcdefghijklmnopabcdefghijklmnop",
108                                      "ponmlkjihgfedcbaponmlkjihgfedcba"));
109
110   EXPECT_FALSE(info->all_ids);
111
112   EXPECT_FALSE(info->matches.MatchesURL(GURL("http://google.com/index.html")));
113 }
114
115 TEST_F(ExternallyConnectableTest, Matches) {
116   scoped_refptr<Extension> extension =
117       LoadAndExpectSuccess("externally_connectable_matches.json");
118   ASSERT_TRUE(extension.get());
119
120   EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kWebConnectable));
121
122   ExternallyConnectableInfo* info =
123       ExternallyConnectableInfo::Get(extension.get());
124   ASSERT_TRUE(info);
125
126   EXPECT_THAT(info->ids, ElementsAre());
127
128   EXPECT_FALSE(info->all_ids);
129
130   EXPECT_FALSE(info->accepts_tls_channel_id);
131
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")));
135
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/")));
143
144   EXPECT_TRUE(info->matches.MatchesURL(GURL("http://build.chromium.org")));
145   EXPECT_TRUE(info->matches.MatchesURL(GURL("http://build.chromium.org/")));
146   EXPECT_TRUE(
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/")));
150   EXPECT_FALSE(
151       info->matches.MatchesURL(GURL("http://foo.chromium.org/index.html")));
152
153   EXPECT_FALSE(info->matches.MatchesURL(GURL("http://yahoo.com")));
154   EXPECT_FALSE(info->matches.MatchesURL(GURL("http://yahoo.com/")));
155 }
156
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());
162
163   EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kWebConnectable));
164
165   ExternallyConnectableInfo* info =
166       ExternallyConnectableInfo::Get(extension.get());
167   ASSERT_TRUE(info);
168
169   EXPECT_THAT(info->ids, ElementsAre());
170
171   EXPECT_FALSE(info->all_ids);
172
173   EXPECT_TRUE(info->accepts_tls_channel_id);
174
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
177   // repeated here.
178   EXPECT_TRUE(info->matches.MatchesURL(GURL("http://example.com")));
179   EXPECT_FALSE(info->matches.MatchesURL(GURL("http://example.com/index.html")));
180 }
181
182 TEST_F(ExternallyConnectableTest, AllIDs) {
183   scoped_refptr<Extension> extension =
184       LoadAndExpectSuccess("externally_connectable_all_ids.json");
185   ASSERT_TRUE(extension.get());
186
187   EXPECT_FALSE(extension->HasAPIPermission(APIPermission::kWebConnectable));
188
189   ExternallyConnectableInfo* info =
190       ExternallyConnectableInfo::Get(extension.get());
191   ASSERT_TRUE(info);
192
193   EXPECT_THAT(info->ids, ElementsAre("abcdefghijklmnopabcdefghijklmnop",
194                                      "ponmlkjihgfedcbaponmlkjihgfedcba"));
195
196   EXPECT_TRUE(info->all_ids);
197
198   EXPECT_FALSE(info->matches.MatchesURL(GURL("http://google.com/index.html")));
199 }
200
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));
206
207   std::string nomatches_ids_array[] = { "2", "3", "1" };
208
209   // all_ids = false.
210   {
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]));
216   }
217
218   // all_ids = true.
219   {
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]));
225   }
226 }
227
228 TEST_F(ExternallyConnectableTest, ErrorWrongFormat) {
229   LoadAndExpectError("externally_connectable_error_wrong_format.json",
230                      "expected dictionary, got string");
231 }
232
233 TEST_F(ExternallyConnectableTest, ErrorBadID) {
234   LoadAndExpectError(
235       "externally_connectable_bad_id.json",
236       ErrorUtils::FormatErrorMessage(errors::kErrorInvalidId, "badid"));
237 }
238
239 TEST_F(ExternallyConnectableTest, ErrorBadMatches) {
240   LoadAndExpectError(
241       "externally_connectable_error_bad_matches.json",
242       ErrorUtils::FormatErrorMessage(errors::kErrorInvalidMatchPattern,
243                                      "www.yahoo.com"));
244 }
245
246 TEST_F(ExternallyConnectableTest, WarningNoAllURLs) {
247   scoped_refptr<Extension> extension = LoadAndExpectWarning(
248       "externally_connectable_error_all_urls.json",
249       ErrorUtils::FormatErrorMessage(errors::kErrorWildcardHostsNotAllowed,
250                                      "<all_urls>"));
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")));
256 }
257
258 TEST_F(ExternallyConnectableTest, WarningWildcardHost) {
259   scoped_refptr<Extension> extension = LoadAndExpectWarning(
260       "externally_connectable_error_wildcard_host.json",
261       ErrorUtils::FormatErrorMessage(errors::kErrorWildcardHostsNotAllowed,
262                                      "http://*/*"));
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")));
268 }
269
270 TEST_F(ExternallyConnectableTest, WarningNoTLD) {
271   scoped_refptr<Extension> extension = LoadAndExpectWarning(
272       "externally_connectable_error_tld.json",
273       ErrorUtils::FormatErrorMessage(
274           errors::kErrorTopLevelDomainsNotAllowed,
275           "co.uk",
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")));
282 }
283
284 TEST_F(ExternallyConnectableTest, WarningNoEffectiveTLD) {
285   scoped_refptr<Extension> extension = LoadAndExpectWarning(
286       "externally_connectable_error_effective_tld.json",
287       ErrorUtils::FormatErrorMessage(
288           errors::kErrorTopLevelDomainsNotAllowed,
289           "appspot.com",
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")));
296 }
297
298 TEST_F(ExternallyConnectableTest, WarningUnknownTLD) {
299   scoped_refptr<Extension> extension = LoadAndExpectWarning(
300       "externally_connectable_error_unknown_tld.json",
301       ErrorUtils::FormatErrorMessage(
302           errors::kErrorTopLevelDomainsNotAllowed,
303           "notatld",
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")));
310 }
311
312 TEST_F(ExternallyConnectableTest, WarningNothingSpecified) {
313   LoadAndExpectWarning("externally_connectable_nothing_specified.json",
314                        errors::kErrorNothingSpecified);
315 }
316
317 }  // namespace extensions