blob: 60d9d0164983b9a143f2d9d25456d69b5ecb98ae (
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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
#!/bin/bash
#----------------------------------------------------------------------
# Test suite for InterIMAP
# Copyright © 2019 Guilhem Moulin <guilhem@fripost.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#----------------------------------------------------------------------
set -ue
PATH=/usr/bin:/bin
export PATH
if [ $# -ne 1 ]; then
printf "Usage: %s TESTNAME\\n" "$0" >&2
exit 1
fi
TEST="${1%/}"
TEST="${TEST##*/}"
NAME="${TEST#[0-9]*-}"
TESTDIR="$(dirname -- "$0")/$TEST"
if [ ! -d "$TESTDIR" ]; then
printf "ERROR: Not a directory: %s\\n" "$TESTDIR" >&2
exit 1
fi
ROOTDIR="$(mktemp --tmpdir="${TMPDIR:-/dev/shm}" --directory "$NAME.XXXXXXXXXX")"
trap 'rm -rf -- "$ROOTDIR"' EXIT INT TERM
STDOUT="$ROOTDIR/stdout"
STDERR="$ROOTDIR/stderr"
TMPDIR="$ROOTDIR/tmp"
mkdir -- "$TMPDIR" "$ROOTDIR/home"
# Set environment for the given user
environ_set() {
local user="$1" home
eval home="\$HOME_$user"
ENVIRON=(
PATH="$PATH"
USER="$user"
HOME="$home"
XDG_CONFIG_HOME="$home/.config"
XDG_DATA_HOME="$home/.local/share"
)
}
# Prepare the test harness
prepare() {
declare -a ENVIRON=()
local src cfg target u home
# copy dovecot config
for src in "$TESTDIR/local.conf" "$TESTDIR"/remote*.conf; do
[ -r "$src" ] || continue
u="${src#"$TESTDIR/"}"
u="${u%.conf}"
home="$ROOTDIR/home/$u"
export "HOME_$u"="$home"
mkdir -pm0755 -- "$home/.local/bin"
mkdir -pm0700 -- "$home/.config/dovecot"
cat >"$home/.config/dovecot/config" <<-EOF
log_path = /dev/null
mail_home = $ROOTDIR/home/%u
ssl = no
EOF
cat >>"$home/.config/dovecot/config" <"$src"
environ_set "$u"
cat >"$home/.local/bin/doveadm" <<-EOF
#!/bin/sh
exec env -i ${ENVIRON[@]@Q} \\
doveadm -c ${home@Q}/.config/dovecot/config "\$@"
EOF
chmod +x -- "$home/.local/bin/doveadm"
done
# copy interimap config
mkdir -pm0700 -- "$HOME_local/.local/share/interimap"
mkdir -pm0700 -- "$HOME_local/.config/interimap"
for cfg in "$TESTDIR"/remote*.conf; do
cfg="${cfg#"$TESTDIR/remote"}"
cfg="${cfg%.conf}"
u="remote$cfg"
eval home="\$HOME_$u"
if [ -f "$TESTDIR/interimap.conf" ]; then
cat <"$TESTDIR/interimap.conf" >>"$HOME_local/.config/interimap/config$cfg"
fi
cat >>"$HOME_local/.config/interimap/config$cfg" <<-EOF
database = $u.db
[local]
type = tunnel
command = exec ${HOME_local@Q}/.local/bin/doveadm exec imap
null-stderr = NO
[remote]
type = tunnel
command = exec ${home@Q}/.local/bin/doveadm exec imap
null-stderr = NO
EOF
done
}
prepare
# Wrappers for interimap(1) and doveadm(1)
interimap() {
declare -a ENVIRON=()
environ_set "local"
env -i "${ENVIRON[@]}" perl -I./lib -T ./interimap "$@"
}
doveadm() {
if [ $# -le 2 ] || [ "$1" != "-u" ]; then
echo "Usage: doveadm -u USER ..." >&2
exit 1
fi
local u="$2" home
eval home="\$HOME_$u"
shift 2
"$home/.local/bin/doveadm" "$@"
}
# Sample (random) message
sample_message() {
cat <<-EOF
From: <sender@example.net>
To: <recipient@example.net>
Date: $(date -R)
Message-ID: <$(< /proc/sys/kernel/random/uuid)@example.net>
EOF
local len="$(shuf -i1-4096 -n1)"
xxd -ps -c30 -l"$len" /dev/urandom # 3 to 8329 bytes
}
# Wrapper for dovecot-lda(1)
deliver() {
local -a argv
while [ $# -gt 0 ] && [ "$1" != "--" ]; do
argv+=( "$1" )
shift
done
if [ $# -gt 0 ] && [ "$1" = "--" ]; then
shift
fi
doveadm "${argv[@]}" exec dovecot-lda -e "$@"
}
# Dump test results
dump_test_result() {
local below=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
local above="<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
local src u home
declare -a ENVIRON=()
for src in "$TESTDIR/local.conf" "$TESTDIR"/remote*.conf; do
u="${src#"$TESTDIR/"}"
u="${u%.conf}"
environ_set "$u"
eval home="\$HOME_$u"
printf "%s dovecot configuration:\\n%s\\n" "$u" "$below"
env -i "${ENVIRON[@]}" doveconf -c "$home/.config/dovecot/config" -n
printf "%s\\n\\n" "$above"
done
printf "(local) interimap configuration:\\n%s\\n" "$below"
cat <"$HOME_local/.config/interimap/config"
printf "%s\\n\\n" "$above"
printf "standard output was:\\n%s\\n" "$below"
cat <"$STDOUT"
printf "%s\\n\\n" "$above"
printf "standard error was:\\n%s\\n" "$below"
cat <"$STDERR"
printf "%s\\n\\n" "$above"
}
# Check mailbox consistency between the local/remote server and interimap's database
check_mailbox_status() {
local mailbox="$1" lns="inbox" lsep lprefix rns="inbox" rsep rprefix
lsep="$(doveconf -c "$HOME_local/.config/dovecot/config" -h "namespace/$lns/separator")"
lprefix="$(doveconf -c "$HOME_local/.config/dovecot/config" -h "namespace/$lns/prefix")"
rsep="$(doveconf -c "$HOME_remote/.config/dovecot/config" -h "namespace/$lns/separator")"
rprefix="$(doveconf -c "$HOME_remote/.config/dovecot/config" -h "namespace/$lns/prefix")"
local blob="x'$(printf "%s" "$mailbox" | tr "$lsep" "\\0" | xxd -c256 -ps)'"
local rmailbox="$(printf "%s" "$mailbox" | tr "$lsep" "$rsep")"
check_mailbox_status2 "$blob" "$lprefix$mailbox" "remote" "$rprefix$rmailbox"
}
check_mailbox_status2() {
local blob="$1" lmailbox="$2" u="$3" rmailbox="$4"
local lUIDVALIDITY lUIDNEXT lHIGHESTMODSEQ rUIDVALIDITY rUIDNEXT rHIGHESTMODSEQ
read lUIDVALIDITY lUIDNEXT lHIGHESTMODSEQ rUIDVALIDITY rUIDNEXT rHIGHESTMODSEQ < <(
sqlite3 "$XDG_DATA_HOME/interimap/$u.db" <<-EOF
.mode csv
.separator " " "\\n"
SELECT l.UIDVALIDITY, l.UIDNEXT, l.HIGHESTMODSEQ, r.UIDVALIDITY, r.UIDNEXT, r.HIGHESTMODSEQ
FROM mailboxes m JOIN local l ON m.idx = l.idx JOIN remote r ON m.idx = r.idx
WHERE mailbox = $blob
EOF
)
lHIGHESTMODSEQ="$(printf "%llu" "$lHIGHESTMODSEQ")"
rHIGHESTMODSEQ="$(printf "%llu" "$rHIGHESTMODSEQ")"
local MESSAGES
read MESSAGES < <( sqlite3 "$XDG_DATA_HOME/interimap/$u.db" <<-EOF
.mode csv
.separator " " "\\n"
SELECT COUNT(*)
FROM mailboxes a JOIN mapping b ON a.idx = b.idx
WHERE mailbox = $blob
EOF
)
check_mailbox_status_values "local" "$lmailbox" $lUIDVALIDITY $lUIDNEXT $lHIGHESTMODSEQ $MESSAGES
check_mailbox_status_values "$u" "$rmailbox" $rUIDVALIDITY $rUIDNEXT $rHIGHESTMODSEQ $MESSAGES
local a b
a="$(doveadm -u "local" -f "flow" mailbox status "messages unseen vsize" -- "$lmailbox" | \
sed -nr '/.*\s+(\w+=[0-9]+\s+\w+=[0-9]+\s+\w+=[0-9]+)$/ {s//\1/p;q}')"
b="$(doveadm -u "$u" -f "flow" mailbox status "messages unseen vsize" -- "$rmailbox" | \
sed -nr '/.*\s+(\w+=[0-9]+\s+\w+=[0-9]+\s+\w+=[0-9]+)$/ {s//\1/p;q}')"
if [ "$a" != "$b" ]; then
echo "Mailbox $lmailbox status differs: \"$a\" != \"$b\"" >&2
exit 1
fi
}
check_mailbox_status_values() {
local user="$1" mailbox="$2" UIDVALIDITY="$3" UIDNEXT="$4" HIGHESTMODSEQ="$5" MESSAGES="$6" x xs v k
xs="$(doveadm -u "$user" -f "flow" mailbox status "uidvalidity uidnext highestmodseq messages" -- "$mailbox" | \
sed -nr '/.*\s+(\w+=[0-9]+\s+\w+=[0-9]+\s+\w+=[0-9]+\s+\w+=[0-9]+)$/ {s//\1/p;q}')"
[ -n "$xs" ] || exit 1
for x in $xs; do
k="${x%%=*}"
case "${k^^[a-z]}" in
UIDVALIDITY) v="$UIDVALIDITY";;
UIDNEXT) v="$UIDNEXT";;
HIGHESTMODSEQ) v="$HIGHESTMODSEQ";;
MESSAGES) v="$MESSAGES";;
*) echo "Uh? $x" >&2; exit 1
esac
if [ "${x#*=}" != "$v" ]; then
echo "$user($mailbox): ${k^^[a-z]} doesn't match! ${x#*=} != $v" >&2
exit 1
fi
done
}
check_mailboxes_status() {
local mailbox
for mailbox in "$@"; do
check_mailbox_status "$mailbox"
done
}
# Check mailbox list constency between the local and remote servers
check_mailbox_list() {
local m i lns="inbox" lsep lprefix rns="inbox" rsep rprefix sub=
lsep="$(doveconf -c "$HOME_local/.config/dovecot/config" -h "namespace/$lns/separator")"
lprefix="$(doveconf -c "$HOME_local/.config/dovecot/config" -h "namespace/$lns/prefix")"
rsep="$(doveconf -c "$HOME_remote/.config/dovecot/config" -h "namespace/$lns/separator")"
rprefix="$(doveconf -c "$HOME_remote/.config/dovecot/config" -h "namespace/$lns/prefix")"
if [ $# -gt 0 ] && [ "$1" = "-s" ]; then
sub="-s"
shift
fi
declare -a lmailboxes=() rmailboxes=()
if [ $# -eq 0 ]; then
lmailboxes=( "${lprefix}*" )
rmailboxes=( "${rprefix}*" )
else
for m in "$@"; do
lmailboxes+=( "$lprefix$m" )
rmailboxes+=( "$rprefix${m//"$lsep"/"$rsep"}" )
done
fi
mapfile -t lmailboxes < <( doveadm -u "local" mailbox list $sub -- "${lmailboxes[@]}" )
for ((i = 0; i < ${#lmailboxes[@]}; i++)); do
lmailboxes[i]="${lmailboxes[i]#"$lprefix"}"
done
mapfile -t rmailboxes < <( doveadm -u "remote" mailbox list $sub -- "${rmailboxes[@]}" )
for ((i = 0; i < ${#rmailboxes[@]}; i++)); do
rmailboxes[i]="${rmailboxes[i]#"$rprefix"}"
rmailboxes[i]="${rmailboxes[i]//"$rsep"/"$lsep"}"
done
local IFS=$'\n'
diff -u --label="local/mailboxes" --label="remote/mailboxes" \
<( printf "%s" "${lmailboxes[*]}" | sort ) <( printf "%s" "${rmailboxes[*]}" | sort )
}
# Wrappers for grep(1) and `grep -C`
xgrep() {
if ! grep -q "$@"; then
printf "\`grep %s\` failed on line %d\\n" "${*@Q}" ${BASH_LINENO[0]} >&2
exit 1
fi
}
xcgrep() {
local m="$1" n
shift
if ! n="$(grep -c "$@")" || [ $m -ne $n ]; then
printf "\`grep -c %s\` failed on line %d: %d != %d\\n" "${*@Q}" ${BASH_LINENO[0]} "$m" "$n" >&2
exit 1
fi
}
# Run test in a sub-shell
declare -a ENVIRON=()
environ_set "local"
export TMPDIR TESTDIR STDOUT STDERR "${ENVIRON[@]}"
export -f environ_set doveadm interimap sample_message deliver
export -f check_mailbox_status check_mailbox_status_values check_mailbox_status2
export -f check_mailboxes_status check_mailbox_list xgrep xcgrep
printf "%s..." "$TEST"
if ! bash -ue "$TESTDIR/run" >"$STDOUT" 2>"$STDERR"; then
echo " FAILED"
dump_test_result
exit 1
else
echo " OK"
if grep -Paq "\\x00" -- "$STDOUT" "$STDERR"; then
printf "\\tWarn: binary output (outstanding \\0)!\\n"
fi
exit 0
fi
|