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