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