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