]> git.saurik.com Git - apt.git/blob - test/integration/framework
51c7b8cae9165a88f706862c7d2b94b2286df292
[apt.git] / test / integration / framework
1 #!/bin/sh -- # no runable script, just for vi
2
3 EXIT_CODE=0
4
5 # we all like colorful messages
6 if [ "$MSGCOLOR" != 'NO' ]; then
7 if [ ! -t 1 ]; then # but check that we output to a terminal
8 export MSGCOLOR='NO'
9 fi
10 fi
11
12
13 if [ "$MSGCOLOR" != 'NO' ]; then
14 CERROR="\033[1;31m" # red
15 CWARNING="\033[1;33m" # yellow
16 CMSG="\033[1;32m" # green
17 CINFO="\033[1;96m" # light blue
18 CDEBUG="\033[1;94m" # blue
19 CNORMAL="\033[0;39m" # default system console color
20 CDONE="\033[1;32m" # green
21 CPASS="\033[1;32m" # green
22 CFAIL="\033[1;31m" # red
23 CCMD="\033[1;35m" # pink
24 fi
25
26 msgprintf() {
27 local START="$1"
28 local MIDDLE="$2"
29 local END="$3"
30 shift 3
31 if [ -n "$1" ]; then
32 printf "$START " "$1"
33 shift
34 while [ -n "$1" ]; do
35 printf "$MIDDLE " "$(echo "$1" | sed -e 's#^apt\([cfghs]\)#apt-\1#')"
36 shift
37 done
38 fi
39 printf "${END}"
40 }
41 msgdie() { msgprintf "${CERROR}E: %s" '%s' "${CNORMAL}\n" "$@" >&2; exit 1; }
42 msgwarn() { msgprintf "${CWARNING}W: %s" '%s' "${CNORMAL}\n" "$@" >&2; }
43 msgmsg() { msgprintf "${CMSG}%s" '%s' "${CNORMAL}\n" "$@"; }
44 msginfo() { msgprintf "${CINFO}I: %s" '%s' "${CNORMAL}\n" "$@"; }
45 msgdebug() { msgprintf "${CDEBUG}D: %s" '%s' "${CNORMAL}\n" "$@"; }
46 msgdone() { msgprintf "${CDONE}DONE" '%s' "${CNORMAL}\n" "$@"; }
47 msgnwarn() { msgprintf "${CWARNING}W: %s" '%s' "${CNORMAL}" "$@" >&2; }
48 msgnmsg() { msgprintf "${CMSG}%s" '%s' "${CNORMAL}" "$@"; }
49 msgninfo() { msgprintf "${CINFO}I: %s" '%s' "${CNORMAL}" "$@"; }
50 msgndebug() { msgprintf "${CDEBUG}D: %s" '%s' "${CNORMAL}" "$@"; }
51 msgtest() { msgprintf "${CINFO}%s" "${CCMD}%s${CINFO}" "…${CNORMAL} " "$@"; }
52 msgpass() { printf "${CPASS}PASS${CNORMAL}\n"; }
53 msgskip() {
54 if [ $# -gt 0 ]; then printf "${CWARNING}SKIP: $*${CNORMAL}\n" >&2;
55 else printf "${CWARNING}SKIP${CNORMAL}\n" >&2; fi
56 }
57 msgfail() {
58 if [ $# -gt 0 ]; then printf "${CFAIL}FAIL: $*${CNORMAL}\n" >&2;
59 else printf "${CFAIL}FAIL${CNORMAL}\n" >&2; fi
60 if [ -n "$APT_DEBUG_TESTS" ]; then
61 $SHELL
62 fi
63 EXIT_CODE=$((EXIT_CODE+1));
64 }
65
66 # enable / disable Debugging
67 MSGLEVEL=${MSGLEVEL:-3}
68 if [ $MSGLEVEL -le 0 ]; then
69 msgdie() { true; }
70 fi
71 if [ $MSGLEVEL -le 1 ]; then
72 msgwarn() { true; }
73 msgnwarn() { true; }
74 fi
75 if [ $MSGLEVEL -le 2 ]; then
76 msgmsg() { true; }
77 msgnmsg() { true; }
78 msgtest() { true; }
79 msgpass() { printf " ${CPASS}P${CNORMAL}"; }
80 fi
81 if [ $MSGLEVEL -le 3 ]; then
82 msginfo() { true; }
83 msgninfo() { true; }
84 fi
85 if [ $MSGLEVEL -le 4 ]; then
86 msgdebug() { true; }
87 msgndebug() { true; }
88 fi
89 msgdone() {
90 if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
91 [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
92 [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
93 [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
94 [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
95 true;
96 else
97 printf "${CDONE}DONE${CNORMAL}\n";
98 fi
99 }
100 getaptconfig() {
101 if [ -f ./aptconfig.conf ]; then
102 echo "./aptconfig.conf"
103 elif [ -f ../aptconfig.conf ]; then
104 echo "../aptconfig.conf"
105 fi
106 }
107 runapt() {
108 msgdebug "Executing: ${CCMD}$*${CDEBUG} "
109 local CMD="$1"
110 shift
111 case $CMD in
112 sh|aptitude|*/*|command) ;;
113 *) CMD="${BUILDDIRECTORY}/$CMD";;
114 esac
115 MALLOC_PERTURB_=21 MALLOC_CHECK_=2 APT_CONFIG="$(getaptconfig)" LD_LIBRARY_PATH=${LIBRARYPATH} $CMD "$@"
116 }
117 aptconfig() { runapt apt-config "$@"; }
118 aptcache() { runapt apt-cache "$@"; }
119 aptcdrom() { runapt apt-cdrom "$@"; }
120 aptget() { runapt apt-get "$@"; }
121 aptftparchive() { runapt apt-ftparchive "$@"; }
122 aptkey() { runapt apt-key "$@"; }
123 aptmark() { runapt apt-mark "$@"; }
124 aptsortpkgs() { runapt apt-sortpkgs "$@"; }
125 apt() { runapt apt "$@"; }
126 apthelper() { runapt "${APTHELPERBINDIR}/apt-helper" "$@"; }
127 aptwebserver() { runapt "${APTWEBSERVERBINDIR}/aptwebserver" "$@"; }
128 aptitude() { runapt aptitude "$@"; }
129 aptextracttemplates() { runapt apt-extracttemplates "$@"; }
130 aptinternalsolver() { runapt "${APTINTERNALSOLVER}" "$@"; }
131 aptdumpsolver() { runapt "${APTDUMPSOLVER}" "$@"; }
132
133 dpkg() {
134 command dpkg --root=${TMPWORKINGDIRECTORY}/rootdir --force-not-root --force-bad-path --log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log "$@"
135 }
136 dpkgcheckbuilddeps() {
137 command dpkg-checkbuilddeps --admindir=${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg "$@"
138 }
139 gdb() {
140 local CMD="$1"
141 shift
142 runapt command gdb --quiet -ex run "${BUILDDIRECTORY}/$CMD" --args "${BUILDDIRECTORY}/$CMD" "$@"
143 }
144
145 exitwithstatus() {
146 # error if we about to overflow, but ...
147 # "255 failures ought to be enough for everybody"
148 if [ $EXIT_CODE -gt 255 ]; then
149 msgdie "Total failure count $EXIT_CODE too big"
150 fi
151 exit $((EXIT_CODE <= 255 ? EXIT_CODE : 255));
152 }
153
154 shellsetedetector() {
155 local exit_status=$?
156 if [ "$exit_status" != '0' ]; then
157 printf >&2 "${CERROR}E: Looks like the testcases ended prematurely with exitcode: ${exit_status}${CNORMAL}\n"
158 if [ "$EXIT_CODE" = '0' ]; then
159 EXIT_CODE="$exit_status"
160 fi
161 fi
162 }
163
164 addtrap() {
165 if [ "$1" = 'prefix' ]; then
166 CURRENTTRAP="$2 $CURRENTTRAP"
167 else
168 CURRENTTRAP="$CURRENTTRAP $1"
169 fi
170 trap "shellsetedetector; $CURRENTTRAP exitwithstatus;" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
171 }
172
173 setupenvironment() {
174 # privilege dropping and testing doesn't work if /tmp isn't world-writeable (as e.g. with libpam-tmpdir)
175 if [ -n "$TMPDIR" ] && [ "$(id -u)" = '0' ] && [ "$(stat --format '%a' "$TMPDIR")" != '1777' ]; then
176 unset TMPDIR
177 fi
178 TMPWORKINGDIRECTORY=$(mktemp -d)
179 addtrap "cd /; rm -rf $TMPWORKINGDIRECTORY;"
180 msgninfo "Preparing environment for ${CCMD}$(basename $0)${CINFO} in ${TMPWORKINGDIRECTORY}… "
181
182 mkdir -m 700 "${TMPWORKINGDIRECTORY}/downloaded"
183 if [ "$(id -u)" = '0' ]; then
184 # relax permissions so that running as root with user switching works
185 umask 022
186 chmod 711 "$TMPWORKINGDIRECTORY"
187 chown _apt:root "${TMPWORKINGDIRECTORY}/downloaded"
188 fi
189
190 TESTDIRECTORY=$(readlink -f $(dirname $0))
191 # allow overriding the default BUILDDIR location
192 BUILDDIRECTORY=${APT_INTEGRATION_TESTS_BUILD_DIR:-"${TESTDIRECTORY}/../../build/bin"}
193 LIBRARYPATH=${APT_INTEGRATION_TESTS_LIBRARY_PATH:-"${BUILDDIRECTORY}"}
194 METHODSDIR=${APT_INTEGRATION_TESTS_METHODS_DIR:-"${BUILDDIRECTORY}/methods"}
195 APTHELPERBINDIR=${APT_INTEGRATION_TESTS_LIBEXEC_DIR:-"${BUILDDIRECTORY}"}
196 APTWEBSERVERBINDIR=${APT_INTEGRATION_TESTS_WEBSERVER_BIN_DIR:-"${BUILDDIRECTORY}"}
197 APTINTERNALSOLVER=${APT_INTEGRATION_TESTS_INTERNAL_SOLVER:-"${BUILDDIRECTORY}/apt-internal-solver"}
198 APTDUMPSOLVER=${APT_INTEGRATION_TESTS_DUMP_SOLVER:-"${BUILDDIRECTORY}/apt-dump-solver"}
199 test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
200 # -----
201
202 cd $TMPWORKINGDIRECTORY
203 mkdir rootdir aptarchive keys
204 cd rootdir
205 mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d
206 mkdir -p usr/bin var/cache var/lib var/log tmp
207 mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
208 touch var/lib/dpkg/available
209 mkdir -p usr/lib/apt
210 ln -s ${METHODSDIR} usr/lib/apt/methods
211 if [ "$BUILDDIRECTORY" = "$LIBRARYPATH" ]; then
212 mkdir -p usr/lib/apt/solvers
213 ln -s "${BUILDDIRECTORY}/apt-dump-solver" usr/lib/apt/solvers/dump
214 ln -s "${BUILDDIRECTORY}/apt-internal-solver" usr/lib/apt/solvers/apt
215 echo "Dir::Bin::Solvers \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/solvers\";" > etc/apt/apt.conf.d/externalsolver.conf
216 fi
217 # use the autoremove from the BUILDDIRECTORY if its there, otherwise
218 # system
219 if [ -e ${BUILDDIRECTORY}/../../debian/apt.conf.autoremove ]; then
220 ln -s ${BUILDDIRECTORY}/../../debian/apt.conf.autoremove etc/apt/apt.conf.d/01autoremove
221 else
222 ln -s /etc/apt/apt.conf.d/01autoremove etc/apt/apt.conf.d/01autoremove
223 fi
224 cd ..
225 local PACKAGESFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Packages-/' -e 's/^skip-/Packages-/')
226 if [ -f "${TESTDIRECTORY}/${PACKAGESFILE}" ]; then
227 cp "${TESTDIRECTORY}/${PACKAGESFILE}" aptarchive/Packages
228 fi
229 local SOURCESSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Sources-/' -e 's/^skip-/Sources-/')
230 if [ -f "${TESTDIRECTORY}/${SOURCESSFILE}" ]; then
231 cp "${TESTDIRECTORY}/${SOURCESSFILE}" aptarchive/Sources
232 fi
233 cp $(find $TESTDIRECTORY -name '*.pub' -o -name '*.sec') keys/
234 chmod 644 $(find keys -name '*.pub' -o -name '*.sec')
235 ln -s ${TMPWORKINGDIRECTORY}/keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
236
237 echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
238 echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
239 echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
240 echo "Dir::Bin::Methods \"${METHODSDIR}\";" >> aptconfig.conf
241 # store apt-key were we can access it, even if we run it as a different user
242 # destroys coverage reporting though, so just do it for root for now
243 if [ "$(id -u)" = '0' ]; then
244 cp "${BUILDDIRECTORY}/apt-key" "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/"
245 chmod o+rx "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/apt-key"
246 echo "Dir::Bin::apt-key \"${TMPWORKINGDIRECTORY}/rootdir/usr/bin/apt-key\";" >> aptconfig.conf
247 else
248 echo "Dir::Bin::apt-key \"${BUILDDIRECTORY}/apt-key\";" >> aptconfig.conf
249 fi
250 echo "Dir::Bin::dpkg \"fakeroot\";" >> aptconfig.conf
251 echo "DPKG::options:: \"dpkg\";" >> aptconfig.conf
252 echo "DPKG::options:: \"--root=${TMPWORKINGDIRECTORY}/rootdir\";" >> aptconfig.conf
253 echo "DPKG::options:: \"--force-not-root\";" >> aptconfig.conf
254 echo "DPKG::options:: \"--force-bad-path\";" >> aptconfig.conf
255 if ! command dpkg --assert-multi-arch >/dev/null 2>&1; then
256 echo "DPKG::options:: \"--force-architecture\";" >> aptconfig.conf # Added to test multiarch before dpkg is ready for it…
257 fi
258 echo "DPKG::options:: \"--log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log\";" >> aptconfig.conf
259 echo 'quiet::NoUpdate "true";' >> aptconfig.conf
260 echo 'quiet::NoStatistic "true";' >> aptconfig.conf
261 # too distracting for users, but helpful to detect changes
262 echo 'Acquire::Progress::Ignore::ShowErrorText "true";' >> aptconfig.conf
263 # in testcases, it can appear as if localhost has a rotation setup,
264 # hide this as we can't really deal with it properly
265 echo 'Acquire::Failure::ShowIP "false";' >> aptconfig.conf
266
267 cp "${TESTDIRECTORY}/apt.pem" "${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem"
268 if [ "$(id -u)" = '0' ]; then
269 chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem"
270 fi
271 echo "Acquire::https::CaInfo \"${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem\";" > rootdir/etc/apt/apt.conf.d/99https
272 echo "Apt::Cmd::Disable-Script-Warning \"1\";" > rootdir/etc/apt/apt.conf.d/apt-binary
273 configcompression '.' 'gz' #'bz2' 'lzma' 'xz'
274
275 # create some files in /tmp and look at user/group to get what this means
276 TEST_DEFAULT_USER="$USER"
277 if [ "$(uname)" = 'GNU/kFreeBSD' ]; then
278 TEST_DEFAULT_GROUP='root'
279 else
280 TEST_DEFAULT_GROUP="$USER"
281 fi
282
283 # Acquire::AllowInsecureRepositories=false is not yet the default
284 # but we want it to be the default soon
285 configallowinsecurerepositories "false";
286
287 # cleanup the environment a bit
288 # prefer our apt binaries over the system apt binaries
289 export PATH="${BUILDDIRECTORY}:${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
290 export LC_ALL=C.UTF-8
291 unset LANGUAGE APT_CONFIG
292 unset GREP_OPTIONS DEB_BUILD_PROFILES
293
294 msgdone "info"
295 }
296
297 getarchitecture() {
298 if [ "$1" = "native" -o -z "$1" ]; then
299 eval `aptconfig shell ARCH APT::Architecture`
300 if [ -n "$ARCH" ]; then
301 echo $ARCH
302 else
303 dpkg --print-architecture
304 fi
305 else
306 echo $1
307 fi
308 }
309
310 getarchitectures() {
311 aptconfig dump --no-empty --format '%v%n' APT::Architecture APT::Architectures | sort -u | tr '\n' ' '
312 }
313
314 getarchitecturesfromcommalist() {
315 echo "$1" | sed -e 's#,#\n#g' | sed -e "s/^native\$/$(getarchitecture 'native')/"
316 }
317
318 configarchitecture() {
319 {
320 echo "APT::Architecture \"$(getarchitecture $1)\";"
321 while [ -n "$1" ]; do
322 echo "APT::Architectures:: \"$(getarchitecture $1)\";"
323 shift
324 done
325 } >rootdir/etc/apt/apt.conf.d/01multiarch.conf
326 configdpkg
327 }
328
329 configdpkg() {
330 if [ ! -e rootdir/var/lib/dpkg/status ]; then
331 local STATUSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/status-/' -e 's/^skip-/status-/')
332 if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
333 cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
334 else
335 echo -n > rootdir/var/lib/dpkg/status
336 fi
337 fi
338 rm -f rootdir/etc/apt/apt.conf.d/00foreigndpkg
339 if command dpkg --assert-multi-arch >/dev/null 2>&1 ; then
340 local ARCHS="$(getarchitectures)"
341 if echo "$ARCHS" | grep -E -q '[^ ]+ [^ ]+'; then
342 DPKGARCH="$(dpkg --print-architecture)"
343 for ARCH in ${ARCHS}; do
344 if [ "${ARCH}" != "${DPKGARCH}" ]; then
345 if ! dpkg --add-architecture ${ARCH} >/dev/null 2>&1; then
346 # old-style used e.g. in Ubuntu-P – and as it seems travis
347 echo "DPKG::options:: \"--foreign-architecture\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
348 echo "DPKG::options:: \"${ARCH}\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
349 fi
350 fi
351 done
352 if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
353 # dpkg doesn't really check the version as long as it is fully installed,
354 # but just to be sure we choose one above the required version
355 insertinstalledpackage 'dpkg' "all" '1.16.2+fake'
356 fi
357 fi
358 fi
359 }
360
361 configallowinsecurerepositories() {
362 echo "Acquire::AllowInsecureRepositories \"$1\";" > rootdir/etc/apt/apt.conf.d/allow-insecure-repositories.conf
363
364 }
365
366 configcompression() {
367 while [ -n "$1" ]; do
368 case "$1" in
369 '.') printf ".\t.\tcat\n";;
370 'gz') printf "gzip\tgz\tgzip\n";;
371 'bz2') printf "bzip2\tbz2\tbzip2\n";;
372 'lzma') printf "lzma\tlzma\txz --format=lzma\n";;
373 'xz') printf "xz\txz\txz\n";;
374 *) printf "$1\t$1\t$1\n";;
375 esac
376 shift
377 done > ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf
378 }
379
380 forcecompressor() {
381 COMPRESSOR="$1"
382 COMPRESSOR_CMD="$1"
383 case $COMPRESSOR in
384 gzip) COMPRESS='gz';;
385 bzip2) COMPRESS='bz2';;
386 lzma) COMPRESS='lzma';;
387 xz) COMPRESS='xz';;
388 *) msgdie "Compressor $COMPRESSOR is unknown to framework, so can't be forced by forcecompressor!";;
389 esac
390 local CONFFILE="${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/00force-compressor"
391 echo "Acquire::CompressionTypes::Order { \"${COMPRESS}\"; };
392 Dir::Bin::uncompressed \"/does/not/exist\";
393 Dir::Bin::gzip \"/does/not/exist\";
394 Dir::Bin::bzip2 \"/does/not/exist\";
395 Dir::Bin::lzma \"/does/not/exist\";
396 Dir::Bin::xz \"/does/not/exist\";" > "$CONFFILE"
397 if [ -e "/bin/${COMPRESSOR}" ]; then
398 echo "Dir::Bin::${COMPRESSOR} \"/bin/${COMPRESSOR}\";" >> "$CONFFILE"
399 elif [ -e "/usr/bin/${COMPRESSOR}" ]; then
400 echo "Dir::Bin::${COMPRESSOR} \"/usr/bin/${COMPRESSOR}\";" >> "$CONFFILE"
401 elif [ "${COMPRESSOR}" = 'lzma' ]; then
402 echo 'Dir::Bin::xz "/usr/bin/xz";' >> "$CONFFILE"
403 COMPRESSOR_CMD='xz --format=lzma'
404 else
405 msgtest 'Test for availability of compressor' "${COMPRESSOR}"
406 msgfail
407 fi
408 }
409
410 setupsimplenativepackage() {
411 local NAME="$1"
412 local ARCH="$2"
413 local VERSION="$3"
414 local RELEASE="${4:-unstable}"
415 local DEPENDENCIES="$5"
416 local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
417 If you find such a package installed on your system,
418 something went horribly wrong! They are autogenerated
419 und used only by testcases and surf no other propose…"}"
420
421 local SECTION="${7:-others}"
422 local DISTSECTION
423 if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
424 DISTSECTION="main"
425 else
426 DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
427 fi
428 local BUILDDIR=incoming/${NAME}-${VERSION}
429 mkdir -p ${BUILDDIR}/debian/source
430 cd ${BUILDDIR}
431 echo "* most suckless software product ever" > FEATURES
432 test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright
433 test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
434
435 * Initial release
436
437 -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog
438 test -e debian/control || echo "Source: $NAME
439 Section: $SECTION
440 Priority: optional
441 Maintainer: Joe Sixpack <joe@example.org>
442 Build-Depends: debhelper (>= 7)
443 Standards-Version: 3.9.1
444
445 Package: $NAME" > debian/control
446 if [ "$ARCH" = 'all' ]; then
447 echo "Architecture: all" >> debian/control
448 else
449 echo "Architecture: any" >> debian/control
450 fi
451 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
452 echo "Description: $DESCRIPTION" >> debian/control
453
454 test -e debian/compat || echo "7" > debian/compat
455 test -e debian/source/format || echo "3.0 (native)" > debian/source/format
456 test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
457 cd - > /dev/null
458 }
459
460 buildsimplenativepackage() {
461 local NAME="$1"
462 local ARCH="$2"
463 local VERSION="$3"
464 local RELEASE="${4:-unstable}"
465 local DEPENDENCIES="$5"
466 local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
467 If you find such a package installed on your system,
468 something went horribly wrong! They are autogenerated
469 und used only by testcases and surf no other propose…"}"
470
471 local SECTION="${7:-others}"
472 local PRIORITY="${8:-optional}"
473 local FILE_TREE="$9"
474 local COMPRESS_TYPE="${10:-gzip}"
475 local DISTSECTION
476 if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
477 DISTSECTION="main"
478 else
479 DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
480 fi
481 local BUILDDIR=${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}
482
483 msgninfo "Build package ${NAME} in ${VERSION} for ${RELEASE} in ${DISTSECTION}… "
484 mkdir -p $BUILDDIR/debian/source
485 echo "* most suckless software product ever" > ${BUILDDIR}/FEATURES
486 echo "#!/bin/sh
487 echo '$NAME says \"Hello!\"'" > ${BUILDDIR}/${NAME}
488
489 echo "Copyleft by Joe Sixpack $(date +%Y)" > ${BUILDDIR}/debian/copyright
490 echo "$NAME ($VERSION) $RELEASE; urgency=low
491
492 * Initial release
493
494 -- Joe Sixpack <joe@example.org> $(date -R)" > ${BUILDDIR}/debian/changelog
495 echo "Source: $NAME
496 Section: $SECTION
497 Priority: $PRIORITY
498 Maintainer: Joe Sixpack <joe@example.org>
499 Standards-Version: 3.9.3" > ${BUILDDIR}/debian/control
500 local BUILDDEPS="$(echo "$DEPENDENCIES" | grep '^Build-')"
501 test -z "$BUILDDEPS" || echo "$BUILDDEPS" >> ${BUILDDIR}/debian/control
502 echo "
503 Package: $NAME" >> ${BUILDDIR}/debian/control
504
505 if [ "$ARCH" = 'all' ]; then
506 echo "Architecture: all" >> ${BUILDDIR}/debian/control
507 else
508 echo "Architecture: any" >> ${BUILDDIR}/debian/control
509 fi
510 local DEPS="$(echo "$DEPENDENCIES" | grep -v '^Build-')"
511 test -z "$DEPS" || echo "$DEPS" >> ${BUILDDIR}/debian/control
512 echo "Description: $DESCRIPTION" >> ${BUILDDIR}/debian/control
513
514 echo '3.0 (native)' > ${BUILDDIR}/debian/source/format
515 (cd ${BUILDDIR}/..; dpkg-source -b ${NAME}-${VERSION} 2>&1) | sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' \
516 | while read SRC; do
517 echo "pool/${SRC}" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist
518 # if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
519 # aptkey --keyring ./keys/joesixpack.pub --secret-keyring ./keys/joesixpack.sec --quiet --readonly \
520 # adv --yes --default-key 'Joe Sixpack' \
521 # --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
522 # mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
523 # fi
524 done
525
526 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
527 rm -rf ${BUILDDIR}/debian/tmp
528 mkdir -p ${BUILDDIR}/debian/tmp/DEBIAN ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME} ${BUILDDIR}/debian/tmp/usr/bin
529 cp ${BUILDDIR}/debian/copyright ${BUILDDIR}/debian/changelog ${BUILDDIR}/FEATURES ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}
530 cp ${BUILDDIR}/${NAME} ${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}
531 if [ -n "$FILE_TREE" ]; then
532 cp -ar "$FILE_TREE" ${BUILDDIR}/debian/tmp
533 fi
534
535 (cd ${BUILDDIR}; dpkg-gencontrol -DArchitecture=$arch)
536 (cd ${BUILDDIR}/debian/tmp; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
537 local LOG="${BUILDDIR}/../${NAME}_${VERSION}_${arch}.dpkg-deb.log"
538 # ensure the right permissions as dpkg-deb ensists
539 chmod 755 ${BUILDDIR}/debian/tmp/DEBIAN
540 if ! dpkg-deb -Z${COMPRESS_TYPE} --build ${BUILDDIR}/debian/tmp ${BUILDDIR}/.. >$LOG 2>&1; then
541 cat $LOG
542 false
543 fi
544 rm $LOG
545 echo "pool/${NAME}_${VERSION}_${arch}.deb" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist
546 done
547
548 mkdir -p ${BUILDDIR}/../${NAME}_${VERSION}
549 cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}/
550 cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}.changelog
551 rm -rf "${BUILDDIR}"
552 msgdone "info"
553 }
554
555 buildpackage() {
556 local BUILDDIR=$1
557 local RELEASE=$2
558 local SECTION=$3
559 local ARCH=$(getarchitecture $4)
560 local PKGNAME="$(echo "$BUILDDIR" | grep -o '[^/]*$')"
561 local BUILDLOG="$(readlink -f "${BUILDDIR}/../${PKGNAME}_${RELEASE}_${SECTION}.dpkg-bp.log")"
562 msgninfo "Build package ${PKGNAME} for ${RELEASE} in ${SECTION}… "
563 cd $BUILDDIR
564 if [ "$ARCH" = "all" ]; then
565 ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
566 fi
567 if ! dpkg-buildpackage -uc -us -a$ARCH >$BUILDLOG 2>&1 ; then
568 cat $BUILDLOG
569 false
570 fi
571 local PKGS="$(grep '^dpkg-deb: building package' $BUILDLOG | cut -d'/' -f 2 | sed -e "s#'\.##")"
572 local SRCS="$(grep '^dpkg-source: info: building' $BUILDLOG | grep -o '[a-z0-9._+~-]*$')"
573 cd - > /dev/null
574 for PKG in $PKGS; do
575 echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
576 done
577 for SRC in $SRCS; do
578 echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
579 done
580 msgdone "info"
581 }
582
583 buildaptarchive() {
584 if [ -d incoming ]; then
585 buildaptarchivefromincoming "$@"
586 else
587 buildaptarchivefromfiles "$@"
588 fi
589 }
590
591 createaptftparchiveconfig() {
592 local COMPRESSORS="$(cut -d' ' -f 1 ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | tr '\n' ' ')"
593 COMPRESSORS="${COMPRESSORS%* }"
594 local ARCHS="$(find pool/ -name '*.deb' | grep -oE '_[a-z0-9-]+\.deb$' | sort | uniq | sed -e '/^_all.deb$/ d' -e 's#^_\([a-z0-9-]*\)\.deb$#\1#' | tr '\n' ' ')"
595 if [ -z "$ARCHS" ]; then
596 # the pool is empty, so we will operate on faked packages - let us use the configured archs
597 ARCHS="$(getarchitectures)"
598 fi
599 echo -n 'Dir {
600 ArchiveDir "' >> ftparchive.conf
601 echo -n $(readlink -f .) >> ftparchive.conf
602 echo -n '";
603 CacheDir "' >> ftparchive.conf
604 echo -n $(readlink -f ..) >> ftparchive.conf
605 echo -n '";
606 FileListDir "' >> ftparchive.conf
607 echo -n $(readlink -f pool/) >> ftparchive.conf
608 echo -n '";
609 };
610 Default {
611 Packages::Compress "'"$COMPRESSORS"'";
612 Sources::Compress "'"$COMPRESSORS"'";
613 Contents::Compress "'"$COMPRESSORS"'";
614 Translation::Compress "'"$COMPRESSORS"'";
615 LongDescription "false";
616 };
617 TreeDefault {
618 Directory "pool/";
619 SrcDirectory "pool/";
620 };
621 APT {
622 FTPArchive {
623 Release {
624 Origin "joesixpack";
625 Label "apttestcases";
626 Suite "unstable";
627 Description "repository with dummy packages";
628 Architectures "' >> ftparchive.conf
629 echo -n "$ARCHS" >> ftparchive.conf
630 echo 'source";
631 };
632 };
633 };' >> ftparchive.conf
634 for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
635 echo -n 'tree "dists/' >> ftparchive.conf
636 echo -n "$DIST" >> ftparchive.conf
637 echo -n '" {
638 Architectures "' >> ftparchive.conf
639 echo -n "$ARCHS" >> ftparchive.conf
640 echo -n 'source";
641 FileList "' >> ftparchive.conf
642 echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
643 echo -n '";
644 SourceFileList "' >> ftparchive.conf
645 echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
646 echo -n '";
647 Sections "' >> ftparchive.conf
648 echo -n "$(find ./pool/ -maxdepth 1 -name "${DIST}.*.pkglist" -type f | cut -d'/' -f 3 | cut -d'.' -f 2 | sort | uniq | tr '\n' ' ')" >> ftparchive.conf
649 echo '";
650 };' >> ftparchive.conf
651 done
652 }
653
654 buildaptftparchivedirectorystructure() {
655 local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
656 for DIST in $DISTS; do
657 local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
658 for SECTION in $SECTIONS; do
659 local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
660 for ARCH in $ARCHS; do
661 mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
662 done
663 mkdir -p dists/${DIST}/${SECTION}/source
664 mkdir -p dists/${DIST}/${SECTION}/i18n
665 done
666 done
667 }
668
669 insertpackage() {
670 local RELEASES="$1"
671 local NAME="$2"
672 local ARCH="$3"
673 local VERSION="$4"
674 local DEPENDENCIES="$5"
675 local PRIORITY="${6:-optional}"
676 local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASES}
677 If you find such a package installed on your system,
678 something went horribly wrong! They are autogenerated
679 und used only by testcases and surf no other propose…"}"
680 local ARCHS=""
681 for RELEASE in $(printf '%s' "$RELEASES" | tr ',' '\n'); do
682 if [ "$RELEASE" = 'installed' ]; then
683 insertinstalledpackage "$2" "$3" "$4" "$5" "$6" "$7"
684 continue
685 fi
686 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
687 if [ "$arch" = 'all' -o "$arch" = 'none' ]; then
688 ARCHS="$(getarchitectures)"
689 else
690 ARCHS="$arch"
691 fi
692 for BUILDARCH in $ARCHS; do
693 local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
694 mkdir -p $PPATH
695 local FILE="${PPATH}/Packages"
696 echo "Package: $NAME
697 Priority: $PRIORITY
698 Section: other
699 Installed-Size: 42
700 Maintainer: Joe Sixpack <joe@example.org>" >> $FILE
701 test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
702 echo "Version: $VERSION
703 Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
704 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
705 echo "Description: $(printf '%s' "$DESCRIPTION" | head -n 1)" >> $FILE
706 echo "Description-md5: $(printf '%s' "$DESCRIPTION" | md5sum | cut -d' ' -f 1)" >> $FILE
707 echo >> $FILE
708 done
709 done
710 mkdir -p aptarchive/dists/${RELEASE}/main/source aptarchive/dists/${RELEASE}/main/i18n
711 touch aptarchive/dists/${RELEASE}/main/source/Sources
712 echo "Package: $NAME
713 Description-md5: $(printf '%s' "$DESCRIPTION" | md5sum | cut -d' ' -f 1)
714 Description-en: $DESCRIPTION
715 " >> aptarchive/dists/${RELEASE}/main/i18n/Translation-en
716 done
717 }
718
719 insertsource() {
720 local RELEASE="$1"
721 local NAME="$2"
722 local ARCH="$3"
723 local VERSION="$4"
724 local DEPENDENCIES="$5"
725 local ARCHS=""
726 local SPATH="aptarchive/dists/${RELEASE}/main/source"
727 mkdir -p $SPATH
728 local FILE="${SPATH}/Sources"
729 echo "Package: $NAME
730 Binary: $NAME
731 Version: $VERSION
732 Maintainer: Joe Sixpack <joe@example.org>
733 Architecture: $ARCH" >> $FILE
734 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
735 echo "Files:
736 d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.dsc
737 d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.tar.gz
738 " >> $FILE
739 }
740
741 insertinstalledpackage() {
742 local NAME="$1"
743 local ARCH="$2"
744 local VERSION="$3"
745 local DEPENDENCIES="$4"
746 local PRIORITY="${5:-optional}"
747 local STATUS="${6:-install ok installed}"
748 local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/installed
749 If you find such a package installed on your system,
750 something went horribly wrong! They are autogenerated
751 und used only by testcases and surf no other propose…"}"
752
753 local FILE='rootdir/var/lib/dpkg/status'
754 local INFO='rootdir/var/lib/dpkg/info'
755 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
756 echo "Package: $NAME
757 Status: $STATUS
758 Priority: $PRIORITY
759 Section: other
760 Installed-Size: 42
761 Maintainer: Joe Sixpack <joe@example.org>
762 Version: $VERSION" >> $FILE
763 test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
764 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
765 echo "Description: $DESCRIPTION" >> $FILE
766 echo >> $FILE
767 if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
768 echo -n > ${INFO}/${NAME}:${arch}.list
769 else
770 echo -n > ${INFO}/${NAME}.list
771 fi
772 done
773 }
774
775
776 buildaptarchivefromincoming() {
777 msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
778 cd aptarchive
779 [ -e pool ] || ln -s ../incoming pool
780 [ -e ftparchive.conf ] || createaptftparchiveconfig
781 [ -e dists ] || buildaptftparchivedirectorystructure
782 msgninfo "\tGenerate Packages, Sources and Contents files… "
783 aptftparchive -qq generate ftparchive.conf
784 cd - > /dev/null
785 msgdone "info"
786 generatereleasefiles "$@"
787 }
788
789 buildaptarchivefromfiles() {
790 msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
791 find aptarchive -name 'Packages' -o -name 'Sources' -o -name 'Translation-*' | while read line; do
792 msgninfo "\t${line} file… "
793 compressfile "$line" "$1"
794 msgdone "info"
795 done
796 generatereleasefiles "$@"
797 }
798
799 compressfile() {
800 cat ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | while read compressor extension command; do
801 if [ "$compressor" = '.' ]; then
802 if [ -n "$2" ]; then
803 touch -d "$2" "$1"
804 fi
805 continue
806 fi
807 cat "$1" | $command > "${1}.${extension}"
808 if [ -n "$2" ]; then
809 touch -d "$2" "${1}.${extension}"
810 fi
811 done
812 }
813
814 # can be overridden by testcases for their pleasure
815 getcodenamefromsuite() {
816 case "$1" in
817 unstable) echo 'sid';;
818 *) echo -n "$1";;
819 esac
820 }
821 getreleaseversionfromsuite() { true; }
822 getlabelfromsuite() { true; }
823
824 generatereleasefiles() {
825 # $1 is the Date header and $2 is the ValidUntil header to be set
826 # both should be given in notation date/touch can understand
827 msgninfo "\tGenerate Release files… "
828 if [ -e aptarchive/dists ]; then
829 for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
830 local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
831 local CODENAME="$(getcodenamefromsuite $SUITE)"
832 local VERSION="$(getreleaseversionfromsuite $SUITE)"
833 local LABEL="$(getlabelfromsuite $SUITE)"
834 if [ -n "$VERSION" ]; then
835 VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
836 fi
837 if [ -n "$LABEL" ]; then
838 LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
839 fi
840 aptftparchive -qq release $dir \
841 -o APT::FTPArchive::Release::Suite="${SUITE}" \
842 -o APT::FTPArchive::Release::Codename="${CODENAME}" \
843 ${LABEL} \
844 ${VERSION} \
845 | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
846 if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
847 sed -i '/^Date: / a\
848 NotAutomatic: yes' $dir/Release
849 fi
850 if [ -n "$1" -a "$1" != "now" ]; then
851 sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
852 fi
853 if [ -n "$2" ]; then
854 sed -i "/^Date: / a\
855 Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
856 fi
857 done
858 else
859 aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
860 fi
861 if [ -n "$1" -a "$1" != "now" ]; then
862 for release in $(find ./aptarchive -name 'Release'); do
863 touch -d "$1" $release
864 done
865 fi
866 msgdone "info"
867 }
868
869 setupdistsaptarchive() {
870 local APTARCHIVE=$(readlink -f ./aptarchive)
871 rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
872 rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
873 for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
874 SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
875 msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
876 echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
877 echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
878 msgdone "info"
879 done
880 }
881
882 setupflataptarchive() {
883 local APTARCHIVE=$(readlink -f ./aptarchive)
884 if [ -f ${APTARCHIVE}/Packages ]; then
885 msgninfo "\tadd deb sources.list line… "
886 echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
887 msgdone "info"
888 else
889 rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
890 fi
891 if [ -f ${APTARCHIVE}/Sources ]; then
892 msgninfo "\tadd deb-src sources.list line… "
893 echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
894 msgdone "info"
895 else
896 rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
897 fi
898 }
899
900 setupaptarchive() {
901 local NOUPDATE=0
902 if [ "$1" = '--no-update' ]; then
903 NOUPDATE=1
904 shift
905 fi
906 buildaptarchive "$@"
907 if [ -e aptarchive/dists ]; then
908 setupdistsaptarchive
909 else
910 setupflataptarchive
911 fi
912 signreleasefiles 'Joe Sixpack'
913 if [ "1" != "$NOUPDATE" ]; then
914 testsuccess aptget update -o Debug::pkgAcquire::Worker=true -o Debug::Acquire::gpgv=true
915 fi
916 }
917
918 signreleasefiles() {
919 local SIGNER="${1:-Joe Sixpack}"
920 local REPODIR="${2:-aptarchive}"
921 local KEY="keys/$(echo "$SIGNER" | tr 'A-Z' 'a-z' | sed 's# ##g')"
922 local GPG="aptkey --quiet --keyring ${KEY}.pub --secret-keyring ${KEY}.sec --readonly adv --batch --yes"
923 msgninfo "\tSign archive with $SIGNER key $KEY… "
924 local REXKEY='keys/rexexpired'
925 local SECEXPIREBAK="${REXKEY}.sec.bak"
926 local PUBEXPIREBAK="${REXKEY}.pub.bak"
927 if [ "${SIGNER}" = 'Rex Expired' ]; then
928 # the key is expired, so gpg doesn't allow to sign with and the --faked-system-time
929 # option doesn't exist anymore (and using faketime would add a new obscure dependency)
930 # therefore we 'temporary' make the key not expired and restore a backup after signing
931 cp ${REXKEY}.sec $SECEXPIREBAK
932 cp ${REXKEY}.pub $PUBEXPIREBAK
933 local SECUNEXPIRED="${REXKEY}.sec.unexpired"
934 local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
935 if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
936 cp $SECUNEXPIRED ${REXKEY}.sec
937 cp $PUBUNEXPIRED ${REXKEY}.pub
938 else
939 if ! printf "expire\n1w\nsave\n" | $GPG --default-key "$SIGNER" --command-fd 0 --edit-key "${SIGNER}" >setexpire.gpg 2>&1; then
940 cat setexpire.gpg
941 exit 1
942 fi
943 cp ${REXKEY}.sec $SECUNEXPIRED
944 cp ${REXKEY}.pub $PUBUNEXPIRED
945 fi
946 fi
947 for RELEASE in $(find ${REPODIR}/ -name Release); do
948 $GPG --default-key "$SIGNER" --armor --detach-sign --sign --output ${RELEASE}.gpg ${RELEASE}
949 local INRELEASE="$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')"
950 $GPG --default-key "$SIGNER" --clearsign --output $INRELEASE $RELEASE
951 # we might have set a specific date for the Release file, so copy it
952 touch -d "$(stat --format "%y" ${RELEASE})" ${RELEASE}.gpg ${INRELEASE}
953 done
954 if [ -f "$SECEXPIREBAK" ] && [ -f "$PUBEXPIREBAK" ]; then
955 mv -f $SECEXPIREBAK ${REXKEY}.sec
956 mv -f $PUBEXPIREBAK ${REXKEY}.pub
957 fi
958 msgdone "info"
959 }
960
961 webserverconfig() {
962 local NOCHECK=false
963 if [ "$1" = '--no-check' ]; then
964 NOCHECK=true
965 shift
966 fi
967 local DOWNLOG='rootdir/tmp/download-testfile.log'
968 local STATUS='downloaded/webserverconfig.status'
969 rm -f "$STATUS" "$DOWNLOG"
970 local URI
971 if [ -n "$2" ]; then
972 msgtest "Set webserver config option '${1}' to" "$2"
973 URI="http://localhost:8080/_config/set/${1}/${2}"
974 else
975 msgtest 'Clear webserver config option' "${1}"
976 URI="http://localhost:8080/_config/clear/${1}"
977 fi
978 if downloadfile "$URI" "$STATUS" > "$DOWNLOG"; then
979 msgpass
980 else
981 cat "$DOWNLOG" "$STATUS" || true
982 msgfail
983 fi
984 $NOCHECK || testwebserverlaststatuscode '200'
985 }
986
987 rewritesourceslist() {
988 local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive")"
989 for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
990 sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#http://localhost:8080/#${1}#" -e "s#https://localhost:4433/#${1}#"
991 done
992 }
993
994 # wait for up to 10s for a pid file to appear to avoid possible race
995 # when a helper is started and dosn't write the PID quick enough
996 waitforpidfile() {
997 local PIDFILE="$1"
998 for i in $(seq 10); do
999 if test -s "$PIDFILE"; then
1000 return 0
1001 fi
1002 sleep 1
1003 done
1004 msgdie "waiting for $PIDFILE failed"
1005 return 1
1006 }
1007
1008 changetowebserver() {
1009 if [ "$1" != '--no-rewrite' ]; then
1010 rewritesourceslist 'http://localhost:8080/'
1011 else
1012 shift
1013 fi
1014 if test -x ${APTWEBSERVERBINDIR}/aptwebserver; then
1015 cd aptarchive
1016 local LOG="webserver.log"
1017 if ! aptwebserver -o aptwebserver::fork=1 "$@" >$LOG 2>&1 ; then
1018 cat $LOG
1019 false
1020 fi
1021 waitforpidfile aptwebserver.pid
1022 local PID="$(cat aptwebserver.pid)"
1023 if [ -z "$PID" ]; then
1024 msgdie 'Could not fork aptwebserver successfully'
1025 fi
1026 addtrap "kill $PID;"
1027 cd - > /dev/null
1028 else
1029 msgdie 'You have to build aptwerbserver or install a webserver'
1030 fi
1031 }
1032
1033 changetohttpswebserver() {
1034 if ! which stunnel4 >/dev/null; then
1035 msgdie 'You need to install stunnel4 for https testcases'
1036 fi
1037 if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
1038 changetowebserver --no-rewrite "$@"
1039 fi
1040 echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
1041 cert = ${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem
1042 output = /dev/null
1043
1044 [https]
1045 accept = 4433
1046 connect = 8080
1047 " > ${TMPWORKINGDIRECTORY}/stunnel.conf
1048 stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
1049 waitforpidfile "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid"
1050 local PID="$(cat ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid)"
1051 if [ -z "$PID" ]; then
1052 msgdie 'Could not fork stunnel4 successfully'
1053 fi
1054 addtrap 'prefix' "kill ${PID};"
1055 rewritesourceslist 'https://localhost:4433/'
1056 }
1057
1058 changetocdrom() {
1059 mkdir -p rootdir/media/cdrom/.disk
1060 local CD="$(readlink -f rootdir/media/cdrom)"
1061 echo "acquire::cdrom::mount \"${CD}\";
1062 acquire::cdrom::${CD}/::mount \"mv ${CD}-unmounted ${CD}\";
1063 acquire::cdrom::${CD}/::umount \"mv ${CD} ${CD}-unmounted\";
1064 acquire::cdrom::autodetect 0;" > rootdir/etc/apt/apt.conf.d/00cdrom
1065 echo -n "$1" > ${CD}/.disk/info
1066 if [ ! -d aptarchive/dists ]; then
1067 msgdie 'Flat file archive cdroms can not be created currently'
1068 return 1
1069 fi
1070 mv aptarchive/dists "$CD"
1071 ln -s "$(readlink -f ./incoming)" $CD/pool
1072 find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
1073 # start with an unmounted disk
1074 mv "${CD}" "${CD}-unmounted"
1075 # we don't want the disk to be modifiable
1076 addtrap 'prefix' "chmod -f -R +w $PWD/rootdir/media/cdrom/dists/ $PWD/rootdir/media/cdrom-unmounted/dists/ || true;"
1077 chmod -R 555 rootdir/media/cdrom-unmounted/dists
1078 }
1079
1080 downloadfile() {
1081 local PROTO="$(echo "$1" | cut -d':' -f 1 )"
1082 apthelper -o Debug::Acquire::${PROTO}=1 \
1083 download-file "$1" "$2" 2>&1 || true
1084 # only if the file exists the download was successful
1085 if [ -r "$2" ]; then
1086 return 0
1087 else
1088 return 1
1089 fi
1090 }
1091
1092 checkdiff() {
1093 local DIFFTEXT="$(command diff -u "$@" 2>&1 | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
1094 if [ -n "$DIFFTEXT" ]; then
1095 echo >&2
1096 echo >&2 "$DIFFTEXT"
1097 return 1
1098 else
1099 return 0
1100 fi
1101 }
1102
1103 testfileequal() {
1104 local FILE="$1"
1105 shift
1106 msgtest "Test for correctness of file" "$FILE"
1107 if [ -z "$*" ]; then
1108 echo -n "" | checkdiff $FILE - && msgpass || msgfail
1109 else
1110 echo "$*" | checkdiff $FILE - && msgpass || msgfail
1111 fi
1112 }
1113
1114 testempty() {
1115 msgtest "Test for no output of" "$*"
1116 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testempty.comparefile"
1117 if "$@" >$COMPAREFILE 2>&1 && test ! -s $COMPAREFILE; then
1118 msgpass
1119 else
1120 echo
1121 cat $COMPAREFILE
1122 msgfail
1123 fi
1124 aptautotest 'testempty' "$@"
1125 }
1126
1127 testequal() {
1128 local MSG='Test of equality of'
1129 if [ "$1" = '--nomsg' ]; then
1130 MSG=''
1131 shift
1132 fi
1133
1134 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
1135 echo "$1" > $COMPAREFILE
1136 shift
1137
1138 if [ -n "$MSG" ]; then
1139 msgtest "$MSG" "$*"
1140 fi
1141 "$@" 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
1142 aptautotest 'testequal' "$@"
1143 }
1144
1145 testequalor2() {
1146 local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
1147 local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
1148 local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
1149 echo "$1" > $COMPAREFILE1
1150 echo "$2" > $COMPAREFILE2
1151 shift 2
1152 msgtest "Test for equality OR of" "$*"
1153 "$@" >$COMPAREAGAINST 2>&1 || true
1154 if checkdiff $COMPAREFILE1 $COMPAREAGAINST >/dev/null 2>&1 || \
1155 checkdiff $COMPAREFILE2 $COMPAREAGAINST >/dev/null 2>&1
1156 then
1157 msgpass
1158 else
1159 echo -n "\n${CINFO}Diff against OR 1${CNORMAL}"
1160 checkdiff $COMPAREFILE1 $COMPAREAGAINST || true
1161 echo -n "${CINFO}Diff against OR 2${CNORMAL}"
1162 checkdiff $COMPAREFILE2 $COMPAREAGAINST || true
1163 msgfail
1164 fi
1165 aptautotest 'testequalor2' "$@"
1166 }
1167
1168 testshowvirtual() {
1169 local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
1170 local PACKAGE="$1"
1171 shift
1172 while [ -n "$1" ]; do
1173 VIRTUAL="${VIRTUAL}
1174 N: Can't select versions from package '$1' as it is purely virtual"
1175 PACKAGE="${PACKAGE} $1"
1176 shift
1177 done
1178 msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
1179 VIRTUAL="${VIRTUAL}
1180 N: No packages found"
1181 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
1182 local ARCH="$(getarchitecture 'native')"
1183 echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
1184 aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
1185 }
1186
1187 testnopackage() {
1188 msgtest "Test for non-existent packages" "apt-cache show $*"
1189 local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
1190 if [ -n "$SHOWPKG" ]; then
1191 echo >&2
1192 echo >&2 "$SHOWPKG"
1193 msgfail
1194 else
1195 msgpass
1196 fi
1197 }
1198
1199 testdpkginstalled() {
1200 msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
1201 local PKGS="$(dpkg -l "$@" 2>/dev/null | grep '^i' | wc -l)"
1202 if [ "$PKGS" != $# ]; then
1203 echo >&2 $PKGS
1204 dpkg -l "$@" | grep '^[a-z]' >&2
1205 msgfail
1206 else
1207 msgpass
1208 fi
1209 }
1210
1211 testdpkgnotinstalled() {
1212 msgtest "Test for correctly not-installed package(s) with" "dpkg -l $*"
1213 local PKGS="$(dpkg -l "$@" 2> /dev/null | grep '^i' | wc -l)"
1214 if [ "$PKGS" != 0 ]; then
1215 echo
1216 dpkg -l "$@" | grep '^[a-z]' >&2
1217 msgfail
1218 else
1219 msgpass
1220 fi
1221 }
1222
1223 testmarkedauto() {
1224 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
1225 if [ -n "$1" ]; then
1226 msgtest 'Test for correctly marked as auto-installed' "$*"
1227 while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
1228 else
1229 msgtest 'Test for correctly marked as auto-installed' 'no package'
1230 echo -n > $COMPAREFILE
1231 fi
1232 aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
1233 }
1234
1235 msgfailoutput() {
1236 local MSG="$1"
1237 local OUTPUT="$2"
1238 shift 2
1239 echo >&2
1240 if [ "$1" = 'grep' ]; then
1241 while [ -n "$2" ]; do shift; done
1242 echo "#### Complete file: $1 ####"
1243 cat >&2 "$1" || true
1244 echo '#### grep output ####'
1245 elif [ "$1" = 'test' ]; then
1246 # doesn't support ! or non-file flags
1247 msgfailoutputstatfile() {
1248 local FILEFLAGS='^-[bcdefgGhkLOprsStuwx]$'
1249 if expr match "$1" "$FILEFLAGS" >/dev/null; then
1250 echo "#### stat(2) of file: $2 ####"
1251 stat "$2" || true
1252 fi
1253 }
1254 msgfailoutputstatfile "$2" "$3"
1255 while [ -n "$5" ] && [ "$4" = '-o' -o "$4" = '-a' ]; do
1256 shift 3
1257 msgfailoutputstatfile "$2" "$3"
1258 done
1259 echo '#### test output ####'
1260 fi
1261 cat >&2 $OUTPUT
1262 msgfail "$MSG"
1263 }
1264
1265 testsuccess() {
1266 if [ "$1" = '--nomsg' ]; then
1267 shift
1268 else
1269 msgtest 'Test for successful execution of' "$*"
1270 fi
1271 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output"
1272 if "$@" >${OUTPUT} 2>&1; then
1273 if expr match "$1" '^apt.*' >/dev/null; then
1274 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1275 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1276 elif grep -q -E '^[WE]: ' "$OUTPUT"; then
1277 msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
1278 else
1279 msgpass
1280 fi
1281 else
1282 msgpass
1283 fi
1284 else
1285 local EXITCODE=$?
1286 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
1287 fi
1288 aptautotest 'testsuccess' "$@"
1289 }
1290 testwarning() {
1291 if [ "$1" = '--nomsg' ]; then
1292 shift
1293 else
1294 msgtest 'Test for successful execution with warnings of' "$*"
1295 fi
1296 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output"
1297 if "$@" >${OUTPUT} 2>&1; then
1298 if expr match "$1" '^apt.*' >/dev/null; then
1299 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1300 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1301 elif grep -q -E '^E: ' "$OUTPUT"; then
1302 msgfailoutput 'successful run, but output contains errors' "$OUTPUT" "$@"
1303 elif ! grep -q -E '^W: ' "$OUTPUT"; then
1304 msgfailoutput 'successful run, but output contains no warnings' "$OUTPUT" "$@"
1305 else
1306 msgpass
1307 fi
1308 else
1309 msgpass
1310 fi
1311 else
1312 local EXITCODE=$?
1313 echo >&2
1314 cat >&2 $OUTPUT
1315 msgfail "exitcode $EXITCODE"
1316 fi
1317 aptautotest 'testwarning' "$@"
1318 }
1319 testfailure() {
1320 if [ "$1" = '--nomsg' ]; then
1321 shift
1322 else
1323 msgtest 'Test for failure in execution of' "$*"
1324 fi
1325 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
1326 if "$@" >${OUTPUT} 2>&1; then
1327 local EXITCODE=$?
1328 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
1329 else
1330 local EXITCODE=$?
1331 if expr match "$1" '^apt.*' >/dev/null; then
1332 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1333 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1334 elif ! grep -q -E '^E: ' "$OUTPUT"; then
1335 msgfailoutput "run failed with exitcode ${EXITCODE}, but with no errors" "$OUTPUT" "$@"
1336 else
1337 msgpass
1338 fi
1339 else
1340 msgpass
1341 fi
1342 fi
1343 aptautotest 'testfailure' "$@"
1344 }
1345
1346 testfilestats() {
1347 msgtest "Test that file $1 has $2 $3" "$4"
1348 if [ "$4" "$3" "$(stat --format "$2" "$1")" ]; then
1349 msgpass
1350 else
1351 echo >&2
1352 ls -ld >&2 "$1"
1353 echo -n >&2 "stat(1) reports for $2: "
1354 stat --format "$2" "$1"
1355 msgfail
1356 fi
1357 }
1358 testaccessrights() {
1359 testfilestats "$1" '%a' '=' "$2"
1360 }
1361
1362 testwebserverlaststatuscode() {
1363 local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
1364 local STATUS='downloaded/webserverstatus-statusfile.log'
1365 rm -f "$DOWNLOG" "$STATUS"
1366 msgtest 'Test last status code from the webserver was' "$1"
1367 if downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG" && [ "$(cat "$STATUS")" = "$1" ]; then
1368 msgpass
1369 else
1370 echo >&2
1371 if [ -n "$2" ]; then
1372 shift
1373 echo >&2 '#### Additionally provided output files contain:'
1374 cat >&2 "$@"
1375 fi
1376 echo >&2 '#### Download log of the status code:'
1377 cat >&2 "$DOWNLOG"
1378 msgfail "Status was $(cat "$STATUS")"
1379 fi
1380 }
1381
1382 pause() {
1383 echo "STOPPED execution. Press enter to continue"
1384 local IGNORE
1385 read IGNORE
1386 }
1387
1388 listcurrentlistsdirectory() {
1389 find rootdir/var/lib/apt/lists -maxdepth 1 -type d | while read line; do
1390 stat --format '%U:%G:%a:%n' "$line"
1391 done
1392 find rootdir/var/lib/apt/lists -maxdepth 1 \! -type d | while read line; do
1393 stat --format '%U:%G:%a:%s:%y:%n' "$line"
1394 done
1395 }
1396
1397 ### convinience hacks ###
1398 mkdir() {
1399 # creating some directories by hand is a tedious task, so make it look simple
1400 if [ "$*" = '-p rootdir/var/lib/apt/lists' ] || [ "$*" = "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" ] ||
1401 [ "$*" = '-p rootdir/var/lib/apt/lists/partial' ] || [ "$*" = "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" ]; then
1402 # only the last directory created by mkdir is effected by the -m !
1403 command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt"
1404 command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"
1405 command mkdir -m 700 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
1406 touch "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/lock"
1407 if [ "$(id -u)" = '0' ]; then
1408 chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
1409 fi
1410 else
1411 command mkdir "$@"
1412 fi
1413 }
1414
1415 ### The following tests are run by most test methods automatically to check
1416 ### general things about commands executed without writing the test every time.
1417
1418 aptautotest() {
1419 local TESTCALL="$1"
1420 local CMD="$2"
1421 local FIRSTOPT="$3"
1422 local AUTOTEST="aptautotest_$(basename "$CMD" | tr -d '-')_$(echo "$FIRSTOPT" | tr -d '-')"
1423 if command -v $AUTOTEST >/dev/null; then
1424 shift 3
1425 # save and restore the *.output files from other tests
1426 # as we might otherwise override them in these automatic tests
1427 rm -rf ${TMPWORKINGDIRECTORY}/rootdir/tmp-before
1428 mv ${TMPWORKINGDIRECTORY}/rootdir/tmp ${TMPWORKINGDIRECTORY}/rootdir/tmp-before
1429 mkdir ${TMPWORKINGDIRECTORY}/rootdir/tmp
1430 $AUTOTEST "$TESTCALL" "$@"
1431 rm -rf ${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest
1432 mv ${TMPWORKINGDIRECTORY}/rootdir/tmp ${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest
1433 mv ${TMPWORKINGDIRECTORY}/rootdir/tmp-before ${TMPWORKINGDIRECTORY}/rootdir/tmp
1434 fi
1435 }
1436
1437 aptautotest_aptget_update() {
1438 if ! test -d "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"; then return; fi
1439 testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
1440 testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
1441 # all copied files are properly chmodded
1442 for file in $(find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" -maxdepth 1 -type f ! -name 'lock'); do
1443 testfilestats "$file" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:644"
1444 done
1445 }
1446 aptautotest_apt_update() { aptautotest_aptget_update "$@"; }