/
test-detection.js
executable file
·249 lines (238 loc) · 7.92 KB
/
test-detection.js
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
#!/usr/bin/env node
const crypto = require("crypto");
const testCases = require("./testcases.json");
const retire = require("../node/lib/retire.js");
const repo = require("../node/lib/repo.js");
const reporting = require("../node/lib/reporting.js");
const deepScan = require("../node/lib/deepscan.js").deepScan;
const queries = require("./jsrepository-ast.js").queries;
const options = {
log: reporting.open({}),
};
const limit = process.argv[2];
var hash = {
sha1: function (data) {
shasum = crypto.createHash("sha1");
shasum.update(data);
return shasum.digest("hex");
},
};
const https = require("https");
const dlCache = {};
async function dl(uri) {
const start = Date.now();
process.stdout.write(` Downloading ${uri} `);
return new Promise((resolve, reject) => {
if (dlCache[uri]) return resolve(dlCache[uri]);
let d = https.get(uri, (res) => {
if (res.statusCode != 200)
return reject(
"Could not download: " +
uri +
" - status: " +
res.statusCode +
" " +
res.statusMessage
);
let d = [];
res.on("data", (data) => d.push(data));
res.on("end", () => {
const finish = Date.now();
console.log(`(${finish - start}ms)`);
if (res.statusCode != 200) {
return reject("Failed to download " + uri + ": " + res.statusCode);
}
dlCache[uri] = Buffer.concat(d);
resolve(dlCache[uri]);
});
});
d.on("error", (err) => {
failure(err);
return reject("Failed to download " + uri);
});
});
}
function exitWithError(...msg) {
failure(...msg);
process.exit(1);
}
const colors = {
Reset: "\x1b[0m",
Green: "\x1b[32m",
Red: "\x1b[31m",
};
function success(...msg) {
console.log(colors.Green, ...msg, colors.Reset);
}
function failure(...msg) {
console.warn(colors.Red, ...msg, colors.Reset);
}
async function runTests(jsRepo) {
for (let [name, content] of Object.entries(testCases)) {
if (limit && limit != name) continue;
console.log(`Testing ${name}`);
for (let [template, tcontent] of Object.entries(content)) {
let {
versions,
subversions,
contentOnly,
additionalVersions,
allowedOtherComponents,
allowAstMiss,
} = tcontent;
if (limit) {
versions = Array.from(
new Set(versions.concat(additionalVersions || []))
);
}
subversions = subversions || [""];
for (let version of versions) {
for (let sub of subversions) {
let t = template
.replace(/§§version§§/g, version)
.replace(/§§subversion§§/g, sub);
if (!contentOnly) {
let resultsUri = retire.scanUri(t, jsRepo);
let resultsFilename = retire.scanFileName(
t.split("/").pop(),
jsRepo
);
let results = resultsUri.concat(resultsFilename);
if (results.length == 0) {
exitWithError(
`Did not detect ${version} of ${name} using uri or filename on ${t}`
);
}
if (results.length > 1) {
exitWithError(
`Detect multiple components in ${name} using uri and filename on ${t} : ${results
.map((a) => a.component)
.join(", ")}`
);
}
if (results[0].component != name) {
exitWithError(
`Wrong component for ${version} of ${name} using uri or filename on ${t}: ${results[0].component}`
);
}
if (!results[0].version.startsWith(version)) {
exitWithError(
`Wrong version for ${version} of ${name} using uri or filename on ${t}: ${results[0].version}`
);
}
}
let content = "";
try {
content = await dl(t);
} catch (e) {
if (e.message && e.message.includes("Failed to download")) {
console.log("Failed to download, ignoring");
continue;
}
if (sub == ".min") {
console.log("Ignoring missing minified version", e);
continue;
}
exitWithError(`Failed to download ${t}: ${e}`);
}
const cRs = Date.now();
let contentResults = retire.scanFileContent(content, jsRepo, hash);
const cRt = Date.now() - cRs;
if (allowedOtherComponents)
contentResults = contentResults.filter(
(x) => !allowedOtherComponents.includes(x.component)
);
if (contentResults.length == 0) {
exitWithError(
`Did not detect ${version} of ${name} using content on ${t}`
);
}
if (
contentResults.length > 1 &&
contentResults[0].component != "jquery-ui"
) {
//Allow multiple detections for jquery ui due to dialog, autocomplete etc.
exitWithError(
`Detect multiple components in ${name} using content on ${t} : ${contentResults
.map((a) => a.component)
.join(", ")}`
);
}
if (contentResults[0].component != name) {
exitWithError(
`Wrong component for ${version} of ${name} using uri or filename on ${t}: ${contentResults[0].component}`
);
}
if (!contentResults[0].version.startsWith(version)) {
exitWithError(
`Wrong version for ${version} of ${name} using content on ${t}: ${contentResults[0].version}`
);
}
let bRt = "-";
if (queries[name]) {
const bRs = Date.now();
let astResults = unique(deepScan(content.toString(), jsRepo));
bRt = Date.now() - bRs;
if (allowedOtherComponents)
astResults = astResults.filter(
(x) => !allowedOtherComponents.includes(x.component)
);
if (
astResults.length == 0 &&
(!allowAstMiss || !allowAstMiss.includes(version))
) {
exitWithError(
`Did not detect ${version} of ${name} using ast on ${t}`
);
}
if (astResults.length > 1) {
exitWithError(
`Detect multiple components in ${name} using ast on ${t} : ${astResults
.map((a) => a.component + " " + a.version)
.join(", ")}`
);
}
if (
(!allowAstMiss || !allowAstMiss.includes(version)) &&
astResults[0].component != name
) {
exitWithError(
`Wrong component for ${version} of ${name} using ast on ${t}: ${astResults[0].component}`
);
}
if (
(!allowAstMiss || !allowAstMiss.includes(version)) &&
!astResults[0].version.startsWith(version)
) {
exitWithError(
`Wrong version for ${version} of ${name} using ast on ${t}: ${astResults[0].version}`
);
}
}
success(
` - ${contentResults[0].component} @ ${contentResults[0].version} C: ${cRt}ms B: ${bRt}ms`
);
}
}
}
success(" Successfully tested uri/filename and content detection!");
}
}
function unique(a) {
return a.reduce((p, c) => {
const existing = p.find((x) => x.component == c.component);
if (existing) {
if (existing.version.split("-")[0] == c.version.split("-")[0]) {
existing.version = existing.version.split("-")[0];
return p;
}
}
p.push(c);
return p;
}, []);
}
repo
.loadrepositoryFromFile("./jsrepository-v2.json", options)
.then((jsRepo) => runTests(jsRepo))
.then(() => console.log("Done!"))
.catch((err) => console.warn("Failed!", err));