diff --git a/nselib/ipOps.lua b/nselib/ipOps.lua index 1279b8fac..4220e2370 100644 --- a/nselib/ipOps.lua +++ b/nselib/ipOps.lua @@ -655,6 +655,11 @@ hex_to_bin = function( hex ) end +--Ignore the rest if we are not testing. +if not unittest.testing() then + return _ENV +end + test_suite = unittest.TestSuite:new() test_suite:add_test(unittest.is_true(isPrivate("192.168.123.123")), "192.168.123.123 is private") test_suite:add_test(unittest.is_false(isPrivate("1.1.1.1")), "1.1.1.1 is not private") diff --git a/nselib/smbauth.lua b/nselib/smbauth.lua index 9531e6766..78b6283a1 100644 --- a/nselib/smbauth.lua +++ b/nselib/smbauth.lua @@ -920,6 +920,10 @@ function calculate_signature(mac_key, data) end end +if not unittest.testing() then + return _ENV +end + test_suite = unittest.TestSuite:new() test_suite:add_test(unittest.equal( stdnse.tohex(select(-1, lm_create_hash("passphrase"))), diff --git a/nselib/unicode.lua b/nselib/unicode.lua index a365d30fa..af43f0051 100644 --- a/nselib/unicode.lua +++ b/nselib/unicode.lua @@ -396,6 +396,10 @@ function utf8to16(from) return table.concat(buf) end +if not unittest.testing() then + return _ENV +end + test_suite = unittest.TestSuite:new() test_suite:add_test(function() local pos, cp = utf8_dec("\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E") diff --git a/nselib/unittest.lua b/nselib/unittest.lua index c686c1fba..0bd2187ae 100644 --- a/nselib/unittest.lua +++ b/nselib/unittest.lua @@ -15,6 +15,9 @@ -- -- The library is driven by the unittest NSE script. -- +-- @args unittest.run Run tests. Causes unittest.testing() to +-- return true. +-- -- @copyright Same as Nmap--See http://nmap.org/book/man-legal.html local stdnse = require "stdnse" @@ -136,11 +139,22 @@ local libs = { "xmpp", } +local am_testing = stdnse.get_script_args('unittest.run') +---Check whether tests are being run +-- +-- Libraries can use this function to avoid the overhead of creating tests if +-- the user hasn't chosen to run them. +-- @return true if unittests are being run, false otherwise. +function testing() + return am_testing +end + --- -- Run tests provided by NSE libraries -- @param to_test A list (table) of libraries to test. If none is provided, all -- libraries are tested. run_tests = function(to_test) + am_testing = true if to_test == nil then to_test = libs end @@ -354,6 +368,11 @@ expected_failure = function(test) end end + +if not testing() then + return _ENV +end + -- Self test test_suite = TestSuite:new() diff --git a/scripts/unittest.nse b/scripts/unittest.nse index aa525f18d..242e79ba1 100644 --- a/scripts/unittest.nse +++ b/scripts/unittest.nse @@ -6,7 +6,6 @@ Runs unit tests on all NSE libraries. ]] --- --- @args unittest.run Run tests -- @args unittest.tests Run tests from only these libraries (defaults to all) -- -- @usage @@ -24,7 +23,7 @@ license = "Same as Nmap--See http://nmap.org/book/man-legal.html" categories = {"safe"} -prerule = function() return stdnse.get_script_args("unittest.run") end +prerule = unittest.testing action = function() local libs = stdnse.get_script_args("unittest.tests")