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