xref: /openbmc/linux/tools/bootconfig/test-bootconfig.sh (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1081c6536SMasami Hiramatsu#!/bin/sh
2081c6536SMasami Hiramatsu# SPDX-License-Identifier: GPL-2.0-only
3081c6536SMasami Hiramatsu
4081c6536SMasami Hiramatsuecho "Boot config test script"
5081c6536SMasami Hiramatsu
6306b69dcSMasami Hiramatsuif [ -d "$1" ]; then
7306b69dcSMasami Hiramatsu  TESTDIR=$1
8306b69dcSMasami Hiramatsuelse
9306b69dcSMasami Hiramatsu  TESTDIR=.
10306b69dcSMasami Hiramatsufi
11306b69dcSMasami HiramatsuBOOTCONF=${TESTDIR}/bootconfig
12e1cef2d4SMasami HiramatsuALIGN=4
13306b69dcSMasami Hiramatsu
14306b69dcSMasami HiramatsuINITRD=`mktemp ${TESTDIR}/initrd-XXXX`
15306b69dcSMasami HiramatsuTEMPCONF=`mktemp ${TESTDIR}/temp-XXXX.bconf`
16306b69dcSMasami HiramatsuOUTFILE=`mktemp ${TESTDIR}/tempout-XXXX`
17081c6536SMasami HiramatsuNG=0
18081c6536SMasami Hiramatsu
19081c6536SMasami Hiramatsucleanup() {
205f811c57SMasami Hiramatsu  rm -f $INITRD $TEMPCONF $OUTFILE
21081c6536SMasami Hiramatsu  exit $NG
22081c6536SMasami Hiramatsu}
23081c6536SMasami Hiramatsu
24081c6536SMasami Hiramatsutrap cleanup EXIT TERM
25081c6536SMasami Hiramatsu
26081c6536SMasami HiramatsuNO=1
27081c6536SMasami Hiramatsu
28081c6536SMasami Hiramatsuxpass() { # pass test command
2947914d4eSMasami Hiramatsu  echo "test case $NO ($*)... "
30081c6536SMasami Hiramatsu  if ! ($@ && echo "\t\t[OK]"); then
31081c6536SMasami Hiramatsu     echo "\t\t[NG]"; NG=$((NG + 1))
32081c6536SMasami Hiramatsu  fi
33081c6536SMasami Hiramatsu  NO=$((NO + 1))
34081c6536SMasami Hiramatsu}
35081c6536SMasami Hiramatsu
36081c6536SMasami Hiramatsuxfail() { # fail test command
3747914d4eSMasami Hiramatsu  echo "test case $NO ($*)... "
38081c6536SMasami Hiramatsu  if ! (! $@ && echo "\t\t[OK]"); then
39081c6536SMasami Hiramatsu     echo "\t\t[NG]"; NG=$((NG + 1))
40081c6536SMasami Hiramatsu  fi
41081c6536SMasami Hiramatsu  NO=$((NO + 1))
42081c6536SMasami Hiramatsu}
43081c6536SMasami Hiramatsu
44081c6536SMasami Hiramatsuecho "Basic command test"
45081c6536SMasami Hiramatsuxpass $BOOTCONF $INITRD
46081c6536SMasami Hiramatsu
47081c6536SMasami Hiramatsuecho "Delete command should success without bootconfig"
48081c6536SMasami Hiramatsuxpass $BOOTCONF -d $INITRD
49081c6536SMasami Hiramatsu
50081c6536SMasami Hiramatsudd if=/dev/zero of=$INITRD bs=4096 count=1
51081c6536SMasami Hiramatsuecho "key = value;" > $TEMPCONF
52081c6536SMasami Hiramatsubconf_size=$(stat -c %s $TEMPCONF)
53081c6536SMasami Hiramatsuinitrd_size=$(stat -c %s $INITRD)
54081c6536SMasami Hiramatsu
55081c6536SMasami Hiramatsuecho "Apply command test"
56081c6536SMasami Hiramatsuxpass $BOOTCONF -a $TEMPCONF $INITRD
57081c6536SMasami Hiramatsunew_size=$(stat -c %s $INITRD)
58081c6536SMasami Hiramatsu
595414251aSMasami Hiramatsuecho "Show command test"
605414251aSMasami Hiramatsuxpass $BOOTCONF $INITRD
615414251aSMasami Hiramatsu
62081c6536SMasami Hiramatsuecho "File size check"
63e1cef2d4SMasami Hiramatsutotal_size=$(expr $bconf_size + $initrd_size + 9 + 12 + $ALIGN - 1 )
64e1cef2d4SMasami Hiramatsutotal_size=$(expr $total_size / $ALIGN)
65e1cef2d4SMasami Hiramatsutotal_size=$(expr $total_size \* $ALIGN)
66e1cef2d4SMasami Hiramatsuxpass test $new_size -eq $total_size
67081c6536SMasami Hiramatsu
68081c6536SMasami Hiramatsuecho "Apply command repeat test"
69081c6536SMasami Hiramatsuxpass $BOOTCONF -a $TEMPCONF $INITRD
70081c6536SMasami Hiramatsu
71081c6536SMasami Hiramatsuecho "File size check"
72081c6536SMasami Hiramatsuxpass test $new_size -eq $(stat -c %s $INITRD)
73081c6536SMasami Hiramatsu
74081c6536SMasami Hiramatsuecho "Delete command check"
75081c6536SMasami Hiramatsuxpass $BOOTCONF -d $INITRD
76081c6536SMasami Hiramatsu
77081c6536SMasami Hiramatsuecho "File size check"
78081c6536SMasami Hiramatsunew_size=$(stat -c %s $INITRD)
79081c6536SMasami Hiramatsuxpass test $new_size -eq $initrd_size
80081c6536SMasami Hiramatsu
8197378001SMasami Hiramatsuecho "No error messge while applying"
8297378001SMasami Hiramatsudd if=/dev/zero of=$INITRD bs=4096 count=1
8397378001SMasami Hiramatsuprintf " \0\0\0 \0\0\0" >> $INITRD
8497378001SMasami Hiramatsu$BOOTCONF -a $TEMPCONF $INITRD > $OUTFILE 2>&1
8597378001SMasami Hiramatsuxfail grep -i "failed" $OUTFILE
8697378001SMasami Hiramatsuxfail grep -i "error" $OUTFILE
8797378001SMasami Hiramatsu
88081c6536SMasami Hiramatsuecho "Max node number check"
89081c6536SMasami Hiramatsu
90*b6924512SMasami Hiramatsu (Google)awk '
91*b6924512SMasami Hiramatsu (Google)BEGIN {
92*b6924512SMasami Hiramatsu (Google)  for (i = 0; i < 26; i += 1)
93*b6924512SMasami Hiramatsu (Google)      printf("%c\n", 65 + i % 26)
94*b6924512SMasami Hiramatsu (Google)  for (i = 26; i < 8192; i += 1)
95*b6924512SMasami Hiramatsu (Google)      printf("%c%c%c\n", 65 + i % 26, 65 + (i / 26) % 26, 65 + (i / 26 / 26))
96*b6924512SMasami Hiramatsu (Google)}
97*b6924512SMasami Hiramatsu (Google)' > $TEMPCONF
98081c6536SMasami Hiramatsuxpass $BOOTCONF -a $TEMPCONF $INITRD
99081c6536SMasami Hiramatsu
100081c6536SMasami Hiramatsuecho "badnode" >> $TEMPCONF
101081c6536SMasami Hiramatsuxfail $BOOTCONF -a $TEMPCONF $INITRD
102081c6536SMasami Hiramatsu
103081c6536SMasami Hiramatsuecho "Max filesize check"
104081c6536SMasami Hiramatsu
105081c6536SMasami Hiramatsu# Max size is 32767 (including terminal byte)
106081c6536SMasami Hiramatsuecho -n "data = \"" > $TEMPCONF
107081c6536SMasami Hiramatsudd if=/dev/urandom bs=768 count=32 | base64 -w0 >> $TEMPCONF
108081c6536SMasami Hiramatsuecho "\"" >> $TEMPCONF
109081c6536SMasami Hiramatsuxfail $BOOTCONF -a $TEMPCONF $INITRD
110081c6536SMasami Hiramatsu
111081c6536SMasami Hiramatsutruncate -s 32764 $TEMPCONF
112081c6536SMasami Hiramatsuecho "\"" >> $TEMPCONF	# add 2 bytes + terminal ('\"\n\0')
113081c6536SMasami Hiramatsuxpass $BOOTCONF -a $TEMPCONF $INITRD
114081c6536SMasami Hiramatsu
1155f811c57SMasami Hiramatsuecho "Adding same-key values"
1165f811c57SMasami Hiramatsucat > $TEMPCONF << EOF
1175f811c57SMasami Hiramatsukey = bar, baz
1185f811c57SMasami Hiramatsukey += qux
1195f811c57SMasami HiramatsuEOF
1205f811c57SMasami Hiramatsuecho > $INITRD
1215f811c57SMasami Hiramatsu
1225f811c57SMasami Hiramatsuxpass $BOOTCONF -a $TEMPCONF $INITRD
1235f811c57SMasami Hiramatsu$BOOTCONF $INITRD > $OUTFILE
1245f811c57SMasami Hiramatsuxpass grep -q "bar" $OUTFILE
1255f811c57SMasami Hiramatsuxpass grep -q "baz" $OUTFILE
1265f811c57SMasami Hiramatsuxpass grep -q "qux" $OUTFILE
1275f811c57SMasami Hiramatsu
12881464192SMasami Hiramatsuecho "Override same-key values"
12981464192SMasami Hiramatsucat > $TEMPCONF << EOF
13081464192SMasami Hiramatsukey = bar, baz
13181464192SMasami Hiramatsukey := qux
13281464192SMasami HiramatsuEOF
13381464192SMasami Hiramatsuecho > $INITRD
13481464192SMasami Hiramatsu
13581464192SMasami Hiramatsuxpass $BOOTCONF -a $TEMPCONF $INITRD
13681464192SMasami Hiramatsu$BOOTCONF $INITRD > $OUTFILE
13781464192SMasami Hiramatsuxfail grep -q "bar" $OUTFILE
13881464192SMasami Hiramatsuxfail grep -q "baz" $OUTFILE
13981464192SMasami Hiramatsuxpass grep -q "qux" $OUTFILE
14081464192SMasami Hiramatsu
1415414251aSMasami Hiramatsuecho "Double/single quotes test"
1425414251aSMasami Hiramatsuecho "key = '\"string\"';" > $TEMPCONF
1435414251aSMasami Hiramatsu$BOOTCONF -a $TEMPCONF $INITRD
1445414251aSMasami Hiramatsu$BOOTCONF $INITRD > $TEMPCONF
1455414251aSMasami Hiramatsucat $TEMPCONF
1465414251aSMasami Hiramatsuxpass grep \'\"string\"\' $TEMPCONF
1475414251aSMasami Hiramatsu
1481d210c16SMasami Hiramatsuecho "Repeat same-key tree"
1491d210c16SMasami Hiramatsucat > $TEMPCONF << EOF
1501d210c16SMasami Hiramatsufoo
1511d210c16SMasami Hiramatsubar
1521d210c16SMasami Hiramatsufoo { buz }
1531d210c16SMasami HiramatsuEOF
1541d210c16SMasami Hiramatsuecho > $INITRD
1551d210c16SMasami Hiramatsu
1561d210c16SMasami Hiramatsuxpass $BOOTCONF -a $TEMPCONF $INITRD
1571d210c16SMasami Hiramatsu$BOOTCONF $INITRD > $OUTFILE
1581d210c16SMasami Hiramatsuxpass grep -q "bar" $OUTFILE
1591d210c16SMasami Hiramatsu
1602f5fb555SMasami Hiramatsu
1612f5fb555SMasami Hiramatsuecho "Remove/keep tailing spaces"
1622f5fb555SMasami Hiramatsucat > $TEMPCONF << EOF
1632f5fb555SMasami Hiramatsufoo = val     # comment
1642f5fb555SMasami Hiramatsubar = "val2 " # comment
1652f5fb555SMasami HiramatsuEOF
1662f5fb555SMasami Hiramatsuecho > $INITRD
1672f5fb555SMasami Hiramatsu
1682f5fb555SMasami Hiramatsuxpass $BOOTCONF -a $TEMPCONF $INITRD
1692f5fb555SMasami Hiramatsu$BOOTCONF $INITRD > $OUTFILE
1702f5fb555SMasami Hiramatsuxfail grep -q val[[:space:]] $OUTFILE
1712f5fb555SMasami Hiramatsuxpass grep -q val2[[:space:]] $OUTFILE
1722f5fb555SMasami Hiramatsu
173081c6536SMasami Hiramatsuecho "=== expected failure cases ==="
174081c6536SMasami Hiramatsufor i in samples/bad-* ; do
175081c6536SMasami Hiramatsu  xfail $BOOTCONF -a $i $INITRD
176081c6536SMasami Hiramatsudone
177081c6536SMasami Hiramatsu
178081c6536SMasami Hiramatsuecho "=== expected success cases ==="
179081c6536SMasami Hiramatsufor i in samples/good-* ; do
180081c6536SMasami Hiramatsu  xpass $BOOTCONF -a $i $INITRD
181081c6536SMasami Hiramatsudone
182081c6536SMasami Hiramatsu
18398d0a685SMasami Hiramatsu
18498d0a685SMasami Hiramatsuecho
18598d0a685SMasami Hiramatsuecho "=== Summary ==="
18698d0a685SMasami Hiramatsuecho "# of Passed: $(expr $NO - $NG - 1)"
18798d0a685SMasami Hiramatsuecho "# of Failed: $NG"
18898d0a685SMasami Hiramatsu
189081c6536SMasami Hiramatsuecho
190081c6536SMasami Hiramatsuif [ $NG -eq 0 ]; then
191081c6536SMasami Hiramatsu	echo "All tests passed"
192081c6536SMasami Hiramatsuelse
193081c6536SMasami Hiramatsu	echo "$NG tests failed"
19498d0a685SMasami Hiramatsu	exit 1
195081c6536SMasami Hiramatsufi
196