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