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