-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wip] Tests for dhcp info and interface address
* Test Files added
- Loading branch information
1 parent
2d35e5b
commit 277e109
Showing
8 changed files
with
505 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
utils = require('utils') | ||
nixio = require('nixio') | ||
ubus_lib = require('ubus') | ||
|
||
ubus = ubus_lib.connect() | ||
-- if not ubus then | ||
-- error('Failed to connect to ubusd') | ||
-- end | ||
|
||
interface_data = ubus:call('network.interface', 'dump', {}) | ||
nixio_data = nixio.getifaddrs() | ||
|
||
function find_default_gateway(routes) | ||
for i = 1, #routes do | ||
if routes[i].target == '0.0.0.0' then | ||
return routes[i].nexthop | ||
end | ||
end | ||
return nil | ||
end | ||
|
||
function new_address_array(address, interface, family) | ||
proto = interface['proto'] | ||
if proto == 'dhcpv6' then | ||
proto = 'dhcp' | ||
end | ||
new_address = { | ||
address = address['address'], | ||
mask = address['mask'], | ||
proto = proto, | ||
family = family, | ||
gateway = find_default_gateway(interface.route) | ||
} | ||
return new_address | ||
end | ||
|
||
-- collect interface addresses | ||
function get_addresses(name) | ||
addresses = {} | ||
interface_list = interface_data['interface'] | ||
addresses_list = {} | ||
for _, interface in pairs(interface_list) do | ||
if interface['l3_device'] == name then | ||
proto = interface['proto'] | ||
if proto == 'dhcpv6' then | ||
proto = 'dhcp' | ||
end | ||
for _, address in pairs(interface['ipv4-address']) do | ||
table.insert(addresses_list, address['address']) | ||
new_address = new_address_array(address, interface, 'ipv4') | ||
table.insert(addresses, new_address) | ||
end | ||
for _, address in pairs(interface['ipv6-address']) do | ||
table.insert(addresses_list, address['address']) | ||
new_address = new_address_array(address, interface, 'ipv6') | ||
table.insert(addresses, new_address) | ||
end | ||
end | ||
end | ||
for i = 1, #nixio_data do | ||
if nixio_data[i].name == name then | ||
if not is_excluded(name) then | ||
family = nixio_data[i].family | ||
addr = nixio_data[i].addr | ||
if family == 'inet' then | ||
family = 'ipv4' | ||
-- Since we don't already know this from the dump, we can | ||
-- consider this dynamically assigned, this is the case for | ||
-- example for OpenVPN interfaces, which get their address | ||
-- from the DHCP server embedded in OpenVPN | ||
proto = 'dhcp' | ||
elseif family == 'inet6' then | ||
family = 'ipv6' | ||
if utils.starts_with(addr, 'fe80') then | ||
proto = 'static' | ||
else | ||
ula = uci_cursor.get('network', 'globals', 'ula_prefix') | ||
ula_prefix = utils.split(ula, '::')[1] | ||
if utils.starts_with(addr, ula_prefix) then | ||
proto = 'static' | ||
else | ||
proto = 'dhcp' | ||
end | ||
end | ||
end | ||
if family == 'ipv4' or family == 'ipv6' then | ||
if not utils.has_value(addresses_list, addr) then | ||
table.insert(addresses, { | ||
address = addr, | ||
mask = nixio_data[i].prefix, | ||
proto = proto, | ||
family = family | ||
}) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
return addresses | ||
end | ||
|
||
print(get_addresses('eth2')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
uci = require('uci') | ||
uci_cursor = uci.cursor() | ||
|
||
utils = require('utils') | ||
|
||
function parse_dhcp_lease_file(path, leases) | ||
local f = io.open('tests/test_files/dhcp_leases.txt', 'r') | ||
if not f then | ||
return leases | ||
end | ||
|
||
for line in f:lines() do | ||
local expiry, mac, ip, name, id = line:match('(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)') | ||
table.insert(leases, { | ||
expiry = tonumber(expiry), | ||
mac = mac, | ||
ip = ip, | ||
client_name = name, | ||
client_id = id | ||
}) | ||
end | ||
|
||
return leases | ||
end | ||
|
||
function get_dhcp_leases() | ||
local dhcp_configs = uci_cursor:get_all('dhcp') | ||
local leases = {} | ||
|
||
if utils.is_table_empty(dhcp_configs) then | ||
return nil | ||
end | ||
|
||
for name, config in pairs(dhcp_configs) do | ||
if config and config['.type'] == 'dnsmasq' and config.leasefile then | ||
leases = parse_dhcp_lease_file(config.leasefile, leases) | ||
end | ||
end | ||
return leases | ||
end | ||
|
||
print(get_dhcp_leases()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package.path = package.path .. ";../?.lua" | ||
|
||
local address_data = require('test_files/address_data') | ||
|
||
local luaunit = require('luaunit') | ||
|
||
local test_file_dir = './test_files/' | ||
|
||
local interface_data = io.open('test_files/interface_data.lua'):read('*a') | ||
|
||
routes = { { | ||
mask = 0, | ||
nexthop = "192.168.0.1", | ||
source = "192.168.0.144/32", | ||
target = "0.0.0.0" | ||
} } | ||
|
||
ipv4_address = { | ||
address = "192.168.0.144", | ||
mask = 24 | ||
} | ||
|
||
interface = { | ||
autostart = true, | ||
available = true, | ||
data = { | ||
hostname = "08-00-27-4F-CB-2E", | ||
leasetime = 86400 | ||
}, | ||
delegation = true, | ||
device = "eth2", | ||
["dns-search"] = {}, | ||
["dns-server"] = { "192.168.0.1" }, | ||
dns_metric = 0, | ||
dynamic = false, | ||
inactive = { | ||
["dns-search"] = {}, | ||
["dns-server"] = {}, | ||
["ipv4-address"] = {}, | ||
["ipv6-address"] = {}, | ||
neighbors = {}, | ||
route = {} | ||
}, | ||
interface = "lan", | ||
["ipv4-address"] = { { | ||
address = "192.168.0.144", | ||
mask = 24 | ||
} }, | ||
["ipv6-address"] = {}, | ||
["ipv6-prefix"] = {}, | ||
["ipv6-prefix-assignment"] = {}, | ||
l3_device = "eth2", | ||
metric = 0, | ||
neighbors = {}, | ||
pending = false, | ||
proto = "dhcp", | ||
route = { { | ||
mask = 0, | ||
nexthop = "192.168.0.1", | ||
source = "192.168.0.144/32", | ||
target = "0.0.0.0" | ||
} }, | ||
up = true, | ||
updated = { "addresses", "routes", "data" }, | ||
uptime = 1973 | ||
} | ||
|
||
address_array = { | ||
address="192.168.0.144", | ||
family="ipv4", | ||
gateway="192.168.0.1", | ||
mask=24, | ||
proto="dhcp" | ||
} | ||
|
||
-- to be removed once address.lua is connected | ||
function find_default_gateway(routes) | ||
for i = 1, #routes do | ||
if routes[i].target == '0.0.0.0' then | ||
return routes[i].nexthop | ||
end | ||
end | ||
return nil | ||
end | ||
|
||
-- remove once address.lua is connected | ||
function new_address_array(address, interface, family) | ||
proto = interface['proto'] | ||
if proto == 'dhcpv6' then | ||
proto = 'dhcp' | ||
end | ||
new_address = { | ||
address = address['address'], | ||
mask = address['mask'], | ||
proto = proto, | ||
family = family, | ||
gateway = find_default_gateway(interface.route) | ||
} | ||
return new_address | ||
end | ||
|
||
function test_find_default_gateway() | ||
luaunit.assertEquals(find_default_gateway(address_data.routes), "192.168.0.1") | ||
end | ||
|
||
function test_new_address_array() | ||
luaunit.assertEquals(new_address_array(address_data.ipv4_address, address_data.eth2_interface, 'ipv4'), address_data.address_array) | ||
end | ||
|
||
os.exit( luaunit.LuaUnit.run() ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package.path = package.path .. ";../?.lua" | ||
|
||
local luaunit = require('luaunit') | ||
|
||
local dhcp = require('dhcp') | ||
|
||
local test_file_dir = './test_files/' | ||
|
||
uci_cursor.get_all = function(arg) | ||
if arg == 'dhcp' then | ||
return io.open(test_file_dir .. 'dhcp.txt') | ||
end | ||
end | ||
|
||
io.open = function(arg) | ||
if arg == '/tmp/dhcp.leases' then | ||
return io.open(test_file_dir .. 'dhcp_leases.txt') | ||
|
||
|
||
function test_get_dhcp_leases() | ||
|
||
end | ||
|
||
os.exit( luaunit.LuaUnit.run() ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
test_data = {} | ||
|
||
|
||
test_data.eth2_interface = { | ||
autostart = true, | ||
available = true, | ||
data = { | ||
hostname = "08-00-27-4F-CB-2E", | ||
leasetime = 86400 | ||
}, | ||
delegation = true, | ||
device = "eth2", | ||
["dns-search"] = {}, | ||
["dns-server"] = { "192.168.0.1" }, | ||
dns_metric = 0, | ||
dynamic = false, | ||
inactive = { | ||
["dns-search"] = {}, | ||
["dns-server"] = {}, | ||
["ipv4-address"] = {}, | ||
["ipv6-address"] = {}, | ||
neighbors = {}, | ||
route = {} | ||
}, | ||
interface = "lan", | ||
["ipv4-address"] = { { | ||
address = "192.168.0.144", | ||
mask = 24 | ||
} }, | ||
["ipv6-address"] = {}, | ||
["ipv6-prefix"] = {}, | ||
["ipv6-prefix-assignment"] = {}, | ||
l3_device = "eth2", | ||
metric = 0, | ||
neighbors = {}, | ||
pending = false, | ||
proto = "dhcp", | ||
route = { { | ||
mask = 0, | ||
nexthop = "192.168.0.1", | ||
source = "192.168.0.144/32", | ||
target = "0.0.0.0" | ||
} }, | ||
up = true, | ||
updated = { "addresses", "routes", "data" }, | ||
uptime = 1973 | ||
} | ||
|
||
test_data.routes = test_data.eth2_interface.route | ||
|
||
test_data.ipv4_address = test_data.eth2_interface['ipv4-address'][1] | ||
|
||
test_data.address_array = { | ||
address="192.168.0.144", | ||
family="ipv4", | ||
gateway="192.168.0.1", | ||
mask=24, | ||
proto="dhcp" | ||
} | ||
|
||
return test_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
subnet 239.252.197.0 netmask 255.255.255.0 { | ||
range 239.252.197.10 239.252.197.107; | ||
default-lease-time 600; | ||
max-lease-time 7200; | ||
} |
Oops, something went wrong.