1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 04:31:29 +00:00

Convert base32 and base64 tests to unittest tests

This commit is contained in:
dmiller
2018-08-28 16:58:02 +00:00
parent 880f883029
commit ed26487ae2
2 changed files with 97 additions and 79 deletions

View File

@@ -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,30 +187,38 @@ 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 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 = {}
@@ -227,7 +237,6 @@ do
remove(path)
assert(output == good)
assert(dec(output) == input)
end
end
return _ENV

View File

@@ -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,29 +149,37 @@ 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 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 = {}
@@ -188,7 +198,6 @@ do
remove(path)
assert(output == good)
assert(dec(output) == input)
end
end
return _ENV