1#!/bin/sh 2# 3# $OpenBSD: statemachine,v 1.6 2018/02/05 18:28:15 anton Exp $ 4#/* 5# * Copyright (c) Rob Pierce <rob@openbsd.org> 6# * 7# * Permission to use, copy, modify, and distribute this software for any 8# * purpose with or without fee is hereby granted, provided that the above 9# * copyright notice and this permission notice appear in all copies. 10# * 11# * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12# * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13# * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14# * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15# * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16# * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17# * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18# */ 19 20# Basic ifstated regression script to test the finite state machine. 21 22# 23# NOTE: Increase LSLEEP as required when adding additional test states. 24# 25 26# Ensure OBJDIR is defined 27: ${OBJDIR?} || exit 1 28 29export OBJDIR 30 31# Global variables 32FILE1="truth1.test" 33FILE2="truth2.test" 34EVERY=2 35SLEEP=5 36LSLEEP=35 37 38cleanup() { 39 rm ${OBJDIR}/$FILE1 >/dev/null 2>&1 40 rm ${OBJDIR}/$FILE2 >/dev/null 2>&1 41 rm ${OBJDIR}/ifstated.conf >/dev/null 2>&1 42 rm ${OBJDIR}/ifstated.log >/dev/null 2>&1 43 rm ${OBJDIR}/output.test >/dev/null 2>&1 44 rm ${OBJDIR}/output.new >/dev/null 2>&1 45 rm ${OBJDIR}/nohup.out >/dev/null 2>&1 46} 47 48fail() { 49 echo FAILED 50 cleanup 51 exit 1 52} 53 54skip() { 55 echo SKIPPED 56 cleanup 57 exit 0 58} 59 60trap 'skip' INT 61 62if [ "$(pgrep ifstated)" ] 63then 64 echo "The ifstated daemon is already running." 65 echo SKIPPED 66 exit 0 67fi 68 69rm -f ${OBJDIR}/${FILE1} 70rm -f ${OBJDIR}/${FILE2} 71 72cat > ${OBJDIR}/ifstated.conf <<EOF 73# This is a config template for ifstated regression testing 74init-state one 75true = '( "true" every $EVERY )' 76false = '( "false" every $EVERY )' 77test1 = '( "test -f ${FILE1}" every $EVERY )' 78test2 = '( "test -f ${FILE2}" every $EVERY )' 79state one { 80 init { 81 run "sleep $SLEEP && ( test -f ${FILE2} || touch ${FILE1} )" 82 } 83 if \$test1 && ! \$test2 84 set-state two 85 if \$test2 86 set-state ninetyeight 87} 88state two { 89 init { 90 run "sleep $SLEEP && rm ${FILE1}" 91 } 92 if ! \$test1 93 set-state three 94} 95state three { 96 if ( \$false || \$false ) || ( ! \$test1 || \$false ) 97 set-state four 98} 99state four { 100 if ( \$true && \$true ) && ( ! \$test1 && \$true ) 101 set-state five 102} 103state five { 104 init { 105 run "sleep $SLEEP && touch ${FILE1}" 106 } 107 if ( \$false || \$true ) && ( ! \$true || \$test1 ) 108 set-state six 109} 110state six { 111 init { 112 run "sleep $SLEEP && touch ${FILE1}" 113 } 114 if ( \$false || \$true ) && ( ! \$true || \$test1 ) 115 if \$true 116 set-state seven 117} 118state seven { 119 if ( ! \$false ) 120 set-state eight 121} 122state eight { 123 if ! ( \$false ) 124 set-state nine 125} 126state nine { 127 if ! ( \$false ) { 128 set-state ten 129 } 130} 131state ten { 132 if \$true && ! ( \$true && ! \$test1 ) { 133 run "rm ${FILE1}" 134 run "touch ${FILE2}" 135 set-state one 136 } 137} 138state ninetyeight { 139 init { 140 if \$true 141 if ( \$true && ! \$false ) && ( \$false || \$true ) { 142 if \$true 143 run "touch ${FILE1}" 144 } 145 } 146 if \$test1 && \$test2 { 147 run "rm ${FILE2}" 148 set-state ninetynine 149 } 150} 151state ninetynine { 152 init { 153 run "touch ${FILE2}" 154 } 155 if \$test1 && \$test2 156 set-state onehundred 157} 158state onehundred { 159 if ! ( ! ( ! \$false ) ) 160 set-state end 161} 162state end { 163 if \$false 164 set-state one 165} 166EOF 167 168cat > ${OBJDIR}/output.test <<EOF 169changing state to one 170changing state to two 171changing state to three 172changing state to four 173changing state to five 174changing state to six 175changing state to seven 176changing state to eight 177changing state to nine 178changing state to ten 179changing state to one 180changing state to ninetyeight 181changing state to ninetynine 182changing state to onehundred 183changing state to end 184EOF 185 186(cd ${OBJDIR} && nohup ifstated -dvf ./ifstated.conf > ifstated.log 2>&1) & 187 188sleep ${LSLEEP} 189 190grep ^changing ${OBJDIR}/ifstated.log > ${OBJDIR}/output.new 191 192kill $(pgrep ifstated) >/dev/null 2>&1 193 194diff ${OBJDIR}/output.test ${OBJDIR}/output.new 195case $? in 1960) echo PASSED 197 cleanup 198 exit 0 199 ;; 2001) fail 201 ;; 202esac 203