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.
 
 
 
 
 
 

1634 lines
54 KiB

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