Repository navigation
Expand file tree
/
Copy pathModuleIdentityTest.cpp
More file actions
446 lines (413 loc) · 17.3 KB
/
Copy pathModuleIdentityTest.cpp
File metadata and controls
446 lines (413 loc) · 17.3 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <cstdio>
#include <optional>
#include <string>
#include <vector>
#include "FakePlatform.h"
#include "ns/LoaderConfig.h"
#include "ns/loader/ModuleIdentity.h"
using namespace nativescript::common;
using namespace nativescript::common::loader;
namespace {
int failures = 0;
void Check(bool ok, const std::string& what) {
if (!ok) {
failures++;
std::fprintf(stderr, "FAIL %s\n", what.c_str());
}
}
void Expect(const std::string& actual, const std::string& expected,
const std::string& what) {
if (actual != expected) {
failures++;
std::fprintf(stderr, "FAIL %s\n expected: %s\n actual: %s\n",
what.c_str(), expected.c_str(), actual.c_str());
}
}
void Expect(const std::optional<std::string>& actual,
const std::optional<std::string>& expected,
const std::string& what) {
Expect(actual.value_or("<nullopt>"), expected.value_or("<nullopt>"), what);
}
// Each runtime's legacy override: iOS reports app files under file:///app,
// the app root's path below the bundle; Android under file://<appRoot>.
LoaderConfig IosLegacy() {
LoaderConfig config;
config.appRoot = "/b/App.app/app";
config.appRootOrigin = "file:///app";
config.legacyOrigins = true;
config.virtualFileRoots = {"/app"};
config.appRootAliases = {{"/private/var", "/var"}};
return config;
}
LoaderConfig AndroidLegacy() {
LoaderConfig config;
config.appRoot = "/data/user/0/pkg/files/app";
config.appRootOrigin = "file:///data/user/0/pkg/files/app";
config.legacyOrigins = true;
config.virtualFileRoots = {"/app", "/android_asset/app"};
config.appRootAliases = {{"/data/data/pkg", "/data/user/0/pkg"}};
return config;
}
LoaderConfig AppScheme() {
LoaderConfig config;
config.appRoot = "/root/app";
config.virtualFileRoots = {"/app"};
return config;
}
void TestNormalizePath() {
const std::vector<std::pair<std::string, std::string>> cases = {
{"", ""},
{"/", "/"},
{"//a//b/", "/a/b"},
{"/a/./b/../c", "/a/c"},
{"/../a", "/a"},
{"/a/b/../../..", "/"},
{"/a/b/..", "/a"},
{"/a/.../b", "/a/.../b"},
{"/a/b?c/../d", "/a/d"},
{"a/../../b", "../b"},
{"./a", "a"},
{"../../a", "../../a"},
};
for (const auto& [input, expected] : cases) {
Expect(NormalizePath(input), expected, "NormalizePath(" + input + ")");
}
const LoaderConfig ios = IosLegacy();
Expect(CanonicalFilePath("/private/var/x/../y", ios), "/var/y",
"an alias moves onto appRoot's spelling");
Expect(CanonicalFilePath("/private/var", ios), "/var", "the alias itself");
Expect(CanonicalFilePath("/private/varx/y", ios), "/private/varx/y",
"an alias matches whole segments only");
}
void TestSchemesAndQueries() {
Check(IsHttpRegistryKey("http://h/x"), "http key");
Check(IsHttpRegistryKey("https://h/x"), "https key");
Check(!IsHttpRegistryKey("HTTP://h/x"), "uppercase is not a key spelling");
Check(!IsHttpRegistryKey("httpx://h"), "httpx");
Check(HasUrlScheme("node:fs"), "node:");
Check(HasUrlScheme("blob:nativescript/x"), "blob:");
Check(!HasUrlScheme("/a:b"), "a colon after a slash");
Check(!HasUrlScheme(":x"), "an empty scheme");
Check(!HasUrlScheme("a b:c"), "a space in the scheme");
const std::vector<std::pair<std::string, std::string>> queries = {
{"./x.mjs", ""},
{"./x.mjs?v=1", "?v=1"},
{"./x.mjs?", "?"},
{"./x.mjs?v=1#f", "?v=1"},
{"./x.mjs#a?b", ""},
{"/x?a/../b", "?a/../b"},
{"file:///x.mjs?v=1", "?v=1"},
// The parser rejects a file URL with a port; the written query still
// keeps two imports apart (Q4).
{"//host:9/x.mjs?v=1#f", "?v=1"},
{"//host:9/x.mjs#f?v=1", ""},
{"./x.mjs?t=1700000000000&a=%41/b?c", "?t=1700000000000&a=%41/b?c"},
{"./x.mjs?a'b", "?a%27b"},
{"./x.mjs?a b<c>\"d", "?a%20b%3Cc%3E%22d"},
{"./x.mjs?caf\xC3\xA9", "?caf%C3%A9"},
{"./x.mjs?a\tb", "?ab"},
};
for (const auto& [input, expected] : queries) {
Expect(ModuleQuery(input), expected, "ModuleQuery(" + input + ")");
}
Expect(std::string(KeyPath("/a/b.mjs?v=1?w")), "/a/b.mjs", "KeyPath");
Expect(std::string(KeyPath("/a/b.mjs")), "/a/b.mjs", "KeyPath, no query");
}
void TestCanonicalizeModuleKey() {
const LoaderConfig ios = IosLegacy();
const LoaderConfig android = AndroidLegacy();
struct Case {
const LoaderConfig* config;
std::string key;
std::string expected;
};
const std::vector<Case> cases = {
{&ios, "http://H/x.mjs#f", "http://h/x.mjs"},
{&ios, "/b/App.app/app/./x.mjs?v=1/../y",
"/b/App.app/app/x.mjs?v=1/../y"},
{&ios, "/private/var/c/x.mjs", "/var/c/x.mjs"},
{&ios, "blob:nativescript/abc", "blob:nativescript/abc"},
{&ios, "node:fs", "node:fs"},
{&ios, "ns:module", "ns:module"},
{&ios, ":x", ":x"},
{&ios, "a_b:c/d", "a_b:c/d"},
// A2: the URL parser reads file: keys, so '#' starts the fragment and
// escapes are decoded.
// Q18: a virtual root names the app root, as it does in
// the resolver.
{&ios, "file:///app/x.mjs?v=1#h", "/b/App.app/app/x.mjs?v=1"},
{&ios, "file:///app/a%20b.mjs", "/b/App.app/app/a b.mjs"},
{&ios, "file:///app/a#b/x.mjs", "/b/App.app/app/a"},
// A3: an absolute file: path names that file.
{&ios, "file:///etc/x.mjs", "/etc/x.mjs"},
// Q3: a host names no path, so the key stays verbatim.
{&ios, "file://host/x.mjs", "file://host/x.mjs"},
// A10: app:/// always names the app root.
{&ios, "app:///sub/x.mjs?v=1", "/b/App.app/app/sub/x.mjs?v=1"},
// The resolver's scheme test: no "//" needed, and any case.
{&ios, "file:/etc/x.mjs", "/etc/x.mjs"},
{&ios, "FILE:///etc/x.mjs", "/etc/x.mjs"},
{&ios, "app:/sub/x.mjs", "/b/App.app/app/sub/x.mjs"},
{&ios, "App:///sub/x.mjs", "/b/App.app/app/sub/x.mjs"},
// A path's query is the query the resolver keys it with.
{&ios, "/etc/x.mjs?a b", "/etc/x.mjs?a%20b"},
{&ios, "/etc/x.mjs?v=1#f", "/etc/x.mjs?v=1"},
{&ios, "/etc/h#.mjs?v=1", "/etc/h#.mjs?v=1"},
{&android, "file:///android_asset/app/x.mjs",
"/data/user/0/pkg/files/app/x.mjs"},
{&android, "file:///app/x.mjs", "/data/user/0/pkg/files/app/x.mjs"},
{&android, "file:///data/data/pkg/files/app/x.mjs",
"/data/user/0/pkg/files/app/x.mjs"},
{&android, "/data/data/pkg/files/app/x.mjs?v",
"/data/user/0/pkg/files/app/x.mjs?v"},
{&android, "", ""},
};
for (const Case& c : cases) {
Expect(CanonicalizeModuleKey(c.key, *c.config, nullptr), c.expected,
"CanonicalizeModuleKey(" + c.key + ")");
}
fake_platform::ClearMessages();
CanonicalizeModuleKey("node:fs", ios, nullptr);
const std::vector<std::string> traced = fake_platform::TracedMessages();
Check(
traced.size() == 1 &&
traced[0] ==
"[resolver][registry-key][custom-scheme] raw=node:fs key=node:fs",
"a custom scheme is traced even unchanged");
}
void TestLegacyOrigins() {
const LoaderConfig ios = IosLegacy();
const LoaderConfig android = AndroidLegacy();
struct Case {
const LoaderConfig* config;
std::string key;
std::string expected;
};
const std::vector<Case> cases = {
{&ios, "/b/App.app/app/x.mjs", "file:///app/x.mjs"},
{&ios, "/b/App.app/app/sub/a b#c%41.mjs?v=1",
"file:///app/sub/a b#c%41.mjs?v=1"},
{&ios, "/etc/x.mjs", "file:///etc/x.mjs"},
// A10, Q5: a bundle file outside the app root gets its absolute path.
{&ios, "/b/App.app/other.js", "file:///b/App.app/other.js"},
{&ios, "", "file:///app/"},
{&ios, "http://h/x.mjs", "http://h/x.mjs"},
{&ios, "node:fs", "node:fs"},
{&ios, "blob:nativescript/abc", "blob:nativescript/abc"},
{&android, "/data/user/0/pkg/files/app/x.mjs?v=1",
"file:///data/user/0/pkg/files/app/x.mjs?v=1"},
{&android, "/sdcard/a b.mjs", "file:///sdcard/a b.mjs"},
{&android, "", "file:///app/"},
};
for (const Case& c : cases) {
Expect(ReportedModuleUrl(c.key, *c.config), c.expected,
"ReportedModuleUrl(" + c.key + ")");
}
Expect(ModuleOrigin("/b/App.app/app/x.mjs", "?v", ios), "file:///app/x.mjs?v",
"ModuleOrigin");
}
void TestLegacyImportMeta() {
const LoaderConfig ios = IosLegacy();
const LoaderConfig android = AndroidLegacy();
ImportMetaStrings meta =
ImportMetaFor("/b/App.app/app/sub/x.mjs?v=1", "", ios);
Expect(meta.url, "file:///app/sub/x.mjs?v=1", "iOS url");
Expect(meta.dirname, std::string("/app/sub"), "iOS dirname is app-relative");
Check(!meta.filename, "no filename in legacy mode");
meta = ImportMetaFor("/b/App.app/app/x.mjs", "", ios);
Expect(meta.dirname, std::string("/app"), "iOS dirname at the app root");
meta = ImportMetaFor("/b/App.app/app/a?b.mjs?v=1", "/b/App.app/app/a?b.mjs",
ios);
Expect(meta.url, "file:///app/a?b.mjs?v=1", "url from the path record");
Expect(meta.dirname, std::string("/app"), "dirname from the path record");
meta = ImportMetaFor("/data/user/0/pkg/files/app/sub/x.mjs", "", android);
Expect(meta.url, "file:///data/user/0/pkg/files/app/sub/x.mjs",
"Android url");
Expect(meta.dirname, std::string("/data/user/0/pkg/files/app/sub"),
"Android dirname is absolute");
// A8: a module with a URL scheme has no directory.
meta = ImportMetaFor("http://h/a/x.mjs", "", android);
Expect(meta.url, "http://h/a/x.mjs", "http url");
Check(!meta.dirname && !meta.filename, "http has no dirname");
meta = ImportMetaFor("blob:nativescript/abc", "", ios);
Check(!meta.dirname, "blob has no dirname");
meta = ImportMetaFor("x.mjs", "", ios);
Expect(meta.dirname, std::string("/app"),
"legacy dirname of a path with no '/'");
meta = ImportMetaFor("", "", ios);
Expect(meta.url, "file:///app/", "unknown module url");
Expect(meta.dirname, std::string("/app"), "unknown module dirname");
}
void TestAppSchemeOrigins() {
const LoaderConfig config = AppScheme();
Expect(ReportedModuleUrl("/root/app/sub/x.mjs?v=1", config),
"app:///sub/x.mjs?v=1", "an app file's origin");
Expect(ReportedModuleUrl("/etc/x.mjs", config), "file:///etc/x.mjs",
"a file outside the app root");
const ImportMetaStrings meta =
ImportMetaFor("/root/app/sub/x.mjs?v=1", "", config);
Expect(meta.url, "file:///root/app/sub/x.mjs?v=1",
"import.meta.url is the real file: URL");
Expect(meta.filename, std::string("/root/app/sub/x.mjs"), "filename");
Expect(meta.dirname, std::string("/root/app/sub"), "dirname");
// "/app" is the legacy origins' app root, which this mode does not have.
Expect(ImportMetaFor("x.mjs", "", config).dirname, std::string("."),
"dirname of a path with no '/'");
Expect(OriginToPath("app:///sub/x.mjs?v=1#h", config),
std::string("/root/app/sub/x.mjs"), "app: origin");
Expect(OriginToPath("file:///etc/x.mjs", config), std::string("/etc/x.mjs"),
"file: origin");
Expect(OriginToPath("app://h/x.mjs", config), std::nullopt, "app: host");
Expect(OriginToPath("app://localhost/sub/x.mjs", config),
std::string("/root/app/sub/x.mjs"), "app: localhost");
Expect(OriginToPath("app:x.mjs", config), std::nullopt, "app: opaque path");
Expect(OriginToPath("app:x/../../y.mjs", config), std::nullopt,
"app: opaque path with dot segments");
Expect(OriginToPath("app:///%2e%2e/etc/x.mjs", config),
std::string("/root/app/etc/x.mjs"), "app: encoded dot segments");
}
// OriginToPath undoes ModuleOrigin for every file name it can spell.
void TestRoundTrip() {
const std::vector<std::string> names = {
"x.mjs", "a b.mjs", "a#b.mjs", "100%.mjs", "c%41.mjs", "é.mjs", "a^b.mjs",
};
const std::vector<std::string> appOnly = {"a?b.mjs", "back\\slash.mjs",
"{x}.mjs", "q\"t.mjs"};
for (const LoaderConfig& config :
{IosLegacy(), AndroidLegacy(), AppScheme()}) {
std::vector<std::string> all = names;
if (!config.legacyOrigins) {
all.insert(all.end(), appOnly.begin(), appOnly.end());
}
for (const std::string& name : all) {
for (const std::string& dir :
{config.appRoot + "/sub/", std::string("/etc/")}) {
const std::string path = dir + name;
const std::string origin = ModuleOrigin(path, "?v=1", config);
Expect(OriginToPath(origin, config), path,
"OriginToPath(" + origin + ")");
if (!config.legacyOrigins) {
const ImportMetaStrings meta =
ImportMetaFor(path + "?v=1", path, config);
// Q3: an encoded '\\' names no path, as in Node, so such a file
// cannot be imported through its own import.meta.url.
const bool backslash = name.find('\\') != std::string::npos;
Expect(FileUrlToPath(meta.url, config),
backslash ? std::nullopt : std::optional<std::string>(path),
"FileUrlToPath(import.meta.url) for " + name);
}
}
}
}
}
void TestLegacyOriginToPath() {
const LoaderConfig ios = IosLegacy();
const LoaderConfig android = AndroidLegacy();
struct Case {
const LoaderConfig* config;
std::string origin;
std::optional<std::string> expected;
};
const std::vector<Case> cases = {
{&ios, "file:///app/sub/x.mjs?v=1", "/b/App.app/app/sub/x.mjs"},
// Q1: an origin the runtime wrote is read verbatim.
{&ios, "file:///app/a#b.mjs", "/b/App.app/app/a#b.mjs"},
{&ios, "file:///app/a%20b.mjs", "/b/App.app/app/a%20b.mjs"},
{&ios, "file:///app", "/b/App.app/app"},
{&ios, "file:///appx/y.mjs", "/appx/y.mjs"},
{&ios, "file:///etc/x.mjs", "/etc/x.mjs"},
{&ios, "app:///x%20y.mjs", "/b/App.app/app/x y.mjs"},
{&ios, "file://host/x.mjs", std::nullopt},
{&ios, "http://h/x.mjs", std::nullopt},
{&ios, "x.mjs", std::nullopt},
{&android, "file:///data/user/0/pkg/files/app/x.mjs",
"/data/user/0/pkg/files/app/x.mjs"},
{&android, "file:///data/data/pkg/files/app/x.mjs",
"/data/user/0/pkg/files/app/x.mjs"},
// Read verbatim, a NUL would reach a C-string path API.
{&ios, std::string("file:///app/x\0y.mjs", 19), std::nullopt},
{&ios, std::string("file:///etc/x\0y.mjs", 19), std::nullopt},
{&android, std::string("file:///data/user/0/pkg/files/app/x\0y", 37),
std::nullopt},
{&android, std::string("file:///sdcard/x\0y", 18), std::nullopt},
};
for (const Case& c : cases) {
Expect(OriginToPath(c.origin, *c.config), c.expected,
"OriginToPath(" + c.origin + ")");
}
}
void TestFileUrlToPath() {
const LoaderConfig ios = IosLegacy();
const LoaderConfig android = AndroidLegacy();
const LoaderConfig app = AppScheme();
struct Case {
const LoaderConfig* config;
std::string url;
std::optional<std::string> expected;
};
const std::vector<Case> cases = {
{&ios, "file:///app/x.mjs", "/b/App.app/app/x.mjs"},
{&ios, "file:///app", "/b/App.app/app"},
{&ios, "file:///app/sub/../x.mjs?v#h", "/b/App.app/app/x.mjs"},
{&ios, "file:///etc/x.mjs", "/etc/x.mjs"},
{&ios, "file:///a%20b", "/a b"},
{&ios, "file:///a#b/x.mjs", "/a"},
{&ios, "file://localhost/etc/x", "/etc/x"},
{&ios, "app:///x.mjs", "/b/App.app/app/x.mjs"},
{&ios, "http://h/x.mjs", std::nullopt},
{&ios, "node:fs", std::nullopt},
// Q3: Node's ESM resolver rejects a host, %2F and %5C; NUL is refused
// as well, since no path API can open it.
{&ios, "file://host/x.mjs", std::nullopt},
{&ios, "file:///a%2Fb", std::nullopt},
{&ios, "file:///a%2fb", std::nullopt},
{&ios, "file:///a%5Cb", std::nullopt},
{&ios, "file:///a%5cb", std::nullopt},
{&ios, "file:///a%00b", std::nullopt},
{&ios, "app://host/x.mjs", std::nullopt},
{&android, "file:///android_asset/app/x.mjs",
"/data/user/0/pkg/files/app/x.mjs"},
{&android, "file:///android_asset/appx", "/android_asset/appx"},
{&android, "file:///data/data/pkg/files/app/x.mjs",
"/data/user/0/pkg/files/app/x.mjs"},
{&app, "file:///app/x.mjs", "/root/app/x.mjs"},
{&app, "app:///x.mjs", "/root/app/x.mjs"},
{&app, "app:/x.mjs", "/root/app/x.mjs"},
// As for file:, localhost is no host.
{&app, "app://localhost/x.mjs", "/root/app/x.mjs"},
{&app, "app://LocalHost/x.mjs", "/root/app/x.mjs"},
{&app, "app://localhost", "/root/app"},
{&app, "app://localhostx/x.mjs", std::nullopt},
{&app, "app:", "/root/app"},
// A10: app: names a path under appRoot, so a path that does not start
// with '/' (an opaque path, which the parser leaves unnormalized) names
// none, and '..' never leaves appRoot.
{&app, "app:x.mjs", std::nullopt},
{&app, "app:x/../../y.mjs", std::nullopt},
{&app, "app:///../../etc/x.mjs", "/root/app/etc/x.mjs"},
{&app, "app:///%2e%2e/%2E%2e/etc/x.mjs", "/root/app/etc/x.mjs"},
{&app, "app:///.%2e/x.mjs", "/root/app/x.mjs"},
};
for (const Case& c : cases) {
Expect(FileUrlToPath(c.url, *c.config), c.expected,
"FileUrlToPath(" + c.url + ")");
}
}
} // namespace
int main() {
TestNormalizePath();
TestSchemesAndQueries();
TestCanonicalizeModuleKey();
TestLegacyOrigins();
TestLegacyImportMeta();
TestAppSchemeOrigins();
TestRoundTrip();
TestLegacyOriginToPath();
TestFileUrlToPath();
if (failures != 0) {
std::fprintf(stderr, "%d failure(s)\n", failures);
return 1;
}
std::printf("ModuleIdentityTest: ok\n");
return 0;
}