]> git.saurik.com Git - apt.git/blame - test/integration/framework
Run ./prepare-release pre-export
[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'
61e92778
DK
12 elif [ "$1" = '--color=yes' ]; then
13 export MSGCOLOR='YES'
14 elif [ "$1" = '--color' ]; then
15 export MSGCOLOR="$(echo "$2" | tr 'a-z' 'A-Z')"
16 shift
17 elif [ "$1" = '--level' ]; then
18 export MSGLEVEL=$2
19 shift
68042532
DK
20 else
21 echo >&2 "WARNING: Unknown parameter »$1« will be ignored"
22 fi
23 shift
24done
25export MSGLEVEL="${MSGLEVEL:-3}"
26
8d876415 27# we all like colorful messages
61e92778 28if [ "${MSGCOLOR:-YES}" = 'YES' ]; then
27cb4f6c 29 if [ ! -t 1 ]; then # but check that we output to a terminal
1290422a
DK
30 export MSGCOLOR='NO'
31 fi
32fi
33
34
35if [ "$MSGCOLOR" != 'NO' ]; then
36 CERROR="\033[1;31m" # red
37 CWARNING="\033[1;33m" # yellow
38 CMSG="\033[1;32m" # green
39 CINFO="\033[1;96m" # light blue
40 CDEBUG="\033[1;94m" # blue
41 CNORMAL="\033[0;39m" # default system console color
42 CDONE="\033[1;32m" # green
43 CPASS="\033[1;32m" # green
44 CFAIL="\033[1;31m" # red
45 CCMD="\033[1;35m" # pink
682a3bf7 46fi
8d876415 47
de81b2e2
DK
48msgprintf() {
49 local START="$1"
50 local MIDDLE="$2"
51 local END="$3"
52 shift 3
53 if [ -n "$1" ]; then
54 printf "$START " "$1"
d73840dc 55 shift
de81b2e2 56 while [ -n "$1" ]; do
03aa0847 57 printf "$MIDDLE " "$(echo "$1" | sed -e 's#^apt\([cfghs]\)#apt-\1#')"
de81b2e2
DK
58 shift
59 done
60 fi
61 printf "${END}"
2a2a7ef4 62}
de81b2e2
DK
63msgdie() { msgprintf "${CERROR}E: %s" '%s' "${CNORMAL}\n" "$@" >&2; exit 1; }
64msgwarn() { msgprintf "${CWARNING}W: %s" '%s' "${CNORMAL}\n" "$@" >&2; }
65msgmsg() { msgprintf "${CMSG}%s" '%s' "${CNORMAL}\n" "$@"; }
66msginfo() { msgprintf "${CINFO}I: %s" '%s' "${CNORMAL}\n" "$@"; }
67msgdebug() { msgprintf "${CDEBUG}D: %s" '%s' "${CNORMAL}\n" "$@"; }
68msgdone() { msgprintf "${CDONE}DONE" '%s' "${CNORMAL}\n" "$@"; }
69msgnwarn() { msgprintf "${CWARNING}W: %s" '%s' "${CNORMAL}" "$@" >&2; }
70msgnmsg() { msgprintf "${CMSG}%s" '%s' "${CNORMAL}" "$@"; }
71msgninfo() { msgprintf "${CINFO}I: %s" '%s' "${CNORMAL}" "$@"; }
72msgndebug() { msgprintf "${CDEBUG}D: %s" '%s' "${CNORMAL}" "$@"; }
73msgtest() { msgprintf "${CINFO}%s" "${CCMD}%s${CINFO}" "…${CNORMAL} " "$@"; }
3c528b91 74msgpass() { printf "${CPASS}PASS${CNORMAL}\n"; }
1501a2c9 75msgreportheader() {
68042532 76 if [ -n "$MSGTEST_MSG" ]; then
1501a2c9 77 test "$1" != 'msgfailoutput' || echo
ae7fce5a
DK
78 if [ -n "$MSGTEST_MSGMSG" ]; then
79 echo "$MSGTEST_MSGMSG"
80 fi
22ac12b2 81 if [ -n "$MSGTEST_GRP" ] && [ "$MSGTEST_GRP" != 'NEXT' ] && [ "$MSGTEST_GRP" != "$MSGTEST_MSG" ]; then
1501a2c9 82 echo "${CFAIL}Part of the test group: $MSGTEST_GRP"
22ac12b2 83 fi
68042532
DK
84 echo -n "$MSGTEST_MSG"
85 unset MSGTEST_MSG
86 fi
1501a2c9
DK
87}
88msgskip() {
89 msgreportheader 'msgskip'
68ba0b7f
DK
90 if [ $# -gt 0 ]; then printf "${CWARNING}SKIP: $*${CNORMAL}\n" >&2;
91 else printf "${CWARNING}SKIP${CNORMAL}\n" >&2; fi
92}
5229b285 93msgfail() {
1501a2c9 94 msgreportheader 'msgfail'
68042532 95 if [ $# -gt 0 ] && [ -n "$1" ]; then printf "${CFAIL}FAIL: $*${CNORMAL}\n" >&2;
3c528b91 96 else printf "${CFAIL}FAIL${CNORMAL}\n" >&2; fi
03aa0847
DK
97 if [ -n "$APT_DEBUG_TESTS" ]; then
98 $SHELL
99 fi
5229b285
DK
100 EXIT_CODE=$((EXIT_CODE+1));
101}
9beb11aa
DK
102MSGGROUP_LEVEL=0
103msggroup() {
104 if [ -n "$1" ]; then
105 if [ $MSGGROUP_LEVEL = 0 ]; then
106 MSGTEST_GRP='NEXT'
107 fi
108 MSGGROUP_LEVEL=$((MSGGROUP_LEVEL+1));
109 else
110 MSGGROUP_LEVEL=$((MSGGROUP_LEVEL-1));
111 if [ $MSGGROUP_LEVEL = 0 ]; then
112 unset MSGTEST_GRP
113 fi
114 fi
115}
8d876415
DK
116
117# enable / disable Debugging
fc89263e
DK
118if [ $MSGLEVEL -le 0 ]; then
119 msgdie() { true; }
120fi
121if [ $MSGLEVEL -le 1 ]; then
122 msgwarn() { true; }
123 msgnwarn() { true; }
124fi
125if [ $MSGLEVEL -le 2 ]; then
ae7fce5a
DK
126 msgmsg() {
127 MSGTEST_MSGMSG="$(msgprintf "${CMSG}%s" '%s' "${CNORMAL}" "$@")"
128 }
fc89263e 129 msgnmsg() { true; }
9beb11aa
DK
130 msgtest() {
131 MSGTEST_MSG="$(msgprintf "${CINFO}%s" "${CCMD}%s${CINFO}" "…${CNORMAL} " "$@")"
132 if [ "$MSGTEST_GRP" = 'NEXT' ]; then
133 MSGTEST_GRP="$MSGTEST_MSG"
134 fi
135 }
3c528b91 136 msgpass() { printf " ${CPASS}P${CNORMAL}"; }
fc89263e
DK
137fi
138if [ $MSGLEVEL -le 3 ]; then
139 msginfo() { true; }
140 msgninfo() { true; }
141fi
142if [ $MSGLEVEL -le 4 ]; then
143 msgdebug() { true; }
144 msgndebug() { true; }
145fi
146msgdone() {
147 if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
148 [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
149 [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
150 [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
151 [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
152 true;
153 else
3c528b91 154 printf "${CDONE}DONE${CNORMAL}\n";
fc89263e
DK
155 fi
156}
e43a426e
MV
157getaptconfig() {
158 if [ -f ./aptconfig.conf ]; then
5465192b 159 echo "$(readlink -f ./aptconfig.conf)"
e43a426e 160 elif [ -f ../aptconfig.conf ]; then
5465192b
DK
161 echo "$(readlink -f ../aptconfig.conf)"
162 elif [ -f ../../aptconfig.conf ]; then
163 echo "$(readlink -f ../../aptconfig.conf)"
164 elif [ -f "${TMPWORKINGDIRECTORY}/aptconfig.conf" ]; then
165 echo "$(readlink -f "${TMPWORKINGDIRECTORY}/aptconfig.conf")"
166 fi
e43a426e 167}
8d876415
DK
168runapt() {
169 msgdebug "Executing: ${CCMD}$*${CDEBUG} "
846856f4
DK
170 local CMD="$1"
171 shift
3b8eb3bc 172 case $CMD in
a311fb96 173 sh|aptitude|*/*|command) ;;
3b8eb3bc
DK
174 *) CMD="${BUILDDIRECTORY}/$CMD";;
175 esac
a311fb96 176 MALLOC_PERTURB_=21 MALLOC_CHECK_=2 APT_CONFIG="$(getaptconfig)" LD_LIBRARY_PATH=${LIBRARYPATH} $CMD "$@"
8d876415 177}
846856f4
DK
178aptconfig() { runapt apt-config "$@"; }
179aptcache() { runapt apt-cache "$@"; }
180aptcdrom() { runapt apt-cdrom "$@"; }
181aptget() { runapt apt-get "$@"; }
182aptftparchive() { runapt apt-ftparchive "$@"; }
183aptkey() { runapt apt-key "$@"; }
184aptmark() { runapt apt-mark "$@"; }
4f6d26b4 185aptsortpkgs() { runapt apt-sortpkgs "$@"; }
796673c3 186apt() { runapt apt "$@"; }
3b8eb3bc
DK
187apthelper() { runapt "${APTHELPERBINDIR}/apt-helper" "$@"; }
188aptwebserver() { runapt "${APTWEBSERVERBINDIR}/aptwebserver" "$@"; }
189aptitude() { runapt aptitude "$@"; }
8d50b63f 190aptextracttemplates() { runapt apt-extracttemplates "$@"; }
3082603f 191aptinternalsolver() { runapt "${APTINTERNALSOLVER}" "$@"; }
ad7e0941 192aptdumpsolver() { runapt "${APTDUMPSOLVER}" "$@"; }
3b8eb3bc 193
158fda31 194dpkg() {
b0be0e09 195 "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" "$@"
158fda31 196}
ce7f128c 197dpkgcheckbuilddeps() {
63c71412 198 command dpkg-checkbuilddeps --admindir="${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg" "$@"
b6b5a542 199}
3fa950f1 200gdb() {
b07aeb1a
DK
201 local CMD
202 case "$1" in
203 aptget) CMD="apt-get";;
204 aptcache) CMD="apt-cache";;
5465192b
DK
205 aptcdrom) CMD="apt-cdrom";;
206 aptconfig) CMD="apt-config";;
b07aeb1a
DK
207 aptmark) CMD="apt-mark";;
208 apthelper) CMD="apt-helper";;
3d8232bf 209 aptftparchive) CMD="apt-ftparchive";;
63c71412 210 dpkg) shift; runapt "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg" "$@"; return;;
b07aeb1a
DK
211 *) CMD="$1";;
212 esac
ce928105 213 shift
921a9626
DK
214 if [ "${CMD##*/}" = "$CMD" ]; then
215 CMD="${BUILDDIRECTORY}/${CMD}"
216 fi
217 runapt command gdb --quiet -ex run "$CMD" --args "$CMD" "$@"
ae99ce2e 218}
8d876415 219
8c1dd12c 220exitwithstatus() {
f91bd741
MV
221 # error if we about to overflow, but ...
222 # "255 failures ought to be enough for everybody"
5d76cee1
MV
223 if [ $EXIT_CODE -gt 255 ]; then
224 msgdie "Total failure count $EXIT_CODE too big"
f91bd741 225 fi
5d76cee1 226 exit $((EXIT_CODE <= 255 ? EXIT_CODE : 255));
8c1dd12c
MV
227}
228
804d4a0d
DK
229shellsetedetector() {
230 local exit_status=$?
231 if [ "$exit_status" != '0' ]; then
3c528b91 232 printf >&2 "${CERROR}E: Looks like the testcases ended prematurely with exitcode: ${exit_status}${CNORMAL}\n"
804d4a0d
DK
233 if [ "$EXIT_CODE" = '0' ]; then
234 EXIT_CODE="$exit_status"
235 fi
236 fi
237}
238
b720d0bd 239addtrap() {
8437b7d4
DK
240 if [ "$1" = 'prefix' ]; then
241 CURRENTTRAP="$2 $CURRENTTRAP"
242 else
243 CURRENTTRAP="$CURRENTTRAP $1"
244 fi
804d4a0d 245 trap "shellsetedetector; $CURRENTTRAP exitwithstatus;" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
b720d0bd 246}
8d876415
DK
247
248setupenvironment() {
0d303f17
DK
249 # privilege dropping and testing doesn't work if /tmp isn't world-writeable (as e.g. with libpam-tmpdir)
250 if [ -n "$TMPDIR" ] && [ "$(id -u)" = '0' ] && [ "$(stat --format '%a' "$TMPDIR")" != '1777' ]; then
251 unset TMPDIR
252 fi
63c71412
DK
253 TMPWORKINGDIRECTORY="$(mktemp -d)"
254 addtrap "cd /; rm -rf \"$TMPWORKINGDIRECTORY\";"
255 msgninfo "Preparing environment for ${0##*/} in ${TMPWORKINGDIRECTORY}…"
5c0dd6fc 256
03aa0847 257 mkdir -m 700 "${TMPWORKINGDIRECTORY}/downloaded"
68ba0b7f
DK
258 if [ "$(id -u)" = '0' ]; then
259 # relax permissions so that running as root with user switching works
260 umask 022
03aa0847
DK
261 chmod 711 "$TMPWORKINGDIRECTORY"
262 chown _apt:root "${TMPWORKINGDIRECTORY}/downloaded"
68ba0b7f
DK
263 fi
264
63c71412 265 TESTDIRECTORY="$(readlink -f "$(dirname $0)")"
5c0dd6fc 266 # allow overriding the default BUILDDIR location
63c71412
DK
267 SOURCEDIRECTORY="${APT_INTEGRATION_TESTS_SOURCE_DIR:-"${TESTDIRECTORY}/../../"}"
268 BUILDDIRECTORY="${APT_INTEGRATION_TESTS_BUILD_DIR:-"${TESTDIRECTORY}/../../build/bin"}"
269 LIBRARYPATH="${APT_INTEGRATION_TESTS_LIBRARY_PATH:-"${BUILDDIRECTORY}"}"
270 METHODSDIR="${APT_INTEGRATION_TESTS_METHODS_DIR:-"${BUILDDIRECTORY}/methods"}"
271 APTHELPERBINDIR="${APT_INTEGRATION_TESTS_LIBEXEC_DIR:-"${BUILDDIRECTORY}"}"
272 APTWEBSERVERBINDIR="${APT_INTEGRATION_TESTS_WEBSERVER_BIN_DIR:-"${BUILDDIRECTORY}"}"
273 APTINTERNALSOLVER="${APT_INTEGRATION_TESTS_INTERNAL_SOLVER:-"${BUILDDIRECTORY}/apt-internal-solver"}"
274 APTDUMPSOLVER="${APT_INTEGRATION_TESTS_DUMP_SOLVER:-"${BUILDDIRECTORY}/apt-dump-solver"}"
8d876415 275 test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
5c0dd6fc
MV
276 # -----
277
63c71412 278 cd "$TMPWORKINGDIRECTORY"
cd725954 279 mkdir rootdir aptarchive keys
8d876415 280 cd rootdir
b29c3712 281 mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d
8fe964f1 282 mkdir -p usr/bin var/cache var/lib var/log tmp
cffea9af 283 mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
cd725954 284 touch var/lib/dpkg/available
8d876415 285 mkdir -p usr/lib/apt
63c71412 286 ln -s "${METHODSDIR}" usr/lib/apt/methods
1f6cf9e7
DK
287 if [ "$BUILDDIRECTORY" = "$LIBRARYPATH" ]; then
288 mkdir -p usr/lib/apt/solvers
289 ln -s "${BUILDDIRECTORY}/apt-dump-solver" usr/lib/apt/solvers/dump
290 ln -s "${BUILDDIRECTORY}/apt-internal-solver" usr/lib/apt/solvers/apt
291 echo "Dir::Bin::Solvers \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/solvers\";" > etc/apt/apt.conf.d/externalsolver.conf
292 fi
291a386f
MV
293 # use the autoremove from the BUILDDIRECTORY if its there, otherwise
294 # system
63c71412
DK
295 if [ -e "${BUILDDIRECTORY}/../../debian/apt.conf.autoremove" ]; then
296 ln -s "${BUILDDIRECTORY}/../../debian/apt.conf.autoremove" etc/apt/apt.conf.d/01autoremove
291a386f
MV
297 else
298 ln -s /etc/apt/apt.conf.d/01autoremove etc/apt/apt.conf.d/01autoremove
299 fi
8d876415 300 cd ..
63c71412
DK
301 local BASENAME="${0##*/}"
302 local PACKAGESFILE="Packages-${BASENAME#*-}"
53ea1b56
DK
303 if [ -f "${TESTDIRECTORY}/${PACKAGESFILE}" ]; then
304 cp "${TESTDIRECTORY}/${PACKAGESFILE}" aptarchive/Packages
8f8169ac 305 fi
63c71412 306 local SOURCESSFILE="Sources-${BASENAME#*-}"
53ea1b56
DK
307 if [ -f "${TESTDIRECTORY}/${SOURCESSFILE}" ]; then
308 cp "${TESTDIRECTORY}/${SOURCESSFILE}" aptarchive/Sources
8f8169ac 309 fi
63c71412
DK
310 for key in $(find "$TESTDIRECTORY" -name '*.pub' -o -name '*.sec'); do
311 cp "$key" keys/
312 chmod 644 "$key"
313 done
314 ln -s "${TMPWORKINGDIRECTORY}/keys/joesixpack.pub" rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
d6cf2345 315
cffea9af 316 echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
94eb3bee 317 echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
8d876415 318 echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
9d2a8a73 319 echo "Dir::Bin::Methods \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/methods\";" >> aptconfig.conf
23e64f6d
DK
320 # either store apt-key were we can access it, even if we run it as a different user
321 #cp "${BUILDDIRECTORY}/apt-key" "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/"
322 #chmod o+rx "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/apt-key"
323 #echo "Dir::Bin::apt-key \"${TMPWORKINGDIRECTORY}/rootdir/usr/bin/apt-key\";" >> aptconfig.conf
324 # destroys coverage reporting though, so we disable changing user for the calling gpgv
325 echo "Dir::Bin::apt-key \"${BUILDDIRECTORY}/apt-key\";" >> aptconfig.conf
68ba0b7f 326 if [ "$(id -u)" = '0' ]; then
23e64f6d 327 echo 'Binary::gpgv::Debug::NoDropPrivs "true";' >>aptconfig.conf
68ba0b7f 328 fi
b0be0e09 329
63c71412 330 cat > "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" <<EOF
b0be0e09
DK
331#!/bin/sh
332set -e
333if [ -r "${TMPWORKINGDIRECTORY}/noopchroot.so" ]; then
63c71412
DK
334 if [ -n "\$LD_LIBRARY_PATH" ]; then
335 export LD_LIBRARY_PATH="${TMPWORKINGDIRECTORY}:${LD_LIBRARY_PATH}"
b0be0e09 336 else
63c71412 337 export LD_LIBRARY_PATH="${TMPWORKINGDIRECTORY}"
b0be0e09 338 fi
921a9626 339 if [ -n "\$LD_PRELOAD" ]; then
63c71412 340 export LD_PRELOAD="noopchroot.so \${LD_PRELOAD}"
921a9626 341 else
63c71412 342 export LD_PRELOAD="noopchroot.so"
921a9626
DK
343 fi
344fi
63c71412
DK
345EOF
346 cp "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg"
347 cat >> "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" <<EOF
348exec fakeroot "${DPKG:-dpkg}" --root="${TMPWORKINGDIRECTORY}/rootdir" \\
349 --log="${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log" \\
350 --force-not-root --force-bad-path "\$@"
351EOF
352 cat >> "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg" <<EOF
921a9626 353exec fakeroot gdb --quiet -ex run "${DPKG:-dpkg}" --args "${DPKG:-dpkg}" --root="${TMPWORKINGDIRECTORY}/rootdir" \\
63c71412 354 --log="${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log" \\
b0be0e09
DK
355 --force-not-root --force-bad-path "\$@"
356EOF
921a9626 357 chmod +x "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg"
b0be0e09
DK
358 echo "Dir::Bin::dpkg \"${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg\";" > rootdir/etc/apt/apt.conf.d/99dpkg
359
533fe3d1
DK
360 {
361 if ! command dpkg --assert-multi-arch >/dev/null 2>&1; then
362 echo "DPKG::options:: \"--force-architecture\";" # Added to test multiarch before dpkg is ready for it…
363 fi
87d6947d 364 echo 'quiet "0";'
533fe3d1
DK
365 echo 'quiet::NoUpdate "true";'
366 echo 'quiet::NoStatistic "true";'
367 # too distracting for users, but helpful to detect changes
368 echo 'Acquire::Progress::Ignore::ShowErrorText "true";'
369 echo 'Acquire::Progress::Diffpercent "true";'
370 # in testcases, it can appear as if localhost has a rotation setup,
371 # hide this as we can't really deal with it properly
372 echo 'Acquire::Failure::ShowIP "false";'
373 } >> aptconfig.conf
d6cf2345 374
68ba0b7f
DK
375 cp "${TESTDIRECTORY}/apt.pem" "${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem"
376 if [ "$(id -u)" = '0' ]; then
377 chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem"
378 fi
379 echo "Acquire::https::CaInfo \"${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem\";" > rootdir/etc/apt/apt.conf.d/99https
d6cf2345 380 echo "Apt::Cmd::Disable-Script-Warning \"1\";" > rootdir/etc/apt/apt.conf.d/apt-binary
23d35ec1 381 echo 'Acquire::Connect::AddrConfig "false";' > rootdir/etc/apt/apt.conf.d/connect-addrconfig
276e51dd 382 configcompression '.' 'gz' #'bz2' 'lzma' 'xz'
c5ede4ca 383 confighashes 'SHA1' # these are tests, not security best-practices
77a45beb 384
4bb006d1
DK
385 # create some files in /tmp and look at user/group to get what this means
386 TEST_DEFAULT_USER="$USER"
387 if [ "$(uname)" = 'GNU/kFreeBSD' ]; then
388 TEST_DEFAULT_GROUP='root'
389 else
390 TEST_DEFAULT_GROUP="$USER"
391 fi
392
ce7f128c 393 # cleanup the environment a bit
995a4bf6 394 # prefer our apt binaries over the system apt binaries
8c617819 395 export PATH="${BUILDDIRECTORY}:${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
be233796 396 export LC_ALL=C.UTF-8
3b8eb3bc 397 unset LANGUAGE APT_CONFIG
ce7f128c 398 unset GREP_OPTIONS DEB_BUILD_PROFILES
8d876415
DK
399 msgdone "info"
400}
401
ea65d079
DK
402getarchitecture() {
403 if [ "$1" = "native" -o -z "$1" ]; then
404 eval `aptconfig shell ARCH APT::Architecture`
405 if [ -n "$ARCH" ]; then
406 echo $ARCH
407 else
5834d7a1 408 dpkg --print-architecture
ea65d079
DK
409 fi
410 else
411 echo $1
412 fi
413}
414
53ea1b56 415getarchitectures() {
081c9d44 416 aptconfig dump --no-empty --format '%v%n' APT::Architecture APT::Architectures | sort -u | tr '\n' ' '
53ea1b56
DK
417}
418
3dcdc1f9
DK
419getarchitecturesfromcommalist() {
420 echo "$1" | sed -e 's#,#\n#g' | sed -e "s/^native\$/$(getarchitecture 'native')/"
421}
422
8d876415 423configarchitecture() {
18908589
DK
424 {
425 echo "APT::Architecture \"$(getarchitecture $1)\";"
426 while [ -n "$1" ]; do
427 echo "APT::Architectures:: \"$(getarchitecture $1)\";"
428 shift
429 done
430 } >rootdir/etc/apt/apt.conf.d/01multiarch.conf
53ea1b56
DK
431 configdpkg
432}
433
434configdpkg() {
435 if [ ! -e rootdir/var/lib/dpkg/status ]; then
63c71412
DK
436 local BASENAME="${0##*/}"
437 local STATUSFILE="status-${BASENAME#*-}"
53ea1b56
DK
438 if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
439 cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
440 else
441 echo -n > rootdir/var/lib/dpkg/status
442 fi
443 fi
18908589 444 rm -f rootdir/etc/apt/apt.conf.d/00foreigndpkg
846856f4 445 if command dpkg --assert-multi-arch >/dev/null 2>&1 ; then
53ea1b56
DK
446 local ARCHS="$(getarchitectures)"
447 if echo "$ARCHS" | grep -E -q '[^ ]+ [^ ]+'; then
448 DPKGARCH="$(dpkg --print-architecture)"
449 for ARCH in ${ARCHS}; do
feae193b 450 if [ "${ARCH}" != "${DPKGARCH}" ]; then
18908589 451 if ! dpkg --add-architecture ${ARCH} >/dev/null 2>&1; then
feae193b 452 # old-style used e.g. in Ubuntu-P – and as it seems travis
18908589
DK
453 echo "DPKG::options:: \"--foreign-architecture\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
454 echo "DPKG::options:: \"${ARCH}\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
feae193b
DK
455 fi
456 fi
53ea1b56
DK
457 done
458 if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
05343a22
DK
459 # dpkg doesn't really check the version as long as it is fully installed,
460 # but just to be sure we choose one above the required version
461 insertinstalledpackage 'dpkg' "all" '1.16.2+fake'
53ea1b56
DK
462 fi
463 fi
464 fi
8d876415
DK
465}
466
0c787570
DK
467configdpkgnoopchroot() {
468 # create a library to noop chroot() and rewrite maintainer script executions
469 # via execvp() as used by dpkg as we don't want our rootdir to be a fullblown
470 # chroot directory dpkg could chroot into to execute the maintainer scripts
471 msgtest 'Building library to preload to make maintainerscript work in' 'dpkg'
63c71412 472 cat > noopchroot.c << EOF
0c787570
DK
473#define _GNU_SOURCE
474#include <stdio.h>
475#include <stdlib.h>
476#include <string.h>
477#include <dlfcn.h>
478
479static char * chrootdir = NULL;
480
481int chroot(const char *path) {
482 printf("WARNING: CHROOTing to %s was ignored!\n", path);
483 free(chrootdir);
484 chrootdir = strdup(path);
485 return 0;
486}
487int execvp(const char *file, char *const argv[]) {
488 static int (*func_execvp) (const char *, char * const []) = NULL;
489 if (func_execvp == NULL)
490 func_execvp = (int (*) (const char *, char * const [])) dlsym(RTLD_NEXT, "execvp");
491 if (chrootdir == NULL || strncmp(file, "/var/lib/dpkg/", strlen("/var/lib/dpkg/")) != 0)
492 return func_execvp(file, argv);
493 printf("REWRITE execvp call %s into %s\n", file, chrootdir);
494 char newfile[strlen(chrootdir) + strlen(file)];
495 strcpy(newfile, chrootdir);
496 strcat(newfile, file);
b0be0e09
DK
497 char const * const baseadmindir = "/var/lib/dpkg";
498 char admindir[strlen(chrootdir) + strlen(baseadmindir)];
499 strcpy(admindir, chrootdir);
500 strcat(admindir, baseadmindir);
501 setenv("DPKG_ADMINDIR", admindir, 1);
0c787570
DK
502 return func_execvp(newfile, argv);
503}
504EOF
505 testsuccess --nomsg gcc -fPIC -shared -o noopchroot.so noopchroot.c -ldl
0c787570 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" {
1dd20368 779 Architectures "$ARCHS all source";
63c71412
DK
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 820 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
1dd20368 821 if [ "$arch" = 'none' ]; then
10e100e5
DK
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; }
1dd20368 967getarchitecturesfromreleasefile() { echo "all $(getarchitectures)"; }
a3bbbab7 968
c5ede4ca
DK
969aptftparchiverelease() {
970 aptftparchive -qq release "$@" | sed -e '/0 Release$/ d' # remove the self reference
971}
9b78cda6 972generatereleasefiles() {
884a4c0a
DK
973 # $1 is the Date header and $2 is the ValidUntil header to be set
974 # both should be given in notation date/touch can understand
1dd20368
DK
975 local DATE="$1"
976 local VALIDUNTIL="$2"
9b78cda6 977 if [ -e aptarchive/dists ]; then
87d6947d 978 msgninfo "\tGenerate Release files for dists… "
9b78cda6 979 for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
1dd20368 980 local ARCHITECTURES="$(getarchitecturesfromreleasefile "$dir")"
a3bbbab7
DK
981 local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
982 local CODENAME="$(getcodenamefromsuite $SUITE)"
983 local VERSION="$(getreleaseversionfromsuite $SUITE)"
718f797c 984 local LABEL="$(getlabelfromsuite $SUITE)"
d56e2917 985 local ORIGIN="$(getoriginfromsuite $SUITE)"
63c71412 986 aptftparchiverelease "$dir" \
884a4c0a
DK
987 -o APT::FTPArchive::Release::Suite="${SUITE}" \
988 -o APT::FTPArchive::Release::Codename="${CODENAME}" \
bc7a59dd
DK
989 -o APT::FTPArchive::Release::Architectures="${ARCHITECTURES}" \
990 -o APT::FTPArchive::Release::Label="${LABEL}" \
991 -o APT::FTPArchive::Release::Origin="${ORIGIN}" \
992 -o APT::FTPArchive::Release::Version="${VERSION}" \
63c71412 993 > "$dir/Release"
a3bbbab7 994 if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
35faae11 995 sed -i '/^Date: / a\
63c71412 996NotAutomatic: yes' "$dir/Release"
35faae11 997 fi
9b78cda6
DK
998 done
999 else
87d6947d 1000 msgninfo "\tGenerate Release files for flat… "
c5ede4ca 1001 aptftparchiverelease ./aptarchive > aptarchive/Release
8d876415 1002 fi
1dd20368 1003 if [ -n "$DATE" -a "$DATE" != "now" ]; then
fe0f7911 1004 for release in $(find ./aptarchive -name 'Release'); do
1dd20368
DK
1005 sed -i "s/^Date: .*$/Date: $(date -d "$DATE" '+%a, %d %b %Y %H:%M:%S %Z')/" "$release"
1006 touch -d "$DATE" "$release"
fe0f7911 1007 done
8d876415 1008 fi
1dd20368 1009 if [ -n "$VALIDUNTIL" ]; then
c5ede4ca 1010 sed -i "/^Date: / a\
1dd20368 1011Valid-Until: $(date -d "$VALIDUNTIL" '+%a, %d %b %Y %H:%M:%S %Z')" $(find ./aptarchive -name 'Release')
c5ede4ca 1012 fi
b7899b00 1013 msgdone "info"
8d876415
DK
1014}
1015
b7899b00 1016setupdistsaptarchive() {
63c71412 1017 local APTARCHIVE="$(readlink -f ./aptarchive | sed 's# #%20#g')"
b7899b00
DK
1018 rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
1019 rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
1020 for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
63c71412 1021 SECTIONS=$(find "./aptarchive/dists/${DISTS}/" -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
b7899b00 1022 msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
63c71412
DK
1023 echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > "rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list"
1024 echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > "rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list"
b7899b00
DK
1025 msgdone "info"
1026 done
1027}
1028
1029setupflataptarchive() {
63c71412
DK
1030 local APTARCHIVE="$(readlink -f ./aptarchive)"
1031 local APTARCHIVEURI="$(readlink -f ./aptarchive | sed 's# #%20#g')"
1032 if [ -f "${APTARCHIVE}/Packages" ]; then
8d876415 1033 msgninfo "\tadd deb sources.list line… "
63c71412
DK
1034 echo "deb file://$APTARCHIVEURI /" > 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list'
1035 msgdone 'info'
8d876415 1036 else
63c71412 1037 rm -f 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list'
8d876415 1038 fi
63c71412 1039 if [ -f "${APTARCHIVE}/Sources" ]; then
8d876415 1040 msgninfo "\tadd deb-src sources.list line… "
63c71412
DK
1041 echo "deb-src file://$APTARCHIVEURI /" > 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list'
1042 msgdone 'info'
8d876415 1043 else
63c71412 1044 rm -f 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list'
8d876415 1045 fi
b7899b00
DK
1046}
1047
1048setupaptarchive() {
4dbfe436
DK
1049 local NOUPDATE=0
1050 if [ "$1" = '--no-update' ]; then
1051 NOUPDATE=1
1052 shift
1053 fi
1054 buildaptarchive "$@"
b7899b00
DK
1055 if [ -e aptarchive/dists ]; then
1056 setupdistsaptarchive
1057 else
1058 setupflataptarchive
1059 fi
9d653a6d 1060 signreleasefiles 'Joe Sixpack'
4dbfe436 1061 if [ "1" != "$NOUPDATE" ]; then
5684f71f 1062 testsuccess aptget update -o Debug::pkgAcquire::Worker=true -o Debug::Acquire::gpgv=true
b2ea1a47 1063 fi
8d876415
DK
1064}
1065
f213b6ea
DK
1066signreleasefiles() {
1067 local SIGNER="${1:-Joe Sixpack}"
07cb47e7 1068 local REPODIR="${2:-aptarchive}"
f1e1abd8 1069 local KEY="keys/$(echo "$SIGNER" | tr 'A-Z' 'a-z' | sed 's# ##g')"
33a22672 1070 local GPG="aptkey --quiet --keyring ${KEY}.pub --secret-keyring ${KEY}.sec --readonly adv --batch --yes"
f1e1abd8 1071 msgninfo "\tSign archive with $SIGNER key $KEY… "
29a59c46
DK
1072 local REXKEY='keys/rexexpired'
1073 local SECEXPIREBAK="${REXKEY}.sec.bak"
1074 local PUBEXPIREBAK="${REXKEY}.pub.bak"
1075 if [ "${SIGNER}" = 'Rex Expired' ]; then
1076 # the key is expired, so gpg doesn't allow to sign with and the --faked-system-time
1077 # option doesn't exist anymore (and using faketime would add a new obscure dependency)
1078 # therefore we 'temporary' make the key not expired and restore a backup after signing
63c71412
DK
1079 cp "${REXKEY}.sec" "$SECEXPIREBAK"
1080 cp "${REXKEY}.pub" "$PUBEXPIREBAK"
29a59c46
DK
1081 local SECUNEXPIRED="${REXKEY}.sec.unexpired"
1082 local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
1083 if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
63c71412
DK
1084 cp "$SECUNEXPIRED" "${REXKEY}.sec"
1085 cp "$PUBUNEXPIRED" "${REXKEY}.pub"
29a59c46 1086 else
f1e1abd8
DK
1087 if ! printf "expire\n1w\nsave\n" | $GPG --default-key "$SIGNER" --command-fd 0 --edit-key "${SIGNER}" >setexpire.gpg 2>&1; then
1088 cat setexpire.gpg
1089 exit 1
1090 fi
63c71412
DK
1091 cp "${REXKEY}.sec" "$SECUNEXPIRED"
1092 cp "${REXKEY}.pub" "$PUBUNEXPIRED"
29a59c46
DK
1093 fi
1094 fi
63c71412
DK
1095 for RELEASE in $(find "${REPODIR}/" -name Release); do
1096 $GPG --default-key "$SIGNER" --armor --detach-sign --sign --output "${RELEASE}.gpg" "${RELEASE}"
e3c62328 1097 local INRELEASE="$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')"
63c71412 1098 $GPG --default-key "$SIGNER" --clearsign --output "$INRELEASE" "$RELEASE"
e3c62328 1099 # we might have set a specific date for the Release file, so copy it
63c71412 1100 touch -d "$(stat --format "%y" ${RELEASE})" "${RELEASE}.gpg" "${INRELEASE}"
f213b6ea 1101 done
29a59c46 1102 if [ -f "$SECEXPIREBAK" ] && [ -f "$PUBEXPIREBAK" ]; then
63c71412
DK
1103 mv -f "$SECEXPIREBAK" "${REXKEY}.sec"
1104 mv -f "$PUBEXPIREBAK" "${REXKEY}.pub"
29a59c46 1105 fi
63c71412 1106 msgdone 'info'
f213b6ea
DK
1107}
1108
8eafc759
DK
1109redatereleasefiles() {
1110 local DATE="$(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')"
1111 for release in $(find aptarchive/ -name 'Release'); do
63c71412
DK
1112 sed -i "s/^Date: .*$/Date: ${DATE}/" "$release"
1113 touch -d "$DATE" "$release"
8eafc759
DK
1114 done
1115 signreleasefiles "${2:-Joe Sixpack}"
1116}
1117
f2c0ec8b 1118webserverconfig() {
6c0765c0 1119 local WEBSERVER="${3:-http://localhost:${APTHTTPPORT}}"
b0314abb
DK
1120 local NOCHECK=false
1121 if [ "$1" = '--no-check' ]; then
1122 NOCHECK=true
1123 shift
1124 fi
0d58c26a 1125 local DOWNLOG='rootdir/tmp/download-testfile.log'
b0314abb 1126 local STATUS='downloaded/webserverconfig.status'
0d58c26a 1127 rm -f "$STATUS" "$DOWNLOG"
6c0765c0 1128 # very very basic URI encoding
b0314abb
DK
1129 local URI
1130 if [ -n "$2" ]; then
1131 msgtest "Set webserver config option '${1}' to" "$2"
6c0765c0 1132 URI="${WEBSERVER}/_config/set/$(echo "${1}" | sed -e 's/\//%2f/g')/$(echo "${2}" | sed -e 's/\//%2f/g')"
b0314abb
DK
1133 else
1134 msgtest 'Clear webserver config option' "${1}"
6c0765c0 1135 URI="${WEBSERVER}/_config/clear/$(echo "${1}" | sed -e 's/\//%2f/g')"
b0314abb
DK
1136 fi
1137 if downloadfile "$URI" "$STATUS" > "$DOWNLOG"; then
f2c0ec8b
DK
1138 msgpass
1139 else
68042532
DK
1140 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/webserverconfig.output"
1141 cat "$DOWNLOG" "$STATUS" >"$OUTPUT" 2>&1 || true
1142 msgfailoutput '' "$OUTPUT"
f2c0ec8b 1143 fi
b0314abb 1144 $NOCHECK || testwebserverlaststatuscode '200'
f2c0ec8b
DK
1145}
1146
fd46d305 1147rewritesourceslist() {
63c71412 1148 local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive" | sed 's# #%20#g')"
af9e40c9 1149 local APTARCHIVE2="copy://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive" | sed 's# #%20#g')"
fd46d305 1150 for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
af9e40c9
DK
1151 sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#$APTARCHIVE2#${1}#" \
1152 -e "s#http://localhost:${APTHTTPPORT}/#${1}#" \
1153 -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"
87d6947d 1404 testoutputequal "$COMPAREFILE" aptcache show "$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
671a55ba
DK
1515testsuccesswithglobalerror() {
1516 local TYPE="$1"
1517 local ERRORS="$2"
1518 shift 2
1519 msggroup "$TYPE"
0440d936
DK
1520 if [ "$1" = '--nomsg' ]; then
1521 shift
1522 else
1523 msgtest 'Test for successful execution of' "$*"
1524 fi
671a55ba 1525 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/${TYPE}.output"
63c71412 1526 if "$@" >"${OUTPUT}" 2>&1; then
4fa34122 1527 if expr match "$1" '^apt.*' >/dev/null; then
e52aad52
DK
1528 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1529 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
671a55ba 1530 elif grep -E "^[${ERRORS}]: " "$OUTPUT" > "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" 2>&1; then
3261271e
DK
1531 if [ "$IGNORE_PTY_NOT_MOUNTED" = '1' ]; then
1532 if echo 'E: Can not write log (Is /dev/pts mounted?) - posix_openpt (2: No such file or directory)' \
1533 | cmp - "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" >/dev/null 2>&1; then
1534 msgpass
1535 else
1536 msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
1537 fi
1538 else
1539 msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
1540 fi
671a55ba
DK
1541 elif [ "$TYPE" = 'testsuccesswithnotice' ]; then
1542 if grep -q -E "^N: " "$OUTPUT"; then
1543 msgpass
1544 else
1545 msgfailoutput 'successful run, but output had no notices' "$OUTPUT" "$@"
1546 fi
4fa34122
DK
1547 else
1548 msgpass
1549 fi
1550 else
1551 msgpass
1552 fi
1553 else
1554 local EXITCODE=$?
e52aad52 1555 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
4fa34122 1556 fi
671a55ba 1557 aptautotest "$TYPE" "$@"
9beb11aa 1558 msggroup
4fa34122 1559}
671a55ba
DK
1560testsuccesswithnotice() {
1561 testsuccesswithglobalerror 'testsuccesswithnotice' 'WE' "$@"
1562}
1563testsuccess() {
1564 testsuccesswithglobalerror 'testsuccess' 'NWE' "$@"
1565}
4fa34122 1566testwarning() {
1501a2c9 1567 msggroup 'testwarning'
4fa34122
DK
1568 if [ "$1" = '--nomsg' ]; then
1569 shift
1570 else
1571 msgtest 'Test for successful execution with warnings of' "$*"
1572 fi
25b86db1 1573 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output"
63c71412 1574 if "$@" >"${OUTPUT}" 2>&1; then
1df24acf 1575 if expr match "$1" '^apt.*' >/dev/null; then
e52aad52
DK
1576 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1577 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1578 elif grep -q -E '^E: ' "$OUTPUT"; then
1579 msgfailoutput 'successful run, but output contains errors' "$OUTPUT" "$@"
4fa34122 1580 elif ! grep -q -E '^W: ' "$OUTPUT"; then
e52aad52 1581 msgfailoutput 'successful run, but output contains no warnings' "$OUTPUT" "$@"
1df24acf
DK
1582 else
1583 msgpass
1584 fi
1585 else
1586 msgpass
1587 fi
0440d936 1588 else
07cb47e7 1589 local EXITCODE=$?
68042532 1590 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
0440d936 1591 fi
4fa34122 1592 aptautotest 'testwarning' "$@"
9beb11aa 1593 msggroup
0440d936 1594}
0440d936 1595testfailure() {
1501a2c9 1596 msggroup 'testfailure'
0440d936
DK
1597 if [ "$1" = '--nomsg' ]; then
1598 shift
1599 else
889b0072 1600 msgtest 'Test for failure in execution of' "$*"
0440d936 1601 fi
03938280 1602 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
63c71412 1603 if "$@" >"${OUTPUT}" 2>&1; then
07cb47e7 1604 local EXITCODE=$?
e52aad52 1605 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
0440d936 1606 else
1df24acf
DK
1607 local EXITCODE=$?
1608 if expr match "$1" '^apt.*' >/dev/null; then
b0d40854
DK
1609 if [ "$1" = 'aptkey' ]; then
1610 if grep -q -E " Can't check signature: " "$OUTPUT" || \
1611 grep -q -E " BAD signature from " "$OUTPUT"; then
1612 msgpass
1613 else
1614 msgfailoutput "run failed with exitcode ${EXITCODE}, but no signature error" "$OUTPUT" "$@"
1615 fi
1df24acf 1616 else
b0d40854
DK
1617 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1618 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1619 elif grep -q -E '==ERROR' "$OUTPUT"; then
1620 msgfailoutput 'compiler sanitizers reported errors' "$OUTPUT" "$@"
1621 elif ! grep -q -E '^E: ' "$OUTPUT"; then
1622 msgfailoutput "run failed with exitcode ${EXITCODE}, but with no errors" "$OUTPUT" "$@"
1623 else
1624 msgpass
1625 fi
1df24acf
DK
1626 fi
1627 else
1628 msgpass
1629 fi
0440d936 1630 fi
ab25bf1f 1631 aptautotest 'testfailure' "$@"
9beb11aa 1632 msggroup
0440d936
DK
1633}
1634
7414af7f
DK
1635testreturnstateequal() {
1636 local STATE="$1"
671a55ba
DK
1637 if [ "$STATE" = 'testsuccesswithglobalerror' ]; then
1638 local STATE="$2"
1639 local TYPE="$3"
7414af7f 1640 shift 3
671a55ba
DK
1641 msggroup "${STATE}equal"
1642 if [ "$1" != '--nomsg' ]; then
1643 local CMP="$1"
1644 shift
1645 testsuccesswithglobalerror "$STATE" "$TYPE" "$@"
1646 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1647 else
1648 local CMP="$2"
1649 shift 2
1650 testsuccesswithglobalerror "$STATE" "$TYPE" "$@"
1651 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1652 fi
1653 else
1654 msggroup "${STATE}equal"
1655 if [ "$2" != '--nomsg' ]; then
1656 local CMP="$2"
1657 shift 2
1658 "$STATE" "$@"
1659 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1660 else
1661 local CMP="$3"
1662 shift 3
1663 "$STATE" --nomsg "$@"
1664 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1665 fi
7414af7f 1666 fi
9beb11aa 1667 msggroup
25b86db1 1668}
7414af7f 1669testsuccessequal() {
671a55ba
DK
1670 # we compare output, so we know perfectly well about N:
1671 testreturnstateequal 'testsuccesswithglobalerror' 'testsuccess' 'WE' "$@"
7414af7f 1672}
25b86db1 1673testwarningequal() {
7414af7f 1674 testreturnstateequal 'testwarning' "$@"
25b86db1
DK
1675}
1676testfailureequal() {
7414af7f 1677 testreturnstateequal 'testfailure' "$@"
25b86db1
DK
1678}
1679
27925d82 1680testfailuremsg() {
1501a2c9 1681 msggroup 'testfailuremsg'
27925d82
DK
1682 local CMP="$1"
1683 shift
1684 testfailure "$@"
1685 msgtest 'Check that the output of the previous failed command has expected' 'failures and warnings'
68042532 1686 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailuremsg.comparefile"
002b1bc4 1687 grep '^\(W\|E\|N\):' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output" > "$COMPAREFILE" 2>&1 || true
22ac12b2 1688 testoutputequal "$COMPAREFILE" echo "$CMP"
9beb11aa 1689 msggroup
27925d82 1690}
f18f2338
DK
1691testwarningmsg() {
1692 msggroup 'testwarningmsg'
1693 local CMP="$1"
1694 shift
1695 testwarning "$@"
1696 msgtest 'Check that the output of the previous warned command has expected' 'warnings'
1697 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarningmsg.comparefile"
002b1bc4 1698 grep '^\(W\|E\|N\):' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output" > "$COMPAREFILE" 2>&1 || true
f18f2338
DK
1699 testoutputequal "$COMPAREFILE" echo "$CMP"
1700 msggroup
1701}
25b86db1 1702
de81b2e2 1703testfilestats() {
1501a2c9 1704 msggroup 'testfilestats'
de81b2e2
DK
1705 msgtest "Test that file $1 has $2 $3" "$4"
1706 if [ "$4" "$3" "$(stat --format "$2" "$1")" ]; then
5684f71f
DK
1707 msgpass
1708 else
68042532
DK
1709 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfilestats.output"
1710 {
1711 ls -ld "$1" || true
1712 echo -n "stat(1) reports for $2: "
1713 stat --format "$2" "$1" || true
1714 } >"$OUTPUT" 2>&1
1715 msgfailoutput '' "$OUTPUT"
5684f71f 1716 fi
9beb11aa 1717 msggroup
5684f71f 1718}
de81b2e2 1719testaccessrights() {
1501a2c9 1720 msggroup 'testaccessrights'
de81b2e2 1721 testfilestats "$1" '%a' '=' "$2"
9beb11aa 1722 msggroup
de81b2e2 1723}
5684f71f 1724
0d58c26a 1725testwebserverlaststatuscode() {
1501a2c9 1726 msggroup 'testwebserverlaststatuscode'
0d58c26a 1727 local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
03aa0847 1728 local STATUS='downloaded/webserverstatus-statusfile.log'
0d58c26a
DK
1729 rm -f "$DOWNLOG" "$STATUS"
1730 msgtest 'Test last status code from the webserver was' "$1"
6c0765c0 1731 if downloadfile "http://localhost:${APTHTTPPORT}/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG" && [ "$(cat "$STATUS")" = "$1" ]; then
0d58c26a
DK
1732 msgpass
1733 else
68042532
DK
1734 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwebserverlaststatuscode.output"
1735 {
1736 if [ -n "$2" ]; then
1737 shift
1738 echo >&2 '#### Additionally provided output files contain:'
1739 cat >&2 "$@"
1740 fi
1741 echo >&2 '#### Download log of the status code:'
1742 cat >&2 "$DOWNLOG"
1743 } >"$OUTPUT" 2>&1
1744 msgfailoutput "Status was $(cat "$STATUS")" "$OUTPUT"
0d58c26a 1745 fi
9beb11aa 1746 msggroup
0d58c26a
DK
1747}
1748
89a1aa5d
DK
1749pause() {
1750 echo "STOPPED execution. Press enter to continue"
1751 local IGNORE
1752 read IGNORE
1753}
ab25bf1f 1754
846bc058 1755listcurrentlistsdirectory() {
ba6b79bd
DK
1756 {
1757 find rootdir/var/lib/apt/lists -maxdepth 1 -type d | while read line; do
1758 stat --format '%U:%G:%a:%n' "$line"
1759 done
1760 find rootdir/var/lib/apt/lists -maxdepth 1 \! -type d | while read line; do
1761 stat --format '%U:%G:%a:%s:%y:%n' "$line"
1762 done
1763 } | sort
846bc058
DK
1764}
1765
3a8776a3 1766### convenience hacks ###
8fe964f1
DK
1767mkdir() {
1768 # creating some directories by hand is a tedious task, so make it look simple
6aef1942
DK
1769 local PARAMS="$*"
1770 if [ "$PARAMS" != "${PARAMS#*rootdir/var/lib/apt/lists}" ]; then
8fe964f1
DK
1771 # only the last directory created by mkdir is effected by the -m !
1772 command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt"
1773 command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"
1774 command mkdir -m 700 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
1775 touch "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/lock"
1776 if [ "$(id -u)" = '0' ]; then
1777 chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
1778 fi
1779 else
1780 command mkdir "$@"
1781 fi
1782}
1783
ab25bf1f
DK
1784### The following tests are run by most test methods automatically to check
1785### general things about commands executed without writing the test every time.
1786
1787aptautotest() {
1788 local TESTCALL="$1"
1789 local CMD="$2"
1790 local FIRSTOPT="$3"
63c71412 1791 local AUTOTEST="aptautotest_$(echo "${CMD##*/}_${FIRSTOPT}" | tr -d '-')"
ab25bf1f
DK
1792 if command -v $AUTOTEST >/dev/null; then
1793 shift 3
1794 # save and restore the *.output files from other tests
1795 # as we might otherwise override them in these automatic tests
63c71412
DK
1796 rm -rf "${TMPWORKINGDIRECTORY}/rootdir/tmp-before"
1797 mv "${TMPWORKINGDIRECTORY}/rootdir/tmp" "${TMPWORKINGDIRECTORY}/rootdir/tmp-before"
1798 mkdir "${TMPWORKINGDIRECTORY}/rootdir/tmp"
ab25bf1f 1799 $AUTOTEST "$TESTCALL" "$@"
63c71412
DK
1800 rm -rf "${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest"
1801 mv "${TMPWORKINGDIRECTORY}/rootdir/tmp" "${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest"
1802 mv "${TMPWORKINGDIRECTORY}/rootdir/tmp-before" "${TMPWORKINGDIRECTORY}/rootdir/tmp"
ab25bf1f
DK
1803 fi
1804}
1805
1806aptautotest_aptget_update() {
59148d96
DK
1807 local TESTCALL="$1"
1808 while [ -n "$2" ]; do
1809 if [ "$2" = '--print-uris' ]; then return; fi # simulation mode
1810 shift
1811 done
ab25bf1f 1812 if ! test -d "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"; then return; fi
4bb006d1
DK
1813 testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
1814 testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
ab25bf1f 1815 # all copied files are properly chmodded
63c71412
DK
1816 local backupIFS="$IFS"
1817 IFS="$(printf "\n\b")"
146f7715 1818 for file in $(find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" -type f ! -name 'lock'); do
4bb006d1 1819 testfilestats "$file" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:644"
ab25bf1f 1820 done
63c71412 1821 IFS="$backupIFS"
59148d96 1822 if [ "$TESTCALL" = 'testsuccess' ]; then
146f7715
DK
1823 # failure cases can retain partial files and such
1824 testempty find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" -mindepth 1 ! \( -name 'lock' -o -name '*.FAILED' \)
1825 fi
ab25bf1f
DK
1826}
1827aptautotest_apt_update() { aptautotest_aptget_update "$@"; }
d84da499 1828aptautotest_aptcdrom_add() { aptautotest_aptget_update "$@"; }
016bea82
DK
1829
1830testaptautotestnodpkgwarning() {
1831 local TESTCALL="$1"
1832 while [ -n "$2" ]; do
6bf93605
DK
1833 if expr match "$2" '^-[a-z]*s' >/dev/null 2>&1; then return; fi # simulation mode
1834 if expr match "$2" '^-dy\?' >/dev/null 2>&1; then return; fi # download-only mode
016bea82
DK
1835 shift
1836 done
1837 testfailure grep '^dpkg: warning:.*ignor.*' "${TMPWORKINGDIRECTORY}/rootdir/tmp-before/${TESTCALL}.output"
1838}
1839
1840aptautotest_aptget_install() { testaptautotestnodpkgwarning "$@"; }
1841aptautotest_aptget_remove() { testaptautotestnodpkgwarning "$@"; }
1842aptautotest_aptget_purge() { testaptautotestnodpkgwarning "$@"; }
1843aptautotest_apt_install() { testaptautotestnodpkgwarning "$@"; }
1844aptautotest_apt_remove() { testaptautotestnodpkgwarning "$@"; }
1845aptautotest_apt_purge() { testaptautotestnodpkgwarning "$@"; }