Path: blob/main/libexec/nuageinit/tests/adddoas.lua
288923 views
#!/usr/libexec/flua1---2-- SPDX-License-Identifier: BSD-2-Clause3--4-- Copyright (c) 2026 Baptiste Daroussin <[email protected]>56local n = require("nuage")78local root = os.getenv("NUAGE_FAKE_ROOTDIR")9if not root then10root = ""11end1213local function get_localbase()14local f = io.popen("sysctl -in user.localbase 2> /dev/null")15local lb = f:read("*l")16f:close()17if lb == nil or lb:len() == 0 then18lb = "/usr/local"19end20return lb21end2223local function read_doasconf()24local path = root .. get_localbase() .. "/etc/doas.conf"25local f = io.open(path, "r")26if not f then27return nil28end29local content = f:read("*a")30f:close()31return content32end3334-- test with a single string rule with %u substitution35n.adddoas({ name = "testuser", doas = "permit persist %u as root" })36local content = read_doasconf()37if not content then38n.err("doas.conf not created")39end40if content ~= "permit persist testuser as root\n" then41n.err("unexpected doas.conf content with %u: '" .. content .. "'")42end4344-- remove file for next test45os.remove(root .. get_localbase() .. "/etc/doas.conf")4647-- test with a table of rules48n.adddoas({49name = "testuser",50doas = {51"deny %u as foobar",52"permit persist %u as root cmd whoami"53}54})55content = read_doasconf()56if not content then57n.err("doas.conf not created for table")58end59if content ~= "deny testuser as foobar\npermit persist testuser as root cmd whoami\n" then60n.err("unexpected doas.conf content for table: '" .. content .. "'")61end6263os.exit(0)646566