1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-only
3
4echo "Boot config test script"
5
6if [ -d "$1" ]; then
7  TESTDIR=$1
8else
9  TESTDIR=.
10fi
11BOOTCONF=${TESTDIR}/bootconfig
12ALIGN=4
13
14INITRD=`mktemp ${TESTDIR}/initrd-XXXX`
15TEMPCONF=`mktemp ${TESTDIR}/temp-XXXX.bconf`
16OUTFILE=`mktemp ${TESTDIR}/tempout-XXXX`
17NG=0
18
19cleanup() {
20  rm -f $INITRD $TEMPCONF $OUTFILE
21  exit $NG
22}
23
24trap cleanup EXIT TERM
25
26NO=1
27
28xpass() { # pass test command
29  echo "test case $NO ($*)... "
30  if ! ($@ && echo "\t\t[OK]"); then
31     echo "\t\t[NG]"; NG=$((NG + 1))
32  fi
33  NO=$((NO + 1))
34}
35
36xfail() { # fail test command
37  echo "test case $NO ($*)... "
38  if ! (! $@ && echo "\t\t[OK]"); then
39     echo "\t\t[NG]"; NG=$((NG + 1))
40  fi
41  NO=$((NO + 1))
42}
43
44echo "Basic command test"
45xpass $BOOTCONF $INITRD
46
47echo "Delete command should success without bootconfig"
48xpass $BOOTCONF -d $INITRD
49
50dd if=/dev/zero of=$INITRD bs=4096 count=1
51echo "key = value;" > $TEMPCONF
52bconf_size=$(stat -c %s $TEMPCONF)
53initrd_size=$(stat -c %s $INITRD)
54
55echo "Apply command test"
56xpass $BOOTCONF -a $TEMPCONF $INITRD
57new_size=$(stat -c %s $INITRD)
58
59echo "Show command test"
60xpass $BOOTCONF $INITRD
61
62echo "File size check"
63total_size=$(expr $bconf_size + $initrd_size + 9 + 12 + $ALIGN - 1 )
64total_size=$(expr $total_size / $ALIGN)
65total_size=$(expr $total_size \* $ALIGN)
66xpass test $new_size -eq $total_size
67
68echo "Apply command repeat test"
69xpass $BOOTCONF -a $TEMPCONF $INITRD
70
71echo "File size check"
72xpass test $new_size -eq $(stat -c %s $INITRD)
73
74echo "Delete command check"
75xpass $BOOTCONF -d $INITRD
76
77echo "File size check"
78new_size=$(stat -c %s $INITRD)
79xpass test $new_size -eq $initrd_size
80
81echo "No error messge while applying"
82dd if=/dev/zero of=$INITRD bs=4096 count=1
83printf " \0\0\0 \0\0\0" >> $INITRD
84$BOOTCONF -a $TEMPCONF $INITRD > $OUTFILE 2>&1
85xfail grep -i "failed" $OUTFILE
86xfail grep -i "error" $OUTFILE
87
88echo "Max node number check"
89
90echo -n > $TEMPCONF
91for i in `seq 1 1024` ; do
92   echo "node$i" >> $TEMPCONF
93done
94xpass $BOOTCONF -a $TEMPCONF $INITRD
95
96echo "badnode" >> $TEMPCONF
97xfail $BOOTCONF -a $TEMPCONF $INITRD
98
99echo "Max filesize check"
100
101# Max size is 32767 (including terminal byte)
102echo -n "data = \"" > $TEMPCONF
103dd if=/dev/urandom bs=768 count=32 | base64 -w0 >> $TEMPCONF
104echo "\"" >> $TEMPCONF
105xfail $BOOTCONF -a $TEMPCONF $INITRD
106
107truncate -s 32764 $TEMPCONF
108echo "\"" >> $TEMPCONF	# add 2 bytes + terminal ('\"\n\0')
109xpass $BOOTCONF -a $TEMPCONF $INITRD
110
111echo "Adding same-key values"
112cat > $TEMPCONF << EOF
113key = bar, baz
114key += qux
115EOF
116echo > $INITRD
117
118xpass $BOOTCONF -a $TEMPCONF $INITRD
119$BOOTCONF $INITRD > $OUTFILE
120xpass grep -q "bar" $OUTFILE
121xpass grep -q "baz" $OUTFILE
122xpass grep -q "qux" $OUTFILE
123
124echo "Override same-key values"
125cat > $TEMPCONF << EOF
126key = bar, baz
127key := qux
128EOF
129echo > $INITRD
130
131xpass $BOOTCONF -a $TEMPCONF $INITRD
132$BOOTCONF $INITRD > $OUTFILE
133xfail grep -q "bar" $OUTFILE
134xfail grep -q "baz" $OUTFILE
135xpass grep -q "qux" $OUTFILE
136
137echo "Double/single quotes test"
138echo "key = '\"string\"';" > $TEMPCONF
139$BOOTCONF -a $TEMPCONF $INITRD
140$BOOTCONF $INITRD > $TEMPCONF
141cat $TEMPCONF
142xpass grep \'\"string\"\' $TEMPCONF
143
144echo "Repeat same-key tree"
145cat > $TEMPCONF << EOF
146foo
147bar
148foo { buz }
149EOF
150echo > $INITRD
151
152xpass $BOOTCONF -a $TEMPCONF $INITRD
153$BOOTCONF $INITRD > $OUTFILE
154xpass grep -q "bar" $OUTFILE
155
156
157echo "Remove/keep tailing spaces"
158cat > $TEMPCONF << EOF
159foo = val     # comment
160bar = "val2 " # comment
161EOF
162echo > $INITRD
163
164xpass $BOOTCONF -a $TEMPCONF $INITRD
165$BOOTCONF $INITRD > $OUTFILE
166xfail grep -q val[[:space:]] $OUTFILE
167xpass grep -q val2[[:space:]] $OUTFILE
168
169echo "=== expected failure cases ==="
170for i in samples/bad-* ; do
171  xfail $BOOTCONF -a $i $INITRD
172done
173
174echo "=== expected success cases ==="
175for i in samples/good-* ; do
176  xpass $BOOTCONF -a $i $INITRD
177done
178
179
180echo
181echo "=== Summary ==="
182echo "# of Passed: $(expr $NO - $NG - 1)"
183echo "# of Failed: $NG"
184
185echo
186if [ $NG -eq 0 ]; then
187	echo "All tests passed"
188else
189	echo "$NG tests failed"
190	exit 1
191fi
192