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 "File size check"
59xpass test $new_size -eq $(expr $bconf_size + $initrd_size + 9 + 12)
60
61echo "Apply command repeat test"
62xpass $BOOTCONF -a $TEMPCONF $INITRD
63
64echo "File size check"
65xpass test $new_size -eq $(stat -c %s $INITRD)
66
67echo "Delete command check"
68xpass $BOOTCONF -d $INITRD
69
70echo "File size check"
71new_size=$(stat -c %s $INITRD)
72xpass test $new_size -eq $initrd_size
73
74echo "No error messge while applying"
75dd if=/dev/zero of=$INITRD bs=4096 count=1
76printf " \0\0\0 \0\0\0" >> $INITRD
77$BOOTCONF -a $TEMPCONF $INITRD > $OUTFILE 2>&1
78xfail grep -i "failed" $OUTFILE
79xfail grep -i "error" $OUTFILE
80
81echo "Max node number check"
82
83echo -n > $TEMPCONF
84for i in `seq 1 1024` ; do
85   echo "node$i" >> $TEMPCONF
86done
87xpass $BOOTCONF -a $TEMPCONF $INITRD
88
89echo "badnode" >> $TEMPCONF
90xfail $BOOTCONF -a $TEMPCONF $INITRD
91
92echo "Max filesize check"
93
94# Max size is 32767 (including terminal byte)
95echo -n "data = \"" > $TEMPCONF
96dd if=/dev/urandom bs=768 count=32 | base64 -w0 >> $TEMPCONF
97echo "\"" >> $TEMPCONF
98xfail $BOOTCONF -a $TEMPCONF $INITRD
99
100truncate -s 32764 $TEMPCONF
101echo "\"" >> $TEMPCONF	# add 2 bytes + terminal ('\"\n\0')
102xpass $BOOTCONF -a $TEMPCONF $INITRD
103
104echo "Adding same-key values"
105cat > $TEMPCONF << EOF
106key = bar, baz
107key += qux
108EOF
109echo > $INITRD
110
111xpass $BOOTCONF -a $TEMPCONF $INITRD
112$BOOTCONF $INITRD > $OUTFILE
113xpass grep -q "bar" $OUTFILE
114xpass grep -q "baz" $OUTFILE
115xpass grep -q "qux" $OUTFILE
116
117echo "=== expected failure cases ==="
118for i in samples/bad-* ; do
119  xfail $BOOTCONF -a $i $INITRD
120done
121
122echo "=== expected success cases ==="
123for i in samples/good-* ; do
124  xpass $BOOTCONF -a $i $INITRD
125done
126
127echo
128if [ $NG -eq 0 ]; then
129	echo "All tests passed"
130else
131	echo "$NG tests failed"
132fi
133