test-bootconfig.sh 3.8 KB
Newer Older
1 2 3 4 5
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-only

echo "Boot config test script"

6 7 8 9 10 11 12 13 14 15
if [ -d "$1" ]; then
  TESTDIR=$1
else
  TESTDIR=.
fi
BOOTCONF=${TESTDIR}/bootconfig

INITRD=`mktemp ${TESTDIR}/initrd-XXXX`
TEMPCONF=`mktemp ${TESTDIR}/temp-XXXX.bconf`
OUTFILE=`mktemp ${TESTDIR}/tempout-XXXX`
16 17 18
NG=0

cleanup() {
19
  rm -f $INITRD $TEMPCONF $OUTFILE
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  exit $NG
}

trap cleanup EXIT TERM

NO=1

xpass() { # pass test command
  echo "test case $NO ($3)... "
  if ! ($@ && echo "\t\t[OK]"); then
     echo "\t\t[NG]"; NG=$((NG + 1))
  fi
  NO=$((NO + 1))
}

xfail() { # fail test command
  echo "test case $NO ($3)... "
  if ! (! $@ && echo "\t\t[OK]"); then
     echo "\t\t[NG]"; NG=$((NG + 1))
  fi
  NO=$((NO + 1))
}

echo "Basic command test"
xpass $BOOTCONF $INITRD

echo "Delete command should success without bootconfig"
xpass $BOOTCONF -d $INITRD

dd if=/dev/zero of=$INITRD bs=4096 count=1
echo "key = value;" > $TEMPCONF
bconf_size=$(stat -c %s $TEMPCONF)
initrd_size=$(stat -c %s $INITRD)

echo "Apply command test"
xpass $BOOTCONF -a $TEMPCONF $INITRD
new_size=$(stat -c %s $INITRD)

58 59 60
echo "Show command test"
xpass $BOOTCONF $INITRD

61
echo "File size check"
62
xpass test $new_size -eq $(expr $bconf_size + $initrd_size + 9 + 12)
63 64 65 66 67 68 69 70 71 72 73 74 75 76

echo "Apply command repeat test"
xpass $BOOTCONF -a $TEMPCONF $INITRD

echo "File size check"
xpass test $new_size -eq $(stat -c %s $INITRD)

echo "Delete command check"
xpass $BOOTCONF -d $INITRD

echo "File size check"
new_size=$(stat -c %s $INITRD)
xpass test $new_size -eq $initrd_size

77 78 79 80 81 82 83
echo "No error messge while applying"
dd if=/dev/zero of=$INITRD bs=4096 count=1
printf " \0\0\0 \0\0\0" >> $INITRD
$BOOTCONF -a $TEMPCONF $INITRD > $OUTFILE 2>&1
xfail grep -i "failed" $OUTFILE
xfail grep -i "error" $OUTFILE

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
echo "Max node number check"

echo -n > $TEMPCONF
for i in `seq 1 1024` ; do
   echo "node$i" >> $TEMPCONF
done
xpass $BOOTCONF -a $TEMPCONF $INITRD

echo "badnode" >> $TEMPCONF
xfail $BOOTCONF -a $TEMPCONF $INITRD

echo "Max filesize check"

# Max size is 32767 (including terminal byte)
echo -n "data = \"" > $TEMPCONF
dd if=/dev/urandom bs=768 count=32 | base64 -w0 >> $TEMPCONF
echo "\"" >> $TEMPCONF
xfail $BOOTCONF -a $TEMPCONF $INITRD

truncate -s 32764 $TEMPCONF
echo "\"" >> $TEMPCONF	# add 2 bytes + terminal ('\"\n\0')
xpass $BOOTCONF -a $TEMPCONF $INITRD

107 108 109 110 111 112 113 114 115 116 117 118 119
echo "Adding same-key values"
cat > $TEMPCONF << EOF
key = bar, baz
key += qux
EOF
echo > $INITRD

xpass $BOOTCONF -a $TEMPCONF $INITRD
$BOOTCONF $INITRD > $OUTFILE
xpass grep -q "bar" $OUTFILE
xpass grep -q "baz" $OUTFILE
xpass grep -q "qux" $OUTFILE

120 121 122 123 124 125 126 127 128 129 130 131 132
echo "Override same-key values"
cat > $TEMPCONF << EOF
key = bar, baz
key := qux
EOF
echo > $INITRD

xpass $BOOTCONF -a $TEMPCONF $INITRD
$BOOTCONF $INITRD > $OUTFILE
xfail grep -q "bar" $OUTFILE
xfail grep -q "baz" $OUTFILE
xpass grep -q "qux" $OUTFILE

133 134 135 136 137 138 139
echo "Double/single quotes test"
echo "key = '\"string\"';" > $TEMPCONF
$BOOTCONF -a $TEMPCONF $INITRD
$BOOTCONF $INITRD > $TEMPCONF
cat $TEMPCONF
xpass grep \'\"string\"\' $TEMPCONF

140 141 142 143 144 145 146 147 148 149 150 151
echo "Repeat same-key tree"
cat > $TEMPCONF << EOF
foo
bar
foo { buz }
EOF
echo > $INITRD

xpass $BOOTCONF -a $TEMPCONF $INITRD
$BOOTCONF $INITRD > $OUTFILE
xpass grep -q "bar" $OUTFILE

152 153 154 155 156 157 158 159 160 161 162 163 164

echo "Remove/keep tailing spaces"
cat > $TEMPCONF << EOF
foo = val     # comment
bar = "val2 " # comment
EOF
echo > $INITRD

xpass $BOOTCONF -a $TEMPCONF $INITRD
$BOOTCONF $INITRD > $OUTFILE
xfail grep -q val[[:space:]] $OUTFILE
xpass grep -q val2[[:space:]] $OUTFILE

165 166 167 168 169 170 171 172 173 174
echo "=== expected failure cases ==="
for i in samples/bad-* ; do
  xfail $BOOTCONF -a $i $INITRD
done

echo "=== expected success cases ==="
for i in samples/good-* ; do
  xpass $BOOTCONF -a $i $INITRD
done

175 176 177 178 179 180

echo
echo "=== Summary ==="
echo "# of Passed: $(expr $NO - $NG - 1)"
echo "# of Failed: $NG"

181 182 183 184 185
echo
if [ $NG -eq 0 ]; then
	echo "All tests passed"
else
	echo "$NG tests failed"
186
	exit 1
187
fi