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