xref: /freebsd/libexec/nuageinit/tests/decode_base64.lua (revision 68fd0feacb8ce5da9c47deea5505b3241b2ba6ec)
1#!/usr/libexec/flua
2---
3-- SPDX-License-Identifier: BSD-2-Clause
4--
5-- Copyright (c) 2026 Baptiste Daroussin <bapt@FreeBSD.org>
6
7local n = require("nuage")
8
9-- decode_base64 is not exported, test via addfile
10
11local function test_decode(input, expected)
12	local r, err = n.addfile({
13		content = input,
14		encoding = "base64",
15		path = "/tmp/nuage_test_b64"
16	}, false)
17	if not r then
18		n.err(err)
19	end
20	local root = os.getenv("NUAGE_FAKE_ROOTDIR")
21	if not root then
22		root = ""
23	end
24	local f = assert(io.open(root .. "/tmp/nuage_test_b64", "r"))
25	local str = f:read("*all")
26	f:close()
27	if str ~= expected then
28		n.err("base64 decode failed: expected '" .. expected
29			.. "' got '" .. str .. "'")
30	end
31end
32
33-- empty input
34test_decode("", "")
35
36-- single byte: 'a'
37test_decode("YQ==", "a")
38
39-- two bytes: 'ab'
40test_decode("YWI=", "ab")
41
42-- three bytes: 'abc'
43test_decode("YWJj", "abc")
44
45-- newline in base64
46test_decode("YmxhCg==", "bla\n")
47
48-- spaces should be ignored
49test_decode("Y Q = =", "a")
50
51-- b64 alias
52local r, err = n.addfile({
53	content = "YQ==",
54	encoding = "b64",
55	path = "/tmp/nuage_test_b64_b64"
56}, false)
57if not r then
58	n.err("b64 encoding alias should work: " .. tostring(err))
59end
60
61os.exit(0)
62