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