[SERVER-21089] assert.throws() doesn't check 2nd argument properly Created: 23/Oct/15  Updated: 31/Oct/16  Resolved: 27/Oct/16

Status: Closed
Project: Core Server
Component/s: Testing Infrastructure
Affects Version/s: None
Fix Version/s: 3.4.0-rc2

Type: Bug Priority: Major - P3
Reporter: Kevin Pulo Assignee: Robert Guo (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-26503 Fix incorrect uses of assert.throws() Closed
Related
related to SERVER-21087 Some uses of assert.throws() always pass Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: TIG 2016-10-10, TIG 2016-10-31
Participants:

 Description   

The test checks if typeof(params) is "string", but the message says "2nd argument to assert.throws has to be an array", and the usage of params as the 2nd argument to Function.apply confirms it should be an array or array-like object.

assert.throws = function(func, params, msg) {
    if (assert._debug && msg) print("in assert for: " + msg);
    if (params && typeof(params) == "string") {
        throw ("2nd argument to assert.throws has to be an array, not " + params);
    }
    try {
        func.apply(null, params);
    }
    catch (e) {
        return e;
    }
    doassert("did not throw exception: " + msg);
};

Similarly for assert.doesNotThrow.

Since func.apply will throw an exception if params is an unexpected type, this will make assert.throws always succeed in these cases.

> if (assert.throws(function(){throw("the exception")}, [])) print("success")
success
> assert.throws(function(){throw("the exception")}, [])
the exception
> if (assert.throws(function(){}, [])) print("success")
assert: did not throw exception: undefined
doassert@src/mongo/shell/assert.js:15:14
assert.throws@src/mongo/shell/assert.js:229:5
@(shell):1:5
 
2015-10-23T16:33:38.936+1100 E QUERY    [thread1] Error: did not throw exception: undefined :
doassert@src/mongo/shell/assert.js:15:14
assert.throws@src/mongo/shell/assert.js:229:5
@(shell):1:5
 
> if (assert.throws(function(){}, 3)) print("success")
success  // crap
> assert.throws(function(){}, 3)
TypeError: second argument to Function.prototype.apply must be an array

Likewise, assert.doesNotThrow will always fail (obviously less concerning, and there are only 56 uses of assert.doesNotThrow).

The error message should probably also use doassert rather than calling throw directly.



 Comments   
Comment by Githook User [ 27/Oct/16 ]

Author:

{u'username': u'guoyr', u'name': u'Robert Guo', u'email': u'robert.guo@10gen.com'}

Message: SERVER-21089 fix assert.throw/doesNotThrow() argument validation
SERVER-21087 prevent use of "this" in the function passed to assert.throw/doesNotThrow()
Branch: master
https://github.com/mongodb/mongo/commit/5b93d767f48469ac7a9212b2013f8349a30e1f70

Comment by Robert Guo (Inactive) [ 17/Oct/16 ]

PB: https://evergreen.mongodb.com/version/5811192a3ff1225afd0000ad

Generated at Thu Feb 08 03:56:15 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.