File: ApplicationManifestParser.cpp

package info (click to toggle)
wpewebkit 2.38.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 311,508 kB
  • sloc: cpp: 2,653,313; javascript: 289,013; ansic: 121,268; xml: 64,149; python: 35,534; ruby: 17,287; perl: 15,877; asm: 11,072; yacc: 2,326; sh: 1,863; lex: 1,319; java: 937; makefile: 146; pascal: 60
file content (398 lines) | stat: -rw-r--r-- 16,937 bytes parent folder | download | duplicates (2)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * Copyright (C) 2017-2021 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#if ENABLE(APPLICATION_MANIFEST)

#include <JavaScriptCore/InitializeThreading.h>
#include <WebCore/ApplicationManifestParser.h>
#include <wtf/RunLoop.h>

using namespace WebCore;

namespace WebCore {
static inline std::ostream& operator<<(std::ostream& os, const ApplicationManifest::Display& display)
{
    switch (display) {
    case ApplicationManifest::Display::Browser:
        return os << "ApplicationManifest::Display::Browser";
    case ApplicationManifest::Display::MinimalUI:
        return os << "ApplicationManifest::Display::MinimalUI";
    case ApplicationManifest::Display::Standalone:
        return os << "ApplicationManifest::Display::Standalone";
    case ApplicationManifest::Display::Fullscreen:
        return os << "ApplicationManifest::Display::Fullscreen";
    }
}
} // namespace WebCore

class ApplicationManifestParserTest : public testing::Test {
public:
    URL m_manifestURL;
    URL m_documentURL;

    virtual void SetUp()
    {
        JSC::initialize();
        WTF::initializeMainThread();

        m_manifestURL = URL { "https://example.com/manifest.json"_s };
        m_documentURL = URL { "https://example.com/"_s };
    }

    ApplicationManifest parseString(const String& data)
    {
        return ApplicationManifestParser::parse(data, m_manifestURL, m_documentURL);
    }

    ApplicationManifest parseTopLevelProperty(const String& key, const String& value)
    {
        String manifestContent = "{ \"" + key + "\" : " + value + " }";
        return parseString(manifestContent);
    }

    ApplicationManifest parseIconFirstTopLevelProperty(const String& key, const String& value)
    {
        String manifestContent = "{ \"icons\": [{\"" + key + "\": " + value + ", \"src\": \"icon/example.png\" }]}";
        return parseString(manifestContent);
    }

    ApplicationManifest parseIconFirstTopLevelPropertyForSrc(const String& key, const String& value)
    {
        String manifestContent = "{ \"icons\": [{\"" + key + "\": " + value + " }]}";
        return parseString(manifestContent);
    }

    void testStartURL(const String& rawJSON, const String& expectedValue)
    {
        testStartURL(rawJSON, { { }, expectedValue });
    }

    void testStartURL(const String& rawJSON, const URL& expectedValue)
    {
        auto manifest = parseTopLevelProperty("start_url"_s, rawJSON);
        auto value = manifest.startURL;
        EXPECT_STREQ(expectedValue.string().utf8().data(), value.string().utf8().data());
    }

    void testDisplay(const String& rawJSON, ApplicationManifest::Display expectedValue)
    {
        auto manifest = parseTopLevelProperty("display"_s, rawJSON);
        auto value = manifest.display;
        EXPECT_EQ(expectedValue, value);
    }

    void testName(const String& rawJSON, const String& expectedValue)
    {
        auto manifest = parseTopLevelProperty("name"_s, rawJSON);
        auto value = manifest.name;
        EXPECT_STREQ(expectedValue.utf8().data(), value.utf8().data());
    }

    void testDescription(const String& rawJSON, const String& expectedValue)
    {
        auto manifest = parseTopLevelProperty("description"_s, rawJSON);
        auto value = manifest.description;
        EXPECT_STREQ(expectedValue.utf8().data(), value.utf8().data());
    }

    void testShortName(const String& rawJSON, const String& expectedValue)
    {
        auto manifest = parseTopLevelProperty("short_name"_s, rawJSON);
        auto value = manifest.shortName;
        EXPECT_STREQ(expectedValue.utf8().data(), value.utf8().data());
    }

    void testScope(const String& rawJSON, const String& startURL, const String& expectedValue)
    {
        String manifestContent = "{ \"scope\" : " + rawJSON + ", \"start_url\" : \"" + startURL + "\" }";
        auto manifest = parseString(manifestContent);
        auto value = manifest.scope;
        EXPECT_STREQ(expectedValue.utf8().data(), value.string().utf8().data());
    }

