1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-only
3
4echo "Boot config test script"
5
6BOOTCONF=./bootconfig
7INITRD=`mktemp initrd-XXXX`
8TEMPCONF=`mktemp temp-XXXX.bconf`
9NG=0
10
11cleanup() {
12  rm -f $INITRD $TEMPCONF
13  exit $NG
14}
15
16trap cleanup EXIT TERM
17
18NO=1
19
20xpass() { # pass test command
21  echo "test case $NO ($3)... "
22  if ! ($@ && echo "\t\t[OK]"); then
23     echo "\t\t[NG]"; NG=$((NG + 1))
24  fi
25  NO=$((NO + 1))
26}
27
28xfail() { # fail test command
29  echo "test case $NO ($3)... "
30  if ! (! $@ && echo "\t\t[OK]"); then
31     echo "\t\t[NG]"; NG=$((NG + 1))
32  fi
33  NO=$((NO + 1))
34}
35
36echo "Basic command test"
37xpass $BOOTCONF $INITRD
38
39echo "Delete command should success without bootconfig"
40xpass $BOOTCONF -d $INITRD
41
42dd if=/dev/zero of=$INITRD bs=4096 count=1
43echo "key = value;" > $TEMPCONF
44bconf_size=$(stat -c %s $TEMPCONF)
45initrd_size=$(stat -c %s $INITRD)
46
47echo "Apply command test"
48xpass $BOOTCONF -a $TEMPCONF $INITRD
49new_size=$(stat -c %s $INITRD)
50
51echo "File size check"
52xpass test $new_size -eq $(expr $bconf_size + $initrd_size + 9)
53
54echo "Apply command repeat test"
55xpass $BOOTCONF -a $TEMPCONF $INITRD
56
57echo "File size check"
58xpass test $new_size -eq $(stat -c %s $INITRD)
59
60echo "Delete command check"
61xpass $BOOTCONF -d $INITRD
62
63echo "File size check"
64new_size=$(stat -c %s $INITRD)
65xpass test $new_size -eq $initrd_size
66
67echo "Max node number check"
68
69echo -n > $TEMPCONF
70for i in `seq 1 1024` ; do
71   echo "node$i" >> $TEMPCONF
72done
73xpass $BOOTCONF -a $TEMPCONF $INITRD
74
75echo "badnode" >> $TEMPCONF
76xfail $BOOTCONF -a $TEMPCONF $INITRD
77
78echo "Max filesize check"
79
80# Max size is 32767 (including terminal byte)
81echo -n "data = \"" > $TEMPCONF
82dd if=/dev/urandom bs=768 count=32 | base64 -w0 >> $TEMPCONF
83echo "\"" >> $TEMPCONF
84xfail $BOOTCONF -a $TEMPCONF $INITRD
85
86truncate -s 32764 $TEMPCONF
87echo "\"" >> $TEMPCONF	# add 2 bytes + terminal ('\"\n\0')
88xpass $BOOTCONF -a $TEMPCONF $INITRD
89
90echo "=== expected failure cases ==="
91for i in samples/bad-* ; do
92  xfail $BOOTCONF -a $i $INITRD
93done
94
95echo "=== expected success cases ==="
96for i in samples/good-* ; do
97  xpass $BOOTCONF -a $i $INITRD
98done
99
100echo
101if [ $NG -eq 0 ]; then
102	echo "All tests passed"
103else
104	echo "$NG tests failed"
105fi
106