Debian Codegraph @

test/promise/evil-promises.js

raw
1 describe('Evil promises should not be able to break invariants', function () {
2 'use strict';
3
4 specify('resolving to a promise that calls onFulfilled twice', function (done) {
5 // note that we have to create a trivial subclass, as otherwise the
6 // Promise.resolve(evilPromise) is just the identity function.
7 // (And in fact, most native Promise implementations use a private
8 // [[PromiseConstructor]] field in `Promise.resolve` which can't be
9 // easily patched in an ES5 engine, so instead of
10 // `Promise.resolve(evilPromise)` we'll use
11 // `new Promise(function(r){r(evilPromise);})` below.)
12 var EvilPromise = function (executor) {
13 var self = new Promise(executor);
14 Object.setPrototypeOf(self, EvilPromise.prototype);
15 return self;
16 };
17 if (!Object.setPrototypeOf) { return done(); } // skip test if on IE < 11
18 Object.setPrototypeOf(EvilPromise, Promise);
19 EvilPromise.prototype = Object.create(Promise.prototype, {
20 constructor: { value: EvilPromise }
21 });
22
23 var evilPromise = EvilPromise.resolve();
24 evilPromise.then = function (f) {
25 f(1);
26 f(2);
27 };
28
29 var calledAlready = false;
30 new Promise(function (r) { r(evilPromise); }).then(function (value) {
31 assert.strictEqual(calledAlready, false);
32 calledAlready = true;
33 assert.strictEqual(value, 1);
34 }).then(done, done);
35 });
36 });