Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-21089

assert.throws() doesn't check 2nd argument properly

    • Fully Compatible
    • ALL
    • TIG 2016-10-10, TIG 2016-10-31

      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.

            Assignee:
            robert.guo@mongodb.com Robert Guo (Inactive)
            Reporter:
            kevin.pulo@mongodb.com Kevin Pulo
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: