summaryrefslogtreecommitdiff
path: root/test/functions.in
blob: 86366982b1dc197e33036287a203a1f077e59263 (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
#!/bin/bash
srcdir=@SRCDIR@
builddir=@BUILDDIR@
tmpdir=$builddir/test-tmp
export NULLMAILER_TEST_PREFIX=$tmpdir
SYSCONFDIR=$tmpdir/conf
QUEUEDIR=$tmpdir/queue
rm -rf $tmpdir
mkdir -p \
    $tmpdir/protocols \
    $QUEUEDIR/{failed,queue,tmp} \
    $SYSCONFDIR
mknod $QUEUEDIR/trigger p
ln -s $builddir/src $tmpdir/sbin
ln -s $builddir/src $tmpdir/bin
ln -s $builddir/protocols/* $tmpdir/protocols/

exit_cleanup() {
    svc -dx $tmpdir/service/* > /dev/null 2>&1 || :
    # Remove all temporary files on success
    if [ $? -eq 0 ]
    then
        wait
        rm -rf $tmpdir
    fi
}
trap exit_cleanup EXIT

fail() {
    echo "$*"
    exit 1
}
start() {
    local name=$1
    shift
    mkdir -p $tmpdir/service/$name
    {
	echo '#!/bin/sh'
	echo exec "$@"
    } >$tmpdir/service/$name/run
    chmod +x $tmpdir/service/$name/run
    supervise $tmpdir/service/$name </dev/null >$tmpdir/service/${name}-log 2>&1 &
    sleep 1
}
stop() {
    for name in $*; do
	svc -dx $tmpdir/service/$name >/dev/null 2>&1
	wait
    done
}

#not() { if "$@"; then return 1; else return 0; fi }
not() { ! safe "$@"; }
safe() { set +e; "$@"; result=$?; set -e; return $result; }
error() {
  local code=$1
  shift
  if "$@"; then
    echo "Result was 0, should be $code."
    return 1
  else
    result=$?
    if test $result -eq $code; then
      return 0
    else
      echo "Result was $result, should be $code."
      return 1
    fi
  fi
}

inject() { ../src/nullmailer-inject "$@"; }
mailq() { ../src/mailq "$@"; }
smtpd() { ../src/nullmailer-smtpd "$@"; }
queue() {
    (
        echo "$1"
        shift
        while [ x"$1" != x ]
        do
            echo "$1"
            shift
        done
        echo
        echo "Subject: test"
    ) | ../src/nullmailer-queue > $tmpdir/queue-out 2> $tmpdir/queue-err
}
injectlines() {
  for line in "$@"; do
    echo "$line"
  done | inject -n
  return $?
}
injectfield() {
  local field=$1
  shift
  injectlines "$@" | grep -i "^$field:" | cut -d: -f2-
}
protocol() {
    local p=$1
    local opts=""
    shift
    while [ $# -gt 0 -a x"$1" != x-- ]
    do
        opts="$opts $1"
        shift
    done
    shift || :
    for line in "$@"
    do
        echo "$line"
    done > $tmpdir/protocol-in
    ../protocols/$p $opts < $tmpdir/protocol-in > $tmpdir/protocol-log 2>&1
}

# Split an input on blank lines
splitblank() {
  local fn=$1
  local n=1
  while read line
  do
    if [ x"$line" = x ]
    then
      n=$(( $n + 1 ))
    else
      echo "$line" >> ${fn}.$n
    fi
  done
}

export PATH=/bin:/usr/bin:/usr/local/bin
rm -f $SYSCONFDIR/*
echo f.q.d.n >$SYSCONFDIR/../mailname
echo q.d.n >$SYSCONFDIR/defaultdomain
set -e