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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
#!/bin/sh
#
# $OpenBSD: statemachine,v 1.6 2018/02/05 18:28:15 anton Exp $
#/*
# * Copyright (c) Rob Pierce <rob@openbsd.org>
# *
# * Permission to use, copy, modify, and distribute this software for any
# * purpose with or without fee is hereby granted, provided that the above
# * copyright notice and this permission notice appear in all copies.
# *
# * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# */
# Basic ifstated regression script to test the finite state machine.
#
# NOTE: Increase LSLEEP as required when adding additional test states.
#
# Ensure OBJDIR is defined
: ${OBJDIR?} || exit 1
export OBJDIR
# Global variables
FILE1="truth1.test"
FILE2="truth2.test"
EVERY=2
SLEEP=5
LSLEEP=35
cleanup() {
rm ${OBJDIR}/$FILE1 >/dev/null 2>&1
rm ${OBJDIR}/$FILE2 >/dev/null 2>&1
rm ${OBJDIR}/ifstated.conf >/dev/null 2>&1
rm ${OBJDIR}/ifstated.log >/dev/null 2>&1
rm ${OBJDIR}/output.test >/dev/null 2>&1
rm ${OBJDIR}/output.new >/dev/null 2>&1
rm ${OBJDIR}/nohup.out >/dev/null 2>&1
}
fail() {
echo FAILED
cleanup
exit 1
}
skip() {
echo SKIPPED
cleanup
exit 0
}
trap 'skip' INT
if [ "$(pgrep ifstated)" ]
then
echo "The ifstated daemon is already running."
echo SKIPPED
exit 0
fi
rm -f ${OBJDIR}/${FILE1}
rm -f ${OBJDIR}/${FILE2}
cat > ${OBJDIR}/ifstated.conf <<EOF
# This is a config template for ifstated regression testing
init-state one
true = '( "true" every $EVERY )'
false = '( "false" every $EVERY )'
test1 = '( "test -f ${FILE1}" every $EVERY )'
test2 = '( "test -f ${FILE2}" every $EVERY )'
state one {
init {
run "sleep $SLEEP && ( test -f ${FILE2} || touch ${FILE1} )"
}
if \$test1 && ! \$test2
set-state two
if \$test2
set-state ninetyeight
}
state two {
init {
run "sleep $SLEEP && rm ${FILE1}"
}
if ! \$test1
set-state three
}
state three {
if ( \$false || \$false ) || ( ! \$test1 || \$false )
set-state four
}
state four {
if ( \$true && \$true ) && ( ! \$test1 && \$true )
set-state five
}
state five {
init {
run "sleep $SLEEP && touch ${FILE1}"
}
if ( \$false || \$true ) && ( ! \$true || \$test1 )
set-state six
}
state six {
init {
run "sleep $SLEEP && touch ${FILE1}"
}
if ( \$false || \$true ) && ( ! \$true || \$test1 )
if \$true
set-state seven
}
state seven {
if ( ! \$false )
set-state eight
}
state eight {
if ! ( \$false )
set-state nine
}
state nine {
if ! ( \$false ) {
set-state ten
}
}
state ten {
if \$true && ! ( \$true && ! \$test1 ) {
run "rm ${FILE1}"
run "touch ${FILE2}"
set-state one
}
}
state ninetyeight {
init {
if \$true
if ( \$true && ! \$false ) && ( \$false || \$true ) {
if \$true
run "touch ${FILE1}"
}
}
if \$test1 && \$test2 {
run "rm ${FILE2}"
set-state ninetynine
}
}
state ninetynine {
init {
run "touch ${FILE2}"
}
if \$test1 && \$test2
set-state onehundred
}
state onehundred {
if ! ( ! ( ! \$false ) )
set-state end
}
state end {
if \$false
set-state one
}
EOF
cat > ${OBJDIR}/output.test <<EOF
changing state to one
changing state to two
changing state to three
changing state to four
changing state to five
changing state to six
changing state to seven
changing state to eight
changing state to nine
changing state to ten
changing state to one
changing state to ninetyeight
changing state to ninetynine
changing state to onehundred
changing state to end
EOF
(cd ${OBJDIR} && nohup ifstated -dvf ./ifstated.conf > ifstated.log 2>&1) &
sleep ${LSLEEP}
grep ^changing ${OBJDIR}/ifstated.log > ${OBJDIR}/output.new
kill $(pgrep ifstated) >/dev/null 2>&1
diff ${OBJDIR}/output.test ${OBJDIR}/output.new
case $? in
0) echo PASSED
cleanup
exit 0
;;
1) fail
;;
esac
|