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