]> git.saurik.com Git - apt.git/blob - test/integration/framework
Add support for "apt-cache showsrc --only-source srcpkgname"
[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 Priority: $PRIORITY
574 Maintainer: Joe Sixpack <joe@example.org>
575 Standards-Version: 3.9.3" > ${BUILDDIR}/debian/control
576 if [ "$SECTION" != '<none>' ]; then
577 echo "Section: $SECTION" >> ${BUILDDIR}/debian/control
578 fi
579 local BUILDDEPS="$(echo "$DEPENDENCIES" | grep '^Build-')"
580 test -z "$BUILDDEPS" || echo "$BUILDDEPS" >> ${BUILDDIR}/debian/control
581 echo "
582 Package: $NAME" >> ${BUILDDIR}/debian/control
583
584 if [ "$ARCH" = 'all' ]; then
585 echo "Architecture: all" >> ${BUILDDIR}/debian/control
586 else
587 echo "Architecture: any" >> ${BUILDDIR}/debian/control
588 fi
589 local DEPS="$(echo "$DEPENDENCIES" | grep -v '^Build-')"
590 test -z "$DEPS" || echo "$DEPS" >> ${BUILDDIR}/debian/control
591 echo "Description: $DESCRIPTION" >> ${BUILDDIR}/debian/control
592
593 echo '3.0 (native)' > ${BUILDDIR}/debian/source/format
594 cd ${BUILDDIR}/..
595 testsuccess --nomsg dpkg-source -b ${NAME}-${VERSION}
596 cd - >/dev/null
597 sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' ${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output \
598 | while read SRC; do
599 echo "pool/${SRC}" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist
600 # if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
601 # aptkey --keyring ./keys/joesixpack.pub --secret-keyring ./keys/joesixpack.sec --quiet --readonly \
602 # adv --yes --default-key 'Joe Sixpack' \
603 # --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
604 # mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
605 # fi
606 done
607
608 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
609 msgtest "Build binary package for ${RELEASE} in ${SECTION}" "$NAME"
610 rm -rf ${BUILDDIR}/debian/tmp
611 mkdir -p ${BUILDDIR}/debian/tmp/DEBIAN ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME} ${BUILDDIR}/debian/tmp/usr/bin
612 cp ${BUILDDIR}/debian/copyright ${BUILDDIR}/debian/changelog ${BUILDDIR}/FEATURES ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}
613 cp ${BUILDDIR}/${NAME} ${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}
614 if [ -n "$FILE_TREE" ]; then
615 cp -ar "$FILE_TREE" ${BUILDDIR}/debian/tmp
616 fi
617
618 (cd ${BUILDDIR}; dpkg-gencontrol -DArchitecture=$arch)
619 (cd ${BUILDDIR}/debian/tmp; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
620 local LOG="${BUILDDIR}/../${NAME}_${VERSION}_${arch}.dpkg-deb.log"
621 # ensure the right permissions as dpkg-deb insists
622 chmod 755 ${BUILDDIR}/debian/tmp/DEBIAN
623 testsuccess --nomsg dpkg-deb -Z${COMPRESS_TYPE} --build ${BUILDDIR}/debian/tmp ${BUILDDIR}/..
624 echo "pool/${NAME}_${VERSION}_${arch}.deb" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist
625 done
626
627 local CHANGEPATH="${BUILDDIR}/../${DISTSECTION}/${NM}/${NAME}/${NAME}_${VERSION}"
628 mkdir -p $CHANGEPATH
629 cp ${BUILDDIR}/debian/changelog $CHANGEPATH
630 rm -rf "${BUILDDIR}"
631 msgdone "info"
632 }
633
634 buildpackage() {
635 local BUILDDIR=$1
636 local RELEASE=$2
637 local SECTION=$3
638 local ARCH=$(getarchitecture $4)
639 local PKGNAME="$(echo "$BUILDDIR" | grep -o '[^/]*$')"
640 local BUILDLOG="$(readlink -f "${BUILDDIR}/../${PKGNAME}_${RELEASE}_${SECTION}.dpkg-bp.log")"
641 msgtest "Build package for ${RELEASE} in ${SECTION}" "$PKGNAME"
642 cd $BUILDDIR
643 if [ "$ARCH" = "all" ]; then
644 ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
645 fi
646 testsuccess --nomsg dpkg-buildpackage -uc -us -a$ARCH
647 cp ${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output $BUILDLOG
648 local PKGS="$(grep '^dpkg-deb: building package' $BUILDLOG | cut -d'/' -f 2 | sed -e "s#'\.##")"
649 local SRCS="$(grep '^dpkg-source: info: building' $BUILDLOG | grep -o '[a-z0-9._+~-]*$')"
650 cd - > /dev/null
651 for PKG in $PKGS; do
652 echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
653 done
654 for SRC in $SRCS; do
655 echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
656 done
657 }
658
659 buildaptarchive() {
660 if [ -d incoming ]; then
661 buildaptarchivefromincoming "$@"
662 else
663 buildaptarchivefromfiles "$@"
664 fi
665 }
666
667 createaptftparchiveconfig() {
668 local COMPRESSORS="$(cut -d' ' -f 1 ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | tr '\n' ' ')"
669 local COMPRESSORS="${COMPRESSORS%* }"
670 local ARCHS="$(getarchitectures)"
671 echo -n 'Dir {
672 ArchiveDir "' >> ftparchive.conf
673 echo -n $(readlink -f .) >> ftparchive.conf
674 echo -n '";
675 CacheDir "' >> ftparchive.conf
676 echo -n $(readlink -f ..) >> ftparchive.conf
677 echo -n '";
678 FileListDir "' >> ftparchive.conf
679 echo -n $(readlink -f pool/) >> ftparchive.conf
680 echo -n '";
681 };
682 Default {
683 Packages::Compress "'"$COMPRESSORS"'";
684 Sources::Compress "'"$COMPRESSORS"'";
685 Contents::Compress "'"$COMPRESSORS"'";
686 Translation::Compress "'"$COMPRESSORS"'";
687 LongDescription "false";
688 };
689 TreeDefault {
690 Directory "pool/";
691 SrcDirectory "pool/";
692 };
693 APT {
694 FTPArchive {
695 Release {
696 Origin "joesixpack";
697 Label "apttestcases";
698 Suite "unstable";
699 Description "repository with dummy packages";
700 Architectures "' >> ftparchive.conf
701 echo -n "$ARCHS" >> ftparchive.conf
702 echo 'source";
703 };
704 };
705 };' >> ftparchive.conf
706 for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
707 echo -n 'tree "dists/' >> ftparchive.conf
708 echo -n "$DIST" >> ftparchive.conf
709 echo -n '" {
710 Architectures "' >> ftparchive.conf
711 echo -n "$ARCHS" >> ftparchive.conf
712 echo -n 'source";
713 FileList "' >> ftparchive.conf
714 echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
715 echo -n '";
716 SourceFileList "' >> ftparchive.conf
717 echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
718 echo -n '";
719 Sections "' >> ftparchive.conf
720 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
721 echo '";
722 };' >> ftparchive.conf
723 done
724 }
725
726 buildaptftparchivedirectorystructure() {
727 local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
728 for DIST in $DISTS; do
729 local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
730 for SECTION in $SECTIONS; do
731 local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
732 for ARCH in $ARCHS; do
733 mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
734 done
735 mkdir -p dists/${DIST}/${SECTION}/source
736 mkdir -p dists/${DIST}/${SECTION}/i18n
737 done
738 done
739 }
740
741 insertpackage() {
742 local RELEASES="$1"
743 local NAME="$2"
744 local ARCH="$3"
745 local VERSION="$4"
746 local DEPENDENCIES="$5"
747 local PRIORITY="${6:-optional}"
748 local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASES}
749 If you find such a package installed on your system,
750 something went horribly wrong! They are autogenerated
751 und used only by testcases and surf no other propose…"}"
752 local ARCHS=""
753 for RELEASE in $(printf '%s' "$RELEASES" | tr ',' '\n'); do
754 if [ "$RELEASE" = 'installed' ]; then
755 insertinstalledpackage "$2" "$3" "$4" "$5" "$6" "$7"
756 continue
757 fi
758 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
759 if [ "$arch" = 'all' -o "$arch" = 'none' ]; then
760 ARCHS="$(getarchitectures)"
761 else
762 ARCHS="$arch"
763 fi
764 for BUILDARCH in $ARCHS; do
765 local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
766 mkdir -p $PPATH
767 local FILE="${PPATH}/Packages"
768 echo "Package: $NAME
769 Priority: $PRIORITY
770 Section: other
771 Installed-Size: 42
772 Maintainer: Joe Sixpack <joe@example.org>" >> $FILE
773 test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
774 echo "Version: $VERSION
775 Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
776 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
777 echo "Description: $(printf '%s' "$DESCRIPTION" | head -n 1)" >> $FILE
778 echo "Description-md5: $(printf '%s' "$DESCRIPTION" | md5sum | cut -d' ' -f 1)" >> $FILE
779 echo >> $FILE
780 done
781 done
782 mkdir -p aptarchive/dists/${RELEASE}/main/source aptarchive/dists/${RELEASE}/main/i18n
783 touch aptarchive/dists/${RELEASE}/main/source/Sources
784 echo "Package: $NAME
785 Description-md5: $(printf '%s' "$DESCRIPTION" | md5sum | cut -d' ' -f 1)
786 Description-en: $DESCRIPTION
787 " >> aptarchive/dists/${RELEASE}/main/i18n/Translation-en
788 done
789 }
790
791 insertsource() {
792 local RELEASE="$1"
793 local NAME="$2"
794 local ARCH="$3"
795 local VERSION="$4"
796 local DEPENDENCIES="$5"
797 local BINARY="${6:-$NAME}"
798 local ARCHS=""
799 local SPATH="aptarchive/dists/${RELEASE}/main/source"
800 mkdir -p $SPATH
801 local FILE="${SPATH}/Sources"
802 local DSCFILE="${NAME}_${VERSION}.dsc"
803 local TARFILE="${NAME}_${VERSION}.tar.gz"
804 echo "Package: $NAME
805 Binary: $BINARY
806 Version: $VERSION
807 Maintainer: Joe Sixpack <joe@example.org>
808 Architecture: $ARCH" >> $FILE
809 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
810 echo "Files:
811 $(echo -n "$DSCFILE" | md5sum | cut -d' ' -f 1) $(echo -n "$DSCFILE" | wc -c) $DSCFILE
812 $(echo -n "$TARFILE" | md5sum | cut -d' ' -f 1) $(echo -n "$TARFILE" | wc -c) $TARFILE
813 " >> $FILE
814 }
815
816 insertinstalledpackage() {
817 local NAME="$1"
818 local ARCH="$2"
819 local VERSION="$3"
820 local DEPENDENCIES="$4"
821 local PRIORITY="${5:-optional}"
822 local STATUS="${6:-install ok installed}"
823 local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/installed
824 If you find such a package installed on your system,
825 something went horribly wrong! They are autogenerated
826 und used only by testcases and surf no other propose…"}"
827
828 local FILE='rootdir/var/lib/dpkg/status'
829 local INFO='rootdir/var/lib/dpkg/info'
830 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
831 echo "Package: $NAME
832 Status: $STATUS
833 Priority: $PRIORITY
834 Section: other
835 Installed-Size: 42
836 Maintainer: Joe Sixpack <joe@example.org>
837 Version: $VERSION" >> $FILE
838 test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
839 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
840 echo "Description: $DESCRIPTION" >> $FILE
841 echo >> $FILE
842 if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
843 echo -n > ${INFO}/${NAME}:${arch}.list
844 else
845 echo -n > ${INFO}/${NAME}.list
846 fi
847 done
848 }
849
850
851 buildaptarchivefromincoming() {
852 msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
853 cd aptarchive
854 [ -e pool ] || ln -s ../incoming pool
855 [ -e ftparchive.conf ] || createaptftparchiveconfig
856 [ -e dists ] || buildaptftparchivedirectorystructure
857 msgninfo "\tGenerate Packages, Sources and Contents files… "
858 testsuccess aptftparchive generate ftparchive.conf
859 cd - > /dev/null
860 msgdone "info"
861 generatereleasefiles "$@"
862 }
863
864 buildaptarchivefromfiles() {
865 msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
866 local DIR='aptarchive'
867 if [ -d "${DIR}/dists" ]; then DIR="${DIR}/dists"; fi
868 find "$DIR" -name 'Packages' -o -name 'Sources' -o -name 'Translation-*' | while read line; do
869 msgninfo "\t${line} file… "
870 compressfile "$line" "$1"
871 msgdone "info"
872 done
873 generatereleasefiles "$@"
874 }
875
876 compressfile() {
877 cat ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | while read compressor extension command; do
878 if [ "$compressor" = '.' ]; then
879 if [ -n "$2" ]; then
880 touch -d "$2" "$1"
881 fi
882 continue
883 fi
884 cat "$1" | $command > "${1}.${extension}"
885 if [ -n "$2" ]; then
886 touch -d "$2" "${1}.${extension}"
887 fi
888 done
889 }
890
891 # can be overridden by testcases for their pleasure
892 getcodenamefromsuite() {
893 case "$1" in
894 unstable) echo 'sid';;
895 *) echo -n "$1";;
896 esac
897 }
898 getreleaseversionfromsuite() { true; }
899 getlabelfromsuite() { true; }
900 getoriginfromsuite() { true; }
901
902 generatereleasefiles() {
903 # $1 is the Date header and $2 is the ValidUntil header to be set
904 # both should be given in notation date/touch can understand
905 msgninfo "\tGenerate Release files… "
906 if [ -e aptarchive/dists ]; then
907 for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
908 local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
909 local CODENAME="$(getcodenamefromsuite $SUITE)"
910 local VERSION="$(getreleaseversionfromsuite $SUITE)"
911 local LABEL="$(getlabelfromsuite $SUITE)"
912 local ORIGIN="$(getoriginfromsuite $SUITE)"
913 if [ -n "$VERSION" ]; then
914 VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
915 fi
916 if [ -n "$LABEL" ]; then
917 LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
918 fi
919 if [ -n "$ORIGIN" ]; then
920 ORIGIN="-o APT::FTPArchive::Release::Origin=${ORIGIN}"
921 fi
922 aptftparchive -qq release $dir \
923 -o APT::FTPArchive::Release::Suite="${SUITE}" \
924 -o APT::FTPArchive::Release::Codename="${CODENAME}" \
925 ${LABEL} \
926 ${ORIGIN} \
927 ${VERSION} \
928 | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
929 if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
930 sed -i '/^Date: / a\
931 NotAutomatic: yes' $dir/Release
932 fi
933 if [ -n "$1" -a "$1" != "now" ]; then
934 sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
935 fi
936 if [ -n "$2" ]; then
937 sed -i "/^Date: / a\
938 Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
939 fi
940 done
941 else
942 aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
943 fi
944 if [ -n "$1" -a "$1" != "now" ]; then
945 for release in $(find ./aptarchive -name 'Release'); do
946 touch -d "$1" $release
947 done
948 fi
949 msgdone "info"
950 }
951
952 setupdistsaptarchive() {
953 local APTARCHIVE=$(readlink -f ./aptarchive)
954 rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
955 rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
956 for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
957 SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
958 msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
959 echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
960 echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
961 msgdone "info"
962 done
963 }
964
965 setupflataptarchive() {
966 local APTARCHIVE=$(readlink -f ./aptarchive)
967 if [ -f ${APTARCHIVE}/Packages ]; then
968 msgninfo "\tadd deb sources.list line… "
969 echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
970 msgdone "info"
971 else
972 rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
973 fi
974 if [ -f ${APTARCHIVE}/Sources ]; then
975 msgninfo "\tadd deb-src sources.list line… "
976 echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
977 msgdone "info"
978 else
979 rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
980 fi
981 }
982
983 setupaptarchive() {
984 local NOUPDATE=0
985 if [ "$1" = '--no-update' ]; then
986 NOUPDATE=1
987 shift
988 fi
989 buildaptarchive "$@"
990 if [ -e aptarchive/dists ]; then
991 setupdistsaptarchive
992 else
993 setupflataptarchive
994 fi
995 signreleasefiles 'Joe Sixpack'
996 if [ "1" != "$NOUPDATE" ]; then
997 testsuccess aptget update -o Debug::pkgAcquire::Worker=true -o Debug::Acquire::gpgv=true
998 fi
999 }
1000
1001 signreleasefiles() {
1002 local SIGNER="${1:-Joe Sixpack}"
1003 local REPODIR="${2:-aptarchive}"
1004 local KEY="keys/$(echo "$SIGNER" | tr 'A-Z' 'a-z' | sed 's# ##g')"
1005 local GPG="aptkey --quiet --keyring ${KEY}.pub --secret-keyring ${KEY}.sec --readonly adv --batch --yes"
1006 msgninfo "\tSign archive with $SIGNER key $KEY… "
1007 local REXKEY='keys/rexexpired'
1008 local SECEXPIREBAK="${REXKEY}.sec.bak"
1009 local PUBEXPIREBAK="${REXKEY}.pub.bak"
1010 if [ "${SIGNER}" = 'Rex Expired' ]; then
1011 # the key is expired, so gpg doesn't allow to sign with and the --faked-system-time
1012 # option doesn't exist anymore (and using faketime would add a new obscure dependency)
1013 # therefore we 'temporary' make the key not expired and restore a backup after signing
1014 cp ${REXKEY}.sec $SECEXPIREBAK
1015 cp ${REXKEY}.pub $PUBEXPIREBAK
1016 local SECUNEXPIRED="${REXKEY}.sec.unexpired"
1017 local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
1018 if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
1019 cp $SECUNEXPIRED ${REXKEY}.sec
1020 cp $PUBUNEXPIRED ${REXKEY}.pub
1021 else
1022 if ! printf "expire\n1w\nsave\n" | $GPG --default-key "$SIGNER" --command-fd 0 --edit-key "${SIGNER}" >setexpire.gpg 2>&1; then
1023 cat setexpire.gpg
1024 exit 1
1025 fi
1026 cp ${REXKEY}.sec $SECUNEXPIRED
1027 cp ${REXKEY}.pub $PUBUNEXPIRED
1028 fi
1029 fi
1030 for RELEASE in $(find ${REPODIR}/ -name Release); do
1031 $GPG --default-key "$SIGNER" --armor --detach-sign --sign --output ${RELEASE}.gpg ${RELEASE}
1032 local INRELEASE="$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')"
1033 $GPG --default-key "$SIGNER" --clearsign --output $INRELEASE $RELEASE
1034 # we might have set a specific date for the Release file, so copy it
1035 touch -d "$(stat --format "%y" ${RELEASE})" ${RELEASE}.gpg ${INRELEASE}
1036 done
1037 if [ -f "$SECEXPIREBAK" ] && [ -f "$PUBEXPIREBAK" ]; then
1038 mv -f $SECEXPIREBAK ${REXKEY}.sec
1039 mv -f $PUBEXPIREBAK ${REXKEY}.pub
1040 fi
1041 msgdone "info"
1042 }
1043
1044 redatereleasefiles() {
1045 local DATE="$(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')"
1046 for release in $(find aptarchive/ -name 'Release'); do
1047 sed -i "s/^Date: .*$/Date: ${DATE}/" $release
1048 touch -d "$DATE" $release
1049 done
1050 signreleasefiles "${2:-Joe Sixpack}"
1051 }
1052
1053 webserverconfig() {
1054 local WEBSERVER="${3:-http://localhost:8080}"
1055 local NOCHECK=false
1056 if [ "$1" = '--no-check' ]; then
1057 NOCHECK=true
1058 shift
1059 fi
1060 local DOWNLOG='rootdir/tmp/download-testfile.log'
1061 local STATUS='downloaded/webserverconfig.status'
1062 rm -f "$STATUS" "$DOWNLOG"
1063 local URI
1064 if [ -n "$2" ]; then
1065 msgtest "Set webserver config option '${1}' to" "$2"
1066 URI="${WEBSERVER}/_config/set/${1}/${2}"
1067 else
1068 msgtest 'Clear webserver config option' "${1}"
1069 URI="${WEBSERVER}/_config/clear/${1}"
1070 fi
1071 if downloadfile "$URI" "$STATUS" > "$DOWNLOG"; then
1072 msgpass
1073 else
1074 cat "$DOWNLOG" "$STATUS" || true
1075 msgfail
1076 fi
1077 $NOCHECK || testwebserverlaststatuscode '200'
1078 }
1079
1080 rewritesourceslist() {
1081 local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive")"
1082 for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
1083 sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#http://localhost:8080/#${1}#" -e "s#https://localhost:4433/#${1}#"
1084 done
1085 }
1086
1087 # wait for up to 10s for a pid file to appear to avoid possible race
1088 # when a helper is started and dosn't write the PID quick enough
1089 waitforpidfile() {
1090 local PIDFILE="$1"
1091 for i in $(seq 10); do
1092 if test -s "$PIDFILE"; then
1093 return 0
1094 fi
1095 sleep 1
1096 done
1097 msgdie "waiting for $PIDFILE failed"
1098 return 1
1099 }
1100
1101 changetowebserver() {
1102 if [ "$1" != '--no-rewrite' ]; then
1103 rewritesourceslist 'http://localhost:8080/'
1104 else
1105 shift
1106 fi
1107 if test -x ${APTWEBSERVERBINDIR}/aptwebserver; then
1108 cd aptarchive
1109 local LOG="webserver.log"
1110 if ! aptwebserver -o aptwebserver::fork=1 "$@" >$LOG 2>&1 ; then
1111 cat $LOG
1112 false
1113 fi
1114 waitforpidfile aptwebserver.pid
1115 local PID="$(cat aptwebserver.pid)"
1116 if [ -z "$PID" ]; then
1117 msgdie 'Could not fork aptwebserver successfully'
1118 fi
1119 addtrap "kill $PID;"
1120 cd - > /dev/null
1121 else
1122 msgdie 'You have to build aptwerbserver or install a webserver'
1123 fi
1124 }
1125
1126 changetohttpswebserver() {
1127 if ! which stunnel4 >/dev/null; then
1128 msgdie 'You need to install stunnel4 for https testcases'
1129 fi
1130 if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
1131 changetowebserver --no-rewrite "$@"
1132 fi
1133 echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
1134 cert = ${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem
1135 output = /dev/null
1136
1137 [https]
1138 accept = 4433
1139 connect = 8080
1140 " > ${TMPWORKINGDIRECTORY}/stunnel.conf
1141 stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
1142 waitforpidfile "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid"
1143 local PID="$(cat ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid)"
1144 if [ -z "$PID" ]; then
1145 msgdie 'Could not fork stunnel4 successfully'
1146 fi
1147 addtrap 'prefix' "kill ${PID};"
1148 rewritesourceslist 'https://localhost:4433/'
1149 }
1150
1151 changetocdrom() {
1152 mkdir -p rootdir/media/cdrom/.disk
1153 local CD="$(readlink -f rootdir/media/cdrom)"
1154 echo "acquire::cdrom::mount \"${CD}\";
1155 acquire::cdrom::${CD}/::mount \"mv ${CD}-unmounted ${CD}\";
1156 acquire::cdrom::${CD}/::umount \"mv ${CD} ${CD}-unmounted\";
1157 acquire::cdrom::autodetect 0;" > rootdir/etc/apt/apt.conf.d/00cdrom
1158 echo -n "$1" > ${CD}/.disk/info
1159 if [ ! -d aptarchive/dists ]; then
1160 msgdie 'Flat file archive cdroms can not be created currently'
1161 return 1
1162 fi
1163 mv aptarchive/dists "$CD"
1164 ln -s "$(readlink -f ./incoming)" $CD/pool
1165 find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
1166 # start with an unmounted disk
1167 mv "${CD}" "${CD}-unmounted"
1168 # we don't want the disk to be modifiable
1169 addtrap 'prefix' "chmod -f -R +w $PWD/rootdir/media/cdrom/dists/ $PWD/rootdir/media/cdrom-unmounted/dists/ || true;"
1170 chmod -R 555 rootdir/media/cdrom-unmounted/dists
1171 }
1172
1173 downloadfile() {
1174 local PROTO="${1%%:*}"
1175 if ! apthelper -o Debug::Acquire::${PROTO}=1 -o Debug::pkgAcquire::Worker=1 \
1176 download-file "$1" "$2" "$3" 2>&1 ; then
1177 return 1
1178 fi
1179 # only if the file exists the download was successful
1180 if [ -r "$2" ]; then
1181 return 0
1182 else
1183 return 1
1184 fi
1185 }
1186
1187 checkdiff() {
1188 local DIFFTEXT="$(command diff -u "$@" 2>&1 | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
1189 if [ -n "$DIFFTEXT" ]; then
1190 echo >&2
1191 echo >&2 "$DIFFTEXT"
1192 return 1
1193 else
1194 return 0
1195 fi
1196 }
1197
1198 testfileequal() {
1199 local FILE="$1"
1200 shift
1201 msgtest "Test for correctness of file" "$FILE"
1202 if [ -z "$*" ]; then
1203 echo -n "" | checkdiff - $FILE && msgpass || msgfail
1204 else
1205 echo "$*" | checkdiff - $FILE && msgpass || msgfail
1206 fi
1207 }
1208
1209 testempty() {
1210 msgtest "Test for no output of" "$*"
1211 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testempty.comparefile"
1212 if "$@" >$COMPAREFILE 2>&1 && test ! -s $COMPAREFILE; then
1213 msgpass
1214 else
1215 echo
1216 cat $COMPAREFILE
1217 msgfail
1218 fi
1219 aptautotest 'testempty' "$@"
1220 }
1221
1222 testequal() {
1223 local MSG='Test of equality of'
1224 if [ "$1" = '--nomsg' ]; then
1225 MSG=''
1226 shift
1227 fi
1228
1229 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
1230 echo "$1" > $COMPAREFILE
1231 shift
1232
1233 if [ -n "$MSG" ]; then
1234 msgtest "$MSG" "$*"
1235 fi
1236 "$@" 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
1237 aptautotest 'testequal' "$@"
1238 }
1239
1240 testequalor2() {
1241 local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
1242 local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
1243 local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
1244 echo "$1" > $COMPAREFILE1
1245 echo "$2" > $COMPAREFILE2
1246 shift 2
1247 msgtest "Test for equality OR of" "$*"
1248 "$@" >$COMPAREAGAINST 2>&1 || true
1249 if checkdiff $COMPAREFILE1 $COMPAREAGAINST >/dev/null 2>&1 || \
1250 checkdiff $COMPAREFILE2 $COMPAREAGAINST >/dev/null 2>&1
1251 then
1252 msgpass
1253 else
1254 echo -n "\n${CINFO}Diff against OR 1${CNORMAL}"
1255 checkdiff $COMPAREFILE1 $COMPAREAGAINST || true
1256 echo -n "${CINFO}Diff against OR 2${CNORMAL}"
1257 checkdiff $COMPAREFILE2 $COMPAREAGAINST || true
1258 msgfail
1259 fi
1260 aptautotest 'testequalor2' "$@"
1261 }
1262
1263 testshowvirtual() {
1264 local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
1265 local PACKAGE="$1"
1266 shift
1267 while [ -n "$1" ]; do
1268 VIRTUAL="${VIRTUAL}
1269 N: Can't select versions from package '$1' as it is purely virtual"
1270 PACKAGE="${PACKAGE} $1"
1271 shift
1272 done
1273 msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
1274 VIRTUAL="${VIRTUAL}
1275 N: No packages found"
1276 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
1277 local ARCH="$(getarchitecture 'native')"
1278 echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
1279 aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
1280 }
1281
1282 testnopackage() {
1283 msgtest "Test for non-existent packages" "apt-cache show $*"
1284 local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
1285 if [ -n "$SHOWPKG" ]; then
1286 echo >&2
1287 echo >&2 "$SHOWPKG"
1288 msgfail
1289 else
1290 msgpass
1291 fi
1292 }
1293
1294 testdpkgstatus() {
1295 local STATE="$1"
1296 local NR="$2"
1297 shift 2
1298 msgtest "Test that $NR package(s) are in state $STATE with" "dpkg -l $*"
1299 local PKGS="$(dpkg -l "$@" 2>/dev/null | grep "^${STATE}" | wc -l)"
1300 if [ "$PKGS" != $NR ]; then
1301 echo >&2 $PKGS
1302 dpkg -l "$@" | grep '^[a-z]' >&2 || true
1303 msgfail
1304 else
1305 msgpass
1306 fi
1307 }
1308
1309 testdpkginstalled() {
1310 testdpkgstatus 'ii' "$#" "$@"
1311 }
1312
1313 testdpkgnotinstalled() {
1314 testdpkgstatus 'ii' '0' "$@"
1315 }
1316
1317 testmarkedauto() {
1318 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
1319 if [ -n "$1" ]; then
1320 msgtest 'Test for correctly marked as auto-installed' "$*"
1321 while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
1322 else
1323 msgtest 'Test for correctly marked as auto-installed' 'no package'
1324 echo -n > $COMPAREFILE
1325 fi
1326 aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
1327 }
1328 testmarkedmanual() {
1329 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedmanual.comparefile"
1330 if [ -n "$1" ]; then
1331 msgtest 'Test for correctly marked as manually installed' "$*"
1332 while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
1333 else
1334 msgtest 'Test for correctly marked as manually installed' 'no package'
1335 echo -n > $COMPAREFILE
1336 fi
1337 aptmark showmanual 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
1338 }
1339
1340 msgfailoutput() {
1341 local MSG="$1"
1342 local OUTPUT="$2"
1343 shift 2
1344 echo >&2
1345 if [ "$1" = 'grep' ]; then
1346 while [ -n "$2" ]; do shift; done
1347 echo "#### Complete file: $1 ####"
1348 cat >&2 "$1" || true
1349 echo '#### grep output ####'
1350 elif [ "$1" = 'test' ]; then
1351 # doesn't support ! or non-file flags
1352 msgfailoutputstatfile() {
1353 local FILEFLAGS='^-[bcdefgGhkLOprsStuwx]$'
1354 if expr match "$1" "$FILEFLAGS" >/dev/null; then
1355 echo "#### stat(2) of file: $2 ####"
1356 stat "$2" || true
1357 fi
1358 }
1359 msgfailoutputstatfile "$2" "$3"
1360 while [ -n "$5" ] && [ "$4" = '-o' -o "$4" = '-a' ]; do
1361 shift 3
1362 msgfailoutputstatfile "$2" "$3"
1363 done
1364 echo '#### test output ####'
1365 fi
1366 cat >&2 $OUTPUT
1367 msgfail "$MSG"
1368 }
1369
1370 testsuccess() {
1371 if [ "$1" = '--nomsg' ]; then
1372 shift
1373 else
1374 msgtest 'Test for successful execution of' "$*"
1375 fi
1376 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output"
1377 if "$@" >${OUTPUT} 2>&1; then
1378 if expr match "$1" '^apt.*' >/dev/null; then
1379 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1380 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1381 elif grep -E '^[WE]: ' "$OUTPUT" > "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" 2>&1; then
1382 if [ "$IGNORE_PTY_NOT_MOUNTED" = '1' ]; then
1383 if echo 'E: Can not write log (Is /dev/pts mounted?) - posix_openpt (2: No such file or directory)' \
1384 | cmp - "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" >/dev/null 2>&1; then
1385 msgpass
1386 else
1387 msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
1388 fi
1389 else
1390 msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
1391 fi
1392 else
1393 msgpass
1394 fi
1395 else
1396 msgpass
1397 fi
1398 else
1399 local EXITCODE=$?
1400 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
1401 fi
1402 aptautotest 'testsuccess' "$@"
1403 }
1404 testwarning() {
1405 if [ "$1" = '--nomsg' ]; then
1406 shift
1407 else
1408 msgtest 'Test for successful execution with warnings of' "$*"
1409 fi
1410 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output"
1411 if "$@" >${OUTPUT} 2>&1; then
1412 if expr match "$1" '^apt.*' >/dev/null; then
1413 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1414 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1415 elif grep -q -E '^E: ' "$OUTPUT"; then
1416 msgfailoutput 'successful run, but output contains errors' "$OUTPUT" "$@"
1417 elif ! grep -q -E '^W: ' "$OUTPUT"; then
1418 msgfailoutput 'successful run, but output contains no warnings' "$OUTPUT" "$@"
1419 else
1420 msgpass
1421 fi
1422 else
1423 msgpass
1424 fi
1425 else
1426 local EXITCODE=$?
1427 echo >&2
1428 cat >&2 $OUTPUT
1429 msgfail "exitcode $EXITCODE"
1430 fi
1431 aptautotest 'testwarning' "$@"
1432 }
1433 testfailure() {
1434 if [ "$1" = '--nomsg' ]; then
1435 shift
1436 else
1437 msgtest 'Test for failure in execution of' "$*"
1438 fi
1439 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
1440 if "$@" >${OUTPUT} 2>&1; then
1441 local EXITCODE=$?
1442 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
1443 else
1444 local EXITCODE=$?
1445 if expr match "$1" '^apt.*' >/dev/null; then
1446 if [ "$1" = 'aptkey' ]; then
1447 if grep -q -E " Can't check signature: " "$OUTPUT" || \
1448 grep -q -E " BAD signature from " "$OUTPUT"; then
1449 msgpass
1450 else
1451 msgfailoutput "run failed with exitcode ${EXITCODE}, but no signature error" "$OUTPUT" "$@"
1452 fi
1453 else
1454 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1455 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1456 elif grep -q -E '==ERROR' "$OUTPUT"; then
1457 msgfailoutput 'compiler sanitizers reported errors' "$OUTPUT" "$@"
1458 elif ! grep -q -E '^E: ' "$OUTPUT"; then
1459 msgfailoutput "run failed with exitcode ${EXITCODE}, but with no errors" "$OUTPUT" "$@"
1460 else
1461 msgpass
1462 fi
1463 fi
1464 else
1465 msgpass
1466 fi
1467 fi
1468 aptautotest 'testfailure' "$@"
1469 }
1470
1471 testsuccessequal() {
1472 local CMP="$1"
1473 shift
1474 testsuccess "$@"
1475 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output" "$CMP"
1476 }
1477 testwarningequal() {
1478 local CMP="$1"
1479 shift
1480 testwarning "$@"
1481 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output" "$CMP"
1482 }
1483 testfailureequal() {
1484 local CMP="$1"
1485 shift
1486 testfailure "$@"
1487 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output" "$CMP"
1488 }
1489
1490 testfailuremsg() {
1491 local CMP="$1"
1492 shift
1493 testfailure "$@"
1494 msgtest 'Check that the output of the previous failed command has expected' 'failures and warnings'
1495 grep '^\(W\|E\):' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output" > "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailureequal.output" 2>&1 || true
1496 if echo "$CMP" | checkdiff - "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailureequal.output"; then
1497 msgpass
1498 else
1499 echo '### Complete output ###'
1500 cat "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
1501 msgfail
1502 fi
1503 }
1504
1505 testfilestats() {
1506 msgtest "Test that file $1 has $2 $3" "$4"
1507 if [ "$4" "$3" "$(stat --format "$2" "$1")" ]; then
1508 msgpass
1509 else
1510 echo >&2
1511 ls -ld >&2 "$1" || true
1512 echo -n >&2 "stat(1) reports for $2: "
1513 stat --format "$2" "$1" || true
1514 msgfail
1515 fi
1516 }
1517 testaccessrights() {
1518 testfilestats "$1" '%a' '=' "$2"
1519 }
1520
1521 testwebserverlaststatuscode() {
1522 local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
1523 local STATUS='downloaded/webserverstatus-statusfile.log'
1524 rm -f "$DOWNLOG" "$STATUS"
1525 msgtest 'Test last status code from the webserver was' "$1"
1526 if downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG" && [ "$(cat "$STATUS")" = "$1" ]; then
1527 msgpass
1528 else
1529 echo >&2
1530 if [ -n "$2" ]; then
1531 shift
1532 echo >&2 '#### Additionally provided output files contain:'
1533 cat >&2 "$@"
1534 fi
1535 echo >&2 '#### Download log of the status code:'
1536 cat >&2 "$DOWNLOG"
1537 msgfail "Status was $(cat "$STATUS")"
1538 fi
1539 }
1540
1541 pause() {
1542 echo "STOPPED execution. Press enter to continue"
1543 local IGNORE
1544 read IGNORE
1545 }
1546
1547 listcurrentlistsdirectory() {
1548 {
1549 find rootdir/var/lib/apt/lists -maxdepth 1 -type d | while read line; do
1550 stat --format '%U:%G:%a:%n' "$line"
1551 done
1552 find rootdir/var/lib/apt/lists -maxdepth 1 \! -type d | while read line; do
1553 stat --format '%U:%G:%a:%s:%y:%n' "$line"
1554 done
1555 } | sort
1556 }
1557
1558 ### convinience hacks ###
1559 mkdir() {
1560 # creating some directories by hand is a tedious task, so make it look simple
1561 if [ "$*" = '-p rootdir/var/lib/apt/lists' ] || [ "$*" = "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" ] ||
1562 [ "$*" = '-p rootdir/var/lib/apt/lists/partial' ] || [ "$*" = "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" ]; then
1563 # only the last directory created by mkdir is effected by the -m !
1564 command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt"
1565 command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"
1566 command mkdir -m 700 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
1567 touch "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/lock"
1568 if [ "$(id -u)" = '0' ]; then
1569 chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
1570 fi
1571 else
1572 command mkdir "$@"
1573 fi
1574 }
1575
1576 ### The following tests are run by most test methods automatically to check
1577 ### general things about commands executed without writing the test every time.
1578
1579 aptautotest() {
1580 local TESTCALL="$1"
1581 local CMD="$2"
1582 local FIRSTOPT="$3"
1583 local AUTOTEST="aptautotest_$(basename "$CMD" | tr -d '-')_$(echo "$FIRSTOPT" | tr -d '-')"
1584 if command -v $AUTOTEST >/dev/null; then
1585 shift 3
1586 # save and restore the *.output files from other tests
1587 # as we might otherwise override them in these automatic tests
1588 rm -rf ${TMPWORKINGDIRECTORY}/rootdir/tmp-before
1589 mv ${TMPWORKINGDIRECTORY}/rootdir/tmp ${TMPWORKINGDIRECTORY}/rootdir/tmp-before
1590 mkdir ${TMPWORKINGDIRECTORY}/rootdir/tmp
1591 $AUTOTEST "$TESTCALL" "$@"
1592 rm -rf ${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest
1593 mv ${TMPWORKINGDIRECTORY}/rootdir/tmp ${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest
1594 mv ${TMPWORKINGDIRECTORY}/rootdir/tmp-before ${TMPWORKINGDIRECTORY}/rootdir/tmp
1595 fi
1596 }
1597
1598 aptautotest_aptget_update() {
1599 local TESTCALL="$1"
1600 while [ -n "$2" ]; do
1601 if [ "$2" = '--print-uris' ]; then return; fi # simulation mode
1602 shift
1603 done
1604 if ! test -d "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"; then return; fi
1605 testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
1606 testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
1607 # all copied files are properly chmodded
1608 for file in $(find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" -type f ! -name 'lock'); do
1609 testfilestats "$file" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:644"
1610 done
1611 if [ "$TESTCALL" = 'testsuccess' ]; then
1612 # failure cases can retain partial files and such
1613 testempty find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" -mindepth 1 ! \( -name 'lock' -o -name '*.FAILED' \)
1614 fi
1615 }
1616 aptautotest_apt_update() { aptautotest_aptget_update "$@"; }
1617 aptautotest_aptcdrom_add() { aptautotest_aptget_update "$@"; }
1618
1619 testaptautotestnodpkgwarning() {
1620 local TESTCALL="$1"
1621 while [ -n "$2" ]; do
1622 if expr match "$2" '^-[a-z]*s' >/dev/null 2>&1; then return; fi # simulation mode
1623 if expr match "$2" '^-dy\?' >/dev/null 2>&1; then return; fi # download-only mode
1624 shift
1625 done
1626 testfailure grep '^dpkg: warning:.*ignor.*' "${TMPWORKINGDIRECTORY}/rootdir/tmp-before/${TESTCALL}.output"
1627 }
1628
1629 aptautotest_aptget_install() { testaptautotestnodpkgwarning "$@"; }
1630 aptautotest_aptget_remove() { testaptautotestnodpkgwarning "$@"; }
1631 aptautotest_aptget_purge() { testaptautotestnodpkgwarning "$@"; }
1632 aptautotest_apt_install() { testaptautotestnodpkgwarning "$@"; }
1633 aptautotest_apt_remove() { testaptautotestnodpkgwarning "$@"; }
1634 aptautotest_apt_purge() { testaptautotestnodpkgwarning "$@"; }