summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJo-Philipp Wich2022-06-13 13:23:23 +0000
committerJo-Philipp Wich2022-06-14 14:54:06 +0000
commit5994466353ecbd4e6fac738aa956b2cbd9f6308b (patch)
treee75b82ff402d9711756a79e44635144cf31a31f3
parent880dd31353c8db8bad4b193cc4928ba01ff29c78 (diff)
downloadfirewall4-5994466353ecbd4e6fac738aa956b2cbd9f6308b.tar.gz
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 <jo@mein.io>
-rw-r--r--root/usr/share/ucode/fw4.uc11
-rw-r--r--tests/01_configuration/01_ruleset4
-rw-r--r--tests/lib/mocklib/fs.uc16
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 <null>
[call] fs.popen cmdline </usr/sbin/nft --terse --json list flowtables inet> mode <r>
-[call] fs.open path </sys/class/net/br-lan/flags> mode <r>
-[call] fs.open path </sys/class/net/br-lan/flags> mode <r>
+[call] fs.readfile path </sys/class/net/br-lan/flags> limit <null>
+[call] fs.readfile path </sys/class/net/br-lan/flags> limit <null>
-- 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),