| 1 |
-- inet addr tests |
| 2 |
|
| 3 |
context("Inet addr check functions", function() |
| 4 |
local ok, ffi = pcall(require, "ffi") |
| 5 |
if not ok then |
| 6 |
ffi = require("cffi") |
| 7 |
end |
| 8 |
|
| 9 |
ffi.cdef[[ |
| 10 |
typedef struct rspamd_inet_addr_s rspamd_inet_addr_t; |
| 11 |
bool rspamd_parse_inet_address (rspamd_inet_addr_t **target, |
| 12 |
const char *src, size_t len); |
| 13 |
void rspamd_inet_address_free (rspamd_inet_addr_t *addr); |
| 14 |
]] |
| 15 |
|
| 16 |
local cases = { |
| 17 |
{'192.168.1.1', true}, |
| 18 |
{'2a01:4f8:190:43b5::99', true}, |
| 19 |
{'256.1.1.1', false}, |
| 20 |
{'/tmp/socket', true}, |
| 21 |
{'./socket', true}, |
| 22 |
{'[fe80::f919:8b26:ff93:3092%5]', true}, |
| 23 |
{'[fe80::f919:8b26:ff93:3092]', true}, |
| 24 |
{'IPv6:::1', true}, |
| 25 |
{'IPv6:[::1]', true}, |
| 26 |
{'IPv6[:::1]', false}, |
| 27 |
{'[::]', true}, |
| 28 |
{'[1::]', true}, |
| 29 |
{'[000:01:02:003:004:5:6:007]', true}, -- leading zeros |
| 30 |
{'[A:b:c:DE:fF:0:1:aC]', true}, -- mixed case |
| 31 |
{'[::192.168.0.1]', true}, -- embedded ipv4 |
| 32 |
{'[1:2:192.168.0.1:5:6]', false}, -- poor octets |
| 33 |
{'[::ffff:192.1.2]', false}, -- ipv4 without last octet (maybe should be true?) |
| 34 |
{'[0:0::0:0:8]', true}, -- bogus zeros |
| 35 |
{'[::192.168.0.0.1]', false}, -- invalid mapping |
| 36 |
} |
| 37 |
|
| 38 |
for i,c in ipairs(cases) do |
| 39 |
test("Create inet addr from string " .. c[1] .. '; expect ' .. tostring(c[2]), function() |
| 40 |
local ip = ffi.new("rspamd_inet_addr_t* [1]"); |
| 41 |
local res = ffi.C.rspamd_parse_inet_address(ip, c[1], #c[1]) |
| 42 |
assert_equal(res, c[2], "Expect " .. tostring(c[2]) .. " while parsing " .. c[1]) |
| 43 |
if res then |
| 44 |
ffi.C.rspamd_inet_address_free(ip[0]) |
| 45 |
end |
| 46 |
end) |
| 47 |
|
| 48 |
end |
| 49 |
end) |