Skip to content

Commit 5fd9451

Browse files
committed
assert: check object prototype in deepEqual
Objects with different prototypes should not be considered deep equal, e.g. `assert.deepEqual({}, [])` should throw. Previously `deepEqual` was implemented as per CommonJS Unit Testing/1.0 spec. It states that objects should have 'an identical "prototype" property' to be considered deep equal, which is incorrect. Instead object prototypes should be compared, i.e. `Object.getPrototypeOf` or `__proto__`. Fixes: nodejs#620
1 parent 69ce064 commit 5fd9451

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

lib/assert.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,12 @@ function isArguments(object) {
199199
function objEquiv(a, b) {
200200
if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b))
201201
return false;
202-
// an identical 'prototype' property.
203-
if (a.prototype !== b.prototype) return false;
204202
// if one is a primitive, the other must be same
205203
if (util.isPrimitive(a) || util.isPrimitive(b))
206204
return a === b;
205+
// an identical prototype.
206+
if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b))
207+
return false;
207208
var aIsArgs = isArguments(a),
208209
bIsArgs = isArguments(b);
209210
if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))

test/parallel/test-assert.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'}));
9191
assert.doesNotThrow(makeBlock(a.deepEqual, [4], ['4']));
9292
assert.throws(makeBlock(a.deepEqual, {a: 4}, {a: 4, b: true}),
9393
a.AssertionError);
94-
assert.doesNotThrow(makeBlock(a.deepEqual, ['a'], {0: 'a'}));
94+
assert.throws(makeBlock(a.deepEqual, ['a'], {0: 'a'}));
9595
//(although not necessarily the same order),
9696
assert.doesNotThrow(makeBlock(a.deepEqual, {a: 4, b: '1'}, {b: '1', a: 4}));
9797
var a1 = [1, 2, 3];
@@ -144,10 +144,16 @@ if (typeof Symbol === 'symbol') {
144144
}
145145

146146
// primitive wrappers and object
147-
assert.doesNotThrow(makeBlock(a.deepEqual, new String('a'), ['a']), a.AssertionError);
148-
assert.doesNotThrow(makeBlock(a.deepEqual, new String('a'), {0: 'a'}), a.AssertionError);
149-
assert.doesNotThrow(makeBlock(a.deepEqual, new Number(1), {}), a.AssertionError);
150-
assert.doesNotThrow(makeBlock(a.deepEqual, new Boolean(true), {}), a.AssertionError);
147+
assert.throws(makeBlock(a.deepEqual, new String('a'), ['a']),
148+
a.AssertionError);
149+
assert.throws(makeBlock(a.deepEqual, new String('a'),{0: 'a'}),
150+
a.AssertionError);
151+
assert.throws(makeBlock(a.deepEqual, new Number(1), {}), a.AssertionError);
152+
assert.throws(makeBlock(a.deepEqual, new Boolean(true), {}), a.AssertionError);
153+
154+
// prototypes
155+
assert.throws(makeBlock(assert.deepEqual, Object.create({}), {}),
156+
a.AssertionError);
151157

152158
// Testing the throwing
153159
function thrower(errorConstructor) {

test/parallel/test-url.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,8 @@ for (var u in parseTests) {
867867
}
868868
});
869869

870-
assert.deepEqual(actual, expected);
871-
assert.deepEqual(spaced, expected);
870+
assert.deepEqual(JSON.parse(JSON.stringify(actual)), expected);
871+
assert.deepEqual(JSON.parse(JSON.stringify(spaced)), expected);
872872

873873
var expected = parseTests[u].href,
874874
actual = url.format(parseTests[u]);
@@ -937,7 +937,7 @@ for (var u in parseTestsWithQueryString) {
937937
}
938938
}
939939

940-
assert.deepEqual(actual, expected);
940+
assert.deepEqual(JSON.parse(JSON.stringify(actual)), expected);
941941
}
942942

943943
// some extra formatting tests, just to verify

test/parallel/test-vm-create-context-accessors.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ Object.defineProperty(ctx, 'setter', {
2323
ctx = vm.createContext(ctx);
2424

2525
var result = vm.runInContext('setter = "test";[getter,setter]', ctx);
26-
assert.deepEqual(result, ['ok', 'ok=test']);
26+
assert.equal(result[0], 'ok');
27+
assert.equal(result[1], 'ok=test');

0 commit comments

Comments
 (0)