]> git.saurik.com Git - apt.git/blame - test/integration/framework
tests: don't use hardcoded port for http and https
[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() {
6c0765c0 1122 local WEBSERVER="${3:-http://localhost:${APTHTTPPORT}}"
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"
6c0765c0 1131 # very very basic URI encoding
b0314abb
DK
1132 local URI
1133 if [ -n "$2" ]; then
1134 msgtest "Set webserver config option '${1}' to" "$2"
6c0765c0 1135 URI="${WEBSERVER}/_config/set/$(echo "${1}" | sed -e 's/\//%2f/g')/$(echo "${2}" | sed -e 's/\//%2f/g')"
b0314abb
DK
1136 else
1137 msgtest 'Clear webserver config option' "${1}"
6c0765c0 1138 URI="${WEBSERVER}/_config/clear/$(echo "${1}" | sed -e 's/\//%2f/g')"
b0314abb
DK
1139 fi
1140 if downloadfile "$URI" "$STATUS" > "$DOWNLOG"; then
f2c0ec8b
DK
1141 msgpass
1142 else
68042532
DK
1143 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/webserverconfig.output"
1144 cat "$DOWNLOG" "$STATUS" >"$OUTPUT" 2>&1 || true
1145 msgfailoutput '' "$OUTPUT"
f2c0ec8b 1146 fi
b0314abb 1147 $NOCHECK || testwebserverlaststatuscode '200'
f2c0ec8b
DK
1148}
1149
fd46d305 1150rewritesourceslist() {
63c71412 1151 local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive" | sed 's# #%20#g')"
fd46d305 1152 for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
6c0765c0 1153 sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#http://localhost:${APTHTTPPORT}/#${1}#" -e "s#https://localhost:${APTHTTPSPORT}/#${1}#"
fd46d305
DK
1154 done
1155}
1156
5572f6bd
MV
1157# wait for up to 10s for a pid file to appear to avoid possible race
1158# when a helper is started and dosn't write the PID quick enough
1159waitforpidfile() {
1160 local PIDFILE="$1"
1161 for i in $(seq 10); do
1162 if test -s "$PIDFILE"; then
1163 return 0
1164 fi
1165 sleep 1
1166 done
1167 msgdie "waiting for $PIDFILE failed"
1168 return 1
1169}
1170
f213b6ea 1171changetowebserver() {
6c0765c0 1172 local REWRITE='no'
23af9f40 1173 if [ "$1" != '--no-rewrite' ]; then
6c0765c0 1174 REWRITE='yes'
23af9f40
DK
1175 else
1176 shift
1177 fi
63c71412 1178 if test -x "${APTWEBSERVERBINDIR}/aptwebserver"; then
fbd29dd6 1179 cd aptarchive
a0db467c 1180 local LOG="webserver.log"
6c0765c0 1181 if ! aptwebserver --port 0 -o aptwebserver::fork=1 -o aptwebserver::portfile='aptwebserver.port' "$@" >$LOG 2>&1 ; then
bee0670b
DK
1182 cat $LOG
1183 false
1184 fi
6c0765c0 1185 waitforpidfile aptwebserver.pid
e3c62328
DK
1186 local PID="$(cat aptwebserver.pid)"
1187 if [ -z "$PID" ]; then
1188 msgdie 'Could not fork aptwebserver successfully'
1189 fi
1190 addtrap "kill $PID;"
6c0765c0
DK
1191 waitforpidfile aptwebserver.port
1192 APTHTTPPORT="$(cat aptwebserver.port)"
1193 if [ -z "$APTHTTPPORT" ]; then
1194 msgdie 'Could not get port for aptwebserver successfully'
1195 fi
fbd29dd6 1196 cd - > /dev/null
c5bcc607 1197 else
fbd29dd6 1198 msgdie 'You have to build aptwerbserver or install a webserver'
f213b6ea 1199 fi
6c0765c0
DK
1200 if [ "$REWRTE" != 'yes' ]; then
1201 rewritesourceslist "http://localhost:${APTHTTPPORT}/"
1202 fi
fd46d305
DK
1203}
1204
1205changetohttpswebserver() {
1206 if ! which stunnel4 >/dev/null; then
1207 msgdie 'You need to install stunnel4 for https testcases'
1208 fi
1209 if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
dc95fee1 1210 changetowebserver --no-rewrite "$@"
fd46d305
DK
1211 fi
1212 echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
68ba0b7f 1213cert = ${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem
23af9f40 1214output = /dev/null
fd46d305
DK
1215
1216[https]
6c0765c0
DK
1217accept = 0
1218connect = $APTHTTPPORT
63c71412 1219" > "${TMPWORKINGDIRECTORY}/stunnel.conf"
fd46d305 1220 stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
5572f6bd 1221 waitforpidfile "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid"
63c71412 1222 local PID="$(cat "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid")"
5572f6bd
MV
1223 if [ -z "$PID" ]; then
1224 msgdie 'Could not fork stunnel4 successfully'
1225 fi
fd46d305 1226 addtrap 'prefix' "kill ${PID};"
6c0765c0
DK
1227 APTHTTPSPORT="$(lsof -i | awk "/^stunnel4 / && \$2 == \"${PID}\" {print \$9; exit; }" | cut -d':' -f 2)"
1228 webserverconfig 'aptwebserver::port::https' "$APTHTTPSPORT" "https://localhost:${APTHTTPSPORT}"
1229 rewritesourceslist "https://localhost:${APTHTTPSPORT}/"
f213b6ea
DK
1230}
1231
c45233ea
DK
1232changetocdrom() {
1233 mkdir -p rootdir/media/cdrom/.disk
1234 local CD="$(readlink -f rootdir/media/cdrom)"
63c71412
DK
1235 cat > rootdir/etc/apt/apt.conf.d/00cdrom <<EOF
1236acquire::cdrom::mount "${CD}";
1237acquire::cdrom::"${CD}/"::mount "mv ${CD}-unmounted ${CD}";
1238acquire::cdrom::"${CD}/"::umount "mv ${CD} ${CD}-unmounted";
1239acquire::cdrom::autodetect 0;
1240EOF
1241 echo -n "$1" > "${CD}/.disk/info"
c45233ea
DK
1242 if [ ! -d aptarchive/dists ]; then
1243 msgdie 'Flat file archive cdroms can not be created currently'
1244 return 1
1245 fi
a0975c8d 1246 mv aptarchive/dists "$CD"
63c71412 1247 ln -s "$(readlink -f ./incoming)" "$CD/pool"
c45233ea 1248 find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
a0975c8d
DK
1249 # start with an unmounted disk
1250 mv "${CD}" "${CD}-unmounted"
1251 # we don't want the disk to be modifiable
63c71412 1252 addtrap 'prefix' "chmod -f -R +w \"$PWD/rootdir/media/cdrom/dists/\" \"$PWD/rootdir/media/cdrom-unmounted/dists/\" || true;"
b0314abb 1253 chmod -R 555 rootdir/media/cdrom-unmounted/dists
c45233ea
DK
1254}
1255
fd46d305 1256downloadfile() {
ed793a19 1257 local PROTO="${1%%:*}"
27925d82 1258 if ! apthelper -o Debug::Acquire::${PROTO}=1 -o Debug::pkgAcquire::Worker=1 \
dcbb364f 1259 download-file "$1" "$2" "$3" 2>&1 ; then
27925d82
DK
1260 return 1
1261 fi
fd46d305 1262 # only if the file exists the download was successful
b0314abb 1263 if [ -r "$2" ]; then
fd46d305
DK
1264 return 0
1265 else
1266 return 1
1267 fi
1268}
1269
f213b6ea 1270checkdiff() {
03aa0847 1271 local DIFFTEXT="$(command diff -u "$@" 2>&1 | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
8d876415 1272 if [ -n "$DIFFTEXT" ]; then
0d58c26a
DK
1273 echo >&2
1274 echo >&2 "$DIFFTEXT"
8d876415
DK
1275 return 1
1276 else
1277 return 0
1278 fi
1279}
1280
22ac12b2
DK
1281testoutputequal() {
1282 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testoutputequal.output"
1283 local COMPAREFILE="$1"
1284 shift
63c71412 1285 if "$@" 2>&1 | checkdiff "$COMPAREFILE" - >"$OUTPUT" 2>&1; then
22ac12b2
DK
1286 msgpass
1287 else
1288 echo "=== content of file we compared with (${COMPAREFILE}) ===" >>"${OUTPUT}"
1289 cat "$COMPAREFILE" >>"${OUTPUT}"
1290 msgfailoutput '' "$OUTPUT" "$@"
1291 fi
1292}
1293
75954ae2 1294testfileequal() {
1501a2c9 1295 msggroup 'testfileequal'
e6a12ff7
DK
1296 local MSG='Test for correctness of file'
1297 if [ "$1" = '--nomsg' ]; then
1298 MSG=''
1299 shift
1300 fi
75954ae2
DK
1301 local FILE="$1"
1302 shift
e6a12ff7
DK
1303 if [ -n "$MSG" ]; then
1304 msgtest "$MSG" "$FILE"
1305 fi
68042532 1306 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfileequal.output"
75954ae2 1307 if [ -z "$*" ]; then
22ac12b2 1308 testoutputequal "$FILE" echo -n ''
75954ae2 1309 else
22ac12b2 1310 testoutputequal "$FILE" echo "$*"
75954ae2 1311 fi
9beb11aa 1312 msggroup
75954ae2
DK
1313}
1314
b855a400 1315testempty() {
1501a2c9 1316 msggroup 'testempty'
b855a400 1317 msgtest "Test for no output of" "$*"
859093da 1318 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testempty.comparefile"
63c71412 1319 if ("$@" >"$COMPAREFILE" 2>&1 || true) && test ! -s "$COMPAREFILE"; then
859093da
DK
1320 msgpass
1321 else
22ac12b2 1322 msgfailoutput '' "$COMPAREFILE" "$@"
859093da 1323 fi
ab25bf1f 1324 aptautotest 'testempty' "$@"
9beb11aa 1325 msggroup
b855a400 1326}
63c71412
DK
1327testnotempty() {
1328 msggroup 'testnotempty'
1329 msgtest "Test for some output of" "$*"
1330 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnotempty.comparefile"
1331 if ("$@" >"$COMPAREFILE" 2>&1 || true) && test -s "$COMPAREFILE"; then
1332 msgpass
1333 else
1334 msgfailoutput '' "$COMPAREFILE" "$@"
1335 fi
1336 aptautotest 'testnotempty' "$@"
1337 msggroup
1338}
b855a400 1339
f74a6fa1 1340testequal() {
1501a2c9 1341 msggroup 'testequal'
f74a6fa1
DK
1342 local MSG='Test of equality of'
1343 if [ "$1" = '--nomsg' ]; then
1344 MSG=''
1345 shift
1346 fi
1347
03938280 1348 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
63c71412 1349 echo "$1" > "$COMPAREFILE"
8d876415 1350 shift
d2d68aaf 1351
f74a6fa1
DK
1352 if [ -n "$MSG" ]; then
1353 msgtest "$MSG" "$*"
1354 fi
22ac12b2 1355 testoutputequal "$COMPAREFILE" "$@"
ab25bf1f 1356 aptautotest 'testequal' "$@"
9beb11aa 1357 msggroup
8d876415
DK
1358}
1359
685625bd 1360testequalor2() {
1501a2c9 1361 msggroup 'testequalor2'
03938280
DK
1362 local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
1363 local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
1364 local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
63c71412
DK
1365 echo "$1" > "$COMPAREFILE1"
1366 echo "$2" > "$COMPAREFILE2"
685625bd
DK
1367 shift 2
1368 msgtest "Test for equality OR of" "$*"
63c71412
DK
1369 "$@" >"$COMPAREAGAINST" 2>&1 || true
1370 if checkdiff "$COMPAREFILE1" "$COMPAREAGAINST" >/dev/null 2>&1 || \
1371 checkdiff "$COMPAREFILE2" "$COMPAREAGAINST" >/dev/null 2>&1
0d58c26a 1372 then
0caa5a4c
DK
1373 msgpass
1374 else
68042532
DK
1375 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.output"
1376 echo -n "\n${CINFO}Diff against OR 1${CNORMAL}" >"$OUTPUT" 2>&1
1377 checkdiff "$COMPAREFILE1" "$COMPAREAGAINST" >"$OUTPUT" 2>&1 || true
1378 echo -n "${CINFO}Diff against OR 2${CNORMAL}" >"$OUTPUT" 2>&1
1379 checkdiff "$COMPAREFILE2" "$COMPAREAGAINST" >"$OUTPUT" 2>&1 || true
1380 msgfailoutput '' "$OUTPUT"
0caa5a4c 1381 fi
ab25bf1f 1382 aptautotest 'testequalor2' "$@"
9beb11aa 1383 msggroup
685625bd
DK
1384}
1385
8d876415 1386testshowvirtual() {
1501a2c9 1387 msggroup 'testshowvirtual'
edc0ef10 1388 local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
8d876415
DK
1389 local PACKAGE="$1"
1390 shift
1391 while [ -n "$1" ]; do
1392 VIRTUAL="${VIRTUAL}
edc0ef10 1393N: Can't select versions from package '$1' as it is purely virtual"
8d876415
DK
1394 PACKAGE="${PACKAGE} $1"
1395 shift
1396 done
1397 msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
1398 VIRTUAL="${VIRTUAL}
4bec02c2 1399N: No packages found"
03938280 1400 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
ea65d079 1401 local ARCH="$(getarchitecture 'native')"
68042532
DK
1402 echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' >"$COMPAREFILE"
1403 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.output"
22ac12b2 1404 testoutputequal "$COMPAREFILE" aptcache show -q=0 "$PACKAGE"
9beb11aa 1405 msggroup
8d876415
DK
1406}
1407
1408testnopackage() {
1501a2c9 1409 msggroup 'testnopackage'
8d876415 1410 msgtest "Test for non-existent packages" "apt-cache show $*"
846856f4 1411 local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
8d876415 1412 if [ -n "$SHOWPKG" ]; then
68042532
DK
1413 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnopackage.output"
1414 echo "$SHOWPKG" >"$OUTPUT"
1415 msgfailoutput '' "$OUTPUT"
0d58c26a
DK
1416 else
1417 msgpass
8d876415 1418 fi
9beb11aa 1419 msggroup
8d876415 1420}
01a6e24c 1421
ecb777dd 1422testdpkgstatus() {
1501a2c9 1423 msggroup 'testdpkgstatus'
ecb777dd
DK
1424 local STATE="$1"
1425 local NR="$2"
1426 shift 2
1427 msgtest "Test that $NR package(s) are in state $STATE with" "dpkg -l $*"
1428 local PKGS="$(dpkg -l "$@" 2>/dev/null | grep "^${STATE}" | wc -l)"
1429 if [ "$PKGS" != $NR ]; then
68042532
DK
1430 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnopackage.output"
1431 echo "$PKGS" >"$OUTPUT"
1432 dpkg -l "$@" | grep '^[a-z]' >"$OUTPUT" >&2 || true
1433 msgfailoutput '' "$OUTPUT"
0d58c26a
DK
1434 else
1435 msgpass
01a6e24c 1436 fi
9beb11aa 1437 msggroup
01a6e24c
DK
1438}
1439
ecb777dd 1440testdpkginstalled() {
1501a2c9 1441 msggroup 'testdpkginstalled'
ecb777dd 1442 testdpkgstatus 'ii' "$#" "$@"
9beb11aa 1443 msggroup
ecb777dd
DK
1444}
1445
5cf733e1 1446testdpkgnotinstalled() {
1501a2c9 1447 msggroup 'testdpkgnotinstalled'
ecb777dd 1448 testdpkgstatus 'ii' '0' "$@"
9beb11aa 1449 msggroup
01a6e24c 1450}
ec7f904e
DK
1451
1452testmarkedauto() {
1501a2c9 1453 msggroup 'testmarkedauto'
03938280 1454 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
ec7f904e
DK
1455 if [ -n "$1" ]; then
1456 msgtest 'Test for correctly marked as auto-installed' "$*"
63c71412 1457 while [ -n "$1" ]; do echo "$1"; shift; done | sort > "$COMPAREFILE"
ec7f904e
DK
1458 else
1459 msgtest 'Test for correctly marked as auto-installed' 'no package'
63c71412 1460 echo -n > "$COMPAREFILE"
ec7f904e 1461 fi
22ac12b2 1462 testoutputequal "$COMPAREFILE" aptmark showauto
9beb11aa 1463 msggroup
ec7f904e 1464}
7c2cc4a7 1465testmarkedmanual() {
1501a2c9 1466 msggroup 'testmarkedmanual'
7c2cc4a7
DK
1467 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedmanual.comparefile"
1468 if [ -n "$1" ]; then
1469 msgtest 'Test for correctly marked as manually installed' "$*"
63c71412 1470 while [ -n "$1" ]; do echo "$1"; shift; done | sort > "$COMPAREFILE"
7c2cc4a7
DK
1471 else
1472 msgtest 'Test for correctly marked as manually installed' 'no package'
63c71412 1473 echo -n > "$COMPAREFILE"
7c2cc4a7 1474 fi
22ac12b2 1475 testoutputequal "$COMPAREFILE" aptmark showmanual
9beb11aa 1476 msggroup
7c2cc4a7 1477}
89a1aa5d 1478
e52aad52 1479msgfailoutput() {
1501a2c9 1480 msgreportheader 'msgfailoutput'
e52aad52
DK
1481 local MSG="$1"
1482 local OUTPUT="$2"
1483 shift 2
e52aad52 1484 if [ "$1" = 'grep' ]; then
68042532 1485 echo >&2
e52aad52
DK
1486 while [ -n "$2" ]; do shift; done
1487 echo "#### Complete file: $1 ####"
1488 cat >&2 "$1" || true
1489 echo '#### grep output ####'
1490 elif [ "$1" = 'test' ]; then
68042532 1491 echo >&2
e52aad52
DK
1492 # doesn't support ! or non-file flags
1493 msgfailoutputstatfile() {
1494 local FILEFLAGS='^-[bcdefgGhkLOprsStuwx]$'
1495 if expr match "$1" "$FILEFLAGS" >/dev/null; then
1496 echo "#### stat(2) of file: $2 ####"
1497 stat "$2" || true
3196dae8
DK
1498 if test -e "$2"; then
1499 echo "#### Complete file: $2 ####"
1500 cat >&2 "$2" || true
1501 fi
e52aad52
DK
1502 fi
1503 }
1504 msgfailoutputstatfile "$2" "$3"
1505 while [ -n "$5" ] && [ "$4" = '-o' -o "$4" = '-a' ]; do
1506 shift 3
1507 msgfailoutputstatfile "$2" "$3"
1508 done
1509 echo '#### test output ####'
1510 fi
63c71412 1511 cat >&2 "$OUTPUT"
e52aad52
DK
1512 msgfail "$MSG"
1513}
1514
0440d936 1515testsuccess() {
1501a2c9 1516 msggroup 'testsuccess'
0440d936
DK
1517 if [ "$1" = '--nomsg' ]; then
1518 shift
1519 else
1520 msgtest 'Test for successful execution of' "$*"
1521 fi
03938280 1522 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output"
63c71412 1523 if "$@" >"${OUTPUT}" 2>&1; then
4fa34122 1524 if expr match "$1" '^apt.*' >/dev/null; then
e52aad52
DK
1525 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1526 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
3261271e
DK
1527 elif grep -E '^[WE]: ' "$OUTPUT" > "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" 2>&1; then
1528 if [ "$IGNORE_PTY_NOT_MOUNTED" = '1' ]; then
1529 if echo 'E: Can not write log (Is /dev/pts mounted?) - posix_openpt (2: No such file or directory)' \
1530 | cmp - "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" >/dev/null 2>&1; then
1531 msgpass
1532 else
1533 msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
1534 fi
1535 else
1536 msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
1537 fi
4fa34122
DK
1538 else
1539 msgpass
1540 fi
1541 else
1542 msgpass
1543 fi
1544 else
1545 local EXITCODE=$?
e52aad52 1546 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
4fa34122
DK
1547 fi
1548 aptautotest 'testsuccess' "$@"
9beb11aa 1549 msggroup
4fa34122
DK
1550}
1551testwarning() {
1501a2c9 1552 msggroup 'testwarning'
4fa34122
DK
1553 if [ "$1" = '--nomsg' ]; then
1554 shift
1555 else
1556 msgtest 'Test for successful execution with warnings of' "$*"
1557 fi
25b86db1 1558 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output"
63c71412 1559 if "$@" >"${OUTPUT}" 2>&1; then
1df24acf 1560 if expr match "$1" '^apt.*' >/dev/null; then
e52aad52
DK
1561 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1562 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1563 elif grep -q -E '^E: ' "$OUTPUT"; then
1564 msgfailoutput 'successful run, but output contains errors' "$OUTPUT" "$@"
4fa34122 1565 elif ! grep -q -E '^W: ' "$OUTPUT"; then
e52aad52 1566 msgfailoutput 'successful run, but output contains no warnings' "$OUTPUT" "$@"
1df24acf
DK
1567 else
1568 msgpass
1569 fi
1570 else
1571 msgpass
1572 fi
0440d936 1573 else
07cb47e7 1574 local EXITCODE=$?
68042532 1575 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
0440d936 1576 fi
4fa34122 1577 aptautotest 'testwarning' "$@"
9beb11aa 1578 msggroup
0440d936 1579}
0440d936 1580testfailure() {
1501a2c9 1581 msggroup 'testfailure'
0440d936
DK
1582 if [ "$1" = '--nomsg' ]; then
1583 shift
1584 else
889b0072 1585 msgtest 'Test for failure in execution of' "$*"
0440d936 1586 fi
03938280 1587 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
63c71412 1588 if "$@" >"${OUTPUT}" 2>&1; then
07cb47e7 1589 local EXITCODE=$?
e52aad52 1590 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
0440d936 1591 else
1df24acf
DK
1592 local EXITCODE=$?
1593 if expr match "$1" '^apt.*' >/dev/null; then
b0d40854
DK
1594 if [ "$1" = 'aptkey' ]; then
1595 if grep -q -E " Can't check signature: " "$OUTPUT" || \
1596 grep -q -E " BAD signature from " "$OUTPUT"; then
1597 msgpass
1598 else
1599 msgfailoutput "run failed with exitcode ${EXITCODE}, but no signature error" "$OUTPUT" "$@"
1600 fi
1df24acf 1601 else
b0d40854
DK
1602 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1603 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1604 elif grep -q -E '==ERROR' "$OUTPUT"; then
1605 msgfailoutput 'compiler sanitizers reported errors' "$OUTPUT" "$@"
1606 elif ! grep -q -E '^E: ' "$OUTPUT"; then
1607 msgfailoutput "run failed with exitcode ${EXITCODE}, but with no errors" "$OUTPUT" "$@"
1608 else
1609 msgpass
1610 fi
1df24acf
DK
1611 fi
1612 else
1613 msgpass
1614 fi
0440d936 1615 fi
ab25bf1f 1616 aptautotest 'testfailure' "$@"
9beb11aa 1617 msggroup
0440d936
DK
1618}
1619
7414af7f
DK
1620testreturnstateequal() {
1621 local STATE="$1"
1622 msggroup "${STATE}equal"
1623 if [ "$2" != '--nomsg' ]; then
1624 local CMP="$2"
1625 shift 2
1626 "$STATE" "$@"
1627 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1628 else
1629 local CMP="$3"
1630 shift 3
1631 "$STATE" --nomsg "$@"
1632 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1633 fi
9beb11aa 1634 msggroup
25b86db1 1635}
7414af7f
DK
1636testsuccessequal() {
1637 testreturnstateequal 'testsuccess' "$@"
1638}
25b86db1 1639testwarningequal() {
7414af7f 1640 testreturnstateequal 'testwarning' "$@"
25b86db1
DK
1641}
1642testfailureequal() {
7414af7f 1643 testreturnstateequal 'testfailure' "$@"
25b86db1
DK
1644}
1645
27925d82 1646testfailuremsg() {
1501a2c9 1647 msggroup 'testfailuremsg'
27925d82
DK
1648 local CMP="$1"
1649 shift
1650 testfailure "$@"
1651 msgtest 'Check that the output of the previous failed command has expected' 'failures and warnings'
68042532
DK
1652 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailuremsg.comparefile"
1653 grep '^\(W\|E\):' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output" > "$COMPAREFILE" 2>&1 || true
22ac12b2 1654 testoutputequal "$COMPAREFILE" echo "$CMP"
9beb11aa 1655 msggroup
27925d82 1656}
25b86db1 1657
de81b2e2 1658testfilestats() {
1501a2c9 1659 msggroup 'testfilestats'
de81b2e2
DK
1660 msgtest "Test that file $1 has $2 $3" "$4"
1661 if [ "$4" "$3" "$(stat --format "$2" "$1")" ]; then
5684f71f
DK
1662 msgpass
1663 else
68042532
DK
1664 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfilestats.output"
1665 {
1666 ls -ld "$1" || true
1667 echo -n "stat(1) reports for $2: "
1668 stat --format "$2" "$1" || true
1669 } >"$OUTPUT" 2>&1
1670 msgfailoutput '' "$OUTPUT"
5684f71f 1671 fi
9beb11aa 1672 msggroup
5684f71f 1673}
de81b2e2 1674testaccessrights() {
1501a2c9 1675 msggroup 'testaccessrights'
de81b2e2 1676 testfilestats "$1" '%a' '=' "$2"
9beb11aa 1677 msggroup
de81b2e2 1678}
5684f71f 1679
0d58c26a 1680testwebserverlaststatuscode() {
1501a2c9 1681 msggroup 'testwebserverlaststatuscode'
0d58c26a 1682 local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
03aa0847 1683 local STATUS='downloaded/webserverstatus-statusfile.log'
0d58c26a
DK
1684 rm -f "$DOWNLOG" "$STATUS"
1685 msgtest 'Test last status code from the webserver was' "$1"
6c0765c0 1686 if downloadfile "http://localhost:${APTHTTPPORT}/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG" && [ "$(cat "$STATUS")" = "$1" ]; then
0d58c26a
DK
1687 msgpass
1688 else
68042532
DK
1689 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwebserverlaststatuscode.output"
1690 {
1691 if [ -n "$2" ]; then
1692 shift
1693 echo >&2 '#### Additionally provided output files contain:'
1694 cat >&2 "$@"
1695 fi
1696 echo >&2 '#### Download log of the status code:'
1697 cat >&2 "$DOWNLOG"
1698 } >"$OUTPUT" 2>&1
1699 msgfailoutput "Status was $(cat "$STATUS")" "$OUTPUT"
0d58c26a 1700 fi
9beb11aa 1701 msggroup
0d58c26a
DK
1702}
1703
89a1aa5d
DK
1704pause() {
1705 echo "STOPPED execution. Press enter to continue"
1706 local IGNORE
1707 read IGNORE
1708}
ab25bf1f 1709
846bc058 1710listcurrentlistsdirectory() {
ba6b79bd
DK
1711 {
1712 find rootdir/var/lib/apt/lists -maxdepth 1 -type d | while read line; do
1713 stat --format '%U:%G:%a:%n' "$line"
1714 done
1715 find rootdir/var/lib/apt/lists -maxdepth 1 \! -type d | while read line; do
1716 stat --format '%U:%G:%a:%s:%y:%n' "$line"
1717 done
1718 } | sort
846bc058
DK
1719}
1720
3a8776a3 1721### convenience hacks ###
8fe964f1
DK
1722mkdir() {
1723 # creating some directories by hand is a tedious task, so make it look simple
1724 if [ "$*" = '-p rootdir/var/lib/apt/lists' ] || [ "$*" = "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" ] ||
1725 [ "$*" = '-p rootdir/var/lib/apt/lists/partial' ] || [ "$*" = "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" ]; then
1726 # only the last directory created by mkdir is effected by the -m !
1727 command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt"
1728 command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"
1729 command mkdir -m 700 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
1730 touch "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/lock"
1731 if [ "$(id -u)" = '0' ]; then
1732 chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
1733 fi
1734 else
1735 command mkdir "$@"
1736 fi
1737}
1738
ab25bf1f
DK
1739### The following tests are run by most test methods automatically to check
1740### general things about commands executed without writing the test every time.
1741
1742aptautotest() {
1743 local TESTCALL="$1"
1744 local CMD="$2"
1745 local FIRSTOPT="$3"
63c71412 1746 local AUTOTEST="aptautotest_$(echo "${CMD##*/}_${FIRSTOPT}" | tr -d '-')"
ab25bf1f
DK
1747 if command -v $AUTOTEST >/dev/null; then
1748 shift 3
1749 # save and restore the *.output files from other tests
1750 # as we might otherwise override them in these automatic tests
63c71412
DK
1751 rm -rf "${TMPWORKINGDIRECTORY}/rootdir/tmp-before"
1752 mv "${TMPWORKINGDIRECTORY}/rootdir/tmp" "${TMPWORKINGDIRECTORY}/rootdir/tmp-before"
1753 mkdir "${TMPWORKINGDIRECTORY}/rootdir/tmp"
ab25bf1f 1754 $AUTOTEST "$TESTCALL" "$@"
63c71412
DK
1755 rm -rf "${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest"
1756 mv "${TMPWORKINGDIRECTORY}/rootdir/tmp" "${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest"
1757 mv "${TMPWORKINGDIRECTORY}/rootdir/tmp-before" "${TMPWORKINGDIRECTORY}/rootdir/tmp"
ab25bf1f
DK
1758 fi
1759}
1760
1761aptautotest_aptget_update() {
59148d96
DK
1762 local TESTCALL="$1"
1763 while [ -n "$2" ]; do
1764 if [ "$2" = '--print-uris' ]; then return; fi # simulation mode
1765 shift
1766 done
ab25bf1f 1767 if ! test -d "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"; then return; fi
4bb006d1
DK
1768 testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
1769 testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
ab25bf1f 1770 # all copied files are properly chmodded
63c71412
DK
1771 local backupIFS="$IFS"
1772 IFS="$(printf "\n\b")"
146f7715 1773 for file in $(find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" -type f ! -name 'lock'); do
4bb006d1 1774 testfilestats "$file" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:644"
ab25bf1f 1775 done
63c71412 1776 IFS="$backupIFS"
59148d96 1777 if [ "$TESTCALL" = 'testsuccess' ]; then
146f7715
DK
1778 # failure cases can retain partial files and such
1779 testempty find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" -mindepth 1 ! \( -name 'lock' -o -name '*.FAILED' \)
1780 fi
ab25bf1f
DK
1781}
1782aptautotest_apt_update() { aptautotest_aptget_update "$@"; }
d84da499 1783aptautotest_aptcdrom_add() { aptautotest_aptget_update "$@"; }
016bea82
DK
1784
1785testaptautotestnodpkgwarning() {
1786 local TESTCALL="$1"
1787 while [ -n "$2" ]; do
6bf93605
DK
1788 if expr match "$2" '^-[a-z]*s' >/dev/null 2>&1; then return; fi # simulation mode
1789 if expr match "$2" '^-dy\?' >/dev/null 2>&1; then return; fi # download-only mode
016bea82
DK
1790 shift
1791 done
1792 testfailure grep '^dpkg: warning:.*ignor.*' "${TMPWORKINGDIRECTORY}/rootdir/tmp-before/${TESTCALL}.output"
1793}
1794
1795aptautotest_aptget_install() { testaptautotestnodpkgwarning "$@"; }
1796aptautotest_aptget_remove() { testaptautotestnodpkgwarning "$@"; }
1797aptautotest_aptget_purge() { testaptautotestnodpkgwarning "$@"; }
1798aptautotest_apt_install() { testaptautotestnodpkgwarning "$@"; }
1799aptautotest_apt_remove() { testaptautotestnodpkgwarning "$@"; }
1800aptautotest_apt_purge() { testaptautotestnodpkgwarning "$@"; }