-
Notifications
You must be signed in to change notification settings - Fork 57
/
sandbox-sanity.test.js
212 lines (178 loc) · 7.77 KB
/
sandbox-sanity.test.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
describe('sandbox', function () {
this.timeout(1000 * 60);
var Sandbox = require('../../lib');
it('should create context', function (done) {
Sandbox.createContext(function (err, ctx) {
if (err) { return done(err); }
ctx.on('error', done);
ctx.ping(function (err, ttl, packet) {
expect(err).to.be.null;
expect(packet).to.be.ok;
expect(ttl).to.be.a('number').that.is.above(-1);
done();
});
});
});
it('should execute a piece of code', function (done) {
Sandbox.createContext(function (err, ctx) {
if (err) { return done(err); }
ctx.on('error', done);
ctx.execute('throw new Error("this will regurgitate!")', function (err) {
expect(err).to.be.ok;
expect(err).to.have.property('message', 'this will regurgitate!');
done();
});
});
});
it('should have a few important globals', function (done) {
Sandbox.createContext(function (err, ctx) {
if (err) { return done(err); }
ctx.on('error', done);
ctx.execute(`
var assert = require('assert');
assert.equal(typeof _, 'function');
assert.equal(typeof Error, 'function');
assert.equal(typeof console, 'object');
`, done);
});
});
it('should not have access to uvm bridge', function (done) {
Sandbox.createContext(function (err, ctx) {
if (err) { return done(err); }
ctx.on('error', done);
ctx.execute(`
var assert = require('assert');
assert.equal(typeof bridge, 'undefined');
assert.equal(typeof this.bridge, 'undefined');
assert.equal(typeof Function('return this.bridge')(), 'undefined');
`, done);
});
});
it('should not be able to mutate Error.prepareStackTrace', function (done) {
Sandbox.createContext(function (err, ctx) {
if (err) { return done(err); }
ctx.on('error', done);
ctx.execute(`
var assert = require('assert');
var fn = Error.prepareStackTrace;
Error.prepareStackTrace = () => {};
assert.equal(Error.prepareStackTrace, fn);
var err = new Error('Test');
assert.equal(err.stack.split('\\n')[0], 'Error: Test');
`, done);
});
});
it('should not have access to global properties', function (done) {
Sandbox.createContext({ debug: true }, function (err, ctx) {
if (err) { return done(err); }
ctx.on('error', done);
ctx.execute(`
var assert = require('assert');
var allowedGlobals = ${JSON.stringify(require('uniscope/lib/allowed-globals'))};
var ignoredProps = [
'TEMPORARY', 'PERSISTENT', // DedicatedWorkerGlobalScope constants (in Browser)
'require', 'eval', 'console', // uniscope ignored
]
var propNames = [];
var contextObject = Function('return this;')();
do {
propNames = propNames.concat(Object.getOwnPropertyNames(contextObject));
contextObject = Object.getPrototypeOf(contextObject);
// traverse until Object prototype
// @note since we mutated the scope already, don't check using the constructor
// instead, check for hasOwnProperty existence on the contextObject.
} while (contextObject && !Object.hasOwnProperty.call(contextObject, 'hasOwnProperty'));
// filter out the ignored properties
propNames = propNames.filter(prop => !ignoredProps.includes(prop));
// FIXME: why's 'SharedArrayBuffer' missing from browser's context?
// Temporarily added to fix browser tests
!propNames.includes('SharedArrayBuffer') && propNames.push('SharedArrayBuffer');
// make sure both propNames and allowedGlobals are same
assert.equal(JSON.stringify(propNames.sort()), JSON.stringify(allowedGlobals.sort()));
// double check using the diff
var diff = propNames
.filter(x => !allowedGlobals.includes(x))
.concat(allowedGlobals.filter(x => !propNames.includes(x)));
assert.equal(diff.length, 0);
`, done);
});
});
it('should accept an external execution id', function (done) {
Sandbox.createContext(function (err, ctx) {
if (err) { return done(err); }
ctx.on('error', done);
ctx.execute(`
var assert = require('assert');
assert.equal(typeof _, 'function');
assert.equal(typeof Error, 'function');
assert.equal(typeof console, 'object');
`, {
id: 'my-test-id'
}, function (err, execution) {
if (err) { return done(err); }
expect(execution).to.have.property('id', 'my-test-id');
done();
});
});
});
it('should create context fleet using templates', function (done) {
Sandbox.createContextFleet({
grpc: '',
websocket: ''
}, {}, (err, fleet) => {
if (err) { return done(err); }
expect(fleet).to.be.an('object');
expect(fleet.getContext).to.be.a('function');
expect(fleet.disposeAll).to.be.a('function');
done();
});
});
it('should return the correct context for the given template name', function (done) {
Sandbox.createContextFleet({
grpc: `
function initializeExecution () {
return {
request: {
type: 'grpc-request'
},
response: {
type: 'grpc-response'
}
}
};
function chaiPlugin (chai) {
const Assertion = chai.Assertion;
Assertion.addProperty('grpcRequest', function () {
this.assert(this._obj.type === 'grpc-request',
'expecting a gRPC request but got #{this}',
'not expecting a gRPC request object');
});
Assertion.addProperty('grpcResponse', function () {
this.assert(this._obj.type === 'grpc-response',
'expecting a gRPC response but got #{this}',
'not expecting a gRPC response object');
});
}
module.exports = { initializeExecution, chaiPlugin };
`,
websocket: ''
}, {}, { debug: true }, (err, fleet) => {
if (err) { return done(err); }
fleet.getContext('grpc', (err, ctx) => {
if (err) { return done(err); }
ctx.on('error', done);
ctx.on('execution.assertion', (_, assertions) => {
assertions.forEach((a) => {
expect(a.passed).to.be.true;
});
});
ctx.execute(`
pm.test('Should be gRPC request and response', () => {
pm.request.to.be.grpcRequest;
pm.response.to.be.grpcResponse;
});
`, done);
});
});
});
});