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