Skip to content

Commit 0dfe5be

Browse files
sasurau4targos
authored andcommitted
test: add test of policy about parse error
PR-URL: #26873 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 3370291 commit 0dfe5be

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
if(!common.hasCrypto)common.skip('missing crypto');
5+
6+
consttmpdir=require('../common/tmpdir');
7+
constassert=require('assert');
8+
const{ spawnSync }=require('child_process');
9+
constcrypto=require('crypto');
10+
constfs=require('fs');
11+
constpath=require('path');
12+
const{ pathToFileURL }=require('url');
13+
14+
tmpdir.refresh();
15+
16+
functionhash(algo,body){
17+
consth=crypto.createHash(algo);
18+
h.update(body);
19+
returnh.digest('base64');
20+
}
21+
22+
constpolicyFilepath=path.join(tmpdir.path,'policy');
23+
24+
constparentFilepath=path.join(tmpdir.path,'parent.js');
25+
constparentBody="require('./dep.js')";
26+
27+
constdepFilepath=path.join(tmpdir.path,'dep.js');
28+
constdepURL=pathToFileURL(depFilepath);
29+
constdepBody='';
30+
31+
fs.writeFileSync(parentFilepath,parentBody);
32+
fs.writeFileSync(depFilepath,depBody);
33+
34+
consttmpdirURL=pathToFileURL(tmpdir.path);
35+
if(!tmpdirURL.pathname.endsWith('/')){
36+
tmpdirURL.pathname+='/';
37+
}
38+
39+
functiontest({ shouldFail, integrity }){
40+
constresources={
41+
[depURL]: {
42+
body: depBody,
43+
integrity
44+
}
45+
};
46+
constmanifest={
47+
resources: {},
48+
};
49+
for(const[url,{ body, integrity }]ofObject.entries(resources)){
50+
manifest.resources[url]={
51+
integrity,
52+
};
53+
fs.writeFileSync(newURL(url,tmpdirURL.href),body);
54+
}
55+
fs.writeFileSync(policyFilepath,JSON.stringify(manifest,null,2));
56+
const{ status }=spawnSync(process.execPath,[
57+
'--experimental-policy',
58+
policyFilepath,
59+
depFilepath
60+
]);
61+
if(shouldFail){
62+
assert.notStrictEqual(status,0);
63+
}else{
64+
assert.strictEqual(status,0);
65+
}
66+
}
67+
68+
test({
69+
shouldFail: false,
70+
integrity: `sha256-${hash('sha256',depBody)}`,
71+
});
72+
test({
73+
shouldFail: true,
74+
integrity: `1sha256-${hash('sha256',depBody)}`,
75+
});
76+
test({
77+
shouldFail: true,
78+
integrity: 'hoge',
79+
});
80+
test({
81+
shouldFail: true,
82+
integrity: `sha256-${hash('sha256',depBody)}sha256-${hash(
83+
'sha256',
84+
depBody
85+
)}`,
86+
});

0 commit comments

Comments
 (0)