xref: /freebsd/libexec/nuageinit/tests/nuage.sh (revision 8f9aabbdbcd55b25b698bd762e8693d43f295bbd)
1#-
2# Copyright (c) 2022-2025 Baptiste Daroussin <bapt@FreeBSD.org>
3# Copyright (c) 2025 Jesús Daniel Colmenares Oviedo <dtxdf@FreeBSD.org>
4#
5# SPDX-License-Identifier: BSD-2-Clause
6#
7
8export NUAGE_FAKE_ROOTDIR="$PWD"
9
10atf_test_case sethostname
11atf_test_case settimezone
12atf_test_case addsshkey
13atf_test_case adduser
14atf_test_case adduser_passwd
15atf_test_case addgroup
16atf_test_case addfile
17atf_test_case decode_base64
18atf_test_case addsudo
19atf_test_case adddoas
20atf_test_case update_sshd_config
21
22settimezone_body()
23{
24	atf_check /usr/libexec/flua $(atf_get_srcdir)/settimezone.lua
25	if [ ! -f etc/localtime ]; then
26		atf_fail "localtime not written"
27	fi
28}
29
30sethostname_body()
31{
32	atf_check -e ignore /usr/libexec/flua $(atf_get_srcdir)/sethostname.lua
33	if [ ! -f etc/rc.conf.d/hostname ]; then
34		atf_fail "hostname not written"
35	fi
36	atf_check -o inline:"hostname='myhostname'\n" cat etc/rc.conf.d/hostname
37}
38
39addsshkey_body()
40{
41	atf_check /usr/libexec/flua $(atf_get_srcdir)/addsshkey.lua
42	if [ ! -f .ssh/authorized_keys ]; then
43		atf_fail "ssh key not added"
44	fi
45	atf_check -o inline:"40700\n" stat -f %p .ssh
46	atf_check -o inline:"100600\n" stat -f %p .ssh/authorized_keys
47	atf_check -o inline:"mykey\n" cat .ssh/authorized_keys
48	atf_check /usr/libexec/flua $(atf_get_srcdir)/addsshkey.lua
49	atf_check -o inline:"mykey\nmykey\n" cat .ssh/authorized_keys
50}
51
52adduser_head()
53{
54	atf_set "require.user" root
55}
56adduser_body()
57{
58	mkdir etc
59	printf "root:*:0:0::0:0:Charlie &:/root:/bin/sh\n" > etc/master.passwd
60	pwd_mkdb -d etc etc/master.passwd
61	printf "wheel:*:0:root\n" > etc/group
62	atf_check -e inline:"nuageinit: Argument should be a table\nnuageinit: Argument should be a table\n" /usr/libexec/flua $(atf_get_srcdir)/adduser.lua
63	test -d home/impossible_username || atf_fail "home not created"
64	atf_check -o inline:"impossible_username::1001:1001::0:0:impossible_username User:/home/impossible_username:/bin/sh\n" grep impossible_username etc/master.passwd
65}
66
67adduser_passwd_body()
68{
69	mkdir etc
70	printf "root:*:0:0::0:0:Charlie &:/root:/bin/sh\n" > etc/master.passwd
71	pwd_mkdb -d etc etc/master.passwd
72	printf "wheel:*:0:root\n" > etc/group
73	atf_check /usr/libexec/flua $(atf_get_srcdir)/adduser_passwd.lua
74	test -d home/foo || atf_fail "home not created"
75	passhash=`awk -F ':' '/^foo:/ {print $2}' etc/master.passwd`
76	atf_check -s exit:0 -o inline:$passhash \
77		$(atf_get_srcdir)/crypt $passhash "bar"
78	passhash=`awk -F ':' '/^foocrypted:/ {print $2}' etc/master.passwd`
79	atf_check -s exit:0 -o inline:$passhash \
80		$(atf_get_srcdir)/crypt $passhash "barcrypted"
81}
82
83addgroup_body()
84{
85	mkdir etc
86	printf "wheel:*:0:root\n" > etc/group
87	atf_check -e inline:"nuageinit: Argument should be a table\nnuageinit: Argument should be a table\n" /usr/libexec/flua $(atf_get_srcdir)/addgroup.lua
88	atf_check -o inline:"impossible_groupname:*:1001:\n" grep impossible_groupname etc/group
89}
90
91addfile_body()
92{
93	mkdir tmp
94	atf_check /usr/libexec/flua $(atf_get_srcdir)/addfile.lua
95}
96
97decode_base64_body()
98{
99	mkdir tmp
100	atf_check /usr/libexec/flua $(atf_get_srcdir)/decode_base64.lua
101}
102
103addsudo_body()
104{
105	atf_check /usr/libexec/flua $(atf_get_srcdir)/addsudo.lua
106}
107
108adddoas_body()
109{
110	atf_check /usr/libexec/flua $(atf_get_srcdir)/adddoas.lua
111}
112
113update_sshd_config_body()
114{
115	mkdir -p etc/ssh
116	atf_check /usr/libexec/flua $(atf_get_srcdir)/update_sshd_config.lua
117}
118
119atf_init_test_cases()
120{
121	atf_add_test_case sethostname
122	atf_add_test_case addsshkey
123	atf_add_test_case adduser
124	atf_add_test_case adduser_passwd
125	atf_add_test_case addgroup
126	atf_add_test_case addfile
127	atf_add_test_case decode_base64
128	atf_add_test_case addsudo
129	atf_add_test_case adddoas
130	atf_add_test_case update_sshd_config
131}
132