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