local LOG = table.concat({
"2023-11-14T22:13:20Z INFO engine builds the state in 4 ms",
"2023-11-14T22:13:21Z WARN fs denied: /etc/shadow is outside the granted read roots",
"2023-11-14T22:13:22Z ERROR proc denied: curl is not on the executable allowlist",
"2023-11-14T22:13:23Z INFO engine evaluates crates/airsl/examples/text-toolkit/toolkit.lua",
"2023-11-14T22:13:24Z WARN hash denied: /var/db/keys is outside the granted read roots",
}, "\n")
local TARGET = airsstack.path.join("crates", "airsl", "examples", "text-toolkit", "toolkit.lua")
print("regex")
local lines = airsstack.regex.split([[\n]], LOG)
local entry = airsstack.regex.compile(
[[^(?<stamp>\S+) (?<level>INFO|WARN|ERROR)\s+(?<module>\w+) (?<message>.*)$]]
)
local counts, stamps, named_and_numbered_agree = {}, {}, true
for _, line in ipairs(lines) do
assert(entry.is_match(line), "the corpus and the pattern disagree: " .. line)
local caught = entry.captures(line)
named_and_numbered_agree = named_and_numbered_agree and caught[3] == caught.module
counts[caught.level] = (counts[caught.level] or 0) + 1
stamps[#stamps + 1] = caught.stamp
end
print(" split gave " .. #lines .. " lines, all matched by one compiled pattern")
local levels = {}
for level in pairs(counts) do
levels[#levels + 1] = level
end
table.sort(levels)
for index, level in ipairs(levels) do
levels[index] = level .. "=" .. counts[level]
end
print(" levels: " .. table.concat(levels, " "))
print(" group 3 and the name `module` are the same span: " .. tostring(named_and_numbered_agree))
local found = airsstack.regex.find_all([[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z]], LOG)
print(" find_all timestamps: " .. #found .. ", first " .. found[1])
print(" replace_all: " .. airsstack.regex.replace_all([[(denied: )/\S+]], lines[2], "${1}<path>"))
print("")
print("path")
print(" join: " .. TARGET)
print(" dirname: " .. airsstack.path.dirname(TARGET))
print(" basename: " .. airsstack.path.basename(TARGET))
print(" stem / ext: " .. airsstack.path.stem(TARGET) .. " / " .. airsstack.path.ext(TARGET))
local dotted = "crates/airsl/../airsl/./examples//toolkit.lua"
print(" normalize: " .. airsstack.path.normalize(dotted))
print(" relative_to: " .. airsstack.path.relative_to(TARGET, "crates/airsl"))
local outside = pcall(airsstack.path.relative_to, TARGET, "crates/other")
print(" relative_to refuses a path outside its base: " .. tostring(not outside))
print(
" is_absolute: "
.. tostring(airsstack.path.is_absolute(TARGET))
.. " here, "
.. tostring(airsstack.path.is_absolute("/etc/hosts"))
.. " for /etc/hosts"
)
print("")
print("hash")
local body = "the grant is checked inside the host function, before the operation.\n"
print(" sha256: " .. airsstack.hash.sha256(body))
print(" sha1: " .. airsstack.hash.sha1(body))
print(" cache key from sha1(path): " .. string.sub(airsstack.hash.sha1(TARGET), 1, 8))
print(" hex: " .. airsstack.hash.hex("airsl"))
print("")
print("time")
local seconds = airsstack.time.parse(stamps[1])
print(" parse " .. stamps[1] .. " -> " .. seconds)
print(" format: " .. airsstack.time.format(seconds))
print(" format %Y-%m-%d: " .. airsstack.time.format(seconds, "%Y-%m-%d"))
local round_tripped = airsstack.time.parse(airsstack.time.format(seconds))
print(" parse(format(t)) == t: " .. tostring(round_tripped == seconds))
local before = airsstack.time.monotonic()
local sum = 0
for i = 1, 10000 do
sum = sum + i
end
assert(sum == 50005000, "the loop between the two readings did not run")
local after = airsstack.time.monotonic()
print(" monotonic is non-decreasing across two reads: " .. tostring(after >= before))
print("")
print("glob")
for _, pattern in ipairs({ "*.lua", "**/*.lua", "crates/**/text-toolkit/*.lua", "**/*.rs" }) do
print(string.format(" %-30s %s", pattern, tostring(airsstack.glob.match(pattern, TARGET))))
end
local bare = airsstack.glob.match("*.lua", "toolkit.lua")
print(string.format(" %-30s %s", "*.lua, against toolkit.lua", tostring(bare)))