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