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