summaryrefslogtreecommitdiff
path: root/test/main
blob: 7c73fc2a07622f755c6ea3df051636044b5bd459 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env zsh
setopt err_exit
typeset -i benchmark=0 test_extended=0 test_security=0

while [[ $1 == --* ]] {
	case $1 in
		--all) test_extended=1; test_security=1 ;;
		--benchmark) benchmark=1 ;;
		--extended) test_extended=1 ;;
		--security) test_security=1 ;;
	esac
	shift
}

typeset envstore=${1-envstore}
typeset store_file=${2-/tmp/.envstore-$UID}
trap "print -P '\n%N:%i: %B%F{red}Test faild!%F{default}%b'" ZERR
trap "$envstore clear" INT

cat <<- ente
	Usage: $0 [options] [path to envstore] [path to envstore store file]
	valid options are:
	     --all         enable all test options (except --benchmark)
	     --benchmark   do a little benchmark after the test (might cause CPU and HDD stress)
	     --extended    Test for more than just the new envstore API (invalid invocations etc)
	     --security    Test for security (world-writable store file etc)

	Note: this script will remove/overwrite your envstore store file.
	The envstore script needs to be compatible with the original envstore API,
	that is, it should implement the commends "clear", "rm", "eval",
	"save var" and "save var value"

ente

echo "# $envstore clear"
$envstore clear

echo "# $envstore save var"
export hello=world
$envstore save hello
unset hello

echo "# $envstore eval"
eval $($envstore eval)
[[ $hello == world ]]
unset hello

echo "# $envstore rm"
$envstore rm hello
eval $($envstore eval)
[[ -z $hello ]]

echo "# $envstore save var value"
$envstore save hello world

echo "# $envstore eval"
eval $($envstore eval)
[[ $hello == world ]]
unset hello

echo "# $envstore clear"
$envstore clear
eval $($envstore eval)
[[ -z $hello ]]

echo "# $envstore save + eval (spaces in value), save var"
export hello='your mom'
$envstore save hello
unset hello
eval $($envstore eval)
[[ $hello == 'your mom' ]]
unset hello
$envstore clear

echo "# $envstore save + eval (spaces in value), save var value"
$envstore save hello 'your mom'
eval $($envstore eval)
[[ $hello == 'your mom' ]]
unset hello

echo "# $envstore save (overwrite)"
$envstore save hello world
eval $($envstore eval)
[[ $hello == world ]]
$envstore clear

echo "# $envstore save (' in value)"
$envstore save hello "the ' dude"
eval $($envstore eval)
[[ $hello == "the ' dude" ]]
unset hello

echo "# $envstore save (UTF-8)"
export hello='mÿde Rentner… und so'
$envstore save hello
unset hello
eval $($envstore eval)
[[ $hello == 'mÿde Rentner… und so' ]]
unset hello
$envstore clear

if ((test_extended)) {
	echo "# $envstore save (newline in value)"
	export flurbl=$'yo my fresh\nhomies'
	$envstore save flurbl
	unset flurbl
	# XXX eval does not like newlines. Not even in dash.
	source <($envstore eval)
	[[ $flurbl == $'yo my fresh\nhomies' ]]
	unset flurbl
	$envstore clear

	echo "# invalid invocations"
	! $envstore save
	unset nonexistent
	! $envstore save nonexistent
	! $envstore rm

	echo "# other invocations"
	$envstore show
	$envstore eval
	$envstore rm nonexistent
	$envstore clear
}

if ((test_security)) {
	echo "# world-writable store file"
	$envstore save fucked yes
	chmod 777 $store_file
	[[ $($envstore eval) != *fucked=yes* ]]
	rm $store_file
	$envstore save fucked yes
	cat <<- ente
		Now, enter the following as root:
		chown root $store_file
	ente
	echo -n "[press return when done] "
	read
	[[ $($envstore eval) != *fucked=yes* ]]
	cat <<- ente
		Looking good, you may remove the file now
		rm $store_file
	ente
	echo -n "[press return when done] "
	read
}

print -P '\n%F{green}Test passed%F{default}\n'

if ((benchmark)) {
	trap '' ZERR
	echo "## some benchmarks now..."
	TIMEFMT='%*E real, %*U user, %*S system - %P%% CPU - %J'

	echo "# adding 5000 vars (this could take a _long_ time)"
	for i in {0..5000}; {
		(( i % 500 )) || echo "# $((5000-i)) to go"
		$envstore save $i $i$i$i
	}
	echo

	repeat 2 {time $envstore eval > /dev/null}
	time $envstore rm 999 > /dev/null
	repeat 2 {time $envstore clear}
}