diff --git a/openwisp-monitoring/files/lib/openwisp-monitoring/dhcp.lua b/openwisp-monitoring/files/lib/openwisp-monitoring/dhcp.lua index 1edffd9..af4f0d7 100644 --- a/openwisp-monitoring/files/lib/openwisp-monitoring/dhcp.lua +++ b/openwisp-monitoring/files/lib/openwisp-monitoring/dhcp.lua @@ -11,13 +11,16 @@ function dhcp.parse_dhcp_lease_file(path, leases) 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, { + local lease = { expiry = tonumber(expiry), mac = mac, ip = ip, client_name = name, client_id = id - }) + } + if not utils.is_table_empty(lease) then + table.insert(leases, lease) + end end return leases diff --git a/openwisp-monitoring/tests/test_dhcp.lua b/openwisp-monitoring/tests/test_dhcp.lua index 10c76c5..700acd0 100644 --- a/openwisp-monitoring/tests/test_dhcp.lua +++ b/openwisp-monitoring/tests/test_dhcp.lua @@ -5,11 +5,23 @@ local cjson = require('cjson') local luaunit = require('luaunit') local dhcp_data = require('test_files/dhcp_data') +local dhcp_open = function(arg) + local test_file_dir = './test_files/' + if arg == '/tmp/dhcp.leases' then + return io.open(test_file_dir .. 'dhcp_leases.txt') + elseif arg == '/tmp/dhcp.leases.irregular' then + return io.open(test_file_dir .. 'dhcp_leases_irregular.txt') + else + return nil + end +end + TestDhcp = { setUp = function() local env = require('main_env') package.loaded.uci = env.uci package.loaded.io = env.io + package.loaded.io.open = dhcp_open end, tearDown = function() end } @@ -17,7 +29,6 @@ TestDhcp = { TestNetJSON = { setUp = function() local env = require('basic_env') - local test_file_dir = './test_files/' package.loaded.ubus = env.ubus package.loaded.io = { popen = function(arg) @@ -30,13 +41,7 @@ TestNetJSON = { f:seek('set', 0) return f end, - open = function(arg) - if arg == '/tmp/dhcp.leases' then - return io.open(test_file_dir .. 'dhcp_leases.txt') - else - return nil - end - end, + open = dhcp_open, write = function(...) return nil end } package.loaded.uci = { @@ -78,4 +83,19 @@ function TestNetJSON.test_dhcp() luaunit.assertEquals(netjson["dhcp_leases"][1]["expiry"], 1620788343) end +function TestDhcp.test_dhcp_leases_irregular() + local dhcp_functions = require('dhcp') + local expected = { + { + client_id = "01:c6:df:44:00:00:00", + client_name = "*", + expiry = 1705556643, + ip = "192.168.1.163", + mac = "c6:df:44:00:00:00" + } + } + local parsed = dhcp_functions.parse_dhcp_lease_file('/tmp/dhcp.leases.irregular', {}) + luaunit.assertEquals(parsed, expected) +end + os.exit(luaunit.LuaUnit.run()) diff --git a/openwisp-monitoring/tests/test_files/dhcp_leases_irregular.txt b/openwisp-monitoring/tests/test_files/dhcp_leases_irregular.txt new file mode 100644 index 0000000..d83fc9a --- /dev/null +++ b/openwisp-monitoring/tests/test_files/dhcp_leases_irregular.txt @@ -0,0 +1,2 @@ +1705556643 c6:df:44:00:00:00 192.168.1.163 * 01:c6:df:44:00:00:00 +duid 00:01:00:01:00:00:00:00:00:a0:26:36:01:90