    void testScope(const String& rawJSON, const String& expectedValue)
    {
        testScope(rawJSON, String(), expectedValue);
    }

    void testThemeColor(const String& rawJSON, const Color& expectedValue)
    {
        auto manifest = parseTopLevelProperty("theme_color"_s, rawJSON);
        auto value = manifest.themeColor;
        EXPECT_EQ(expectedValue, value);
    }

    void testIconsSrc(const String& rawJSON, const URL& expectedValue)
    {
        auto manifest = parseIconFirstTopLevelPropertyForSrc("src"_s, rawJSON);
        auto value = manifest.icons[0].src;
        EXPECT_STREQ(expectedValue.string().utf8().data(), value.string().utf8().data());
    }

    void testIconsType(const String &rawJSON, const String& expectedValue)
    {
        auto manifest = parseIconFirstTopLevelProperty("type"_s, rawJSON);
        auto value = manifest.icons[0].type;
        EXPECT_STREQ(expectedValue.utf8().data(), value.utf8().data());
    }

    void testIconsSizes(const String &rawJSON, size_t expectedCount, size_t testIndex, const String& expectedValue)
    {
        auto manifest = parseIconFirstTopLevelProperty("sizes"_s, rawJSON);
        auto value = manifest.icons[0].sizes;
        EXPECT_EQ(expectedCount, value.size());
        EXPECT_TRUE(testIndex < value.size());
        EXPECT_STREQ(expectedValue.utf8().data(), value[testIndex].utf8().data());
    }

    void testIconsPurposes(const String &rawJSON, OptionSet<ApplicationManifest::Icon::Purpose> expectedValues)
    {
        auto manifest = parseIconFirstTopLevelProperty("purpose"_s, rawJSON);
        auto value = manifest.icons[0].purposes;
        EXPECT_EQ(expectedValues, value);
    }

};

static void assertManifestHasDefaultValues(const URL& manifestURL, const URL& documentURL, const ApplicationManifest& manifest)
{
    EXPECT_TRUE(manifest.name.isNull());
    EXPECT_TRUE(manifest.shortName.isNull());
    EXPECT_TRUE(manifest.description.isNull());
    EXPECT_STREQ("https://example.com/", manifest.scope.string().utf8().data());
    EXPECT_STREQ(documentURL.string().utf8().data(), manifest.startURL.string().utf8().data());
}

TEST_F(ApplicationManifestParserTest, DefaultManifest)
{
    assertManifestHasDefaultValues(m_manifestURL, m_documentURL, parseString(String()));
    assertManifestHasDefaultValues(m_manifestURL, m_documentURL, parseString(""_s));
    assertManifestHasDefaultValues(m_manifestURL, m_documentURL, parseString("{ }"_s));
    assertManifestHasDefaultValues(m_manifestURL, m_documentURL, parseString("This is 100% not JSON."_s));
}

TEST_F(ApplicationManifestParserTest, StartURL)
{
    m_documentURL = URL { "https://example.com/home"_s };
    m_manifestURL = URL { "https://example.com/manifest.json"_s };

    testStartURL("123"_s, m_documentURL);
    testStartURL("null"_s, m_documentURL);
    testStartURL("true"_s, m_documentURL);
    testStartURL("{ }"_s, m_documentURL);
    testStartURL("[ ]"_s, m_documentURL);
    testStartURL("[ \"http://example.com/somepage\" ]"_s, m_documentURL);
    testStartURL("\"\""_s, m_documentURL);
    testStartURL("\"http:?\""_s, m_documentURL);

    testStartURL("\"appstartpage\""_s, "https://example.com/appstartpage"_s);
    testStartURL("\"a/b/cdefg\""_s, "https://example.com/a/b/cdefg"_s);

    m_documentURL = URL { "https://example.com/subfolder/home"_s };
    m_manifestURL = URL { "https://example.com/resources/manifest.json"_s };

    testStartURL("\"resource-relative-to-manifest-url\""_s, "https://example.com/resources/resource-relative-to-manifest-url"_s);
    testStartURL("\"http://different-page.com/12/34\""_s, m_documentURL);

    m_documentURL = URL { "https://example.com/home"_s };
    m_manifestURL = URL { "https://other-domain.com/manifiest.json"_s };

    testStartURL("\"resource_on_other_domain\""_s, m_documentURL);
    testStartURL("\"http://example.com/scheme-does-not-match-document\""_s, m_documentURL);
    testStartURL("\"https://example.com:123/port-does-not-match-document"_s, m_documentURL);
    testStartURL("\"https://example.com/page2\""_s, "https://example.com/page2"_s);
    testStartURL("\"//example.com/page2\""_s, "https://example.com/page2"_s);

    m_documentURL = URL { "https://example.com/a"_s };
    m_manifestURL = URL { "https://example.com/z/manifest.json"_s };

    testStartURL("\"b/c\""_s, "https://example.com/z/b/c"_s);
    testStartURL("\"/b/c\""_s, "https://example.com/b/c"_s);
    testStartURL("\"?query\""_s, "https://example.com/z/manifest.json?query"_s);

    m_documentURL = URL { "https://example.com/dir1/dir2/page1"_s };
    m_manifestURL = URL { "https://example.com/dir3/manifest.json"_s };

    testStartURL("\"../page2\""_s, "https://example.com/page2"_s);
}

