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