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