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