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