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