Repository navigation
Expand file tree
/
Copy pathModuleRegistryTest.cpp
More file actions
364 lines (330 loc) · 12.6 KB
/
Copy pathModuleRegistryTest.cpp
File metadata and controls
364 lines (330 loc) · 12.6 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
#include <cstdio>
#include <string>
#include <vector>
#include "loader/RegistryTable.h"
#include "ns/LoaderConfig.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());
}
}
struct FakeModule {
int hash;
};
int liveHandles = 0;
// Counts the handles the table holds, as v8::Global would pin them.
class FakeHandle {
public:
FakeHandle() = default;
FakeHandle(const FakeHandle&) = delete;
FakeHandle& operator=(const FakeHandle&) = delete;
FakeHandle(FakeHandle&& other) noexcept : module_(other.module_) {
other.module_ = nullptr;
}
FakeHandle& operator=(FakeHandle&& other) noexcept {
if (this != &other) {
Reset(other.module_);
liveHandles -= other.module_ != nullptr ? 1 : 0;
other.module_ = nullptr;
}
return *this;
}
~FakeHandle() { Reset(nullptr); }
void Reset(const FakeModule* module) {
liveHandles += (module != nullptr ? 1 : 0) - (module_ != nullptr ? 1 : 0);
module_ = module;
}
const FakeModule* Get() const { return module_; }
private:
const FakeModule* module_ = nullptr;
};
// Every Ref the table makes from a handle it holds: a v8::Local in the
// caller's HandleScope.
int materializedRefs = 0;
struct FakeTraits {
using Env = int;
using Ref = const FakeModule*;
using Stored = FakeHandle;
static bool IsEmpty(Ref module) { return module == nullptr; }
static int Hash(Ref module) { return module->hash; }
static Ref Get(Env, const Stored& stored) {
materializedRefs++;
return stored.Get();
}
static void Store(Env, Stored& stored, Ref module) { stored.Reset(module); }
static bool IsStoredEmpty(const Stored& stored) {
return stored.Get() == nullptr;
}
static bool Holds(const Stored& stored, Ref module) {
return stored.Get() == module;
}
static bool SameStored(const Stored& a, const Stored& b) {
return a.Get() == b.Get();
}
static int StoredHash(Env, const Stored& stored) {
return stored.Get()->hash;
}
};
using Table = RegistryTable<FakeTraits>;
constexpr int kEnv = 0;
void TestWritePath() {
// a and b collide on purpose, as identity hashes may.
FakeModule a{7}, b{7}, c{9};
{
Table table;
table.Set(kEnv, "/r/a.mjs", &a, "/r/a.mjs");
table.Set(kEnv, "/r/b.mjs", &b, "/r/b.mjs");
Check(table.Find(kEnv, "/r/a.mjs") == &a, "find a");
Check(table.Find(kEnv, "/r/missing.mjs") == nullptr, "find missing");
Expect(table.FindKey(kEnv, &a), "/r/a.mjs", "key of a, colliding with b");
Expect(table.FindKey(kEnv, &b), "/r/b.mjs", "key of b, colliding with a");
Expect(table.FindKey(kEnv, &c), "", "key of an unregistered module");
table.AddFacade(kEnv, &a, &c);
Check(table.FindFacade(kEnv, &a) == &c, "facade of a");
Check(table.FindFacade(kEnv, &b) == nullptr, "no facade for b");
// Replacing a key unhooks the module that held it.
table.Set(kEnv, "/r/a.mjs", &c, "/r/a.mjs");
Check(table.Find(kEnv, "/r/a.mjs") == &c, "replaced");
Expect(table.FindKey(kEnv, &a), "", "replaced module unindexed");
Expect(table.FindKey(kEnv, &c), "/r/a.mjs", "new module indexed");
Check(table.FindFacade(kEnv, &a) == nullptr, "replaced module's facade");
Check(table.FacadeCount() == 0, "facade released on replace");
Check(table.Size() == 2, "replace keeps the size");
// Registering a module again under its own key drops its facades too.
table.AddFacade(kEnv, &c, &b);
table.Set(kEnv, "/r/a.mjs", &c, "/r/a.mjs");
Check(table.FacadeCount() == 0, "re-register drops the facade");
Expect(table.FindKey(kEnv, &c), "/r/a.mjs", "re-registered module");
Check(table.IndexedKeyCount() == 2, "one index entry per key");
// One module under two keys answers with the first until it leaves.
table.Set(kEnv, "/r/alias.mjs", &b, "/r/alias.mjs");
Expect(table.FindKey(kEnv, &b), "/r/b.mjs", "first of two keys");
Check(table.Erase(kEnv, "/r/b.mjs"), "erase b");
Expect(table.FindKey(kEnv, &b), "/r/alias.mjs", "second key after erase");
Check(!table.Erase(kEnv, "/r/b.mjs"), "erase twice");
Check(!table.Contains("/r/b.mjs"), "erased");
table.AddFacade(kEnv, &b, &a);
Check(table.Erase(kEnv, "/r/alias.mjs"), "erase alias");
Check(table.FacadeCount() == 0, "facade released on erase");
Check(table.IndexedKeyCount() == 1, "index emptied with the registry");
// An empty module may hold a key; it is never indexed.
table.Set(kEnv, "/r/empty.mjs", nullptr, "");
Check(table.Contains("/r/empty.mjs"), "empty entry present");
Check(table.Find(kEnv, "/r/empty.mjs") == nullptr,
"empty entry reads empty");
Check(table.IndexedKeyCount() == 1, "empty entry not indexed");
}
Check(liveHandles == 0, "every handle released");
}
// A module keeps its facades while any key still holds it.
void TestFacadesFollowTheLastKey() {
// a and b collide, so their keys share an index bucket.
FakeModule a{7}, b{7}, c{9}, facade{100};
Table table;
table.Set(kEnv, "/r/a.mjs", &a, "");
table.Set(kEnv, "/r/b1.mjs", &b, "");
table.Set(kEnv, "/r/b2.mjs", &b, "");
table.AddFacade(kEnv, &b, &facade);
Check(table.Erase(kEnv, "/r/b1.mjs"), "erase b's first key");
Check(table.FindFacade(kEnv, &b) == &facade,
"b keeps its facade under its second key");
table.Set(kEnv, "/r/b1.mjs", &b, "");
table.Set(kEnv, "/r/b2.mjs", &c, "");
Check(table.FindFacade(kEnv, &b) == &facade,
"b keeps its facade when one key is replaced");
Check(table.Erase(kEnv, "/r/b1.mjs"), "erase b's last key");
Check(table.FindFacade(kEnv, &b) == nullptr,
"b loses its facade with its last key, though a shares its bucket");
Check(table.FacadeCount() == 0, "facade released");
}
// Only the reads that return a module make a Ref: the writes and FindKey
// return none, so the handles they would make pile up in the caller's
// HandleScope across a large invalidation.
void TestNoRefsOutsideReads() {
std::vector<FakeModule> modules;
for (int i = 0; i < 64; i++) {
modules.push_back(FakeModule{i % 4});
}
FakeModule facade{100};
Table table;
materializedRefs = 0;
for (size_t i = 0; i < modules.size(); i++) {
const std::string key = "/r/" + std::to_string(i) + ".mjs";
table.Set(kEnv, key, &modules[i], key);
table.AddFacade(kEnv, &modules[i], &facade);
Expect(table.FindKey(kEnv, &modules[i]), key, "key of module " + key);
}
table.Set(kEnv, "/r/0.mjs", &modules[1], "/r/0.mjs");
for (size_t i = 0; i < modules.size(); i++) {
table.Erase(kEnv, "/r/" + std::to_string(i) + ".mjs");
}
Check(materializedRefs == 0, "writes and FindKey make no Ref (made " +
std::to_string(materializedRefs) + ")");
Check(table.FacadeCount() == 0 && table.IndexedKeyCount() == 0,
"the table emptied");
table.Set(kEnv, "/r/a.mjs", &modules[0], "");
table.AddFacade(kEnv, &modules[0], &facade);
materializedRefs = 0;
Check(table.Find(kEnv, "/r/a.mjs") == &modules[0], "find");
Check(table.FindFacade(kEnv, &modules[0]) == &facade, "find facade");
Check(materializedRefs == 2, "one Ref per module returned");
}
void TestFilePaths() {
FakeModule a{1};
Table table;
table.Set(kEnv, "/r/q?x.mjs?v=1", &a, "/r/q?x.mjs");
Expect(std::string(table.FilePathFor("/r/q?x.mjs?v=1")), "/r/q?x.mjs",
"recorded path");
Check(table.HasRecordedPath("/r/q?x.mjs?v=1"), "has a record");
// The record belongs to the module that holds the key.
table.Set(kEnv, "/r/q?x.mjs?v=1", &a, "");
Expect(std::string(table.FilePathFor("/r/q?x.mjs?v=1")), "/r/q",
"record cleared");
table.Set(kEnv, "/r/q?x.mjs?v=1", &a, "/r/q?x.mjs");
table.Erase(kEnv, "/r/q?x.mjs?v=1");
Check(!table.HasRecordedPath("/r/q?x.mjs?v=1"), "record erased");
struct Case {
std::string key;
std::string path;
};
const std::vector<Case> unrecorded = {
{"/r/a.mjs", "/r/a.mjs"}, {"/r/a.mjs?v=2", "/r/a.mjs"},
{"/r/a.mjs?", "/r/a.mjs"}, {"file:///r/a.mjs?v=2", "/r/a.mjs"},
{"https://h/a.js", ""}, {"http://h/a.js", ""},
{"blob:nativescript/1", ""}, {"node:fs", ""},
{"app:///a.mjs", ""}, {"", ""},
};
for (const Case& c : unrecorded) {
Expect(std::string(table.FilePathFor(c.key)), c.path,
"unrecorded path of " + c.key);
}
}
void TestCanonicalKey() {
LoaderConfig config;
config.appRoot = "/r";
FakeModule a{1};
Table table;
Expect(table.CanonicalKey("/r/./q?x.mjs", config, nullptr), "/r/q?x.mjs",
"unregistered path: the query is kept whole");
table.Set(kEnv, "/r/q?x.mjs", &a, "/r/q?x.mjs");
Expect(table.CanonicalKey("/r/q?x.mjs", config, nullptr), "/r/q?x.mjs",
"registered file key");
table.Set(kEnv, "/r/./b.mjs", &a, "/r/./b.mjs");
Expect(table.CanonicalKey("/r/./b.mjs", config, nullptr), "/r/./b.mjs",
"a recorded key is never renormalized");
table.Set(kEnv, "/r/./c.mjs", &a, "");
Expect(table.CanonicalKey("/r/./c.mjs", config, nullptr), "/r/c.mjs",
"an unrecorded key is normalized");
}
// Legacy origins spell file names verbatim; the URL parser would read '#'
// as a fragment and decode '%41'.
void TestLegacyOriginKeys() {
LoaderConfig ios;
ios.appRoot = "/b/App.app/app";
ios.appRootOrigin = "file:///app";
ios.legacyOrigins = true;
ios.virtualFileRoots = {"/app"};
LoaderConfig android;
android.appRoot = "/data/user/0/pkg/files/app";
android.appRootOrigin = "file:///data/user/0/pkg/files/app";
android.legacyOrigins = true;
FakeModule a{1};
for (const LoaderConfig* config : {&ios, &android}) {
for (const std::string name : {"/h#.mjs", "/pct%41.mjs", "/d#/x%2F.mjs"}) {
for (const std::string query : {"", "?v=1"}) {
Table table;
const std::string key = config->appRoot + name + query;
table.Set(kEnv, key, &a, config->appRoot + name);
const std::string origin =
ModuleOrigin(config->appRoot + name, query, *config);
Expect(table.CanonicalKey(origin, *config, nullptr), key,
"legacy origin " + origin);
}
}
}
{
Table table;
table.Set(kEnv, "/etc/h#.mjs", &a, "/etc/h#.mjs");
Expect(table.CanonicalKey("file:///etc/h#.mjs", android, nullptr),
"/etc/h#.mjs", "legacy origin outside appRoot");
}
{
Table table;
Expect(table.CanonicalKey("file:///data/user/0/pkg/files/app/pct%41.mjs",
android, nullptr),
"/data/user/0/pkg/files/app/pctA.mjs",
"unregistered: the URL parser reads it");
table.Set(kEnv, "/data/user/0/pkg/files/app/pctA.mjs", &a,
"/data/user/0/pkg/files/app/pctA.mjs");
Expect(table.CanonicalKey("file:///data/user/0/pkg/files/app/pct%41.mjs",
android, nullptr),
"/data/user/0/pkg/files/app/pctA.mjs",
"the verbatim key is not registered: the URL parser reads it");
}
{
LoaderConfig optIn = android;
optIn.legacyOrigins = false;
Table table;
const std::string path = optIn.appRoot + "/h#%41.mjs";
table.Set(kEnv, path + "?v=1", &a, path);
Expect(
table.CanonicalKey(ModuleOrigin(path, "?v=1", optIn), optIn, nullptr),
path + "?v=1", "opt-in origins round-trip through the parser");
}
}
void TestLoadedUrls() {
FakeModule a{1};
Table table;
const std::vector<std::string> keys = {
"https://h/b.js", "http://h/a.js", "blob:nativescript/1",
"/r/a.mjs", "/r/a.mjs?u=x://y", "node:fs",
"ns:custom", "file:///r/a.mjs", "file://",
"app:///x.mjs",
};
for (const std::string& key : keys) {
table.Set(kEnv, key, &a, "");
}
const std::vector<std::string> expected = {
"app:///x.mjs", "blob:nativescript/1", "file://",
"http://h/a.js", "https://h/b.js",
};
const std::vector<std::string> urls = table.LoadedUrls();
std::string joined;
for (const std::string& url : urls) {
joined += url + " ";
}
std::string want;
for (const std::string& url : expected) {
want += url + " ";
}
Expect(joined, want, "loaded module URLs");
}
} // namespace
int main() {
TestWritePath();
TestFacadesFollowTheLastKey();
TestNoRefsOutsideReads();
TestFilePaths();
TestCanonicalKey();
TestLegacyOriginKeys();
TestLoadedUrls();
if (failures != 0) {
std::fprintf(stderr, "%d failure(s)\n", failures);
return 1;
}
std::printf("ModuleRegistryTest: ok\n");
return 0;
}