You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1773 lines
58 KiB

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