aboutsummaryrefslogtreecommitdiff
path: root/contrib/bc/tests/radamsa.sh
blob: 236953ae888ee728002f9de06d9b46badfd0338b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#! /bin/bash
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2018-2021 Gavin D. Howard and contributors.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

getentry() {

	if [ $# -gt 0 ]; then
		entnum="$1"
	else
		entnum=0
	fi

	e=$(cat -)
	num=$(printf '%s\n' "$e" | wc -l)

	if [ "$entnum" -eq 0 ]; then
		rand=$(printf 'irand(%s) + 1\n' "$num" | "$bcdir/bc")
	else
		rand="$entnum"
	fi

	ent=$(printf '%s\n' "$e" | tail -n +$rand | head -n 1)

	printf '%s\n' "$ent"
}

script="$0"

if [ "$#" -lt 1 ]; then
	printf 'usage: %s dir\n' "$0"
	exit 1
fi

d="$1"
shift

dir=$(dirname "$script")

. "$dir/../functions.sh"

bcdir="$dir/../bin"

if [ "$d" = "bc" ]; then
	inputs="$dir/../../inputs"
	opts="-lq"
elif [ "$d" = "dc" ]; then
	inputs="$dir/../../inputs_dc"
	opts="-x"
else
	err_exit "wrong type of executable" 1
fi

export ASAN_OPTIONS="abort_on_error=1"

entries=$(cat "$dir/radamsa.txt")

IFS=$'\n'

go=1

while [ "$go" -ne 0 ]; do

	if [ "$d" = "bc" ]; then

		entry=$(cat -- "$dir/radamsa.txt" | getentry)
		items=$(printf '%s\n' "$entry" | radamsa -n 10)

		printf '%s\n' "$items"

		for i in `seq 1 10`; do

			item=$(printf '%s\n' "$items" | getentry "$i")

			export BC_ENV_ARGS="$item"
			echo 'halt' | "$bcdir/$d"
			err=$?

			checkcrash "$d" "$err" "radamsa env args: \"$item\""
		done

	fi

	f=$(ls "$inputs" | getentry)
	l=$(cat "$inputs/$f" | wc -l)
	ll=$(printf '%s^2\n' "$l" | bc)

	for i in $(seq 1 2); do
		data=$(cat "$inputs/$f" | radamsa -n 1)
		printf '%s\n' "$data" > "$dir/../.log_${d}_test.txt"
		printf '%s\n' "$data" | timeout -s SIGTERM 5 "$bcdir/$d" "$opts" > /dev/null
		err=$?
		checkcrash "$d" "$err" "radamsa stdin"
	done

done