-
Notifications
You must be signed in to change notification settings - Fork 47
/
test-misc.js
320 lines (298 loc) · 10.8 KB
/
test-misc.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
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
/**
* Copyright (c) 2015-present, Waysact Pty Ltd
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var webpack = require('webpack');
var path = require('path');
var SriPlugin = require('../index');
var fs = require('fs');
var expect = require('expect');
var tmp = require('tmp');
var crypto = require('crypto');
var Promise = require('bluebird');
var webpackVersion = Number(
require('webpack/package.json').version.split('.')[0]
);
var util = require('../util');
function testCompilation() {
return {
warnings: [],
errors: [],
compiler: {
options: {
output: {
crossOriginLoading: 'anonymous'
}
}
}
};
}
describe('Edge Cases', function describe() {
it('should error when code splitting is used with crossOriginLoading', function it() {
var tmpDir = tmp.dirSync();
var mainJs = path.join(tmpDir.name, 'main.js');
var chunkJs = path.join(tmpDir.name, 'chunk.js');
return new Promise((resolve, reject) => {
var webpackConfig;
fs.writeFileSync(
mainJs,
'require.ensure(["./chunk.js"], function(require) { require("./chunk.js"); });'
);
fs.writeFileSync(chunkJs, '');
webpackConfig = Object.assign({
entry: mainJs,
output: {
path: tmpDir.name,
filename: 'bundle.js'
},
plugins: [
new SriPlugin({ hashFuncNames: ['sha256', 'sha384'] })
]
}, webpackVersion >= 4 ? {
mode: 'production'
} : {});
webpack(webpackConfig, function webpackCallback(err, result) {
if (err) {
reject(err);
return;
}
try {
expect(result.compilation.warnings.length).toEqual(1);
expect(result.compilation.warnings[0]).toBeInstanceOf(Error);
expect(result.compilation.warnings[0].message).toMatch(
/Set webpack option output.crossOriginLoading/);
expect(result.compilation.errors.length).toEqual(1);
expect(result.compilation.errors[0]).toBeInstanceOf(Error);
expect(result.compilation.errors[0].message).toMatch(
/webpack option output.crossOriginLoading not set, code splitting will not work!/);
resolve();
} catch (e) {
reject(e);
}
});
}).finally(() => {
fs.unlinkSync(mainJs);
fs.unlinkSync(chunkJs);
if (fs.existsSync(path.join(tmpDir.name, 'bundle.js'))) {
fs.unlinkSync(path.join(tmpDir.name, 'bundle.js'));
try {
fs.unlinkSync(path.join(tmpDir.name, '0.bundle.js'));
} catch (e) {
fs.unlinkSync(path.join(tmpDir.name, '1.bundle.js'));
}
}
tmpDir.removeCallback();
});
});
it('should support multiple compilation', function it() {
var tmpDir = tmp.dirSync();
var mainJs = path.join(tmpDir.name, 'main.js');
var otherJs = path.join(tmpDir.name, 'other.js');
var oldOtherJsSource = 'console.log(123)';
var newOtherJsSource = 'console.log(456)';
var watching;
var callbackCount = 0;
var webpackConfig;
var compiler;
fs.writeFileSync(
mainJs,
'require.ensure(["./other.js"], function(require) { require("./other.js"); });'
);
fs.writeFileSync(otherJs, oldOtherJsSource);
return new Promise((resolve, reject) => {
webpackConfig = Object.assign({
entry: {
bundle: mainJs
},
output: {
path: tmpDir.name,
filename: 'bundle.js',
chunkFilename: 'chunk.js',
crossOriginLoading: 'anonymous'
},
plugins: [new SriPlugin({ hashFuncNames: ['sha256', 'sha384'] })]
}, webpackVersion >= 4 ? { mode: 'development' } : {});
compiler = webpack(webpackConfig);
function handler(error, stats) {
var chunkContents;
var bundleContents;
var hash;
var regex;
var match;
if (error) {
reject(error);
return;
}
if (stats.hasErrors()) {
reject(new Error(stats.toString({ reason: true })));
return;
}
try {
if (callbackCount === 0) {
setTimeout(function updateCode() {
fs.writeFileSync(otherJs, newOtherJsSource);
}, 1000); // FIXME -- brittle and slow
callbackCount += 1;
} else if (callbackCount === 1) {
chunkContents = fs.readFileSync(
path.join(tmpDir.name, 'chunk.js'),
'utf-8'
);
bundleContents = fs.readFileSync(
path.join(tmpDir.name, 'bundle.js'),
'utf-8'
);
if (chunkContents.indexOf(newOtherJsSource) >= 0) {
callbackCount += 1;
watching.close(function afterClose() {
try {
expect(bundleContents).toMatch(/\.integrity\s*=/);
hash = crypto
.createHash('sha256')
.update(chunkContents, 'utf8')
.digest('base64');
regex = /sha256-([^ ]+)/g;
match = regex.exec(bundleContents);
expect(match).not.toBeNull();
expect(match[1]).toEqual(hash);
expect(regex.exec(bundleContents)).toBeFalsy();
resolve();
} catch (err) {
reject(err);
}
});
}
}
} catch (e) {
reject(e);
}
}
watching = compiler.watch({ aggregateTimeout: 0 }, handler);
}).finally(() => {
compiler.purgeInputFileSystem();
fs.unlinkSync(path.join(tmpDir.name, 'chunk.js'));
fs.unlinkSync(path.join(tmpDir.name, 'bundle.js'));
fs.unlinkSync(path.join(tmpDir.name, 'main.js'));
fs.unlinkSync(path.join(tmpDir.name, 'other.js'));
tmpDir.removeCallback();
});
});
});
describe('Plugin Options', function describe() {
it('throws an error when options is not an object', function it() {
expect(function block() {
new SriPlugin(function dummy() {}); // eslint-disable-line no-new
}).toThrow(/argument must be an object/);
});
it('warns when no hash function names are specified', function it() {
var plugin = new SriPlugin();
var dummyCompilation = testCompilation();
expect(plugin.options.hashFuncNames).toBeFalsy();
expect(plugin.options.deprecatedOptions).toBeFalsy();
plugin.validateOptions(dummyCompilation);
expect(dummyCompilation.errors.length).toBe(1);
expect(dummyCompilation.warnings.length).toBe(0);
expect(dummyCompilation.errors[0]).toBeInstanceOf(Error);
expect(dummyCompilation.errors[0].message).toMatch(
/hashFuncNames must be an array of hash function names, instead got 'undefined'/);
});
it('warns when no standard hash function name is specified', function it() {
var plugin = new SriPlugin({
hashFuncNames: ['md5']
});
var dummyCompilation = testCompilation();
expect(plugin.options.hashFuncNames).toEqual(['md5']);
expect(plugin.options.deprecatedOptions).toBeFalsy();
plugin.validateOptions(dummyCompilation);
expect(dummyCompilation.errors.length).toBe(0);
expect(dummyCompilation.warnings.length).toBe(1);
expect(dummyCompilation.warnings[0]).toBeInstanceOf(Error);
expect(dummyCompilation.warnings[0].message).toMatch(new RegExp(
'It is recommended that at least one hash function is part of ' +
'the set for which support is mandated by the specification'));
});
it('supports new constructor with array of hash function names', function it() {
var plugin = new SriPlugin({
hashFuncNames: ['sha256', 'sha384']
});
var dummyCompilation = testCompilation();
expect(plugin.options.hashFuncNames).toEqual(['sha256', 'sha384']);
expect(plugin.options.deprecatedOptions).toBeFalsy();
plugin.validateOptions(dummyCompilation);
expect(dummyCompilation.errors.length).toBe(0);
expect(dummyCompilation.warnings.length).toBe(0);
});
it('errors if hash function names is not an array', function it() {
var plugin = new SriPlugin({
hashFuncNames: 'sha256'
});
var dummyCompilation = testCompilation();
plugin.validateOptions(dummyCompilation);
expect(dummyCompilation.errors.length).toBe(1);
expect(dummyCompilation.warnings.length).toBe(0);
expect(dummyCompilation.errors[0].message).toMatch(
/options.hashFuncNames must be an array of hash function names, instead got 'sha256'/);
expect(plugin.options.enabled).toBeFalsy();
});
it('errors if hash function names contains non-string', function it() {
var plugin = new SriPlugin({
hashFuncNames: [1234]
});
var dummyCompilation = testCompilation();
plugin.validateOptions(dummyCompilation);
expect(dummyCompilation.errors.length).toBe(1);
expect(dummyCompilation.warnings.length).toBe(0);
expect(dummyCompilation.errors[0].message).toMatch(
/options.hashFuncNames must be an array of hash function names, but contained 1234/);
expect(plugin.options.enabled).toBeFalsy();
});
it('errors if hash function names contains unsupported digest', function it() {
var plugin = new SriPlugin({
hashFuncNames: ['frobnicate']
});
var dummyCompilation = testCompilation();
plugin.validateOptions(dummyCompilation);
expect(dummyCompilation.errors.length).toBe(1);
expect(dummyCompilation.warnings.length).toBe(0);
expect(dummyCompilation.errors[0].message).toMatch(
/Cannot use hash function 'frobnicate': Digest method not supported/);
expect(plugin.options.enabled).toBeFalsy();
});
it('uses default options', function it() {
var plugin = new SriPlugin({
hashFuncNames: ['sha256']
});
var dummyCompilation;
expect(plugin.options.hashFuncNames).toEqual(['sha256']);
expect(plugin.options.enabled).toBeTruthy();
expect(plugin.options.deprecatedOptions).toBeFalsy();
dummyCompilation = testCompilation();
plugin.validateOptions(dummyCompilation);
expect(dummyCompilation.errors.length).toBe(0);
expect(dummyCompilation.warnings.length).toBe(0);
});
it('should warn when output.crossOriginLoading is not set', function it() {
var plugin = new SriPlugin({ hashFuncNames: ['sha256'] });
var dummyCompilation = {
warnings: [],
errors: [],
compiler: {
options: {
output: {
crossOriginLoading: false
}
}
}
};
plugin.validateOptions(dummyCompilation);
expect(dummyCompilation.errors.length).toBe(0);
expect(dummyCompilation.warnings.length).toBe(1);
expect(dummyCompilation.warnings[0].message).toMatch(
/Set webpack option output.crossOriginLoading/);
});
it('ignores tags without attributes', function it() {
expect(util.filterTag({ tagName: "script" })).toBeFalsy();
});
});