TEST_F(ApplicationManifestParserTest, Display)
{
    testDisplay("123"_s, ApplicationManifest::Display::Browser);
    testDisplay("null"_s, ApplicationManifest::Display::Browser);
    testDisplay("true"_s, ApplicationManifest::Display::Browser);
    testDisplay("{ }"_s, ApplicationManifest::Display::Browser);
    testDisplay("[ ]"_s, ApplicationManifest::Display::Browser);
    testDisplay("\"\""_s, ApplicationManifest::Display::Browser);
    testDisplay("\"garbage string\""_s, ApplicationManifest::Display::Browser);

    testDisplay("\"browser\""_s, ApplicationManifest::Display::Browser);
    testDisplay("\"standalone\""_s, ApplicationManifest::Display::Standalone);
    testDisplay("\"minimal-ui\""_s, ApplicationManifest::Display::MinimalUI);
    testDisplay("\"fullscreen\""_s, ApplicationManifest::Display::Fullscreen);
    testDisplay("\"\t\nMINIMAL-UI \""_s, ApplicationManifest::Display::MinimalUI);
}

TEST_F(ApplicationManifestParserTest, Name)
{
    testName("123"_s, String());
    testName("null"_s, String());
    testName("true"_s, String());
    testName("{ }"_s, String());
    testName("[ ]"_s, String());
    testName("\"\""_s, emptyString());
    testName("\"example\""_s, "example"_s);
    testName("\"\\t Hello\\nWorld\\t \""_s, "Hello\nWorld"_s);
}

TEST_F(ApplicationManifestParserTest, Description)
{
    testDescription("123"_s, String());
    testDescription("null"_s, String());
    testDescription("true"_s, String());
    testDescription("{ }"_s, String());
    testDescription("[ ]"_s, String());
    testDescription("\"\""_s, emptyString());
    testDescription("\"example\""_s, "example"_s);
    testDescription("\"\\t Hello\\nWorld\\t \""_s, "Hello\nWorld"_s);
}

TEST_F(ApplicationManifestParserTest, ShortName)
{
    testShortName("123"_s, String());
    testShortName("null"_s, String());
    testShortName("true"_s, String());
    testShortName("{ }"_s, String());
    testShortName("[ ]"_s, String());
    testShortName("\"\""_s, ""_s);
    testShortName("\"example\""_s, "example"_s);
    testShortName("\"\\t Hello\\nWorld\\t \""_s, "Hello\nWorld"_s);
}

