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