1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 13:11:28 +00:00

Add unittest.testing() to make test building conditional

This commit is contained in:
dmiller
2014-03-06 17:15:05 +00:00
parent f8242124b8
commit 88146749f6
5 changed files with 33 additions and 2 deletions

View File

@@ -655,6 +655,11 @@ hex_to_bin = function( hex )
end end
--Ignore the rest if we are not testing.
if not unittest.testing() then
return _ENV
end
test_suite = unittest.TestSuite:new() 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_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") test_suite:add_test(unittest.is_false(isPrivate("1.1.1.1")), "1.1.1.1 is not private")

View File

@@ -920,6 +920,10 @@ function calculate_signature(mac_key, data)
end end
end end
if not unittest.testing() then
return _ENV
end
test_suite = unittest.TestSuite:new() test_suite = unittest.TestSuite:new()
test_suite:add_test(unittest.equal( test_suite:add_test(unittest.equal(
stdnse.tohex(select(-1, lm_create_hash("passphrase"))), stdnse.tohex(select(-1, lm_create_hash("passphrase"))),

View File

@@ -396,6 +396,10 @@ function utf8to16(from)
return table.concat(buf) return table.concat(buf)
end end
if not unittest.testing() then
return _ENV
end
test_suite = unittest.TestSuite:new() test_suite = unittest.TestSuite:new()
test_suite:add_test(function() test_suite:add_test(function()
local pos, cp = utf8_dec("\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E") local pos, cp = utf8_dec("\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E")

View File

@@ -15,6 +15,9 @@
-- --
-- The library is driven by the unittest NSE script. -- The library is driven by the unittest NSE script.
-- --
-- @args unittest.run Run tests. Causes <code>unittest.testing()</code> to
-- return true.
--
-- @copyright Same as Nmap--See http://nmap.org/book/man-legal.html -- @copyright Same as Nmap--See http://nmap.org/book/man-legal.html
local stdnse = require "stdnse" local stdnse = require "stdnse"
@@ -136,11 +139,22 @@ local libs = {
"xmpp", "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 -- Run tests provided by NSE libraries
-- @param to_test A list (table) of libraries to test. If none is provided, all -- @param to_test A list (table) of libraries to test. If none is provided, all
-- libraries are tested. -- libraries are tested.
run_tests = function(to_test) run_tests = function(to_test)
am_testing = true
if to_test == nil then if to_test == nil then
to_test = libs to_test = libs
end end
@@ -354,6 +368,11 @@ expected_failure = function(test)
end end
end end
if not testing() then
return _ENV
end
-- Self test -- Self test
test_suite = TestSuite:new() test_suite = TestSuite:new()

View File

@@ -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) -- @args unittest.tests Run tests from only these libraries (defaults to all)
-- --
-- @usage -- @usage
@@ -24,7 +23,7 @@ license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"safe"} categories = {"safe"}
prerule = function() return stdnse.get_script_args("unittest.run") end prerule = unittest.testing
action = function() action = function()
local libs = stdnse.get_script_args("unittest.tests") local libs = stdnse.get_script_args("unittest.tests")