]> git.saurik.com Git - apt.git/blame - test/integration/framework
apt-helper.cc: include <stdlib.h> for atoi
[apt.git] / test / integration / framework
CommitLineData
8d876415
DK
1#!/bin/sh -- # no runable script, just for vi
2
5d76cee1 3EXIT_CODE=0
8c1dd12c 4
68042532
DK
5while [ -n "$1" ]; do
6 if [ "$1" = "-q" ]; then
7 export MSGLEVEL=2
8 elif [ "$1" = "-v" ]; then
9 export MSGLEVEL=4
10 elif [ "$1" = '--color=no' ]; then
11 export MSGCOLOR='NO'
61e92778
DK
12 elif [ "$1" = '--color=yes' ]; then
13 export MSGCOLOR='YES'
14 elif [ "$1" = '--color' ]; then
15 export MSGCOLOR="$(echo "$2" | tr 'a-z' 'A-Z')"
16 shift
17 elif [ "$1" = '--level' ]; then
18 export MSGLEVEL=$2
19 shift
68042532
DK
20 else
21 echo >&2 "WARNING: Unknown parameter »$1« will be ignored"
22 fi
23 shift
24done
25export MSGLEVEL="${MSGLEVEL:-3}"
26
8d876415 27# we all like colorful messages
61e92778 28if [ "${MSGCOLOR:-YES}" = 'YES' ]; then
27cb4f6c 29 if [ ! -t 1 ]; then # but check that we output to a terminal
1290422a
DK
30 export MSGCOLOR='NO'
31 fi
32fi
33
34
35if [ "$MSGCOLOR" != 'NO' ]; then
36 CERROR="\033[1;31m" # red
37 CWARNING="\033[1;33m" # yellow
38 CMSG="\033[1;32m" # green
39 CINFO="\033[1;96m" # light blue
40 CDEBUG="\033[1;94m" # blue
41 CNORMAL="\033[0;39m" # default system console color
42 CDONE="\033[1;32m" # green
43 CPASS="\033[1;32m" # green
44 CFAIL="\033[1;31m" # red
45 CCMD="\033[1;35m" # pink
682a3bf7 46fi
8d876415 47
de81b2e2
DK
48msgprintf() {
49 local START="$1"
50 local MIDDLE="$2"
51 local END="$3"
52 shift 3
53 if [ -n "$1" ]; then
54 printf "$START " "$1"
d73840dc 55 shift
de81b2e2 56 while [ -n "$1" ]; do
03aa0847 57 printf "$MIDDLE " "$(echo "$1" | sed -e 's#^apt\([cfghs]\)#apt-\1#')"
de81b2e2
DK
58 shift
59 done
60 fi
61 printf "${END}"
2a2a7ef4 62}
de81b2e2
DK
63msgdie() { msgprintf "${CERROR}E: %s" '%s' "${CNORMAL}\n" "$@" >&2; exit 1; }
64msgwarn() { msgprintf "${CWARNING}W: %s" '%s' "${CNORMAL}\n" "$@" >&2; }
65msgmsg() { msgprintf "${CMSG}%s" '%s' "${CNORMAL}\n" "$@"; }
66msginfo() { msgprintf "${CINFO}I: %s" '%s' "${CNORMAL}\n" "$@"; }
67msgdebug() { msgprintf "${CDEBUG}D: %s" '%s' "${CNORMAL}\n" "$@"; }
68msgdone() { msgprintf "${CDONE}DONE" '%s' "${CNORMAL}\n" "$@"; }
69msgnwarn() { msgprintf "${CWARNING}W: %s" '%s' "${CNORMAL}" "$@" >&2; }
70msgnmsg() { msgprintf "${CMSG}%s" '%s' "${CNORMAL}" "$@"; }
71msgninfo() { msgprintf "${CINFO}I: %s" '%s' "${CNORMAL}" "$@"; }
72msgndebug() { msgprintf "${CDEBUG}D: %s" '%s' "${CNORMAL}" "$@"; }
73msgtest() { msgprintf "${CINFO}%s" "${CCMD}%s${CINFO}" "…${CNORMAL} " "$@"; }
3c528b91 74msgpass() { printf "${CPASS}PASS${CNORMAL}\n"; }
1501a2c9 75msgreportheader() {
68042532 76 if [ -n "$MSGTEST_MSG" ]; then
1501a2c9 77 test "$1" != 'msgfailoutput' || echo
ae7fce5a
DK
78 if [ -n "$MSGTEST_MSGMSG" ]; then
79 echo "$MSGTEST_MSGMSG"
80 fi
22ac12b2 81 if [ -n "$MSGTEST_GRP" ] && [ "$MSGTEST_GRP" != 'NEXT' ] && [ "$MSGTEST_GRP" != "$MSGTEST_MSG" ]; then
1501a2c9 82 echo "${CFAIL}Part of the test group: $MSGTEST_GRP"
22ac12b2 83 fi
68042532
DK
84 echo -n "$MSGTEST_MSG"
85 unset MSGTEST_MSG
86 fi
1501a2c9
DK
87}
88msgskip() {
89 msgreportheader 'msgskip'
68ba0b7f
DK
90 if [ $# -gt 0 ]; then printf "${CWARNING}SKIP: $*${CNORMAL}\n" >&2;
91 else printf "${CWARNING}SKIP${CNORMAL}\n" >&2; fi
92}
5229b285 93msgfail() {
1501a2c9 94 msgreportheader 'msgfail'
68042532 95 if [ $# -gt 0 ] && [ -n "$1" ]; then printf "${CFAIL}FAIL: $*${CNORMAL}\n" >&2;
3c528b91 96 else printf "${CFAIL}FAIL${CNORMAL}\n" >&2; fi
03aa0847
DK
97 if [ -n "$APT_DEBUG_TESTS" ]; then
98 $SHELL
99 fi
5229b285
DK
100 EXIT_CODE=$((EXIT_CODE+1));
101}
9beb11aa
DK
102MSGGROUP_LEVEL=0
103msggroup() {
104 if [ -n "$1" ]; then
105 if [ $MSGGROUP_LEVEL = 0 ]; then
106 MSGTEST_GRP='NEXT'
107 fi
108 MSGGROUP_LEVEL=$((MSGGROUP_LEVEL+1));
109 else
110 MSGGROUP_LEVEL=$((MSGGROUP_LEVEL-1));
111 if [ $MSGGROUP_LEVEL = 0 ]; then
112 unset MSGTEST_GRP
113 fi
114 fi
115}
8d876415
DK
116
117# enable / disable Debugging
fc89263e
DK
118if [ $MSGLEVEL -le 0 ]; then
119 msgdie() { true; }
120fi
121if [ $MSGLEVEL -le 1 ]; then
122 msgwarn() { true; }
123 msgnwarn() { true; }
124fi
125if [ $MSGLEVEL -le 2 ]; then
ae7fce5a
DK
126 msgmsg() {
127 MSGTEST_MSGMSG="$(msgprintf "${CMSG}%s" '%s' "${CNORMAL}" "$@")"
128 }
fc89263e 129 msgnmsg() { true; }
9beb11aa
DK
130 msgtest() {
131 MSGTEST_MSG="$(msgprintf "${CINFO}%s" "${CCMD}%s${CINFO}" "…${CNORMAL} " "$@")"
132 if [ "$MSGTEST_GRP" = 'NEXT' ]; then
133 MSGTEST_GRP="$MSGTEST_MSG"
134 fi
135 }
3c528b91 136 msgpass() { printf " ${CPASS}P${CNORMAL}"; }
fc89263e
DK
137fi
138if [ $MSGLEVEL -le 3 ]; then
139 msginfo() { true; }
140 msgninfo() { true; }
141fi
142if [ $MSGLEVEL -le 4 ]; then
143 msgdebug() { true; }
144 msgndebug() { true; }
145fi
146msgdone() {
147 if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
148 [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
149 [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
150 [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
151 [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
152 true;
153 else
3c528b91 154 printf "${CDONE}DONE${CNORMAL}\n";
fc89263e
DK
155 fi
156}
e43a426e
MV
157getaptconfig() {
158 if [ -f ./aptconfig.conf ]; then
5465192b 159 echo "$(readlink -f ./aptconfig.conf)"
e43a426e 160 elif [ -f ../aptconfig.conf ]; then
5465192b
DK
161 echo "$(readlink -f ../aptconfig.conf)"
162 elif [ -f ../../aptconfig.conf ]; then
163 echo "$(readlink -f ../../aptconfig.conf)"
164 elif [ -f "${TMPWORKINGDIRECTORY}/aptconfig.conf" ]; then
165 echo "$(readlink -f "${TMPWORKINGDIRECTORY}/aptconfig.conf")"
166 fi
e43a426e 167}
8d876415
DK
168runapt() {
169 msgdebug "Executing: ${CCMD}$*${CDEBUG} "
846856f4
DK
170 local CMD="$1"
171 shift
3b8eb3bc 172 case $CMD in
a311fb96 173 sh|aptitude|*/*|command) ;;
3b8eb3bc
DK
174 *) CMD="${BUILDDIRECTORY}/$CMD";;
175 esac
a311fb96 176 MALLOC_PERTURB_=21 MALLOC_CHECK_=2 APT_CONFIG="$(getaptconfig)" LD_LIBRARY_PATH=${LIBRARYPATH} $CMD "$@"
8d876415 177}
846856f4
DK
178aptconfig() { runapt apt-config "$@"; }
179aptcache() { runapt apt-cache "$@"; }
180aptcdrom() { runapt apt-cdrom "$@"; }
181aptget() { runapt apt-get "$@"; }
182aptftparchive() { runapt apt-ftparchive "$@"; }
183aptkey() { runapt apt-key "$@"; }
184aptmark() { runapt apt-mark "$@"; }
4f6d26b4 185aptsortpkgs() { runapt apt-sortpkgs "$@"; }
796673c3 186apt() { runapt apt "$@"; }
3b8eb3bc
DK
187apthelper() { runapt "${APTHELPERBINDIR}/apt-helper" "$@"; }
188aptwebserver() { runapt "${APTWEBSERVERBINDIR}/aptwebserver" "$@"; }
189aptitude() { runapt aptitude "$@"; }
8d50b63f 190aptextracttemplates() { runapt apt-extracttemplates "$@"; }
3082603f 191aptinternalsolver() { runapt "${APTINTERNALSOLVER}" "$@"; }
ad7e0941 192aptdumpsolver() { runapt "${APTDUMPSOLVER}" "$@"; }
3b8eb3bc 193
158fda31 194dpkg() {
b0be0e09 195 "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" "$@"
158fda31 196}
ce7f128c 197dpkgcheckbuilddeps() {
63c71412 198 command dpkg-checkbuilddeps --admindir="${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg" "$@"
b6b5a542 199}
3fa950f1 200gdb() {
b07aeb1a
DK
201 local CMD
202 case "$1" in
203 aptget) CMD="apt-get";;
204 aptcache) CMD="apt-cache";;
5465192b
DK
205 aptcdrom) CMD="apt-cdrom";;
206 aptconfig) CMD="apt-config";;
b07aeb1a
DK
207 aptmark) CMD="apt-mark";;
208 apthelper) CMD="apt-helper";;
3d8232bf 209 aptftparchive) CMD="apt-ftparchive";;
63c71412 210 dpkg) shift; runapt "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg" "$@"; return;;
b07aeb1a
DK
211 *) CMD="$1";;
212 esac
ce928105 213 shift
921a9626
DK
214 if [ "${CMD##*/}" = "$CMD" ]; then
215 CMD="${BUILDDIRECTORY}/${CMD}"
216 fi
217 runapt command gdb --quiet -ex run "$CMD" --args "$CMD" "$@"
ae99ce2e 218}
8d876415 219
8c1dd12c 220exitwithstatus() {
f91bd741
MV
221 # error if we about to overflow, but ...
222 # "255 failures ought to be enough for everybody"
5d76cee1
MV
223 if [ $EXIT_CODE -gt 255 ]; then
224 msgdie "Total failure count $EXIT_CODE too big"
f91bd741 225 fi
5d76cee1 226 exit $((EXIT_CODE <= 255 ? EXIT_CODE : 255));
8c1dd12c
MV
227}
228
804d4a0d
DK
229shellsetedetector() {
230 local exit_status=$?
231 if [ "$exit_status" != '0' ]; then
3c528b91 232 printf >&2 "${CERROR}E: Looks like the testcases ended prematurely with exitcode: ${exit_status}${CNORMAL}\n"
804d4a0d
DK
233 if [ "$EXIT_CODE" = '0' ]; then
234 EXIT_CODE="$exit_status"
235 fi
236 fi
237}
238
b720d0bd 239addtrap() {
8437b7d4
DK
240 if [ "$1" = 'prefix' ]; then
241 CURRENTTRAP="$2 $CURRENTTRAP"
242 else
243 CURRENTTRAP="$CURRENTTRAP $1"
244 fi
804d4a0d 245 trap "shellsetedetector; $CURRENTTRAP exitwithstatus;" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
b720d0bd 246}
8d876415
DK
247
248setupenvironment() {
0d303f17
DK
249 # privilege dropping and testing doesn't work if /tmp isn't world-writeable (as e.g. with libpam-tmpdir)
250 if [ -n "$TMPDIR" ] && [ "$(id -u)" = '0' ] && [ "$(stat --format '%a' "$TMPDIR")" != '1777' ]; then
251 unset TMPDIR
252 fi
63c71412
DK
253 TMPWORKINGDIRECTORY="$(mktemp -d)"
254 addtrap "cd /; rm -rf \"$TMPWORKINGDIRECTORY\";"
255 msgninfo "Preparing environment for ${0##*/} in ${TMPWORKINGDIRECTORY}…"
5c0dd6fc 256
03aa0847 257 mkdir -m 700 "${TMPWORKINGDIRECTORY}/downloaded"
68ba0b7f
DK
258 if [ "$(id -u)" = '0' ]; then
259 # relax permissions so that running as root with user switching works
260 umask 022
03aa0847
DK
261 chmod 711 "$TMPWORKINGDIRECTORY"
262 chown _apt:root "${TMPWORKINGDIRECTORY}/downloaded"
68ba0b7f
DK
263 fi
264
63c71412 265 TESTDIRECTORY="$(readlink -f "$(dirname $0)")"
5c0dd6fc 266 # allow overriding the default BUILDDIR location
63c71412
DK
267 SOURCEDIRECTORY="${APT_INTEGRATION_TESTS_SOURCE_DIR:-"${TESTDIRECTORY}/../../"}"
268 BUILDDIRECTORY="${APT_INTEGRATION_TESTS_BUILD_DIR:-"${TESTDIRECTORY}/../../build/bin"}"
269 LIBRARYPATH="${APT_INTEGRATION_TESTS_LIBRARY_PATH:-"${BUILDDIRECTORY}"}"
270 METHODSDIR="${APT_INTEGRATION_TESTS_METHODS_DIR:-"${BUILDDIRECTORY}/methods"}"
271 APTHELPERBINDIR="${APT_INTEGRATION_TESTS_LIBEXEC_DIR:-"${BUILDDIRECTORY}"}"
272 APTWEBSERVERBINDIR="${APT_INTEGRATION_TESTS_WEBSERVER_BIN_DIR:-"${BUILDDIRECTORY}"}"
273 APTINTERNALSOLVER="${APT_INTEGRATION_TESTS_INTERNAL_SOLVER:-"${BUILDDIRECTORY}/apt-internal-solver"}"
274 APTDUMPSOLVER="${APT_INTEGRATION_TESTS_DUMP_SOLVER:-"${BUILDDIRECTORY}/apt-dump-solver"}"
8d876415 275 test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
5c0dd6fc
MV
276 # -----
277
63c71412 278 cd "$TMPWORKINGDIRECTORY"
cd725954 279 mkdir rootdir aptarchive keys
8d876415 280 cd rootdir
b29c3712 281 mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d
8fe964f1 282 mkdir -p usr/bin var/cache var/lib var/log tmp
cffea9af 283 mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
cd725954 284 touch var/lib/dpkg/available
8d876415 285 mkdir -p usr/lib/apt
63c71412 286 ln -s "${METHODSDIR}" usr/lib/apt/methods
1f6cf9e7
DK
287 if [ "$BUILDDIRECTORY" = "$LIBRARYPATH" ]; then
288 mkdir -p usr/lib/apt/solvers
289 ln -s "${BUILDDIRECTORY}/apt-dump-solver" usr/lib/apt/solvers/dump
290 ln -s "${BUILDDIRECTORY}/apt-internal-solver" usr/lib/apt/solvers/apt
291 echo "Dir::Bin::Solvers \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/solvers\";" > etc/apt/apt.conf.d/externalsolver.conf
292 fi
291a386f
MV
293 # use the autoremove from the BUILDDIRECTORY if its there, otherwise
294 # system
63c71412
DK
295 if [ -e "${BUILDDIRECTORY}/../../debian/apt.conf.autoremove" ]; then
296 ln -s "${BUILDDIRECTORY}/../../debian/apt.conf.autoremove" etc/apt/apt.conf.d/01autoremove
291a386f
MV
297 else
298 ln -s /etc/apt/apt.conf.d/01autoremove etc/apt/apt.conf.d/01autoremove
299 fi
8d876415 300 cd ..
63c71412
DK
301 local BASENAME="${0##*/}"
302 local PACKAGESFILE="Packages-${BASENAME#*-}"
53ea1b56
DK
303 if [ -f "${TESTDIRECTORY}/${PACKAGESFILE}" ]; then
304 cp "${TESTDIRECTORY}/${PACKAGESFILE}" aptarchive/Packages
8f8169ac 305 fi
63c71412 306 local SOURCESSFILE="Sources-${BASENAME#*-}"
53ea1b56
DK
307 if [ -f "${TESTDIRECTORY}/${SOURCESSFILE}" ]; then
308 cp "${TESTDIRECTORY}/${SOURCESSFILE}" aptarchive/Sources
8f8169ac 309 fi
63c71412
DK
310 for key in $(find "$TESTDIRECTORY" -name '*.pub' -o -name '*.sec'); do
311 cp "$key" keys/
312 chmod 644 "$key"
313 done
314 ln -s "${TMPWORKINGDIRECTORY}/keys/joesixpack.pub" rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
d6cf2345 315
cffea9af 316 echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
94eb3bee 317 echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
8d876415 318 echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
9d2a8a73 319 echo "Dir::Bin::Methods \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/methods\";" >> aptconfig.conf
23e64f6d
DK
320 # either store apt-key were we can access it, even if we run it as a different user
321 #cp "${BUILDDIRECTORY}/apt-key" "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/"
322 #chmod o+rx "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/apt-key"
323 #echo "Dir::Bin::apt-key \"${TMPWORKINGDIRECTORY}/rootdir/usr/bin/apt-key\";" >> aptconfig.conf
324 # destroys coverage reporting though, so we disable changing user for the calling gpgv
325 echo "Dir::Bin::apt-key \"${BUILDDIRECTORY}/apt-key\";" >> aptconfig.conf
68ba0b7f 326 if [ "$(id -u)" = '0' ]; then
23e64f6d 327 echo 'Binary::gpgv::Debug::NoDropPrivs "true";' >>aptconfig.conf
68ba0b7f 328 fi
b0be0e09 329
63c71412 330 cat > "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" <<EOF
b0be0e09
DK
331#!/bin/sh
332set -e
333if [ -r "${TMPWORKINGDIRECTORY}/noopchroot.so" ]; then
63c71412
DK
334 if [ -n "\$LD_LIBRARY_PATH" ]; then
335 export LD_LIBRARY_PATH="${TMPWORKINGDIRECTORY}:${LD_LIBRARY_PATH}"
b0be0e09 336 else
63c71412 337 export LD_LIBRARY_PATH="${TMPWORKINGDIRECTORY}"
b0be0e09 338 fi
921a9626 339 if [ -n "\$LD_PRELOAD" ]; then
63c71412 340 export LD_PRELOAD="noopchroot.so \${LD_PRELOAD}"
921a9626 341 else
63c71412 342 export LD_PRELOAD="noopchroot.so"
921a9626
DK
343 fi
344fi
63c71412
DK
345EOF
346 cp "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg"
347 cat >> "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" <<EOF
348exec fakeroot "${DPKG:-dpkg}" --root="${TMPWORKINGDIRECTORY}/rootdir" \\
349 --log="${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log" \\
350 --force-not-root --force-bad-path "\$@"
351EOF
352 cat >> "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg" <<EOF
921a9626 353exec fakeroot gdb --quiet -ex run "${DPKG:-dpkg}" --args "${DPKG:-dpkg}" --root="${TMPWORKINGDIRECTORY}/rootdir" \\
63c71412 354 --log="${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log" \\
b0be0e09
DK
355 --force-not-root --force-bad-path "\$@"
356EOF
921a9626 357 chmod +x "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg"
b0be0e09
DK
358 echo "Dir::Bin::dpkg \"${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg\";" > rootdir/etc/apt/apt.conf.d/99dpkg
359
533fe3d1
DK
360 {
361 if ! command dpkg --assert-multi-arch >/dev/null 2>&1; then
362 echo "DPKG::options:: \"--force-architecture\";" # Added to test multiarch before dpkg is ready for it…
363 fi
87d6947d 364 echo 'quiet "0";'
533fe3d1
DK
365 echo 'quiet::NoUpdate "true";'
366 echo 'quiet::NoStatistic "true";'
367 # too distracting for users, but helpful to detect changes
368 echo 'Acquire::Progress::Ignore::ShowErrorText "true";'
369 echo 'Acquire::Progress::Diffpercent "true";'
370 # in testcases, it can appear as if localhost has a rotation setup,
371 # hide this as we can't really deal with it properly
372 echo 'Acquire::Failure::ShowIP "false";'
ebca2f25
DK
373 # fakeroot can't fake everything, so disabled in production but good for tests
374 echo 'APT::Sandbox::Verify "true";'
533fe3d1 375 } >> aptconfig.conf
d6cf2345 376
68ba0b7f
DK
377 cp "${TESTDIRECTORY}/apt.pem" "${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem"
378 if [ "$(id -u)" = '0' ]; then
379 chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem"
380 fi
381 echo "Acquire::https::CaInfo \"${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem\";" > rootdir/etc/apt/apt.conf.d/99https
d6cf2345 382 echo "Apt::Cmd::Disable-Script-Warning \"1\";" > rootdir/etc/apt/apt.conf.d/apt-binary
23d35ec1 383 echo 'Acquire::Connect::AddrConfig "false";' > rootdir/etc/apt/apt.conf.d/connect-addrconfig
276e51dd 384 configcompression '.' 'gz' #'bz2' 'lzma' 'xz'
c5ede4ca 385 confighashes 'SHA1' # these are tests, not security best-practices
77a45beb 386
4bb006d1 387 # create some files in /tmp and look at user/group to get what this means
eab57e08 388 TEST_DEFAULT_USER="$(id -un)"
4bb006d1
DK
389 if [ "$(uname)" = 'GNU/kFreeBSD' ]; then
390 TEST_DEFAULT_GROUP='root'
391 else
eab57e08 392 TEST_DEFAULT_GROUP="$(id -gn)"
4bb006d1
DK
393 fi
394
ce7f128c 395 # cleanup the environment a bit
995a4bf6 396 # prefer our apt binaries over the system apt binaries
8c617819 397 export PATH="${BUILDDIRECTORY}:${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
be233796 398 export LC_ALL=C.UTF-8
3b8eb3bc 399 unset LANGUAGE APT_CONFIG
ce7f128c 400 unset GREP_OPTIONS DEB_BUILD_PROFILES
8d876415
DK
401 msgdone "info"
402}
403
ea65d079
DK
404getarchitecture() {
405 if [ "$1" = "native" -o -z "$1" ]; then
406 eval `aptconfig shell ARCH APT::Architecture`
407 if [ -n "$ARCH" ]; then
408 echo $ARCH
409 else
5834d7a1 410 dpkg --print-architecture
ea65d079
DK
411 fi
412 else
413 echo $1
414 fi
415}
416
53ea1b56 417getarchitectures() {
081c9d44 418 aptconfig dump --no-empty --format '%v%n' APT::Architecture APT::Architectures | sort -u | tr '\n' ' '
53ea1b56
DK
419}
420
3dcdc1f9
DK
421getarchitecturesfromcommalist() {
422 echo "$1" | sed -e 's#,#\n#g' | sed -e "s/^native\$/$(getarchitecture 'native')/"
423}
424
8d876415 425configarchitecture() {
18908589
DK
426 {
427 echo "APT::Architecture \"$(getarchitecture $1)\";"
428 while [ -n "$1" ]; do
429 echo "APT::Architectures:: \"$(getarchitecture $1)\";"
430 shift
431 done
432 } >rootdir/etc/apt/apt.conf.d/01multiarch.conf
53ea1b56
DK
433 configdpkg
434}
435
436configdpkg() {
437 if [ ! -e rootdir/var/lib/dpkg/status ]; then
63c71412
DK
438 local BASENAME="${0##*/}"
439 local STATUSFILE="status-${BASENAME#*-}"
53ea1b56
DK
440 if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
441 cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
442 else
443 echo -n > rootdir/var/lib/dpkg/status
444 fi
445 fi
18908589 446 rm -f rootdir/etc/apt/apt.conf.d/00foreigndpkg
846856f4 447 if command dpkg --assert-multi-arch >/dev/null 2>&1 ; then
53ea1b56
DK
448 local ARCHS="$(getarchitectures)"
449 if echo "$ARCHS" | grep -E -q '[^ ]+ [^ ]+'; then
450 DPKGARCH="$(dpkg --print-architecture)"
451 for ARCH in ${ARCHS}; do
feae193b 452 if [ "${ARCH}" != "${DPKGARCH}" ]; then
18908589 453 if ! dpkg --add-architecture ${ARCH} >/dev/null 2>&1; then
feae193b 454 # old-style used e.g. in Ubuntu-P – and as it seems travis
18908589
DK
455 echo "DPKG::options:: \"--foreign-architecture\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
456 echo "DPKG::options:: \"${ARCH}\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
feae193b
DK
457 fi
458 fi
53ea1b56
DK
459 done
460 if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
05343a22
DK
461 # dpkg doesn't really check the version as long as it is fully installed,
462 # but just to be sure we choose one above the required version
463 insertinstalledpackage 'dpkg' "all" '1.16.2+fake'
53ea1b56
DK
464 fi
465 fi
466 fi
8d876415
DK
467}
468
0c787570
DK
469configdpkgnoopchroot() {
470 # create a library to noop chroot() and rewrite maintainer script executions
471 # via execvp() as used by dpkg as we don't want our rootdir to be a fullblown
472 # chroot directory dpkg could chroot into to execute the maintainer scripts
473 msgtest 'Building library to preload to make maintainerscript work in' 'dpkg'
63c71412 474 cat > noopchroot.c << EOF
0c787570
DK
475#define _GNU_SOURCE
476#include <stdio.h>
477#include <stdlib.h>
478#include <string.h>
479#include <dlfcn.h>
480
481static char * chrootdir = NULL;
482
483int chroot(const char *path) {
484 printf("WARNING: CHROOTing to %s was ignored!\n", path);
485 free(chrootdir);
486 chrootdir = strdup(path);
487 return 0;
488}
489int execvp(const char *file, char *const argv[]) {
490 static int (*func_execvp) (const char *, char * const []) = NULL;
491 if (func_execvp == NULL)
492 func_execvp = (int (*) (const char *, char * const [])) dlsym(RTLD_NEXT, "execvp");
493 if (chrootdir == NULL || strncmp(file, "/var/lib/dpkg/", strlen("/var/lib/dpkg/")) != 0)
494 return func_execvp(file, argv);
495 printf("REWRITE execvp call %s into %s\n", file, chrootdir);
496 char newfile[strlen(chrootdir) + strlen(file)];
497 strcpy(newfile, chrootdir);
498 strcat(newfile, file);
b0be0e09
DK
499 char const * const baseadmindir = "/var/lib/dpkg";
500 char admindir[strlen(chrootdir) + strlen(baseadmindir)];
501 strcpy(admindir, chrootdir);
502 strcat(admindir, baseadmindir);
503 setenv("DPKG_ADMINDIR", admindir, 1);
0c787570
DK
504 return func_execvp(newfile, argv);
505}
506EOF
507 testsuccess --nomsg gcc -fPIC -shared -o noopchroot.so noopchroot.c -ldl
0c787570 508}
276e51dd
DK
509configcompression() {
510 while [ -n "$1" ]; do
511 case "$1" in
3c528b91
MO
512 '.') printf ".\t.\tcat\n";;
513 'gz') printf "gzip\tgz\tgzip\n";;
514 'bz2') printf "bzip2\tbz2\tbzip2\n";;
515 'lzma') printf "lzma\tlzma\txz --format=lzma\n";;
516 'xz') printf "xz\txz\txz\n";;
517 *) printf "$1\t$1\t$1\n";;
276e51dd
DK
518 esac
519 shift
63c71412 520 done > "${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf"
276e51dd 521}
c5ede4ca
DK
522confighashes() {
523 {
524 echo 'APT::FTPArchive {'
525 {
526 while [ -n "$1" ]; do
527 printf "$1" | tr 'a-z' 'A-Z'
528 printf "\t\"true\";\n"
529 shift
530 done
531 for h in 'MD5' 'SHA1' 'SHA256' 'SHA512'; do
532 printf "$h\t\"false\";\n"
533 done
534 } | awk '!x[$1]++'
535 echo '};'
536 } >> "${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/ftparchive-hashes.conf"
537}
5f982b9d
DK
538forcecompressor() {
539 COMPRESSOR="$1"
540 COMPRESSOR_CMD="$1"
541 case $COMPRESSOR in
542 gzip) COMPRESS='gz';;
543 bzip2) COMPRESS='bz2';;
544 lzma) COMPRESS='lzma';;
545 xz) COMPRESS='xz';;
546 *) msgdie "Compressor $COMPRESSOR is unknown to framework, so can't be forced by forcecompressor!";;
547 esac
548 local CONFFILE="${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/00force-compressor"
549 echo "Acquire::CompressionTypes::Order { \"${COMPRESS}\"; };
550Dir::Bin::uncompressed \"/does/not/exist\";
551Dir::Bin::gzip \"/does/not/exist\";
552Dir::Bin::bzip2 \"/does/not/exist\";
553Dir::Bin::lzma \"/does/not/exist\";
554Dir::Bin::xz \"/does/not/exist\";" > "$CONFFILE"
555 if [ -e "/bin/${COMPRESSOR}" ]; then
556 echo "Dir::Bin::${COMPRESSOR} \"/bin/${COMPRESSOR}\";" >> "$CONFFILE"
557 elif [ -e "/usr/bin/${COMPRESSOR}" ]; then
558 echo "Dir::Bin::${COMPRESSOR} \"/usr/bin/${COMPRESSOR}\";" >> "$CONFFILE"
559 elif [ "${COMPRESSOR}" = 'lzma' ]; then
560 echo 'Dir::Bin::xz "/usr/bin/xz";' >> "$CONFFILE"
561 COMPRESSOR_CMD='xz --format=lzma'
562 else
563 msgtest 'Test for availability of compressor' "${COMPRESSOR}"
68042532 564 msgfail "${COMPRESSOR} not available"
5f982b9d
DK
565 fi
566}
567
75954ae2 568setupsimplenativepackage() {
ce9864a8
DK
569 local NAME="$1"
570 local ARCH="$2"
571 local VERSION="$3"
572 local RELEASE="${4:-unstable}"
573 local DEPENDENCIES="$5"
14109555 574 local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
18908589
DK
575 If you find such a package installed on your system,
576 something went horribly wrong! They are autogenerated
3d284148 577 und used only by testcases and serve no other purpose…"}"
18908589 578
b7899b00
DK
579 local SECTION="${7:-others}"
580 local DISTSECTION
63c71412 581 if [ "$SECTION" = "${SECTION#*/}" ]; then
b7899b00
DK
582 DISTSECTION="main"
583 else
63c71412 584 DISTSECTION="${SECTION%/*}"
b7899b00 585 fi
ce9864a8
DK
586 local BUILDDIR=incoming/${NAME}-${VERSION}
587 mkdir -p ${BUILDDIR}/debian/source
588 cd ${BUILDDIR}
589 echo "* most suckless software product ever" > FEATURES
b7899b00
DK
590 test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright
591 test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
ce9864a8
DK
592
593 * Initial release
594
b7899b00
DK
595 -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog
596 test -e debian/control || echo "Source: $NAME
597Section: $SECTION
ce9864a8
DK
598Priority: optional
599Maintainer: Joe Sixpack <joe@example.org>
600Build-Depends: debhelper (>= 7)
601Standards-Version: 3.9.1
602
875bcb36
DK
603Package: $NAME" > debian/control
604 if [ "$ARCH" = 'all' ]; then
605 echo "Architecture: all" >> debian/control
606 else
607 echo "Architecture: any" >> debian/control
608 fi
ce9864a8 609 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
18908589
DK
610 echo "Description: $DESCRIPTION" >> debian/control
611
b7899b00
DK
612 test -e debian/compat || echo "7" > debian/compat
613 test -e debian/source/format || echo "3.0 (native)" > debian/source/format
ce9864a8 614 test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
75954ae2
DK
615 cd - > /dev/null
616}
617
618buildsimplenativepackage() {
619 local NAME="$1"
d56e2917
DK
620 local NM
621 if [ "$(echo "$NAME" | cut -c 1-3)" = 'lib' ]; then
622 NM="$(echo "$NAME" | cut -c 1-4)"
623 else
624 NM="$(echo "$NAME" | cut -c 1)"
625 fi
75954ae2
DK
626 local ARCH="$2"
627 local VERSION="$3"
628 local RELEASE="${4:-unstable}"
629 local DEPENDENCIES="$5"
14109555 630 local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
18908589
DK
631 If you find such a package installed on your system,
632 something went horribly wrong! They are autogenerated
3d284148 633 und used only by testcases and serve no other purpose…"}"
18908589 634
75954ae2 635 local SECTION="${7:-others}"
d67004e0 636 local PRIORITY="${8:-optional}"
63c71412
DK
637 local FILE_TREE="$9"
638 local COMPRESS_TYPE="${10:-gzip}"
75954ae2 639 local DISTSECTION
63c71412 640 if [ "$SECTION" = "${SECTION#*/}" ]; then
75954ae2
DK
641 DISTSECTION="main"
642 else
63c71412 643 DISTSECTION="${SECTION%/*}"
75954ae2 644 fi
63c71412 645 local BUILDDIR="${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}"
b761356f 646
0c787570 647 msgtest "Build source package in version ${VERSION} for ${RELEASE} in ${DISTSECTION}" "$NAME"
63c71412
DK
648 mkdir -p "$BUILDDIR/debian/source"
649 echo "* most suckless software product ever" > "${BUILDDIR}/FEATURES"
b761356f 650 echo "#!/bin/sh
63c71412 651echo '$NAME says \"Hello!\"'" > "${BUILDDIR}/${NAME}"
b761356f 652
63c71412 653 echo "Copyleft by Joe Sixpack $(date +%Y)" > "${BUILDDIR}/debian/copyright"
b761356f
DK
654 echo "$NAME ($VERSION) $RELEASE; urgency=low
655
656 * Initial release
657
63c71412
DK
658 -- Joe Sixpack <joe@example.org> $(date -R)" > "${BUILDDIR}/debian/changelog"
659 {
660 echo "Source: $NAME
d67004e0 661Priority: $PRIORITY
b761356f 662Maintainer: Joe Sixpack <joe@example.org>
63c71412
DK
663Standards-Version: 3.9.3"
664 if [ "$SECTION" != '<none>' ]; then
665 echo "Section: $SECTION"
666 fi
667 local BUILDDEPS="$(echo "$DEPENDENCIES" | grep '^Build-')"
668 test -z "$BUILDDEPS" || echo "$BUILDDEPS"
669 echo "
670Package: $NAME"
b761356f 671
63c71412
DK
672 if [ "$ARCH" = 'all' ]; then
673 echo "Architecture: all"
674 else
675 echo "Architecture: any"
676 fi
677 local DEPS="$(echo "$DEPENDENCIES" | grep -v '^Build-')"
678 test -z "$DEPS" || echo "$DEPS"
679 echo "Description: $DESCRIPTION"
680 } > "${BUILDDIR}/debian/control"
01f520ce 681
63c71412
DK
682 echo '3.0 (native)' > "${BUILDDIR}/debian/source/format"
683 cd "${BUILDDIR}/.."
0c787570
DK
684 testsuccess --nomsg dpkg-source -b ${NAME}-${VERSION}
685 cd - >/dev/null
63c71412 686 sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output" \
f1828b69 687 | while read SRC; do
63c71412 688 echo "pool/${SRC}" >> "${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist"
f1828b69 689# if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
33a22672 690# aptkey --keyring ./keys/joesixpack.pub --secret-keyring ./keys/joesixpack.sec --quiet --readonly \
bd7fb5aa 691# adv --yes --default-key 'Joe Sixpack' \
f1828b69
DK
692# --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
693# mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
694# fi
b761356f 695 done
84aa13f4 696
3dcdc1f9 697 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
0c787570 698 msgtest "Build binary package for ${RELEASE} in ${SECTION}" "$NAME"
63c71412
DK
699 rm -rf "${BUILDDIR}/debian/tmp"
700 mkdir -p "${BUILDDIR}/debian/tmp/DEBIAN" "${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}" "${BUILDDIR}/debian/tmp/usr/bin"
701 cp "${BUILDDIR}/debian/copyright" "${BUILDDIR}/debian/changelog" "${BUILDDIR}/FEATURES" "${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}"
702 cp "${BUILDDIR}/${NAME}" "${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}"
42c1513b 703 if [ -n "$FILE_TREE" ]; then
63c71412 704 cp -ar "$FILE_TREE" "${BUILDDIR}/debian/tmp"
42c1513b 705 fi
42c1513b 706
63c71412
DK
707 (cd "${BUILDDIR}"; dpkg-gencontrol -DArchitecture=$arch)
708 (cd "${BUILDDIR}/debian/tmp"; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
846856f4 709 local LOG="${BUILDDIR}/../${NAME}_${VERSION}_${arch}.dpkg-deb.log"
d56e2917 710 # ensure the right permissions as dpkg-deb insists
63c71412
DK
711 chmod 755 "${BUILDDIR}/debian/tmp/DEBIAN"
712 testsuccess --nomsg dpkg-deb -Z${COMPRESS_TYPE} --build "${BUILDDIR}/debian/tmp" "${BUILDDIR}/.."
713 echo "pool/${NAME}_${VERSION}_${arch}.deb" >> "${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist"
84aa13f4
DK
714 done
715
d56e2917 716 local CHANGEPATH="${BUILDDIR}/../${DISTSECTION}/${NM}/${NAME}/${NAME}_${VERSION}"
63c71412
DK
717 mkdir -p "$CHANGEPATH"
718 cp "${BUILDDIR}/debian/changelog" "$CHANGEPATH"
5a635ee4 719 rm -rf "${BUILDDIR}"
b761356f 720 msgdone "info"
75954ae2
DK
721}
722
723buildpackage() {
724 local BUILDDIR=$1
725 local RELEASE=$2
726 local SECTION=$3
ea65d079 727 local ARCH=$(getarchitecture $4)
846856f4
DK
728 local PKGNAME="$(echo "$BUILDDIR" | grep -o '[^/]*$')"
729 local BUILDLOG="$(readlink -f "${BUILDDIR}/../${PKGNAME}_${RELEASE}_${SECTION}.dpkg-bp.log")"
0c787570 730 msgtest "Build package for ${RELEASE} in ${SECTION}" "$PKGNAME"
63c71412 731 cd "$BUILDDIR"
75954ae2
DK
732 if [ "$ARCH" = "all" ]; then
733 ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
734 fi
0c787570 735 testsuccess --nomsg dpkg-buildpackage -uc -us -a$ARCH
63c71412
DK
736 cp "${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output" "$BUILDLOG"
737 local PKGS="$(grep '^dpkg-deb: building package' "$BUILDLOG" | cut -d'/' -f 2 | sed -e "s#'\.##")"
738 local SRCS="$(grep '^dpkg-source: info: building' "$BUILDLOG" | grep -o '[a-z0-9._+~-]*$')"
ce9864a8 739 cd - > /dev/null
b7899b00 740 for PKG in $PKGS; do
63c71412 741 echo "pool/${PKG}" >> "${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist"
b7899b00
DK
742 done
743 for SRC in $SRCS; do
63c71412 744 echo "pool/${SRC}" >> "${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist"
b7899b00 745 done
ce9864a8
DK
746}
747
748buildaptarchive() {
ce9864a8 749 if [ -d incoming ]; then
846856f4 750 buildaptarchivefromincoming "$@"
ce9864a8 751 else
846856f4 752 buildaptarchivefromfiles "$@"
ce9864a8
DK
753 fi
754}
755
756createaptftparchiveconfig() {
63c71412 757 local COMPRESSORS="$(cut -d' ' -f 1 "${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf" | tr '\n' ' ')"
016bea82
DK
758 local COMPRESSORS="${COMPRESSORS%* }"
759 local ARCHS="$(getarchitectures)"
c5ede4ca
DK
760 cat > ftparchive.conf <<EOF
761Dir {
762 ArchiveDir "$(readlink -f .)";
763 CacheDir "$(readlink -f ..)";
764 FileListDir "$(readlink -f pool/)";
b7899b00
DK
765};
766Default {
c5ede4ca
DK
767 Packages::Compress "$COMPRESSORS";
768 Sources::Compress "$COMPRESSORS";
769 Contents::Compress "$COMPRESSORS";
770 Translation::Compress "$COMPRESSORS";
18331adf 771 LongDescription "false";
ce9864a8
DK
772};
773TreeDefault {
774 Directory "pool/";
775 SrcDirectory "pool/";
776};
c5ede4ca 777EOF
b7899b00 778 for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
63c71412
DK
779 cat <<EOF
780tree "dists/$DIST" {
1dd20368 781 Architectures "$ARCHS all source";
63c71412
DK
782 FileList "${DIST}.\$(SECTION).pkglist";
783 SourceFileList "${DIST}.\$(SECTION).srclist";
784 Sections "$(find ./pool/ -maxdepth 1 -name "${DIST}.*.pkglist" -type f | cut -d'/' -f 3 | cut -d'.' -f 2 | sort | uniq | tr '\n' ' ')";
785};
786EOF
787 done >> ftparchive.conf
ce9864a8
DK
788}
789
790buildaptftparchivedirectorystructure() {
b7899b00
DK
791 local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
792 for DIST in $DISTS; do
793 local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
794 for SECTION in $SECTIONS; do
795 local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
796 for ARCH in $ARCHS; do
63c71412 797 mkdir -p "dists/${DIST}/${SECTION}/binary-${ARCH}"
b7899b00 798 done
63c71412
DK
799 mkdir -p "dists/${DIST}/${SECTION}/source"
800 mkdir -p "dists/${DIST}/${SECTION}/i18n"
b7899b00 801 done
ce9864a8 802 done
ce9864a8
DK
803}
804
9b78cda6 805insertpackage() {
10e100e5 806 local RELEASES="$1"
9b78cda6
DK
807 local NAME="$2"
808 local ARCH="$3"
809 local VERSION="$4"
810 local DEPENDENCIES="$5"
d67004e0 811 local PRIORITY="${6:-optional}"
10e100e5 812 local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASES}
18908589
DK
813 If you find such a package installed on your system,
814 something went horribly wrong! They are autogenerated
3d284148 815 und used only by testcases and serve no other purpose…"}"
d67004e0 816 local ARCHS=""
10e100e5
DK
817 for RELEASE in $(printf '%s' "$RELEASES" | tr ',' '\n'); do
818 if [ "$RELEASE" = 'installed' ]; then
819 insertinstalledpackage "$2" "$3" "$4" "$5" "$6" "$7"
820 continue
d67004e0 821 fi
10e100e5 822 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
1dd20368 823 if [ "$arch" = 'none' ]; then
10e100e5
DK
824 ARCHS="$(getarchitectures)"
825 else
826 ARCHS="$arch"
827 fi
828 for BUILDARCH in $ARCHS; do
829 local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
63c71412
DK
830 mkdir -p "$PPATH"
831 {
832 echo "Package: $NAME
d67004e0 833Priority: $PRIORITY
9b78cda6 834Section: other
2c085486 835Installed-Size: 42
63c71412
DK
836Maintainer: Joe Sixpack <joe@example.org>"
837 test "$arch" = 'none' || echo "Architecture: $arch"
838 echo "Version: $VERSION
839Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb"
840 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES"
841 echo "Description: $(printf '%s' "$DESCRIPTION" | head -n 1)"
842 echo "Description-md5: $(printf '%s' "$DESCRIPTION" | md5sum | cut -d' ' -f 1)"
843 echo
844 } >> "${PPATH}/Packages"
10e100e5 845 done
d67004e0 846 done
63c71412
DK
847 mkdir -p "aptarchive/dists/${RELEASE}/main/source" "aptarchive/dists/${RELEASE}/main/i18n"
848 touch "aptarchive/dists/${RELEASE}/main/source/Sources"
10e100e5 849 echo "Package: $NAME
0e0d9919
DK
850Description-md5: $(printf '%s' "$DESCRIPTION" | md5sum | cut -d' ' -f 1)
851Description-en: $DESCRIPTION
63c71412 852" >> "aptarchive/dists/${RELEASE}/main/i18n/Translation-en"
10e100e5 853 done
9b78cda6
DK
854}
855
234675b7
DK
856insertsource() {
857 local RELEASE="$1"
858 local NAME="$2"
859 local ARCH="$3"
860 local VERSION="$4"
861 local DEPENDENCIES="$5"
a8275acf 862 local BINARY="${6:-$NAME}"
234675b7
DK
863 local ARCHS=""
864 local SPATH="aptarchive/dists/${RELEASE}/main/source"
865 mkdir -p $SPATH
866 local FILE="${SPATH}/Sources"
9d2a8a73
DK
867 local DSCFILE="${NAME}_${VERSION}.dsc"
868 local TARFILE="${NAME}_${VERSION}.tar.gz"
234675b7 869 echo "Package: $NAME
a8275acf 870Binary: $BINARY
234675b7
DK
871Version: $VERSION
872Maintainer: Joe Sixpack <joe@example.org>
873Architecture: $ARCH" >> $FILE
63c71412 874 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> "$FILE"
234675b7 875 echo "Files:
63c71412
DK
876 $(echo -n "$DSCFILE" | md5sum | cut -d' ' -f 1) $(echo -n "$DSCFILE" | wc -c) "$DSCFILE"
877 $(echo -n "$TARFILE" | md5sum | cut -d' ' -f 1) $(echo -n "$TARFILE" | wc -c) "$TARFILE"
55ae7a51 878Checksums-Sha256:
63c71412
DK
879 $(echo -n "$DSCFILE" | sha256sum | cut -d' ' -f 1) $(echo -n "$DSCFILE" | wc -c) "$DSCFILE"
880 $(echo -n "$TARFILE" | sha256sum | cut -d' ' -f 1) $(echo -n "$TARFILE" | wc -c) "$TARFILE"
881" >> "$FILE"
234675b7
DK
882}
883
dfc2b1be
DK
884insertinstalledpackage() {
885 local NAME="$1"
886 local ARCH="$2"
887 local VERSION="$3"
888 local DEPENDENCIES="$4"
d67004e0 889 local PRIORITY="${5:-optional}"
d4b4e5ea 890 local STATUS="${6:-install ok installed}"
14109555 891 local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/installed
18908589
DK
892 If you find such a package installed on your system,
893 something went horribly wrong! They are autogenerated
3d284148 894 und used only by testcases and serve no other purpose…"}"
18908589 895
53ea1b56
DK
896 local FILE='rootdir/var/lib/dpkg/status'
897 local INFO='rootdir/var/lib/dpkg/info'
3dcdc1f9 898 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
d67004e0 899 echo "Package: $NAME
d4b4e5ea 900Status: $STATUS
d67004e0 901Priority: $PRIORITY
dfc2b1be
DK
902Section: other
903Installed-Size: 42
904Maintainer: Joe Sixpack <joe@example.org>
63c71412
DK
905Version: $VERSION" >> "$FILE"
906 test "$arch" = 'none' || echo "Architecture: $arch" >> "$FILE"
907 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> "$FILE"
908 echo "Description: $DESCRIPTION" >> "$FILE"
909 echo >> "$FILE"
53ea1b56 910 if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
63c71412 911 echo -n > "${INFO}/${NAME}:${arch}.list"
53ea1b56 912 else
63c71412 913 echo -n > "${INFO}/${NAME}.list"
53ea1b56 914 fi
d67004e0 915 done
dfc2b1be
DK
916}
917
918
ce9864a8 919buildaptarchivefromincoming() {
63c71412 920 msginfo "Build APT archive for ${CCMD}${0##*/}${CINFO} based on incoming packages…"
ce9864a8
DK
921 cd aptarchive
922 [ -e pool ] || ln -s ../incoming pool
923 [ -e ftparchive.conf ] || createaptftparchiveconfig
924 [ -e dists ] || buildaptftparchivedirectorystructure
b7899b00 925 msgninfo "\tGenerate Packages, Sources and Contents files… "
31be38d2 926 testsuccess aptftparchive generate ftparchive.conf
ce9864a8
DK
927 cd - > /dev/null
928 msgdone "info"
4dbfe436 929 generatereleasefiles "$@"
ce9864a8
DK
930}
931
932buildaptarchivefromfiles() {
63c71412 933 msginfo "Build APT archive for ${CCMD}${0##*/}${CINFO} based on prebuild files…"
8d041b4f
DK
934 local DIR='aptarchive'
935 if [ -d "${DIR}/dists" ]; then DIR="${DIR}/dists"; fi
936 find "$DIR" -name 'Packages' -o -name 'Sources' -o -name 'Translation-*' | while read line; do
9b78cda6 937 msgninfo "\t${line} file… "
276e51dd 938 compressfile "$line" "$1"
8d876415 939 msgdone "info"
9b78cda6 940 done
e3c62328 941 generatereleasefiles "$@"
9b78cda6
DK
942}
943
276e51dd 944compressfile() {
63c71412 945 cat "${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf" | while read compressor extension command; do
276e51dd
DK
946 if [ "$compressor" = '.' ]; then
947 if [ -n "$2" ]; then
948 touch -d "$2" "$1"
949 fi
950 continue
951 fi
952 cat "$1" | $command > "${1}.${extension}"
953 if [ -n "$2" ]; then
954 touch -d "$2" "${1}.${extension}"
955 fi
956 done
957}
958
a3bbbab7 959# can be overridden by testcases for their pleasure
bf9e7447
DK
960getcodenamefromsuite() {
961 case "$1" in
962 unstable) echo 'sid';;
963 *) echo -n "$1";;
964 esac
965}
a3bbbab7 966getreleaseversionfromsuite() { true; }
718f797c 967getlabelfromsuite() { true; }
d56e2917 968getoriginfromsuite() { true; }
1dd20368 969getarchitecturesfromreleasefile() { echo "all $(getarchitectures)"; }
a3bbbab7 970
c5ede4ca
DK
971aptftparchiverelease() {
972 aptftparchive -qq release "$@" | sed -e '/0 Release$/ d' # remove the self reference
973}
9b78cda6 974generatereleasefiles() {
884a4c0a
DK
975 # $1 is the Date header and $2 is the ValidUntil header to be set
976 # both should be given in notation date/touch can understand
1dd20368
DK
977 local DATE="$1"
978 local VALIDUNTIL="$2"
9b78cda6 979 if [ -e aptarchive/dists ]; then
87d6947d 980 msgninfo "\tGenerate Release files for dists… "
9b78cda6 981 for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
1dd20368 982 local ARCHITECTURES="$(getarchitecturesfromreleasefile "$dir")"
a3bbbab7
DK
983 local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
984 local CODENAME="$(getcodenamefromsuite $SUITE)"
985 local VERSION="$(getreleaseversionfromsuite $SUITE)"
718f797c 986 local LABEL="$(getlabelfromsuite $SUITE)"
d56e2917 987 local ORIGIN="$(getoriginfromsuite $SUITE)"
63c71412 988 aptftparchiverelease "$dir" \
884a4c0a
DK
989 -o APT::FTPArchive::Release::Suite="${SUITE}" \
990 -o APT::FTPArchive::Release::Codename="${CODENAME}" \
bc7a59dd
DK
991 -o APT::FTPArchive::Release::Architectures="${ARCHITECTURES}" \
992 -o APT::FTPArchive::Release::Label="${LABEL}" \
993 -o APT::FTPArchive::Release::Origin="${ORIGIN}" \
994 -o APT::FTPArchive::Release::Version="${VERSION}" \
63c71412 995 > "$dir/Release"
a3bbbab7 996 if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
35faae11 997 sed -i '/^Date: / a\
63c71412 998NotAutomatic: yes' "$dir/Release"
35faae11 999 fi
9b78cda6
DK
1000 done
1001 else
87d6947d 1002 msgninfo "\tGenerate Release files for flat… "
c5ede4ca 1003 aptftparchiverelease ./aptarchive > aptarchive/Release
8d876415 1004 fi
1dd20368 1005 if [ -n "$DATE" -a "$DATE" != "now" ]; then
fe0f7911 1006 for release in $(find ./aptarchive -name 'Release'); do
1dd20368
DK
1007 sed -i "s/^Date: .*$/Date: $(date -d "$DATE" '+%a, %d %b %Y %H:%M:%S %Z')/" "$release"
1008 touch -d "$DATE" "$release"
fe0f7911 1009 done
8d876415 1010 fi
1dd20368 1011 if [ -n "$VALIDUNTIL" ]; then
c5ede4ca 1012 sed -i "/^Date: / a\
1dd20368 1013Valid-Until: $(date -d "$VALIDUNTIL" '+%a, %d %b %Y %H:%M:%S %Z')" $(find ./aptarchive -name 'Release')
c5ede4ca 1014 fi
b7899b00 1015 msgdone "info"
8d876415
DK
1016}
1017
b7899b00 1018setupdistsaptarchive() {
63c71412 1019 local APTARCHIVE="$(readlink -f ./aptarchive | sed 's# #%20#g')"
b7899b00
DK
1020 rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
1021 rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
1022 for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
63c71412 1023 SECTIONS=$(find "./aptarchive/dists/${DISTS}/" -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
b7899b00 1024 msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
63c71412
DK
1025 echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > "rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list"
1026 echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > "rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list"
b7899b00
DK
1027 msgdone "info"
1028 done
1029}
1030
1031setupflataptarchive() {
63c71412
DK
1032 local APTARCHIVE="$(readlink -f ./aptarchive)"
1033 local APTARCHIVEURI="$(readlink -f ./aptarchive | sed 's# #%20#g')"
1034 if [ -f "${APTARCHIVE}/Packages" ]; then
8d876415 1035 msgninfo "\tadd deb sources.list line… "
63c71412
DK
1036 echo "deb file://$APTARCHIVEURI /" > 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list'
1037 msgdone 'info'
8d876415 1038 else
63c71412 1039 rm -f 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list'
8d876415 1040 fi
63c71412 1041 if [ -f "${APTARCHIVE}/Sources" ]; then
8d876415 1042 msgninfo "\tadd deb-src sources.list line… "
63c71412
DK
1043 echo "deb-src file://$APTARCHIVEURI /" > 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list'
1044 msgdone 'info'
8d876415 1045 else
63c71412 1046 rm -f 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list'
8d876415 1047 fi
b7899b00
DK
1048}
1049
1050setupaptarchive() {
4dbfe436
DK
1051 local NOUPDATE=0
1052 if [ "$1" = '--no-update' ]; then
1053 NOUPDATE=1
1054 shift
1055 fi
1056 buildaptarchive "$@"
b7899b00
DK
1057 if [ -e aptarchive/dists ]; then
1058 setupdistsaptarchive
1059 else
1060 setupflataptarchive
1061 fi
9d653a6d 1062 signreleasefiles 'Joe Sixpack'
4dbfe436 1063 if [ "1" != "$NOUPDATE" ]; then
5684f71f 1064 testsuccess aptget update -o Debug::pkgAcquire::Worker=true -o Debug::Acquire::gpgv=true
b2ea1a47 1065 fi
8d876415
DK
1066}
1067
f213b6ea
DK
1068signreleasefiles() {
1069 local SIGNER="${1:-Joe Sixpack}"
07cb47e7 1070 local REPODIR="${2:-aptarchive}"
f1e1abd8 1071 local KEY="keys/$(echo "$SIGNER" | tr 'A-Z' 'a-z' | sed 's# ##g')"
33a22672 1072 local GPG="aptkey --quiet --keyring ${KEY}.pub --secret-keyring ${KEY}.sec --readonly adv --batch --yes"
f1e1abd8 1073 msgninfo "\tSign archive with $SIGNER key $KEY… "
29a59c46
DK
1074 local REXKEY='keys/rexexpired'
1075 local SECEXPIREBAK="${REXKEY}.sec.bak"
1076 local PUBEXPIREBAK="${REXKEY}.pub.bak"
1077 if [ "${SIGNER}" = 'Rex Expired' ]; then
1078 # the key is expired, so gpg doesn't allow to sign with and the --faked-system-time
1079 # option doesn't exist anymore (and using faketime would add a new obscure dependency)
1080 # therefore we 'temporary' make the key not expired and restore a backup after signing
63c71412
DK
1081 cp "${REXKEY}.sec" "$SECEXPIREBAK"
1082 cp "${REXKEY}.pub" "$PUBEXPIREBAK"
29a59c46
DK
1083 local SECUNEXPIRED="${REXKEY}.sec.unexpired"
1084 local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
1085 if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
63c71412
DK
1086 cp "$SECUNEXPIRED" "${REXKEY}.sec"
1087 cp "$PUBUNEXPIRED" "${REXKEY}.pub"
29a59c46 1088 else
f1e1abd8
DK
1089 if ! printf "expire\n1w\nsave\n" | $GPG --default-key "$SIGNER" --command-fd 0 --edit-key "${SIGNER}" >setexpire.gpg 2>&1; then
1090 cat setexpire.gpg
1091 exit 1
1092 fi
63c71412
DK
1093 cp "${REXKEY}.sec" "$SECUNEXPIRED"
1094 cp "${REXKEY}.pub" "$PUBUNEXPIRED"
29a59c46
DK
1095 fi
1096 fi
63c71412
DK
1097 for RELEASE in $(find "${REPODIR}/" -name Release); do
1098 $GPG --default-key "$SIGNER" --armor --detach-sign --sign --output "${RELEASE}.gpg" "${RELEASE}"
e3c62328 1099 local INRELEASE="$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')"
63c71412 1100 $GPG --default-key "$SIGNER" --clearsign --output "$INRELEASE" "$RELEASE"
e3c62328 1101 # we might have set a specific date for the Release file, so copy it
63c71412 1102 touch -d "$(stat --format "%y" ${RELEASE})" "${RELEASE}.gpg" "${INRELEASE}"
f213b6ea 1103 done
29a59c46 1104 if [ -f "$SECEXPIREBAK" ] && [ -f "$PUBEXPIREBAK" ]; then
63c71412
DK
1105 mv -f "$SECEXPIREBAK" "${REXKEY}.sec"
1106 mv -f "$PUBEXPIREBAK" "${REXKEY}.pub"
29a59c46 1107 fi
63c71412 1108 msgdone 'info'
f213b6ea
DK
1109}
1110
8eafc759
DK
1111redatereleasefiles() {
1112 local DATE="$(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')"
1113 for release in $(find aptarchive/ -name 'Release'); do
63c71412
DK
1114 sed -i "s/^Date: .*$/Date: ${DATE}/" "$release"
1115 touch -d "$DATE" "$release"
8eafc759
DK
1116 done
1117 signreleasefiles "${2:-Joe Sixpack}"
1118}
1119
f2c0ec8b 1120webserverconfig() {
6c0765c0 1121 local WEBSERVER="${3:-http://localhost:${APTHTTPPORT}}"
b0314abb
DK
1122 local NOCHECK=false
1123 if [ "$1" = '--no-check' ]; then
1124 NOCHECK=true
1125 shift
1126 fi
0d58c26a 1127 local DOWNLOG='rootdir/tmp/download-testfile.log'
b0314abb 1128 local STATUS='downloaded/webserverconfig.status'
0d58c26a 1129 rm -f "$STATUS" "$DOWNLOG"
6c0765c0 1130 # very very basic URI encoding
b0314abb
DK
1131 local URI
1132 if [ -n "$2" ]; then
1133 msgtest "Set webserver config option '${1}' to" "$2"
6c0765c0 1134 URI="${WEBSERVER}/_config/set/$(echo "${1}" | sed -e 's/\//%2f/g')/$(echo "${2}" | sed -e 's/\//%2f/g')"
b0314abb
DK
1135 else
1136 msgtest 'Clear webserver config option' "${1}"
6c0765c0 1137 URI="${WEBSERVER}/_config/clear/$(echo "${1}" | sed -e 's/\//%2f/g')"
b0314abb
DK
1138 fi
1139 if downloadfile "$URI" "$STATUS" > "$DOWNLOG"; then
f2c0ec8b
DK
1140 msgpass
1141 else
68042532
DK
1142 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/webserverconfig.output"
1143 cat "$DOWNLOG" "$STATUS" >"$OUTPUT" 2>&1 || true
1144 msgfailoutput '' "$OUTPUT"
f2c0ec8b 1145 fi
b0314abb 1146 $NOCHECK || testwebserverlaststatuscode '200'
f2c0ec8b
DK
1147}
1148
fd46d305 1149rewritesourceslist() {
63c71412 1150 local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive" | sed 's# #%20#g')"
af9e40c9 1151 local APTARCHIVE2="copy://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive" | sed 's# #%20#g')"
fd46d305 1152 for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
af9e40c9
DK
1153 sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#$APTARCHIVE2#${1}#" \
1154 -e "s#http://localhost:${APTHTTPPORT}/#${1}#" \
1155 -e "s#https://localhost:${APTHTTPSPORT}/#${1}#"
fd46d305
DK
1156 done
1157}
1158
5572f6bd
MV
1159# wait for up to 10s for a pid file to appear to avoid possible race
1160# when a helper is started and dosn't write the PID quick enough
1161waitforpidfile() {
1162 local PIDFILE="$1"
1163 for i in $(seq 10); do
1164 if test -s "$PIDFILE"; then
1165 return 0
1166 fi
1167 sleep 1
1168 done
1169 msgdie "waiting for $PIDFILE failed"
1170 return 1
1171}
1172
f213b6ea 1173changetowebserver() {
6c0765c0 1174 local REWRITE='no'
23af9f40 1175 if [ "$1" != '--no-rewrite' ]; then
6c0765c0 1176 REWRITE='yes'
23af9f40
DK
1177 else
1178 shift
1179 fi
63c71412 1180 if test -x "${APTWEBSERVERBINDIR}/aptwebserver"; then
fbd29dd6 1181 cd aptarchive
a0db467c 1182 local LOG="webserver.log"
6c0765c0 1183 if ! aptwebserver --port 0 -o aptwebserver::fork=1 -o aptwebserver::portfile='aptwebserver.port' "$@" >$LOG 2>&1 ; then
bee0670b
DK
1184 cat $LOG
1185 false
1186 fi
6c0765c0 1187 waitforpidfile aptwebserver.pid
e3c62328
DK
1188 local PID="$(cat aptwebserver.pid)"
1189 if [ -z "$PID" ]; then
1190 msgdie 'Could not fork aptwebserver successfully'
1191 fi
1192 addtrap "kill $PID;"
6c0765c0
DK
1193 waitforpidfile aptwebserver.port
1194 APTHTTPPORT="$(cat aptwebserver.port)"
1195 if [ -z "$APTHTTPPORT" ]; then
1196 msgdie 'Could not get port for aptwebserver successfully'
1197 fi
fbd29dd6 1198 cd - > /dev/null
c5bcc607 1199 else
3d284148 1200 msgdie 'You have to build apt from source to have test/interactive-helper/aptwebserver available for tests requiring a webserver'
f213b6ea 1201 fi
6c0765c0
DK
1202 if [ "$REWRTE" != 'yes' ]; then
1203 rewritesourceslist "http://localhost:${APTHTTPPORT}/"
1204 fi
fd46d305
DK
1205}
1206
1207changetohttpswebserver() {
1208 if ! which stunnel4 >/dev/null; then
1209 msgdie 'You need to install stunnel4 for https testcases'
1210 fi
1211 if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
dc95fee1 1212 changetowebserver --no-rewrite "$@"
fd46d305
DK
1213 fi
1214 echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
68ba0b7f 1215cert = ${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem
23af9f40 1216output = /dev/null
fd46d305
DK
1217
1218[https]
6c0765c0
DK
1219accept = 0
1220connect = $APTHTTPPORT
63c71412 1221" > "${TMPWORKINGDIRECTORY}/stunnel.conf"
fd46d305 1222 stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
5572f6bd 1223 waitforpidfile "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid"
63c71412 1224 local PID="$(cat "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid")"
5572f6bd
MV
1225 if [ -z "$PID" ]; then
1226 msgdie 'Could not fork stunnel4 successfully'
1227 fi
fd46d305 1228 addtrap 'prefix' "kill ${PID};"
6c0765c0
DK
1229 APTHTTPSPORT="$(lsof -i | awk "/^stunnel4 / && \$2 == \"${PID}\" {print \$9; exit; }" | cut -d':' -f 2)"
1230 webserverconfig 'aptwebserver::port::https' "$APTHTTPSPORT" "https://localhost:${APTHTTPSPORT}"
1231 rewritesourceslist "https://localhost:${APTHTTPSPORT}/"
f213b6ea
DK
1232}
1233
c45233ea
DK
1234changetocdrom() {
1235 mkdir -p rootdir/media/cdrom/.disk
1236 local CD="$(readlink -f rootdir/media/cdrom)"
63c71412
DK
1237 cat > rootdir/etc/apt/apt.conf.d/00cdrom <<EOF
1238acquire::cdrom::mount "${CD}";
1239acquire::cdrom::"${CD}/"::mount "mv ${CD}-unmounted ${CD}";
1240acquire::cdrom::"${CD}/"::umount "mv ${CD} ${CD}-unmounted";
1241acquire::cdrom::autodetect 0;
1242EOF
1243 echo -n "$1" > "${CD}/.disk/info"
c45233ea
DK
1244 if [ ! -d aptarchive/dists ]; then
1245 msgdie 'Flat file archive cdroms can not be created currently'
1246 return 1
1247 fi
a0975c8d 1248 mv aptarchive/dists "$CD"
63c71412 1249 ln -s "$(readlink -f ./incoming)" "$CD/pool"
c45233ea 1250 find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
a0975c8d
DK
1251 # start with an unmounted disk
1252 mv "${CD}" "${CD}-unmounted"
1253 # we don't want the disk to be modifiable
63c71412 1254 addtrap 'prefix' "chmod -f -R +w \"$PWD/rootdir/media/cdrom/dists/\" \"$PWD/rootdir/media/cdrom-unmounted/dists/\" || true;"
b0314abb 1255 chmod -R 555 rootdir/media/cdrom-unmounted/dists
c45233ea
DK
1256}
1257
fd46d305 1258downloadfile() {
ed793a19 1259 local PROTO="${1%%:*}"
27925d82 1260 if ! apthelper -o Debug::Acquire::${PROTO}=1 -o Debug::pkgAcquire::Worker=1 \
dcbb364f 1261 download-file "$1" "$2" "$3" 2>&1 ; then
27925d82
DK
1262 return 1
1263 fi
fd46d305 1264 # only if the file exists the download was successful
b0314abb 1265 if [ -r "$2" ]; then
fd46d305
DK
1266 return 0
1267 else
1268 return 1
1269 fi
1270}
1271
f213b6ea 1272checkdiff() {
03aa0847 1273 local DIFFTEXT="$(command diff -u "$@" 2>&1 | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
8d876415 1274 if [ -n "$DIFFTEXT" ]; then
0d58c26a
DK
1275 echo >&2
1276 echo >&2 "$DIFFTEXT"
8d876415
DK
1277 return 1
1278 else
1279 return 0
1280 fi
1281}
1282
22ac12b2
DK
1283testoutputequal() {
1284 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testoutputequal.output"
1285 local COMPAREFILE="$1"
1286 shift
63c71412 1287 if "$@" 2>&1 | checkdiff "$COMPAREFILE" - >"$OUTPUT" 2>&1; then
22ac12b2
DK
1288 msgpass
1289 else
1290 echo "=== content of file we compared with (${COMPAREFILE}) ===" >>"${OUTPUT}"
1291 cat "$COMPAREFILE" >>"${OUTPUT}"
1292 msgfailoutput '' "$OUTPUT" "$@"
1293 fi
1294}
1295
75954ae2 1296testfileequal() {
1501a2c9 1297 msggroup 'testfileequal'
e6a12ff7
DK
1298 local MSG='Test for correctness of file'
1299 if [ "$1" = '--nomsg' ]; then
1300 MSG=''
1301 shift
1302 fi
75954ae2
DK
1303 local FILE="$1"
1304 shift
e6a12ff7
DK
1305 if [ -n "$MSG" ]; then
1306 msgtest "$MSG" "$FILE"
1307 fi
68042532 1308 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfileequal.output"
75954ae2 1309 if [ -z "$*" ]; then
22ac12b2 1310 testoutputequal "$FILE" echo -n ''
75954ae2 1311 else
22ac12b2 1312 testoutputequal "$FILE" echo "$*"
75954ae2 1313 fi
9beb11aa 1314 msggroup
75954ae2
DK
1315}
1316
b855a400 1317testempty() {
1501a2c9 1318 msggroup 'testempty'
b855a400 1319 msgtest "Test for no output of" "$*"
859093da 1320 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testempty.comparefile"
63c71412 1321 if ("$@" >"$COMPAREFILE" 2>&1 || true) && test ! -s "$COMPAREFILE"; then
859093da
DK
1322 msgpass
1323 else
22ac12b2 1324 msgfailoutput '' "$COMPAREFILE" "$@"
859093da 1325 fi
ab25bf1f 1326 aptautotest 'testempty' "$@"
9beb11aa 1327 msggroup
b855a400 1328}
63c71412
DK
1329testnotempty() {
1330 msggroup 'testnotempty'
1331 msgtest "Test for some output of" "$*"
1332 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnotempty.comparefile"
1333 if ("$@" >"$COMPAREFILE" 2>&1 || true) && test -s "$COMPAREFILE"; then
1334 msgpass
1335 else
1336 msgfailoutput '' "$COMPAREFILE" "$@"
1337 fi
1338 aptautotest 'testnotempty' "$@"
1339 msggroup
1340}
b855a400 1341
f74a6fa1 1342testequal() {
1501a2c9 1343 msggroup 'testequal'
f74a6fa1
DK
1344 local MSG='Test of equality of'
1345 if [ "$1" = '--nomsg' ]; then
1346 MSG=''
1347 shift
1348 fi
1349
03938280 1350 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
63c71412 1351 echo "$1" > "$COMPAREFILE"
8d876415 1352 shift
d2d68aaf 1353
f74a6fa1
DK
1354 if [ -n "$MSG" ]; then
1355 msgtest "$MSG" "$*"
1356 fi
22ac12b2 1357 testoutputequal "$COMPAREFILE" "$@"
ab25bf1f 1358 aptautotest 'testequal' "$@"
9beb11aa 1359 msggroup
8d876415
DK
1360}
1361
685625bd 1362testequalor2() {
1501a2c9 1363 msggroup 'testequalor2'
03938280
DK
1364 local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
1365 local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
1366 local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
63c71412
DK
1367 echo "$1" > "$COMPAREFILE1"
1368 echo "$2" > "$COMPAREFILE2"
685625bd
DK
1369 shift 2
1370 msgtest "Test for equality OR of" "$*"
63c71412
DK
1371 "$@" >"$COMPAREAGAINST" 2>&1 || true
1372 if checkdiff "$COMPAREFILE1" "$COMPAREAGAINST" >/dev/null 2>&1 || \
1373 checkdiff "$COMPAREFILE2" "$COMPAREAGAINST" >/dev/null 2>&1
0d58c26a 1374 then
0caa5a4c
DK
1375 msgpass
1376 else
68042532
DK
1377 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.output"
1378 echo -n "\n${CINFO}Diff against OR 1${CNORMAL}" >"$OUTPUT" 2>&1
1379 checkdiff "$COMPAREFILE1" "$COMPAREAGAINST" >"$OUTPUT" 2>&1 || true
1380 echo -n "${CINFO}Diff against OR 2${CNORMAL}" >"$OUTPUT" 2>&1
1381 checkdiff "$COMPAREFILE2" "$COMPAREAGAINST" >"$OUTPUT" 2>&1 || true
1382 msgfailoutput '' "$OUTPUT"
0caa5a4c 1383 fi
ab25bf1f 1384 aptautotest 'testequalor2' "$@"
9beb11aa 1385 msggroup
685625bd
DK
1386}
1387
8d876415 1388testshowvirtual() {
1501a2c9 1389 msggroup 'testshowvirtual'
edc0ef10 1390 local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
8d876415
DK
1391 local PACKAGE="$1"
1392 shift
1393 while [ -n "$1" ]; do
1394 VIRTUAL="${VIRTUAL}
edc0ef10 1395N: Can't select versions from package '$1' as it is purely virtual"
8d876415
DK
1396 PACKAGE="${PACKAGE} $1"
1397 shift
1398 done
1399 msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
1400 VIRTUAL="${VIRTUAL}
4bec02c2 1401N: No packages found"
03938280 1402 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
ea65d079 1403 local ARCH="$(getarchitecture 'native')"
68042532
DK
1404 echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' >"$COMPAREFILE"
1405 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.output"
87d6947d 1406 testoutputequal "$COMPAREFILE" aptcache show "$PACKAGE"
9beb11aa 1407 msggroup
8d876415
DK
1408}
1409
1410testnopackage() {
1501a2c9 1411 msggroup 'testnopackage'
8d876415 1412 msgtest "Test for non-existent packages" "apt-cache show $*"
846856f4 1413 local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
8d876415 1414 if [ -n "$SHOWPKG" ]; then
68042532
DK
1415 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnopackage.output"
1416 echo "$SHOWPKG" >"$OUTPUT"
1417 msgfailoutput '' "$OUTPUT"
0d58c26a
DK
1418 else
1419 msgpass
8d876415 1420 fi
9beb11aa 1421 msggroup
8d876415 1422}
01a6e24c 1423
ecb777dd 1424testdpkgstatus() {
1501a2c9 1425 msggroup 'testdpkgstatus'
ecb777dd
DK
1426 local STATE="$1"
1427 local NR="$2"
1428 shift 2
1429 msgtest "Test that $NR package(s) are in state $STATE with" "dpkg -l $*"
1430 local PKGS="$(dpkg -l "$@" 2>/dev/null | grep "^${STATE}" | wc -l)"
1431 if [ "$PKGS" != $NR ]; then
68042532
DK
1432 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnopackage.output"
1433 echo "$PKGS" >"$OUTPUT"
1434 dpkg -l "$@" | grep '^[a-z]' >"$OUTPUT" >&2 || true
1435 msgfailoutput '' "$OUTPUT"
0d58c26a
DK
1436 else
1437 msgpass
01a6e24c 1438 fi
9beb11aa 1439 msggroup
01a6e24c
DK
1440}
1441
ecb777dd 1442testdpkginstalled() {
1501a2c9 1443 msggroup 'testdpkginstalled'
ecb777dd 1444 testdpkgstatus 'ii' "$#" "$@"
9beb11aa 1445 msggroup
ecb777dd
DK
1446}
1447
5cf733e1 1448testdpkgnotinstalled() {
1501a2c9 1449 msggroup 'testdpkgnotinstalled'
ecb777dd 1450 testdpkgstatus 'ii' '0' "$@"
9beb11aa 1451 msggroup
01a6e24c 1452}
ec7f904e
DK
1453
1454testmarkedauto() {
1501a2c9 1455 msggroup 'testmarkedauto'
03938280 1456 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
ec7f904e
DK
1457 if [ -n "$1" ]; then
1458 msgtest 'Test for correctly marked as auto-installed' "$*"
63c71412 1459 while [ -n "$1" ]; do echo "$1"; shift; done | sort > "$COMPAREFILE"
ec7f904e
DK
1460 else
1461 msgtest 'Test for correctly marked as auto-installed' 'no package'
63c71412 1462 echo -n > "$COMPAREFILE"
ec7f904e 1463 fi
22ac12b2 1464 testoutputequal "$COMPAREFILE" aptmark showauto
9beb11aa 1465 msggroup
ec7f904e 1466}
7c2cc4a7 1467testmarkedmanual() {
1501a2c9 1468 msggroup 'testmarkedmanual'
7c2cc4a7
DK
1469 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedmanual.comparefile"
1470 if [ -n "$1" ]; then
1471 msgtest 'Test for correctly marked as manually installed' "$*"
63c71412 1472 while [ -n "$1" ]; do echo "$1"; shift; done | sort > "$COMPAREFILE"
7c2cc4a7
DK
1473 else
1474 msgtest 'Test for correctly marked as manually installed' 'no package'
63c71412 1475 echo -n > "$COMPAREFILE"
7c2cc4a7 1476 fi
22ac12b2 1477 testoutputequal "$COMPAREFILE" aptmark showmanual
9beb11aa 1478 msggroup
7c2cc4a7 1479}
89a1aa5d 1480
e52aad52 1481msgfailoutput() {
1501a2c9 1482 msgreportheader 'msgfailoutput'
e52aad52
DK
1483 local MSG="$1"
1484 local OUTPUT="$2"
1485 shift 2
e52aad52 1486 if [ "$1" = 'grep' ]; then
68042532 1487 echo >&2
e52aad52
DK
1488 while [ -n "$2" ]; do shift; done
1489 echo "#### Complete file: $1 ####"
1490 cat >&2 "$1" || true
1491 echo '#### grep output ####'
1492 elif [ "$1" = 'test' ]; then
68042532 1493 echo >&2
e52aad52
DK
1494 # doesn't support ! or non-file flags
1495 msgfailoutputstatfile() {
1496 local FILEFLAGS='^-[bcdefgGhkLOprsStuwx]$'
1497 if expr match "$1" "$FILEFLAGS" >/dev/null; then
1498 echo "#### stat(2) of file: $2 ####"
1499 stat "$2" || true
3196dae8
DK
1500 if test -e "$2"; then
1501 echo "#### Complete file: $2 ####"
1502 cat >&2 "$2" || true
1503 fi
e52aad52
DK
1504 fi
1505 }
1506 msgfailoutputstatfile "$2" "$3"
1507 while [ -n "$5" ] && [ "$4" = '-o' -o "$4" = '-a' ]; do
1508 shift 3
1509 msgfailoutputstatfile "$2" "$3"
1510 done
1511 echo '#### test output ####'
1512 fi
63c71412 1513 cat >&2 "$OUTPUT"
e52aad52
DK
1514 msgfail "$MSG"
1515}
1516
671a55ba
DK
1517testsuccesswithglobalerror() {
1518 local TYPE="$1"
1519 local ERRORS="$2"
1520 shift 2
1521 msggroup "$TYPE"
0440d936
DK
1522 if [ "$1" = '--nomsg' ]; then
1523 shift
1524 else
1525 msgtest 'Test for successful execution of' "$*"
1526 fi
671a55ba 1527 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/${TYPE}.output"
63c71412 1528 if "$@" >"${OUTPUT}" 2>&1; then
4fa34122 1529 if expr match "$1" '^apt.*' >/dev/null; then
e52aad52
DK
1530 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1531 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
671a55ba 1532 elif grep -E "^[${ERRORS}]: " "$OUTPUT" > "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" 2>&1; then
3261271e
DK
1533 if [ "$IGNORE_PTY_NOT_MOUNTED" = '1' ]; then
1534 if echo 'E: Can not write log (Is /dev/pts mounted?) - posix_openpt (2: No such file or directory)' \
1535 | cmp - "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" >/dev/null 2>&1; then
1536 msgpass
1537 else
1538 msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
1539 fi
1540 else
1541 msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
1542 fi
671a55ba
DK
1543 elif [ "$TYPE" = 'testsuccesswithnotice' ]; then
1544 if grep -q -E "^N: " "$OUTPUT"; then
1545 msgpass
1546 else
1547 msgfailoutput 'successful run, but output had no notices' "$OUTPUT" "$@"
1548 fi
4fa34122
DK
1549 else
1550 msgpass
1551 fi
1552 else
1553 msgpass
1554 fi
1555 else
1556 local EXITCODE=$?
e52aad52 1557 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
4fa34122 1558 fi
671a55ba 1559 aptautotest "$TYPE" "$@"
9beb11aa 1560 msggroup
4fa34122 1561}
671a55ba
DK
1562testsuccesswithnotice() {
1563 testsuccesswithglobalerror 'testsuccesswithnotice' 'WE' "$@"
1564}
1565testsuccess() {
1566 testsuccesswithglobalerror 'testsuccess' 'NWE' "$@"
1567}
4fa34122 1568testwarning() {
1501a2c9 1569 msggroup 'testwarning'
4fa34122
DK
1570 if [ "$1" = '--nomsg' ]; then
1571 shift
1572 else
1573 msgtest 'Test for successful execution with warnings of' "$*"
1574 fi
25b86db1 1575 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output"
63c71412 1576 if "$@" >"${OUTPUT}" 2>&1; then
1df24acf 1577 if expr match "$1" '^apt.*' >/dev/null; then
e52aad52
DK
1578 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1579 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1580 elif grep -q -E '^E: ' "$OUTPUT"; then
1581 msgfailoutput 'successful run, but output contains errors' "$OUTPUT" "$@"
4fa34122 1582 elif ! grep -q -E '^W: ' "$OUTPUT"; then
e52aad52 1583 msgfailoutput 'successful run, but output contains no warnings' "$OUTPUT" "$@"
1df24acf
DK
1584 else
1585 msgpass
1586 fi
1587 else
1588 msgpass
1589 fi
0440d936 1590 else
07cb47e7 1591 local EXITCODE=$?
68042532 1592 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
0440d936 1593 fi
4fa34122 1594 aptautotest 'testwarning' "$@"
9beb11aa 1595 msggroup
0440d936 1596}
0440d936 1597testfailure() {
1501a2c9 1598 msggroup 'testfailure'
0440d936
DK
1599 if [ "$1" = '--nomsg' ]; then
1600 shift
1601 else
889b0072 1602 msgtest 'Test for failure in execution of' "$*"
0440d936 1603 fi
03938280 1604 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
63c71412 1605 if "$@" >"${OUTPUT}" 2>&1; then
07cb47e7 1606 local EXITCODE=$?
e52aad52 1607 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
0440d936 1608 else
1df24acf
DK
1609 local EXITCODE=$?
1610 if expr match "$1" '^apt.*' >/dev/null; then
b0d40854
DK
1611 if [ "$1" = 'aptkey' ]; then
1612 if grep -q -E " Can't check signature: " "$OUTPUT" || \
1613 grep -q -E " BAD signature from " "$OUTPUT"; then
1614 msgpass
1615 else
1616 msgfailoutput "run failed with exitcode ${EXITCODE}, but no signature error" "$OUTPUT" "$@"
1617 fi
1df24acf 1618 else
b0d40854
DK
1619 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1620 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1621 elif grep -q -E '==ERROR' "$OUTPUT"; then
1622 msgfailoutput 'compiler sanitizers reported errors' "$OUTPUT" "$@"
1623 elif ! grep -q -E '^E: ' "$OUTPUT"; then
1624 msgfailoutput "run failed with exitcode ${EXITCODE}, but with no errors" "$OUTPUT" "$@"
1625 else
1626 msgpass
1627 fi
1df24acf
DK
1628 fi
1629 else
1630 msgpass
1631 fi
0440d936 1632 fi
ab25bf1f 1633 aptautotest 'testfailure' "$@"
9beb11aa 1634 msggroup
0440d936
DK
1635}
1636
7414af7f
DK
1637testreturnstateequal() {
1638 local STATE="$1"
671a55ba
DK
1639 if [ "$STATE" = 'testsuccesswithglobalerror' ]; then
1640 local STATE="$2"
1641 local TYPE="$3"
7414af7f 1642 shift 3
671a55ba
DK
1643 msggroup "${STATE}equal"
1644 if [ "$1" != '--nomsg' ]; then
1645 local CMP="$1"
1646 shift
1647 testsuccesswithglobalerror "$STATE" "$TYPE" "$@"
1648 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1649 else
1650 local CMP="$2"
1651 shift 2
1652 testsuccesswithglobalerror "$STATE" "$TYPE" "$@"
1653 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1654 fi
1655 else
1656 msggroup "${STATE}equal"
1657 if [ "$2" != '--nomsg' ]; then
1658 local CMP="$2"
1659 shift 2
1660 "$STATE" "$@"
1661 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1662 else
1663 local CMP="$3"
1664 shift 3
1665 "$STATE" --nomsg "$@"
1666 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1667 fi
7414af7f 1668 fi
9beb11aa 1669 msggroup
25b86db1 1670}
7414af7f 1671testsuccessequal() {
671a55ba
DK
1672 # we compare output, so we know perfectly well about N:
1673 testreturnstateequal 'testsuccesswithglobalerror' 'testsuccess' 'WE' "$@"
7414af7f 1674}
25b86db1 1675testwarningequal() {
7414af7f 1676 testreturnstateequal 'testwarning' "$@"
25b86db1
DK
1677}
1678testfailureequal() {
7414af7f 1679 testreturnstateequal 'testfailure' "$@"
25b86db1
DK
1680}
1681
27925d82 1682testfailuremsg() {
1501a2c9 1683 msggroup 'testfailuremsg'
27925d82
DK
1684 local CMP="$1"
1685 shift
1686 testfailure "$@"
1687 msgtest 'Check that the output of the previous failed command has expected' 'failures and warnings'
68042532 1688 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailuremsg.comparefile"
002b1bc4 1689 grep '^\(W\|E\|N\):' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output" > "$COMPAREFILE" 2>&1 || true
22ac12b2 1690 testoutputequal "$COMPAREFILE" echo "$CMP"
9beb11aa 1691 msggroup
27925d82 1692}
f18f2338
DK
1693testwarningmsg() {
1694 msggroup 'testwarningmsg'
1695 local CMP="$1"
1696 shift
1697 testwarning "$@"
1698 msgtest 'Check that the output of the previous warned command has expected' 'warnings'
1699 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarningmsg.comparefile"
002b1bc4 1700 grep '^\(W\|E\|N\):' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output" > "$COMPAREFILE" 2>&1 || true
f18f2338
DK
1701 testoutputequal "$COMPAREFILE" echo "$CMP"
1702 msggroup
1703}
25b86db1 1704
de81b2e2 1705testfilestats() {
1501a2c9 1706 msggroup 'testfilestats'
de81b2e2
DK
1707 msgtest "Test that file $1 has $2 $3" "$4"
1708 if [ "$4" "$3" "$(stat --format "$2" "$1")" ]; then
5684f71f
DK
1709 msgpass
1710 else
68042532
DK
1711 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfilestats.output"
1712 {
1713 ls -ld "$1" || true
1714 echo -n "stat(1) reports for $2: "
1715 stat --format "$2" "$1" || true
1716 } >"$OUTPUT" 2>&1
1717 msgfailoutput '' "$OUTPUT"
5684f71f 1718 fi
9beb11aa 1719 msggroup
5684f71f 1720}
de81b2e2 1721testaccessrights() {
1501a2c9 1722 msggroup 'testaccessrights'
de81b2e2 1723 testfilestats "$1" '%a' '=' "$2"
9beb11aa 1724 msggroup
de81b2e2 1725}
5684f71f 1726
0d58c26a 1727testwebserverlaststatuscode() {
1501a2c9 1728 msggroup 'testwebserverlaststatuscode'
0d58c26a 1729 local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
03aa0847 1730 local STATUS='downloaded/webserverstatus-statusfile.log'
0d58c26a
DK
1731 rm -f "$DOWNLOG" "$STATUS"
1732 msgtest 'Test last status code from the webserver was' "$1"
6c0765c0 1733 if downloadfile "http://localhost:${APTHTTPPORT}/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG" && [ "$(cat "$STATUS")" = "$1" ]; then
0d58c26a
DK
1734 msgpass
1735 else
68042532
DK
1736 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwebserverlaststatuscode.output"
1737 {
1738 if [ -n "$2" ]; then
1739 shift
1740 echo >&2 '#### Additionally provided output files contain:'
1741 cat >&2 "$@"
1742 fi
1743 echo >&2 '#### Download log of the status code:'
1744 cat >&2 "$DOWNLOG"
1745 } >"$OUTPUT" 2>&1
1746 msgfailoutput "Status was $(cat "$STATUS")" "$OUTPUT"
0d58c26a 1747 fi
9beb11aa 1748 msggroup
0d58c26a
DK
1749}
1750
89a1aa5d
DK
1751pause() {
1752 echo "STOPPED execution. Press enter to continue"
1753 local IGNORE
1754 read IGNORE
1755}
ab25bf1f 1756
846bc058 1757listcurrentlistsdirectory() {
ba6b79bd
DK
1758 {
1759 find rootdir/var/lib/apt/lists -maxdepth 1 -type d | while read line; do
1760 stat --format '%U:%G:%a:%n' "$line"
1761 done
1762 find rootdir/var/lib/apt/lists -maxdepth 1 \! -type d | while read line; do
1763 stat --format '%U:%G:%a:%s:%y:%n' "$line"
1764 done
1765 } | sort
846bc058
DK
1766}
1767
3a8776a3 1768### convenience hacks ###
8fe964f1
DK
1769mkdir() {
1770 # creating some directories by hand is a tedious task, so make it look simple
6aef1942
DK
1771 local PARAMS="$*"
1772 if [ "$PARAMS" != "${PARAMS#*rootdir/var/lib/apt/lists}" ]; then
8fe964f1
DK
1773 # only the last directory created by mkdir is effected by the -m !
1774 command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt"
1775 command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"
1776 command mkdir -m 700 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
1777 touch "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/lock"
1778 if [ "$(id -u)" = '0' ]; then
1779 chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
1780 fi
1781 else
1782 command mkdir "$@"
1783 fi
1784}
1785
ab25bf1f
DK
1786### The following tests are run by most test methods automatically to check
1787### general things about commands executed without writing the test every time.
1788
1789aptautotest() {
1790 local TESTCALL="$1"
1791 local CMD="$2"
1792 local FIRSTOPT="$3"
63c71412 1793 local AUTOTEST="aptautotest_$(echo "${CMD##*/}_${FIRSTOPT}" | tr -d '-')"
ab25bf1f
DK
1794 if command -v $AUTOTEST >/dev/null; then
1795 shift 3
1796 # save and restore the *.output files from other tests
1797 # as we might otherwise override them in these automatic tests
63c71412
DK
1798 rm -rf "${TMPWORKINGDIRECTORY}/rootdir/tmp-before"
1799 mv "${TMPWORKINGDIRECTORY}/rootdir/tmp" "${TMPWORKINGDIRECTORY}/rootdir/tmp-before"
1800 mkdir "${TMPWORKINGDIRECTORY}/rootdir/tmp"
ab25bf1f 1801 $AUTOTEST "$TESTCALL" "$@"
63c71412
DK
1802 rm -rf "${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest"
1803 mv "${TMPWORKINGDIRECTORY}/rootdir/tmp" "${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest"
1804 mv "${TMPWORKINGDIRECTORY}/rootdir/tmp-before" "${TMPWORKINGDIRECTORY}/rootdir/tmp"
ab25bf1f
DK
1805 fi
1806}
1807
1808aptautotest_aptget_update() {
59148d96
DK
1809 local TESTCALL="$1"
1810 while [ -n "$2" ]; do
1811 if [ "$2" = '--print-uris' ]; then return; fi # simulation mode
1812 shift
1813 done
ab25bf1f 1814 if ! test -d "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"; then return; fi
4bb006d1
DK
1815 testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
1816 testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
ab25bf1f 1817 # all copied files are properly chmodded
63c71412
DK
1818 local backupIFS="$IFS"
1819 IFS="$(printf "\n\b")"
146f7715 1820 for file in $(find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" -type f ! -name 'lock'); do
4bb006d1 1821 testfilestats "$file" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:644"
ab25bf1f 1822 done
63c71412 1823 IFS="$backupIFS"
59148d96 1824 if [ "$TESTCALL" = 'testsuccess' ]; then
146f7715
DK
1825 # failure cases can retain partial files and such
1826 testempty find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" -mindepth 1 ! \( -name 'lock' -o -name '*.FAILED' \)
1827 fi
ab25bf1f
DK
1828}
1829aptautotest_apt_update() { aptautotest_aptget_update "$@"; }
d84da499 1830aptautotest_aptcdrom_add() { aptautotest_aptget_update "$@"; }
016bea82
DK
1831
1832testaptautotestnodpkgwarning() {
1833 local TESTCALL="$1"
1834 while [ -n "$2" ]; do
6bf93605
DK
1835 if expr match "$2" '^-[a-z]*s' >/dev/null 2>&1; then return; fi # simulation mode
1836 if expr match "$2" '^-dy\?' >/dev/null 2>&1; then return; fi # download-only mode
016bea82
DK
1837 shift
1838 done
1839 testfailure grep '^dpkg: warning:.*ignor.*' "${TMPWORKINGDIRECTORY}/rootdir/tmp-before/${TESTCALL}.output"
1840}
1841
1842aptautotest_aptget_install() { testaptautotestnodpkgwarning "$@"; }
1843aptautotest_aptget_remove() { testaptautotestnodpkgwarning "$@"; }
1844aptautotest_aptget_purge() { testaptautotestnodpkgwarning "$@"; }
1845aptautotest_apt_install() { testaptautotestnodpkgwarning "$@"; }
1846aptautotest_apt_remove() { testaptautotestnodpkgwarning "$@"; }
1847aptautotest_apt_purge() { testaptautotestnodpkgwarning "$@"; }