diff --git a/nselib/base32.lua b/nselib/base32.lua index 3936b574b..889790888 100644 --- a/nselib/base32.lua +++ b/nselib/base32.lua @@ -42,6 +42,8 @@ local char = require "string".char local concat = require "table".concat +local unittest = require "unittest" + _ENV = require "stdnse".module "base32" local b32standard = { @@ -185,49 +187,56 @@ function dec (b32, hexExtend) return concat(out) end -do - local function test(a, b) - assert(enc(a) == b and dec(b) == a) - end - local function testh(a, b) - assert(enc(a, true) == b and dec(b, true) == a) - end +if not unittest.testing() then + return _ENV +end - test("", "") - test("f", "MY======") - test("fo", "MZXQ====") - test("foo", "MZXW6===") - test("foob", "MZXW6YQ=") - test("fooba", "MZXW6YTB") - test("foobar", "MZXW6YTBOI======") - testh("", "") - testh("f", "CO======") - testh("fo", "CPNG====") - testh("foo", "CPNMU===") - testh("foob", "CPNMUOG=") - testh("foobar", "CPNMUOJ1E8======") +test_suite = unittest.TestSuite:new() - -- extensive tests - if false then - local path = tmpname() - local file = open(path, "w") - local t = {} - for a = 0, 255, random(1, 7) do - for b = 0, 255, random(2, 7) do - for c = 0, 255, random(2, 7) do - t[#t+1] = char(a, b, c, 0xA) - file:write(t[#t]) - end - end - end - assert(file:close()) - local input = concat(t) - local output = enc(input) - local good = assert(popen("base32 < "..path, "r")):read("a"):gsub("%s", "") - remove(path) - assert(output == good) - assert(dec(output) == input) +local equal = unittest.equal +local function test(a, b) + test_suite:add_test(equal(enc(a), b), "encoding") + test_suite:add_test(equal(dec(b), a), "decoding") +end +local function testh(a, b) + test_suite:add_test(equal(enc(a, true), b), "hex encoding") + test_suite:add_test(equal(dec(b, true), a), "hex decoding") +end + +test("", "") +test("f", "MY======") +test("fo", "MZXQ====") +test("foo", "MZXW6===") +test("foob", "MZXW6YQ=") +test("fooba", "MZXW6YTB") +test("foobar", "MZXW6YTBOI======") +testh("", "") +testh("f", "CO======") +testh("fo", "CPNG====") +testh("foo", "CPNMU===") +testh("foob", "CPNMUOG=") +testh("foobar", "CPNMUOJ1E8======") + +-- extensive tests +if false then + local path = tmpname() + local file = open(path, "w") + local t = {} + for a = 0, 255, random(1, 7) do + for b = 0, 255, random(2, 7) do + for c = 0, 255, random(2, 7) do + t[#t+1] = char(a, b, c, 0xA) + file:write(t[#t]) + end end + end + assert(file:close()) + local input = concat(t) + local output = enc(input) + local good = assert(popen("base32 < "..path, "r")):read("a"):gsub("%s", "") + remove(path) + assert(output == good) + assert(dec(output) == input) end return _ENV diff --git a/nselib/base64.lua b/nselib/base64.lua index 1be677099..478e29b02 100644 --- a/nselib/base64.lua +++ b/nselib/base64.lua @@ -43,6 +43,8 @@ local char = require "string".char local concat = require "table".concat +local unittest = require "unittest" + _ENV = require "stdnse".module("base64") local b64table = { @@ -147,48 +149,55 @@ function dec (e) return concat(out) end -do - local function test(a, b) - assert(enc(a) == b and dec(b) == a) - end - test("", "") - test("\x01", "AQ==") - test("\x00", "AA==") - test("\x00\x01", "AAE=") - test("\x00\x01\x02", "AAEC") - test("\x00\x01\x02\x03", "AAECAw==") - test("\x00\x01\x02\x03\x04", "AAECAwQ=") - test("\x00\x01\x02\x03\x04\x05", "AAECAwQF") - test("\x00\x01\x02\x03\x04\x05\x06", "AAECAwQFBg==") - test("\x00\x01\x02\x03\x04\x05\x06\x07", "AAECAwQFBgc=") - for i = 1, 255 do - test(char(i), enc(char(i))) - end +if not unittest.testing() then + return _ENV +end - -- whitespace stripping - assert(dec(" AAEC A\r\nw==") == "\x00\x01\x02\x03") +test_suite = unittest.TestSuite:new() - -- extensive tests - if false then - local path = tmpname() - local file = open(path, "w") - local t = {} - for a = 0, 255, random(1, 7) do - for b = 0, 255, random(2, 7) do - for c = 0, 255, random(2, 7) do - t[#t+1] = char(a, b, c, 0xA) - file:write(t[#t]) - end - end - end - assert(file:close()) - local input = concat(t) - local output = enc(input) - local good = assert(popen("base64 < "..path, "r")):read("a"):gsub("%s", "") - remove(path) - assert(output == good) - assert(dec(output) == input) +local equal = unittest.equal +for i, test in ipairs({ + {"", ""}, + {"\x01", "AQ=="}, + {"\x00", "AA=="}, + {"\x00\x01", "AAE="}, + {"\x00\x01\x02", "AAEC"}, + {"\x00\x01\x02\x03", "AAECAw=="}, + {"\x00\x01\x02\x03\x04", "AAECAwQ="}, + {"\x00\x01\x02\x03\x04\x05", "AAECAwQF"}, + {"\x00\x01\x02\x03\x04\x05\x06", "AAECAwQFBg=="}, + {"\x00\x01\x02\x03\x04\x05\x06\x07", "AAECAwQFBgc="}, + }) do + test_suite:add_test(equal(enc(test[1]), test[2]), ("encoding string %d"):format(i)) + test_suite:add_test(equal(dec(test[2]), test[1]), ("decoding string %d"):format(i)) +end +for i = 1, 255 do + test_suite:add_test(equal(dec(enc(char(i))), char(i)), ("en/decoding char %d"):format(i)) +end + +-- whitespace stripping +test_suite:add_test(equal(dec(" AAEC A\r\nw=="), "\x00\x01\x02\x03"), "whitespace stripping") + +-- extensive tests +if false then + local path = tmpname() + local file = open(path, "w") + local t = {} + for a = 0, 255, random(1, 7) do + for b = 0, 255, random(2, 7) do + for c = 0, 255, random(2, 7) do + t[#t+1] = char(a, b, c, 0xA) + file:write(t[#t]) + end end + end + assert(file:close()) + local input = concat(t) + local output = enc(input) + local good = assert(popen("base64 < "..path, "r")):read("a"):gsub("%s", "") + remove(path) + assert(output == good) + assert(dec(output) == input) end return _ENV