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