TEST_F(ApplicationManifestParserTest, Scope)
{
    // If the scope is not a string or not a valid URL, return the default scope (the parent path of the start URL).
    m_documentURL = URL { "https://example.com/a/page?queryParam=value#fragment"_s };
    m_manifestURL = URL { "https://example.com/manifest.json"_s };
    testScope("123"_s, "https://example.com/a/"_s);
    testScope("null"_s, "https://example.com/a/"_s);
    testScope("true"_s, "https://example.com/a/"_s);
    testScope("{ }"_s, "https://example.com/a/"_s);
    testScope("[ ]"_s, "https://example.com/a/"_s);
    testScope("\"\""_s, "https://example.com/a/"_s);
    testScope("\"http:?\""_s, "https://example.com/a/"_s);

    m_documentURL = URL { "https://example.com/a/pageEndingWithSlash/"_s };
    testScope("null"_s, "https://example.com/a/pageEndingWithSlash/"_s);

    // If scope URL is not same origin as document URL, return the default scope.
    m_documentURL = URL { "https://example.com/home"_s };
    m_manifestURL = URL { "https://other-site.com/manifest.json"_s };
    testScope("\"https://other-site.com/some-scope\""_s, "https://example.com/"_s);

    m_documentURL = URL { "https://example.com/app/home"_s };
    m_manifestURL = URL { "https://example.com/app/manifest.json"_s };

    // If start URL is not within scope of scope URL, return the default scope.
    testScope("\"https://example.com/subdirectory\""_s, "https://example.com/app/"_s);
    testScope("\"https://example.com/app\""_s, "https://example.com/app"_s);
    testScope("\"https://example.com/APP\""_s, "https://example.com/app/"_s);
    testScope("\"https://example.com/a\""_s, "https://example.com/a"_s);

    m_documentURL = URL { "https://example.com/a/b/c/index"_s };
    m_manifestURL = URL { "https://example.com/a/manifest.json"_s };

    testScope("\"./b/c/index\""_s, "https://example.com/a/b/c/index"_s);
    testScope("\"b/somewhere-else/../c\""_s, "https://example.com/a/b/c"_s);
    testScope("\"b\""_s, "https://example.com/a/b"_s);
    testScope("\"b/\""_s, "https://example.com/a/b/"_s);

    m_documentURL = URL { "https://example.com/documents/home"_s };
    m_manifestURL = URL { "https://example.com/resources/manifest.json"_s };

    // It's fine if the document URL or manifest URL aren't within the application scope - only the start URL needs to be.
    testScope("\"https://example.com/other\""_s, "https://example.com/other/start-url"_s, "https://example.com/other"_s);
}

TEST_F(ApplicationManifestParserTest, ThemeColor)
{
    testThemeColor("123"_s, Color());
    testThemeColor("null"_s, Color());
    testThemeColor("true"_s, Color());
    testThemeColor("{ }"_s, Color());
    testThemeColor("[ ]"_s, Color());
    testThemeColor("\"\""_s, Color());
    testThemeColor("\"garbage string\""_s, Color());

    testThemeColor("\"red\""_s, Color::red);
    testThemeColor("\"#f00\""_s, Color::red);
    testThemeColor("\"#ff0000\""_s, Color::red);
    testThemeColor("\"#ff0000ff\""_s, Color::red);
    testThemeColor("\"rgb(255, 0, 0)\""_s, Color::red);
    testThemeColor("\"rgba(255, 0, 0, 1)\""_s, Color::red);
    testThemeColor("\"hsl(0, 100%, 50%)\""_s, Color::red);
    testThemeColor("\"hsla(0, 100%, 50%, 1)\""_s, Color::red);
}

TEST_F(ApplicationManifestParserTest, Whitespace)
{
    auto manifest = parseString("  { \"name\": \"PASS\" }\n"_s);

    EXPECT_STREQ("PASS", manifest.name.utf8().data());
}

TEST_F(ApplicationManifestParserTest, Icons)
{
    URL srcURL = URL { "https://example.com/icon.jpg"_s };
    testIconsSrc("\"icon.jpg\""_s, srcURL);
    testIconsType("\"image/webp\""_s, "image/webp"_s);
    testIconsSizes("\"256x256\""_s, 1, 0, "256x256"_s);
    testIconsSizes("\"72x72 96x96\""_s, 2, 0, "72x72"_s);
    testIconsSizes("\"72x72 96x96\""_s, 2, 1, "96x96"_s);

    OptionSet<ApplicationManifest::Icon::Purpose> purposeAny { ApplicationManifest::Icon::Purpose::Any };
    OptionSet<ApplicationManifest::Icon::Purpose> purposeMonochrome { ApplicationManifest::Icon::Purpose::Monochrome };
    OptionSet<ApplicationManifest::Icon::Purpose> purposeMaskable { ApplicationManifest::Icon::Purpose::Maskable };

    testIconsPurposes("\"monochrome\""_s, purposeMonochrome);
    testIconsPurposes("\"maskable\""_s, purposeMaskable);
    testIconsPurposes("\"any\""_s, purposeAny);
    testIconsPurposes("\"\tMONOCHROME\""_s, purposeMonochrome);

    testIconsPurposes("123"_s, purposeAny);
    testIconsPurposes("null"_s, purposeAny);
    testIconsPurposes("true"_s, purposeAny);
    testIconsPurposes("{ }"_s, purposeAny);
    testIconsPurposes("[ ]"_s, purposeAny);

    OptionSet<ApplicationManifest::Icon::Purpose> purposeMonochromeAny { ApplicationManifest::Icon::Purpose::Monochrome, ApplicationManifest::Icon::Purpose::Any };

    testIconsPurposes("\"monochrome any\""_s, purposeMonochromeAny);

}

#endif