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
90awk '
91BEGIN {
92  for (i = 0; i < 26; i += 1)
93      printf("%c\n", 65 + i % 26)
94  for (i = 26; i < 8192; i += 1)
95      printf("%c%c%c\n", 65 + i % 26, 65 + (i / 26) % 26, 65 + (i / 26 / 26))
96}
97' > $TEMPCONF
98xpass $BOOTCONF -a $TEMPCONF $INITRD
99
100echo "badnode" >> $TEMPCONF
101xfail $BOOTCONF -a $TEMPCONF $INITRD
102
103echo "Max filesize check"
104
105# Max size is 32767 (including terminal byte)
106echo -n "data = \"" > $TEMPCONF
107dd if=/dev/urandom bs=768 count=32 | base64 -w0 >> $TEMPCONF
108echo "\"" >> $TEMPCONF
109xfail $BOOTCONF -a $TEMPCONF $INITRD
110
111truncate -s 32764 $TEMPCONF
112echo "\"" >> $TEMPCONF	# add 2 bytes + terminal ('\"\n\0')
113xpass $BOOTCONF -a $TEMPCONF $INITRD
114
115echo "Adding same-key values"
116cat > $TEMPCONF << EOF
117key = bar, baz
118key += qux
119EOF
120echo > $INITRD
121
122xpass $BOOTCONF -a $TEMPCONF $INITRD
123$BOOTCONF $INITRD > $OUTFILE
124xpass grep -q "bar" $OUTFILE
125xpass grep -q "baz" $OUTFILE
126xpass grep -q "qux" $OUTFILE
127
128echo "Override same-key values"
129cat > $TEMPCONF << EOF
130key = bar, baz
131key := qux
132EOF
133echo > $INITRD
134
135xpass $BOOTCONF -a $TEMPCONF $INITRD
136$BOOTCONF $INITRD > $OUTFILE
137xfail grep -q "bar" $OUTFILE
138xfail grep -q "baz" $OUTFILE
139xpass grep -q "qux" $OUTFILE
140
141echo "Double/single quotes test"
142echo "key = '\"string\"';" > $TEMPCONF
143$BOOTCONF -a $TEMPCONF $INITRD
144$BOOTCONF $INITRD > $TEMPCONF
145cat $TEMPCONF
146xpass grep \'\"string\"\' $TEMPCONF
147
148echo "Repeat same-key tree"
149cat > $TEMPCONF << EOF
150foo
151bar
152foo { buz }
153EOF
154echo > $INITRD
155
156xpass $BOOTCONF -a $TEMPCONF $INITRD
157$BOOTCONF $INITRD > $OUTFILE
158xpass grep -q "bar" $OUTFILE
159
160
161echo "Remove/keep tailing spaces"
162cat > $TEMPCONF << EOF
163foo = val     # comment
164bar = "val2 " # comment
165EOF
166echo > $INITRD
167
168xpass $BOOTCONF -a $TEMPCONF $INITRD
169$BOOTCONF $INITRD > $OUTFILE
170xfail grep -q val[[:space:]] $OUTFILE
171xpass grep -q val2[[:space:]] $OUTFILE
172
173echo "=== expected failure cases ==="
174for i in samples/bad-* ; do
175  xfail $BOOTCONF -a $i $INITRD
176done
177
178echo "=== expected success cases ==="
179for i in samples/good-* ; do
180  xpass $BOOTCONF -a $i $INITRD
181done
182
183
184echo
185echo "=== Summary ==="
186echo "# of Passed: $(expr $NO - $NG - 1)"
187echo "# of Failed: $NG"
188
189echo
190if [ $NG -eq 0 ]; then
191	echo "All tests passed"
192else
193	echo "$NG tests failed"
194	exit 1
195fi
196