From 5994466353ecbd4e6fac738aa956b2cbd9f6308b Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Mon, 13 Jun 2022 15:23:23 +0200 Subject: [PATCH] fw4: simplify `is_loopback_dev()` Use `fs.readfile()` to simplify the code reading flag values from sysfs. Also add a mock implementation of `fs.readfile()` using the same mock data files as `fs.open()`. Signed-off-by: Jo-Philipp Wich --- root/usr/share/ucode/fw4.uc | 11 +---------- tests/01_configuration/01_ruleset | 4 ++-- tests/lib/mocklib/fs.uc | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/root/usr/share/ucode/fw4.uc b/root/usr/share/ucode/fw4.uc index d600528..db1e580 100644 --- a/root/usr/share/ucode/fw4.uc +++ b/root/usr/share/ucode/fw4.uc @@ -1651,16 +1651,7 @@ return { }, is_loopback_dev: function(dev) { - let fd = fs.open(`/sys/class/net/${dev}/flags`, "r"); - - if (!fd) - return false; - - let flags = +fd.read("line"); - - fd.close(); - - return !!(flags & 0x8); + return !!(+fs.readfile(`/sys/class/net/${dev}/flags`) & 0x8); }, is_loopback_addr: function(addr) { diff --git a/tests/01_configuration/01_ruleset b/tests/01_configuration/01_ruleset index dd9750c..1bf8f72 100644 --- a/tests/01_configuration/01_ruleset +++ b/tests/01_configuration/01_ruleset @@ -316,6 +316,6 @@ table inet fw4 { } ' 2>/dev/null> timeout [call] fs.popen cmdline mode -[call] fs.open path mode -[call] fs.open path mode +[call] fs.readfile path limit +[call] fs.readfile path limit -- End -- diff --git a/tests/lib/mocklib/fs.uc b/tests/lib/mocklib/fs.uc index 3cb6252..10f3074 100644 --- a/tests/lib/mocklib/fs.uc +++ b/tests/lib/mocklib/fs.uc @@ -135,6 +135,22 @@ return { }; }, + readfile: (fpath, limit) => { + let path = sprintf("fs/open~%s.txt", replace(fpath, /[^A-Za-z0-9_-]+/g, '_')), + mock = mocklib.read_data_file(path); + + if (!mock) { + mocklib.I("No stdout fixture defined for fs.readfile() path %s.", fpath); + mocklib.I("Provide a mock output through the following text file:\n%s\n", path); + + return null; + } + + mocklib.trace_call("fs", "readfile", { path: fpath, limit }); + + return limit ? substr(mock, 0, limit) : mock; + }, + opendir: (path) => { let file = sprintf("fs/opendir~%s.json", replace(path, /[^A-Za-z0-9_-]+/g, '_')), mock = mocklib.read_json_file(file), -- 2.30.2