blob: 07f005be838191a8ff1bd403b5faf76450da1565 [file] [log] [blame]
[email protected]a507b822014-04-12 05:10:241// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]9b26746e2009-08-06 21:35:452// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avib7348942015-12-25 20:57:105#include <stddef.h>
6
[email protected]9b26746e2009-08-06 21:35:457#include <string>
Victor Costan3a412742018-08-24 21:32:238#include <vector>
[email protected]9b26746e2009-08-06 21:35:459
avib7348942015-12-25 20:57:1010#include "base/macros.h"
[email protected]e4ca79d2014-08-22 13:15:1011#include "content/browser/appcache/appcache_manifest_parser.h"
[email protected]9b26746e2009-08-06 21:35:4512#include "testing/gtest/include/gtest/gtest.h"
[email protected]ad677772013-06-29 14:18:3813#include "url/gurl.h"
[email protected]a507b822014-04-12 05:10:2414
15namespace content {
[email protected]9b26746e2009-08-06 21:35:4516
[email protected]e2cadec82011-12-13 02:00:5317class AppCacheManifestParserTest : public testing::Test {
[email protected]9b26746e2009-08-06 21:35:4518};
19
[email protected]e2cadec82011-12-13 02:00:5320TEST(AppCacheManifestParserTest, NoData) {
[email protected]9b26746e2009-08-06 21:35:4521 GURL url;
mlamouric9230312014-09-09 05:36:4322 AppCacheManifest manifest;
michaelnb4d77cd2017-06-08 20:42:5223 EXPECT_FALSE(ParseManifest(
24 url, "", 0, PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES, manifest));
[email protected]b993171f2014-05-13 06:11:1525 EXPECT_FALSE(ParseManifest(url, "CACHE MANIFEST\r", 0, // Len is 0.
michaelnb4d77cd2017-06-08 20:42:5226 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
27 manifest));
[email protected]9b26746e2009-08-06 21:35:4528}
29
[email protected]e2cadec82011-12-13 02:00:5330TEST(AppCacheManifestParserTest, CheckSignature) {
[email protected]9b26746e2009-08-06 21:35:4531 GURL url;
mlamouric9230312014-09-09 05:36:4332 AppCacheManifest manifest;
[email protected]9b26746e2009-08-06 21:35:4533
34 const std::string kBadSignatures[] = {
35 "foo",
36 "CACHE MANIFEST;V2\r", // not followed by whitespace
37 "CACHE MANIFEST#bad\r", // no whitespace before comment
38 "cache manifest ", // wrong case
39 "#CACHE MANIFEST\r", // comment
40 "xCACHE MANIFEST\n", // bad first char
41 " CACHE MANIFEST\r", // begins with whitespace
42 "\xEF\xBE\xBF" "CACHE MANIFEST\r", // bad UTF-8 BOM value
43 };
44
45 for (size_t i = 0; i < arraysize(kBadSignatures); ++i) {
46 const std::string bad = kBadSignatures[i];
[email protected]b993171f2014-05-13 06:11:1547 EXPECT_FALSE(ParseManifest(url, bad.c_str(), bad.length(),
michaelnb4d77cd2017-06-08 20:42:5248 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
49 manifest));
[email protected]9b26746e2009-08-06 21:35:4550 }
51
52 const std::string kGoodSignatures[] = {
53 "CACHE MANIFEST",
54 "CACHE MANIFEST ",
55 "CACHE MANIFEST\r",
56 "CACHE MANIFEST\n",
57 "CACHE MANIFEST\r\n",
58 "CACHE MANIFEST\t# ignore me\r",
59 "CACHE MANIFEST ignore\r\n",
[email protected]e2cadec82011-12-13 02:00:5360 "CHROMIUM CACHE MANIFEST\r\n",
[email protected]9b26746e2009-08-06 21:35:4561 "\xEF\xBB\xBF" "CACHE MANIFEST \r\n", // BOM present
62 };
63
64 for (size_t i = 0; i < arraysize(kGoodSignatures); ++i) {
65 const std::string good = kGoodSignatures[i];
[email protected]b993171f2014-05-13 06:11:1566 EXPECT_TRUE(ParseManifest(url, good.c_str(), good.length(),
michaelnb4d77cd2017-06-08 20:42:5267 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
68 manifest));
[email protected]9b26746e2009-08-06 21:35:4569 }
70}
71
[email protected]e2cadec82011-12-13 02:00:5372TEST(AppCacheManifestParserTest, NoManifestUrl) {
mlamouric9230312014-09-09 05:36:4373 AppCacheManifest manifest;
[email protected]9b26746e2009-08-06 21:35:4574 const std::string kData("CACHE MANIFEST\r"
75 "relative/tobase.com\r"
76 "http://absolute.com/addme.com");
[email protected]810a52ef2010-01-08 01:22:1577 const GURL kUrl;
[email protected]b993171f2014-05-13 06:11:1578 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
michaelnb4d77cd2017-06-08 20:42:5279 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
80 manifest));
[email protected]9b26746e2009-08-06 21:35:4581 EXPECT_TRUE(manifest.explicit_urls.empty());
[email protected]b160d6d2009-08-10 23:58:1882 EXPECT_TRUE(manifest.fallback_namespaces.empty());
83 EXPECT_TRUE(manifest.online_whitelist_namespaces.empty());
84 EXPECT_FALSE(manifest.online_whitelist_all);
[email protected]9b26746e2009-08-06 21:35:4585}
86
[email protected]e2cadec82011-12-13 02:00:5387TEST(AppCacheManifestParserTest, ExplicitUrls) {
mlamouric9230312014-09-09 05:36:4388 AppCacheManifest manifest;
[email protected]9b26746e2009-08-06 21:35:4589 const GURL kUrl("http://www.foo.com");
90 const std::string kData("CACHE MANIFEST\r"
91 "relative/one\r"
92 "# some comment\r"
93 "http://www.foo.com/two#strip\r\n"
94 "NETWORK:\r"
95 " \t CACHE:\r"
96 "HTTP://www.diff.com/three\r"
97 "FALLBACK:\r"
98 " \t # another comment with leading whitespace\n"
99 "IGNORE:\r"
100 "http://www.foo.com/ignore\r"
101 "CACHE: \r"
102 "garbage:#!@\r"
103 "https://www.foo.com/diffscheme \t \r"
[email protected]b160d6d2009-08-10 23:58:18104 " \t relative/four#stripme\n\r"
105 "*\r");
[email protected]9b26746e2009-08-06 21:35:45106
[email protected]b993171f2014-05-13 06:11:15107 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
michaelnb4d77cd2017-06-08 20:42:52108 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
109 manifest));
[email protected]b160d6d2009-08-10 23:58:18110 EXPECT_TRUE(manifest.fallback_namespaces.empty());
111 EXPECT_TRUE(manifest.online_whitelist_namespaces.empty());
112 EXPECT_FALSE(manifest.online_whitelist_all);
michaelnb4d77cd2017-06-08 20:42:52113 EXPECT_FALSE(manifest.did_ignore_intercept_namespaces);
114 EXPECT_FALSE(manifest.did_ignore_fallback_namespaces);
[email protected]9b26746e2009-08-06 21:35:45115
116 base::hash_set<std::string> urls = manifest.explicit_urls;
[email protected]b160d6d2009-08-10 23:58:18117 const size_t kExpected = 5;
[email protected]9b26746e2009-08-06 21:35:45118 ASSERT_EQ(kExpected, urls.size());
119 EXPECT_TRUE(urls.find("http://www.foo.com/relative/one") != urls.end());
120 EXPECT_TRUE(urls.find("http://www.foo.com/two") != urls.end());
121 EXPECT_TRUE(urls.find("http://www.diff.com/three") != urls.end());
122 EXPECT_TRUE(urls.find("http://www.foo.com/relative/four") != urls.end());
[email protected]b160d6d2009-08-10 23:58:18123
124 // Wildcard is treated as a relative URL in explicit section.
125 EXPECT_TRUE(urls.find("http://www.foo.com/*") != urls.end());
[email protected]b993171f2014-05-13 06:11:15126
michaelnb4d77cd2017-06-08 20:42:52127 // We should get the same results with dangerous features disallowed.
mlamouric9230312014-09-09 05:36:43128 manifest = AppCacheManifest();
[email protected]b993171f2014-05-13 06:11:15129 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
130 PARSE_MANIFEST_PER_STANDARD, manifest));
131 EXPECT_TRUE(manifest.fallback_namespaces.empty());
132 EXPECT_TRUE(manifest.online_whitelist_namespaces.empty());
133 EXPECT_FALSE(manifest.online_whitelist_all);
michaelnb4d77cd2017-06-08 20:42:52134 EXPECT_FALSE(manifest.did_ignore_intercept_namespaces);
135 EXPECT_FALSE(manifest.did_ignore_fallback_namespaces);
[email protected]b993171f2014-05-13 06:11:15136
137 urls = manifest.explicit_urls;
138 ASSERT_EQ(kExpected, urls.size());
139 EXPECT_TRUE(urls.find("http://www.foo.com/relative/one") != urls.end());
140 EXPECT_TRUE(urls.find("http://www.foo.com/two") != urls.end());
141 EXPECT_TRUE(urls.find("http://www.diff.com/three") != urls.end());
142 EXPECT_TRUE(urls.find("http://www.foo.com/relative/four") != urls.end());
143
144 // Wildcard is treated as a relative URL in explicit section.
145 EXPECT_TRUE(urls.find("http://www.foo.com/*") != urls.end());
[email protected]9b26746e2009-08-06 21:35:45146}
147
[email protected]e2cadec82011-12-13 02:00:53148TEST(AppCacheManifestParserTest, WhitelistUrls) {
mlamouric9230312014-09-09 05:36:43149 AppCacheManifest manifest;
[email protected]9b26746e2009-08-06 21:35:45150 const GURL kUrl("http://www.bar.com");
151 const std::string kData("CACHE MANIFEST\r"
152 "NETWORK:\r"
153 "relative/one\r"
154 "# a comment\r"
155 "http://www.bar.com/two\r"
156 "HTTP://www.diff.com/three#strip\n\r"
157 "FALLBACK:\r"
158 "garbage\r"
159 "UNKNOWN:\r"
160 "http://www.bar.com/ignore\r"
161 "CACHE:\r"
162 "NETWORK:\r"
163 "https://www.wrongscheme.com\n"
164 "relative/four#stripref \t \r"
[email protected]b160d6d2009-08-10 23:58:18165 "http://www.five.com\r\n"
166 "*foo\r");
[email protected]9b26746e2009-08-06 21:35:45167
[email protected]b993171f2014-05-13 06:11:15168 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
michaelnb4d77cd2017-06-08 20:42:52169 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
170 manifest));
[email protected]9b26746e2009-08-06 21:35:45171 EXPECT_TRUE(manifest.explicit_urls.empty());
[email protected]b160d6d2009-08-10 23:58:18172 EXPECT_TRUE(manifest.fallback_namespaces.empty());
[email protected]e2cadec82011-12-13 02:00:53173 EXPECT_TRUE(manifest.intercept_namespaces.empty());
[email protected]b160d6d2009-08-10 23:58:18174 EXPECT_FALSE(manifest.online_whitelist_all);
michaelnb4d77cd2017-06-08 20:42:52175 EXPECT_FALSE(manifest.did_ignore_intercept_namespaces);
176 EXPECT_FALSE(manifest.did_ignore_fallback_namespaces);
[email protected]9b26746e2009-08-06 21:35:45177
Victor Costan3a412742018-08-24 21:32:23178 const std::vector<AppCacheNamespace>& online =
179 manifest.online_whitelist_namespaces;
[email protected]b160d6d2009-08-10 23:58:18180 const size_t kExpected = 6;
[email protected]9b26746e2009-08-06 21:35:45181 ASSERT_EQ(kExpected, online.size());
[email protected]10f110fa2014-06-15 23:32:46182 EXPECT_EQ(APPCACHE_NETWORK_NAMESPACE, online[0].type);
[email protected]34e7dc62013-03-30 00:32:42183 EXPECT_FALSE(online[0].is_pattern);
184 EXPECT_TRUE(online[0].target_url.is_empty());
185 EXPECT_EQ(GURL("http://www.bar.com/relative/one"), online[0].namespace_url);
186 EXPECT_EQ(GURL("http://www.bar.com/two"), online[1].namespace_url);
187 EXPECT_EQ(GURL("http://www.diff.com/three"), online[2].namespace_url);
188 EXPECT_EQ(GURL("http://www.bar.com/relative/four"), online[3].namespace_url);
189 EXPECT_EQ(GURL("http://www.five.com"), online[4].namespace_url);
190 EXPECT_EQ(GURL("http://www.bar.com/*foo"), online[5].namespace_url);
[email protected]9b26746e2009-08-06 21:35:45191}
192
[email protected]e2cadec82011-12-13 02:00:53193TEST(AppCacheManifestParserTest, FallbackUrls) {
mlamouric9230312014-09-09 05:36:43194 AppCacheManifest manifest;
[email protected]9b26746e2009-08-06 21:35:45195 const GURL kUrl("http://glorp.com");
196 const std::string kData("CACHE MANIFEST\r"
197 "# a comment\r"
198 "CACHE:\r"
199 "NETWORK:\r"
200 "UNKNOWN:\r"
201 "FALLBACK:\r"
202 "relative/one \t \t http://glorp.com/onefb \t \r"
[email protected]b160d6d2009-08-10 23:58:18203 "*\r"
[email protected]9b26746e2009-08-06 21:35:45204 "https://glorp.com/wrong http://glorp.com/wrongfb\r"
205 "http://glorp.com/two#strip relative/twofb\r"
206 "HTTP://glorp.com/three relative/threefb#strip\n"
207 "http://glorp.com/three http://glorp.com/three-dup\r"
208 "http://glorp.com/solo \t \r\n"
209 "http://diff.com/ignore http://glorp.com/wronghost\r"
210 "http://glorp.com/wronghost http://diff.com/ohwell\r"
211 "relative/badscheme ftp://glorp.com/ignored\r"
212 "garbage\r\n"
213 "CACHE:\r"
214 "# only fallback urls in this test\r"
215 "FALLBACK:\n"
216 "relative/four#strip relative/fourfb#strip\r"
217 "http://www.glorp.com/notsame relative/skipped\r");
218
[email protected]b993171f2014-05-13 06:11:15219 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
michaelnb4d77cd2017-06-08 20:42:52220 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
221 manifest));
[email protected]9b26746e2009-08-06 21:35:45222 EXPECT_TRUE(manifest.explicit_urls.empty());
[email protected]b160d6d2009-08-10 23:58:18223 EXPECT_TRUE(manifest.online_whitelist_namespaces.empty());
224 EXPECT_FALSE(manifest.online_whitelist_all);
michaelnb4d77cd2017-06-08 20:42:52225 EXPECT_FALSE(manifest.did_ignore_intercept_namespaces);
226 EXPECT_FALSE(manifest.did_ignore_fallback_namespaces);
[email protected]9b26746e2009-08-06 21:35:45227
Victor Costan3a412742018-08-24 21:32:23228 const std::vector<AppCacheNamespace>& fallbacks =
229 manifest.fallback_namespaces;
[email protected]9b26746e2009-08-06 21:35:45230 const size_t kExpected = 5;
231 ASSERT_EQ(kExpected, fallbacks.size());
[email protected]10f110fa2014-06-15 23:32:46232 EXPECT_EQ(APPCACHE_FALLBACK_NAMESPACE, fallbacks[0].type);
233 EXPECT_EQ(APPCACHE_FALLBACK_NAMESPACE, fallbacks[1].type);
234 EXPECT_EQ(APPCACHE_FALLBACK_NAMESPACE, fallbacks[2].type);
235 EXPECT_EQ(APPCACHE_FALLBACK_NAMESPACE, fallbacks[3].type);
236 EXPECT_EQ(APPCACHE_FALLBACK_NAMESPACE, fallbacks[4].type);
[email protected]9b26746e2009-08-06 21:35:45237 EXPECT_EQ(GURL("http://glorp.com/relative/one"),
[email protected]e2cadec82011-12-13 02:00:53238 fallbacks[0].namespace_url);
[email protected]9b26746e2009-08-06 21:35:45239 EXPECT_EQ(GURL("http://glorp.com/onefb"),
[email protected]e2cadec82011-12-13 02:00:53240 fallbacks[0].target_url);
[email protected]9b26746e2009-08-06 21:35:45241 EXPECT_EQ(GURL("http://glorp.com/two"),
[email protected]e2cadec82011-12-13 02:00:53242 fallbacks[1].namespace_url);
[email protected]9b26746e2009-08-06 21:35:45243 EXPECT_EQ(GURL("http://glorp.com/relative/twofb"),
[email protected]e2cadec82011-12-13 02:00:53244 fallbacks[1].target_url);
[email protected]9b26746e2009-08-06 21:35:45245 EXPECT_EQ(GURL("http://glorp.com/three"),
[email protected]e2cadec82011-12-13 02:00:53246 fallbacks[2].namespace_url);
[email protected]9b26746e2009-08-06 21:35:45247 EXPECT_EQ(GURL("http://glorp.com/relative/threefb"),
[email protected]e2cadec82011-12-13 02:00:53248 fallbacks[2].target_url);
[email protected]9b26746e2009-08-06 21:35:45249 EXPECT_EQ(GURL("http://glorp.com/three"), // duplicates are stored
[email protected]e2cadec82011-12-13 02:00:53250 fallbacks[3].namespace_url);
[email protected]9b26746e2009-08-06 21:35:45251 EXPECT_EQ(GURL("http://glorp.com/three-dup"),
[email protected]e2cadec82011-12-13 02:00:53252 fallbacks[3].target_url);
[email protected]9b26746e2009-08-06 21:35:45253 EXPECT_EQ(GURL("http://glorp.com/relative/four"),
[email protected]e2cadec82011-12-13 02:00:53254 fallbacks[4].namespace_url);
[email protected]9b26746e2009-08-06 21:35:45255 EXPECT_EQ(GURL("http://glorp.com/relative/fourfb"),
[email protected]e2cadec82011-12-13 02:00:53256 fallbacks[4].target_url);
[email protected]e2cadec82011-12-13 02:00:53257 EXPECT_TRUE(manifest.intercept_namespaces.empty());
michaelnb4d77cd2017-06-08 20:42:52258
259 // Nothing should be ignored since all namespaces are in scope.
260 manifest = AppCacheManifest();
261 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
262 PARSE_MANIFEST_PER_STANDARD, manifest));
263 EXPECT_FALSE(manifest.did_ignore_intercept_namespaces);
264 EXPECT_FALSE(manifest.did_ignore_fallback_namespaces);
[email protected]9b26746e2009-08-06 21:35:45265}
266
[email protected]e2cadec82011-12-13 02:00:53267TEST(AppCacheManifestParserTest, FallbackUrlsWithPort) {
mlamouric9230312014-09-09 05:36:43268 AppCacheManifest manifest;
[email protected]9b26746e2009-08-06 21:35:45269 const GURL kUrl("http://www.portme.com:1234");
270 const std::string kData("CACHE MANIFEST\r"
271 "FALLBACK:\r"
272 "http://www.portme.com:1234/one relative/onefb\r"
273 "HTTP://www.portme.com:9876/wrong http://www.portme.com:1234/ignore\r"
274 "http://www.portme.com:1234/stillwrong http://www.portme.com:42/boo\r"
275 "relative/two relative/twofb\r"
276 "http://www.portme.com:1234/three HTTP://www.portme.com:1234/threefb\r"
277 "http://www.portme.com/noport http://www.portme.com:1234/skipped\r"
278 "http://www.portme.com:1234/skipme http://www.portme.com/noport\r");
279
[email protected]b993171f2014-05-13 06:11:15280 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
michaelnb4d77cd2017-06-08 20:42:52281 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
282 manifest));
[email protected]9b26746e2009-08-06 21:35:45283 EXPECT_TRUE(manifest.explicit_urls.empty());
[email protected]b160d6d2009-08-10 23:58:18284 EXPECT_TRUE(manifest.online_whitelist_namespaces.empty());
285 EXPECT_FALSE(manifest.online_whitelist_all);
[email protected]9b26746e2009-08-06 21:35:45286
Victor Costan3a412742018-08-24 21:32:23287 const std::vector<AppCacheNamespace>& fallbacks =
288 manifest.fallback_namespaces;
[email protected]9b26746e2009-08-06 21:35:45289 const size_t kExpected = 3;
290 ASSERT_EQ(kExpected, fallbacks.size());
[email protected]10f110fa2014-06-15 23:32:46291 EXPECT_EQ(APPCACHE_FALLBACK_NAMESPACE, fallbacks[0].type);
292 EXPECT_EQ(APPCACHE_FALLBACK_NAMESPACE, fallbacks[1].type);
293 EXPECT_EQ(APPCACHE_FALLBACK_NAMESPACE, fallbacks[2].type);
[email protected]9b26746e2009-08-06 21:35:45294 EXPECT_EQ(GURL("http://www.portme.com:1234/one"),
[email protected]e2cadec82011-12-13 02:00:53295 fallbacks[0].namespace_url);
[email protected]9b26746e2009-08-06 21:35:45296 EXPECT_EQ(GURL("http://www.portme.com:1234/relative/onefb"),
[email protected]e2cadec82011-12-13 02:00:53297 fallbacks[0].target_url);
[email protected]9b26746e2009-08-06 21:35:45298 EXPECT_EQ(GURL("http://www.portme.com:1234/relative/two"),
[email protected]e2cadec82011-12-13 02:00:53299 fallbacks[1].namespace_url);
[email protected]9b26746e2009-08-06 21:35:45300 EXPECT_EQ(GURL("http://www.portme.com:1234/relative/twofb"),
[email protected]e2cadec82011-12-13 02:00:53301 fallbacks[1].target_url);
[email protected]9b26746e2009-08-06 21:35:45302 EXPECT_EQ(GURL("http://www.portme.com:1234/three"),
[email protected]e2cadec82011-12-13 02:00:53303 fallbacks[2].namespace_url);
[email protected]9b26746e2009-08-06 21:35:45304 EXPECT_EQ(GURL("http://www.portme.com:1234/threefb"),
[email protected]e2cadec82011-12-13 02:00:53305 fallbacks[2].target_url);
[email protected]e2cadec82011-12-13 02:00:53306 EXPECT_TRUE(manifest.intercept_namespaces.empty());
michaelnb4d77cd2017-06-08 20:42:52307
308 // Nothing should be ignored since all namespaces are in scope.
309 manifest = AppCacheManifest();
310 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
311 PARSE_MANIFEST_PER_STANDARD, manifest));
312 EXPECT_FALSE(manifest.did_ignore_intercept_namespaces);
313 EXPECT_FALSE(manifest.did_ignore_fallback_namespaces);
[email protected]9b26746e2009-08-06 21:35:45314}
315
[email protected]e2cadec82011-12-13 02:00:53316TEST(AppCacheManifestParserTest, InterceptUrls) {
mlamouric9230312014-09-09 05:36:43317 AppCacheManifest manifest;
[email protected]e2cadec82011-12-13 02:00:53318 const GURL kUrl("http://www.portme.com:1234");
319 const std::string kData("CHROMIUM CACHE MANIFEST\r"
320 "CHROMIUM-INTERCEPT:\r"
321 "http://www.portme.com:1234/one return relative/int1\r"
322 "HTTP://www.portme.com:9/wrong return http://www.portme.com:1234/ignore\r"
323 "http://www.portme.com:1234/wrong return http://www.portme.com:9/boo\r"
324 "relative/two return relative/int2\r"
325 "relative/three wrong relative/threefb\r"
326 "http://www.portme.com:1234/three return HTTP://www.portme.com:1234/int3\r"
327 "http://www.portme.com/noport return http://www.portme.com:1234/skipped\r"
328 "http://www.portme.com:1234/skipme return http://www.portme.com/noport\r"
329 "relative/wrong/again missing/intercept_type\r");
330
[email protected]b993171f2014-05-13 06:11:15331 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
michaelnb4d77cd2017-06-08 20:42:52332 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
333 manifest));
[email protected]e2cadec82011-12-13 02:00:53334 EXPECT_TRUE(manifest.fallback_namespaces.empty());
335 EXPECT_TRUE(manifest.explicit_urls.empty());
336 EXPECT_TRUE(manifest.online_whitelist_namespaces.empty());
337 EXPECT_FALSE(manifest.online_whitelist_all);
michaelnb4d77cd2017-06-08 20:42:52338 EXPECT_FALSE(manifest.did_ignore_intercept_namespaces);
339 EXPECT_FALSE(manifest.did_ignore_fallback_namespaces);
[email protected]e2cadec82011-12-13 02:00:53340
Victor Costan3a412742018-08-24 21:32:23341 const std::vector<AppCacheNamespace>& intercepts =
342 manifest.intercept_namespaces;
[email protected]e2cadec82011-12-13 02:00:53343 const size_t kExpected = 3;
344 ASSERT_EQ(kExpected, intercepts.size());
[email protected]10f110fa2014-06-15 23:32:46345 EXPECT_EQ(APPCACHE_INTERCEPT_NAMESPACE, intercepts[0].type);
346 EXPECT_EQ(APPCACHE_INTERCEPT_NAMESPACE, intercepts[1].type);
347 EXPECT_EQ(APPCACHE_INTERCEPT_NAMESPACE, intercepts[2].type);
[email protected]e2cadec82011-12-13 02:00:53348 EXPECT_EQ(GURL("http://www.portme.com:1234/one"),
349 intercepts[0].namespace_url);
350 EXPECT_EQ(GURL("http://www.portme.com:1234/relative/int1"),
351 intercepts[0].target_url);
352 EXPECT_EQ(GURL("http://www.portme.com:1234/relative/two"),
353 intercepts[1].namespace_url);
354 EXPECT_EQ(GURL("http://www.portme.com:1234/relative/int2"),
355 intercepts[1].target_url);
356 EXPECT_EQ(GURL("http://www.portme.com:1234/three"),
357 intercepts[2].namespace_url);
358 EXPECT_EQ(GURL("http://www.portme.com:1234/int3"),
359 intercepts[2].target_url);
[email protected]b993171f2014-05-13 06:11:15360
michaelnb4d77cd2017-06-08 20:42:52361 // Disallow intercepts this time.
mlamouric9230312014-09-09 05:36:43362 manifest = AppCacheManifest();
[email protected]b993171f2014-05-13 06:11:15363 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
364 PARSE_MANIFEST_PER_STANDARD, manifest));
365 EXPECT_TRUE(manifest.fallback_namespaces.empty());
366 EXPECT_TRUE(manifest.explicit_urls.empty());
367 EXPECT_TRUE(manifest.online_whitelist_namespaces.empty());
368 EXPECT_TRUE(manifest.intercept_namespaces.empty());
369 EXPECT_FALSE(manifest.online_whitelist_all);
michaelnb4d77cd2017-06-08 20:42:52370 EXPECT_TRUE(manifest.did_ignore_intercept_namespaces);
371 EXPECT_FALSE(manifest.did_ignore_fallback_namespaces);
[email protected]e2cadec82011-12-13 02:00:53372}
373
374TEST(AppCacheManifestParserTest, ComboUrls) {
mlamouric9230312014-09-09 05:36:43375 AppCacheManifest manifest;
[email protected]9b26746e2009-08-06 21:35:45376 const GURL kUrl("http://combo.com:42");
377 const std::string kData("CACHE MANIFEST\r"
378 "relative/explicit-1\r"
379 "# some comment\r"
380 "http://combo.com:99/explicit-2#strip\r"
381 "NETWORK:\r"
382 "http://combo.com/whitelist-1\r"
383 "HTTP://www.diff.com/whitelist-2#strip\r"
[email protected]b160d6d2009-08-10 23:58:18384 "*\r"
[email protected]9b26746e2009-08-06 21:35:45385 "CACHE:\n\r"
386 "http://www.diff.com/explicit-3\r"
387 "FALLBACK:\r"
388 "http://combo.com:42/fallback-1 http://combo.com:42/fallback-1b\r"
389 "relative/fallback-2 relative/fallback-2b\r"
390 "UNKNOWN:\r\n"
391 "http://combo.com/ignoreme\r"
392 "relative/still-ignored\r"
393 "NETWORK:\r\n"
394 "relative/whitelist-3#strip\r"
395 "http://combo.com:99/whitelist-4\r");
[email protected]b993171f2014-05-13 06:11:15396 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
michaelnb4d77cd2017-06-08 20:42:52397 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
398 manifest));
[email protected]b160d6d2009-08-10 23:58:18399 EXPECT_TRUE(manifest.online_whitelist_all);
[email protected]9b26746e2009-08-06 21:35:45400
401 base::hash_set<std::string> urls = manifest.explicit_urls;
402 size_t expected = 3;
403 ASSERT_EQ(expected, urls.size());
404 EXPECT_TRUE(urls.find("http://combo.com:42/relative/explicit-1") !=
405 urls.end());
406 EXPECT_TRUE(urls.find("http://combo.com:99/explicit-2") != urls.end());
407 EXPECT_TRUE(urls.find("http://www.diff.com/explicit-3") != urls.end());
408
Victor Costan3a412742018-08-24 21:32:23409 const std::vector<AppCacheNamespace>& online =
410 manifest.online_whitelist_namespaces;
[email protected]9b26746e2009-08-06 21:35:45411 expected = 4;
412 ASSERT_EQ(expected, online.size());
[email protected]34e7dc62013-03-30 00:32:42413 EXPECT_EQ(GURL("http://combo.com/whitelist-1"),
414 online[0].namespace_url);
415 EXPECT_EQ(GURL("http://www.diff.com/whitelist-2"),
416 online[1].namespace_url);
417 EXPECT_EQ(GURL("http://combo.com:42/relative/whitelist-3"),
418 online[2].namespace_url);
419 EXPECT_EQ(GURL("http://combo.com:99/whitelist-4"),
420 online[3].namespace_url);
[email protected]9b26746e2009-08-06 21:35:45421
Victor Costan3a412742018-08-24 21:32:23422 const std::vector<AppCacheNamespace>& fallbacks =
423 manifest.fallback_namespaces;
[email protected]9b26746e2009-08-06 21:35:45424 expected = 2;
425 ASSERT_EQ(expected, fallbacks.size());
[email protected]10f110fa2014-06-15 23:32:46426 EXPECT_EQ(APPCACHE_FALLBACK_NAMESPACE, fallbacks[0].type);
427 EXPECT_EQ(APPCACHE_FALLBACK_NAMESPACE, fallbacks[1].type);
[email protected]9b26746e2009-08-06 21:35:45428 EXPECT_EQ(GURL("http://combo.com:42/fallback-1"),
[email protected]e2cadec82011-12-13 02:00:53429 fallbacks[0].namespace_url);
[email protected]9b26746e2009-08-06 21:35:45430 EXPECT_EQ(GURL("http://combo.com:42/fallback-1b"),
[email protected]e2cadec82011-12-13 02:00:53431 fallbacks[0].target_url);
[email protected]9b26746e2009-08-06 21:35:45432 EXPECT_EQ(GURL("http://combo.com:42/relative/fallback-2"),
[email protected]e2cadec82011-12-13 02:00:53433 fallbacks[1].namespace_url);
[email protected]9b26746e2009-08-06 21:35:45434 EXPECT_EQ(GURL("http://combo.com:42/relative/fallback-2b"),
[email protected]e2cadec82011-12-13 02:00:53435 fallbacks[1].target_url);
436
437 EXPECT_TRUE(manifest.intercept_namespaces.empty());
[email protected]9b26746e2009-08-06 21:35:45438}
439
[email protected]e2cadec82011-12-13 02:00:53440TEST(AppCacheManifestParserTest, UnusualUtf8) {
mlamouric9230312014-09-09 05:36:43441 AppCacheManifest manifest;
[email protected]9b26746e2009-08-06 21:35:45442 const GURL kUrl("http://bad.com");
443 const std::string kData("CACHE MANIFEST\r"
444 "\xC0" "invalidutf8\r"
445 "nonbmp" "\xF1\x84\xAB\xBC\r");
[email protected]b993171f2014-05-13 06:11:15446 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
michaelnb4d77cd2017-06-08 20:42:52447 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
448 manifest));
[email protected]9b26746e2009-08-06 21:35:45449 base::hash_set<std::string> urls = manifest.explicit_urls;
Victor Costan92940c92018-09-01 02:39:20450 EXPECT_TRUE(urls.find("http://bad.com/%EF%BF%BDinvalidutf8") != urls.end())
451 << "manifest byte stream was passed through, not UTF-8-decoded";
[email protected]9b26746e2009-08-06 21:35:45452 EXPECT_TRUE(urls.find("http://bad.com/nonbmp%F1%84%AB%BC") != urls.end());
453}
[email protected]97e3edc2009-09-15 22:07:15454
[email protected]e2cadec82011-12-13 02:00:53455TEST(AppCacheManifestParserTest, IgnoreAfterSpace) {
mlamouric9230312014-09-09 05:36:43456 AppCacheManifest manifest;
[email protected]578d7482009-11-16 20:11:07457 const GURL kUrl("http://smorg.borg");
458 const std::string kData(
459 "CACHE MANIFEST\r"
460 "resource.txt this stuff after the white space should be ignored\r");
[email protected]b993171f2014-05-13 06:11:15461 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
michaelnb4d77cd2017-06-08 20:42:52462 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
463 manifest));
[email protected]578d7482009-11-16 20:11:07464
465 base::hash_set<std::string> urls = manifest.explicit_urls;
466 EXPECT_TRUE(urls.find("http://smorg.borg/resource.txt") != urls.end());
467}
468
[email protected]e2cadec82011-12-13 02:00:53469TEST(AppCacheManifestParserTest, DifferentOriginUrlWithSecureScheme) {
mlamouric9230312014-09-09 05:36:43470 AppCacheManifest manifest;
[email protected]a51fb7a2010-03-30 23:52:47471 const GURL kUrl("https://www.foo.com");
472 const std::string kData("CACHE MANIFEST\r"
473 "CACHE: \r"
474 "relative/secureschemesameorigin\r"
475 "https://www.foo.com/secureschemesameorigin\r"
476 "http://www.xyz.com/secureschemedifforigin\r"
477 "https://www.xyz.com/secureschemedifforigin\r");
478
[email protected]b993171f2014-05-13 06:11:15479 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
michaelnb4d77cd2017-06-08 20:42:52480 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
481 manifest));
[email protected]a51fb7a2010-03-30 23:52:47482 EXPECT_TRUE(manifest.fallback_namespaces.empty());
483 EXPECT_TRUE(manifest.online_whitelist_namespaces.empty());
484
485 base::hash_set<std::string> urls = manifest.explicit_urls;
[email protected]eb2ee142011-02-22 20:03:01486 const size_t kExpected = 3;
[email protected]a51fb7a2010-03-30 23:52:47487 ASSERT_EQ(kExpected, urls.size());
488 EXPECT_TRUE(urls.find("https://www.foo.com/relative/secureschemesameorigin")
489 != urls.end());
490 EXPECT_TRUE(urls.find("https://www.foo.com/secureschemesameorigin") !=
491 urls.end());
492 EXPECT_FALSE(urls.find("http://www.xyz.com/secureschemedifforigin") !=
493 urls.end());
[email protected]eb2ee142011-02-22 20:03:01494 EXPECT_TRUE(urls.find("https://www.xyz.com/secureschemedifforigin") !=
[email protected]a51fb7a2010-03-30 23:52:47495 urls.end());
496}
497
[email protected]34e7dc62013-03-30 00:32:42498TEST(AppCacheManifestParserTest, PatternMatching) {
499 const GURL kUrl("http://foo.com/manifest");
500 const std::string kManifestBody(
501 "CACHE MANIFEST\r"
502 "CACHE: \r"
503 "http://foo.com/page.html\r"
504 "CHROMIUM-INTERCEPT:\r"
505 "http://foo.com/intercept_prefix return /prefix\r"
506 "http://foo.com/intercept_pattern return /pattern isPattern\r"
507 "http://foo.com/*/intercept_pattern?query return /pattern isPattern\r"
508 "FALLBACK:\r"
509 "http://foo.com/fallback_prefix /prefix wrongAnnotation\r"
510 "http://foo.com/fallback_pattern* /pattern\tisPattern \r"
511 "NETWORK:\r"
512 "*\r"
513 "isPattern\r" // should not be interpretted as a pattern
514 "http://foo.com/network_pattern* isPattern\r");
515
516
mlamouric9230312014-09-09 05:36:43517 AppCacheManifest manifest;
michaelnb4d77cd2017-06-08 20:42:52518 EXPECT_TRUE(ParseManifest(kUrl, kManifestBody.c_str(), kManifestBody.length(),
519 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
520 manifest));
[email protected]34e7dc62013-03-30 00:32:42521 EXPECT_TRUE(manifest.online_whitelist_all);
michaelnb4d77cd2017-06-08 20:42:52522 EXPECT_FALSE(manifest.did_ignore_intercept_namespaces);
523 EXPECT_FALSE(manifest.did_ignore_fallback_namespaces);
[email protected]34e7dc62013-03-30 00:32:42524 EXPECT_EQ(1u, manifest.explicit_urls.size());
525 EXPECT_EQ(3u, manifest.intercept_namespaces.size());
526 EXPECT_EQ(2u, manifest.fallback_namespaces.size());
527 EXPECT_EQ(2u, manifest.online_whitelist_namespaces.size());
[email protected]10f110fa2014-06-15 23:32:46528 EXPECT_EQ(APPCACHE_INTERCEPT_NAMESPACE,
529 manifest.intercept_namespaces[0].type);
530 EXPECT_EQ(APPCACHE_FALLBACK_NAMESPACE, manifest.fallback_namespaces[0].type);
531 EXPECT_EQ(APPCACHE_NETWORK_NAMESPACE,
532 manifest.online_whitelist_namespaces[0].type);
[email protected]34e7dc62013-03-30 00:32:42533 EXPECT_FALSE(manifest.intercept_namespaces[0].is_pattern);
534 EXPECT_TRUE(manifest.intercept_namespaces[1].is_pattern);
535 EXPECT_TRUE(manifest.intercept_namespaces[2].is_pattern);
536 EXPECT_FALSE(manifest.fallback_namespaces[0].is_pattern);
537 EXPECT_TRUE(manifest.fallback_namespaces[1].is_pattern);
538 EXPECT_FALSE(manifest.online_whitelist_namespaces[0].is_pattern);
539 EXPECT_TRUE(manifest.online_whitelist_namespaces[1].is_pattern);
540 EXPECT_EQ(
541 GURL("http://foo.com/*/intercept_pattern?query"),
542 manifest.intercept_namespaces[2].namespace_url);
543 EXPECT_EQ(
544 GURL("http://foo.com/pattern"),
545 manifest.intercept_namespaces[2].target_url);
546 EXPECT_EQ(
547 GURL("http://foo.com/fallback_pattern*"),
548 manifest.fallback_namespaces[1].namespace_url);
549 EXPECT_EQ(
550 GURL("http://foo.com/pattern"),
551 manifest.fallback_namespaces[1].target_url);
552 EXPECT_EQ(
553 GURL("http://foo.com/isPattern"),
554 manifest.online_whitelist_namespaces[0].namespace_url);
555 EXPECT_EQ(
556 GURL(),
557 manifest.online_whitelist_namespaces[0].target_url);
558 EXPECT_EQ(
559 GURL("http://foo.com/network_pattern*"),
560 manifest.online_whitelist_namespaces[1].namespace_url);
561 EXPECT_EQ(
562 GURL(),
563 manifest.online_whitelist_namespaces[1].target_url);
564}
565
michaelnb4d77cd2017-06-08 20:42:52566TEST(AppCacheManifestParserTest, IgnoreDangerousFallbacks) {
567 const GURL kUrl("http://foo.com/scope/manifest?with_query_args");
568 const std::string kData(
569 "CACHE MANIFEST\r"
570 "FALLBACK:\r"
571 "http://foo.com/scope/ fallback_url\r"
572 "http://foo.com/out_of_scope/ fallback_url\r");
573
574 // Scope matching depends on resolving "." as a relative url.
Giovanni Ortuño Urquidi61b24eda2017-08-09 08:13:10575 EXPECT_EQ(kUrl.GetWithoutFilename().spec(),
576 std::string("http://foo.com/scope/"));
michaelnb4d77cd2017-06-08 20:42:52577
578 AppCacheManifest manifest;
579 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
580 PARSE_MANIFEST_ALLOWING_DANGEROUS_FEATURES,
581 manifest));
582 EXPECT_FALSE(manifest.did_ignore_fallback_namespaces);
583 EXPECT_EQ(2u, manifest.fallback_namespaces.size());
584
585 manifest = AppCacheManifest();
586 EXPECT_TRUE(ParseManifest(kUrl, kData.c_str(), kData.length(),
587 PARSE_MANIFEST_PER_STANDARD, manifest));
588 EXPECT_TRUE(manifest.did_ignore_fallback_namespaces);
589 EXPECT_EQ(1u, manifest.fallback_namespaces.size());
590 EXPECT_EQ(GURL("http://foo.com/scope/"),
591 manifest.fallback_namespaces[0].namespace_url);
592}
593
[email protected]a507b822014-04-12 05:10:24594} // namespace content