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