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.
 
 
 
 
 
 

1642 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 BINARY="${6:-$NAME}"
  741. local ARCHS=""
  742. local SPATH="aptarchive/dists/${RELEASE}/main/source"
  743. mkdir -p $SPATH
  744. local FILE="${SPATH}/Sources"
  745. local DSCFILE="${NAME}_${VERSION}.dsc"
  746. local TARFILE="${NAME}_${VERSION}.tar.gz"
  747. echo "Package: $NAME
  748. Binary: $BINARY
  749. Version: $VERSION
  750. Maintainer: Joe Sixpack <joe@example.org>
  751. Architecture: $ARCH" >> $FILE
  752. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  753. echo "Files:
  754. $(echo -n "$DSCFILE" | md5sum | cut -d' ' -f 1) $(echo -n "$DSCFILE" | wc -c) $DSCFILE
  755. $(echo -n "$TARFILE" | md5sum | cut -d' ' -f 1) $(echo -n "$TARFILE" | wc -c) $TARFILE
  756. " >> $FILE
  757. }
  758. insertinstalledpackage() {
  759. local NAME="$1"
  760. local ARCH="$2"
  761. local VERSION="$3"
  762. local DEPENDENCIES="$4"
  763. local PRIORITY="${5:-optional}"
  764. local STATUS="${6:-install ok installed}"
  765. local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/installed
  766. If you find such a package installed on your system,
  767. something went horribly wrong! They are autogenerated
  768. und used only by testcases and surf no other propose…"}"
  769. local FILE='rootdir/var/lib/dpkg/status'
  770. local INFO='rootdir/var/lib/dpkg/info'
  771. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  772. echo "Package: $NAME
  773. Status: $STATUS
  774. Priority: $PRIORITY
  775. Section: other
  776. Installed-Size: 42
  777. Maintainer: Joe Sixpack <joe@example.org>
  778. Version: $VERSION" >> $FILE
  779. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  780. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  781. echo "Description: $DESCRIPTION" >> $FILE
  782. echo >> $FILE
  783. if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
  784. echo -n > ${INFO}/${NAME}:${arch}.list
  785. else
  786. echo -n > ${INFO}/${NAME}.list
  787. fi
  788. done
  789. }
  790. buildaptarchivefromincoming() {
  791. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
  792. cd aptarchive
  793. [ -e pool ] || ln -s ../incoming pool
  794. [ -e ftparchive.conf ] || createaptftparchiveconfig
  795. [ -e dists ] || buildaptftparchivedirectorystructure
  796. msgninfo "\tGenerate Packages, Sources and Contents files… "
  797. testsuccess aptftparchive generate ftparchive.conf
  798. cd - > /dev/null
  799. msgdone "info"
  800. generatereleasefiles "$@"
  801. }
  802. buildaptarchivefromfiles() {
  803. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
  804. local DIR='aptarchive'
  805. if [ -d "${DIR}/dists" ]; then DIR="${DIR}/dists"; fi
  806. find "$DIR" -name 'Packages' -o -name 'Sources' -o -name 'Translation-*' | while read line; do
  807. msgninfo "\t${line} file… "
  808. compressfile "$line" "$1"
  809. msgdone "info"
  810. done
  811. generatereleasefiles "$@"
  812. }
  813. compressfile() {
  814. cat ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | while read compressor extension command; do
  815. if [ "$compressor" = '.' ]; then
  816. if [ -n "$2" ]; then
  817. touch -d "$2" "$1"
  818. fi
  819. continue
  820. fi
  821. cat "$1" | $command > "${1}.${extension}"
  822. if [ -n "$2" ]; then
  823. touch -d "$2" "${1}.${extension}"
  824. fi
  825. done
  826. }
  827. # can be overridden by testcases for their pleasure
  828. getcodenamefromsuite() {
  829. case "$1" in
  830. unstable) echo 'sid';;
  831. *) echo -n "$1";;
  832. esac
  833. }
  834. getreleaseversionfromsuite() { true; }
  835. getlabelfromsuite() { true; }
  836. getoriginfromsuite() { true; }
  837. generatereleasefiles() {
  838. # $1 is the Date header and $2 is the ValidUntil header to be set
  839. # both should be given in notation date/touch can understand
  840. msgninfo "\tGenerate Release files… "
  841. if [ -e aptarchive/dists ]; then
  842. for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
  843. local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
  844. local CODENAME="$(getcodenamefromsuite $SUITE)"
  845. local VERSION="$(getreleaseversionfromsuite $SUITE)"
  846. local LABEL="$(getlabelfromsuite $SUITE)"
  847. local ORIGIN="$(getoriginfromsuite $SUITE)"
  848. if [ -n "$VERSION" ]; then
  849. VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
  850. fi
  851. if [ -n "$LABEL" ]; then
  852. LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
  853. fi
  854. if [ -n "$ORIGIN" ]; then
  855. ORIGIN="-o APT::FTPArchive::Release::Origin=${ORIGIN}"
  856. fi
  857. aptftparchive -qq release $dir \
  858. -o APT::FTPArchive::Release::Suite="${SUITE}" \
  859. -o APT::FTPArchive::Release::Codename="${CODENAME}" \
  860. ${LABEL} \
  861. ${ORIGIN} \
  862. ${VERSION} \
  863. | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
  864. if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
  865. sed -i '/^Date: / a\
  866. NotAutomatic: yes' $dir/Release
  867. fi
  868. if [ -n "$1" -a "$1" != "now" ]; then
  869. sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
  870. fi
  871. if [ -n "$2" ]; then
  872. sed -i "/^Date: / a\
  873. Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
  874. fi
  875. done
  876. else
  877. aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
  878. fi
  879. if [ -n "$1" -a "$1" != "now" ]; then
  880. for release in $(find ./aptarchive -name 'Release'); do
  881. touch -d "$1" $release
  882. done
  883. fi
  884. msgdone "info"
  885. }
  886. setupdistsaptarchive() {
  887. local APTARCHIVE=$(readlink -f ./aptarchive)
  888. rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
  889. rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
  890. for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
  891. SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
  892. msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
  893. echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
  894. echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
  895. msgdone "info"
  896. done
  897. }
  898. setupflataptarchive() {
  899. local APTARCHIVE=$(readlink -f ./aptarchive)
  900. if [ -f ${APTARCHIVE}/Packages ]; then
  901. msgninfo "\tadd deb sources.list line… "
  902. echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  903. msgdone "info"
  904. else
  905. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  906. fi
  907. if [ -f ${APTARCHIVE}/Sources ]; then
  908. msgninfo "\tadd deb-src sources.list line… "
  909. echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  910. msgdone "info"
  911. else
  912. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  913. fi
  914. }
  915. setupaptarchive() {
  916. local NOUPDATE=0
  917. if [ "$1" = '--no-update' ]; then
  918. NOUPDATE=1
  919. shift
  920. fi
  921. buildaptarchive "$@"
  922. if [ -e aptarchive/dists ]; then
  923. setupdistsaptarchive
  924. else
  925. setupflataptarchive
  926. fi
  927. signreleasefiles 'Joe Sixpack'
  928. if [ "1" != "$NOUPDATE" ]; then
  929. testsuccess aptget update -o Debug::pkgAcquire::Worker=true -o Debug::Acquire::gpgv=true
  930. fi
  931. }
  932. signreleasefiles() {
  933. local SIGNER="${1:-Joe Sixpack}"
  934. local REPODIR="${2:-aptarchive}"
  935. local KEY="keys/$(echo "$SIGNER" | tr 'A-Z' 'a-z' | sed 's# ##g')"
  936. local GPG="aptkey --quiet --keyring ${KEY}.pub --secret-keyring ${KEY}.sec --readonly adv --batch --yes"
  937. msgninfo "\tSign archive with $SIGNER key $KEY… "
  938. local REXKEY='keys/rexexpired'
  939. local SECEXPIREBAK="${REXKEY}.sec.bak"
  940. local PUBEXPIREBAK="${REXKEY}.pub.bak"
  941. if [ "${SIGNER}" = 'Rex Expired' ]; then
  942. # the key is expired, so gpg doesn't allow to sign with and the --faked-system-time
  943. # option doesn't exist anymore (and using faketime would add a new obscure dependency)
  944. # therefore we 'temporary' make the key not expired and restore a backup after signing
  945. cp ${REXKEY}.sec $SECEXPIREBAK
  946. cp ${REXKEY}.pub $PUBEXPIREBAK
  947. local SECUNEXPIRED="${REXKEY}.sec.unexpired"
  948. local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
  949. if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
  950. cp $SECUNEXPIRED ${REXKEY}.sec
  951. cp $PUBUNEXPIRED ${REXKEY}.pub
  952. else
  953. if ! printf "expire\n1w\nsave\n" | $GPG --default-key "$SIGNER" --command-fd 0 --edit-key "${SIGNER}" >setexpire.gpg 2>&1; then
  954. cat setexpire.gpg
  955. exit 1
  956. fi
  957. cp ${REXKEY}.sec $SECUNEXPIRED
  958. cp ${REXKEY}.pub $PUBUNEXPIRED
  959. fi
  960. fi
  961. for RELEASE in $(find ${REPODIR}/ -name Release); do
  962. $GPG --default-key "$SIGNER" --armor --detach-sign --sign --output ${RELEASE}.gpg ${RELEASE}
  963. local INRELEASE="$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')"
  964. $GPG --default-key "$SIGNER" --clearsign --output $INRELEASE $RELEASE
  965. # we might have set a specific date for the Release file, so copy it
  966. touch -d "$(stat --format "%y" ${RELEASE})" ${RELEASE}.gpg ${INRELEASE}
  967. done
  968. if [ -f "$SECEXPIREBAK" ] && [ -f "$PUBEXPIREBAK" ]; then
  969. mv -f $SECEXPIREBAK ${REXKEY}.sec
  970. mv -f $PUBEXPIREBAK ${REXKEY}.pub
  971. fi
  972. msgdone "info"
  973. }
  974. redatereleasefiles() {
  975. local DATE="$(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')"
  976. for release in $(find aptarchive/ -name 'Release'); do
  977. sed -i "s/^Date: .*$/Date: ${DATE}/" $release
  978. touch -d "$DATE" $release
  979. done
  980. signreleasefiles "${2:-Joe Sixpack}"
  981. }
  982. webserverconfig() {
  983. local WEBSERVER="${3:-http://localhost:8080}"
  984. local NOCHECK=false
  985. if [ "$1" = '--no-check' ]; then
  986. NOCHECK=true
  987. shift
  988. fi
  989. local DOWNLOG='rootdir/tmp/download-testfile.log'
  990. local STATUS='downloaded/webserverconfig.status'
  991. rm -f "$STATUS" "$DOWNLOG"
  992. local URI
  993. if [ -n "$2" ]; then
  994. msgtest "Set webserver config option '${1}' to" "$2"
  995. URI="${WEBSERVER}/_config/set/${1}/${2}"
  996. else
  997. msgtest 'Clear webserver config option' "${1}"
  998. URI="${WEBSERVER}/_config/clear/${1}"
  999. fi
  1000. if downloadfile "$URI" "$STATUS" > "$DOWNLOG"; then
  1001. msgpass
  1002. else
  1003. cat "$DOWNLOG" "$STATUS" || true
  1004. msgfail
  1005. fi
  1006. $NOCHECK || testwebserverlaststatuscode '200'
  1007. }
  1008. rewritesourceslist() {
  1009. local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive")"
  1010. for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
  1011. sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#http://localhost:8080/#${1}#" -e "s#https://localhost:4433/#${1}#"
  1012. done
  1013. }
  1014. # wait for up to 10s for a pid file to appear to avoid possible race
  1015. # when a helper is started and dosn't write the PID quick enough
  1016. waitforpidfile() {
  1017. local PIDFILE="$1"
  1018. for i in $(seq 10); do
  1019. if test -s "$PIDFILE"; then
  1020. return 0
  1021. fi
  1022. sleep 1
  1023. done
  1024. msgdie "waiting for $PIDFILE failed"
  1025. return 1
  1026. }
  1027. changetowebserver() {
  1028. if [ "$1" != '--no-rewrite' ]; then
  1029. rewritesourceslist 'http://localhost:8080/'
  1030. else
  1031. shift
  1032. fi
  1033. if test -x ${APTWEBSERVERBINDIR}/aptwebserver; then
  1034. cd aptarchive
  1035. local LOG="webserver.log"
  1036. if ! aptwebserver -o aptwebserver::fork=1 "$@" >$LOG 2>&1 ; then
  1037. cat $LOG
  1038. false
  1039. fi
  1040. waitforpidfile aptwebserver.pid
  1041. local PID="$(cat aptwebserver.pid)"
  1042. if [ -z "$PID" ]; then
  1043. msgdie 'Could not fork aptwebserver successfully'
  1044. fi
  1045. addtrap "kill $PID;"
  1046. cd - > /dev/null
  1047. else
  1048. msgdie 'You have to build aptwerbserver or install a webserver'
  1049. fi
  1050. }
  1051. changetohttpswebserver() {
  1052. if ! which stunnel4 >/dev/null; then
  1053. msgdie 'You need to install stunnel4 for https testcases'
  1054. fi
  1055. if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
  1056. changetowebserver --no-rewrite "$@"
  1057. fi
  1058. echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
  1059. cert = ${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem
  1060. output = /dev/null
  1061. [https]
  1062. accept = 4433
  1063. connect = 8080
  1064. " > ${TMPWORKINGDIRECTORY}/stunnel.conf
  1065. stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
  1066. waitforpidfile "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid"
  1067. local PID="$(cat ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid)"
  1068. if [ -z "$PID" ]; then
  1069. msgdie 'Could not fork stunnel4 successfully'
  1070. fi
  1071. addtrap 'prefix' "kill ${PID};"
  1072. rewritesourceslist 'https://localhost:4433/'
  1073. }
  1074. changetocdrom() {
  1075. mkdir -p rootdir/media/cdrom/.disk
  1076. local CD="$(readlink -f rootdir/media/cdrom)"
  1077. echo "acquire::cdrom::mount \"${CD}\";
  1078. acquire::cdrom::${CD}/::mount \"mv ${CD}-unmounted ${CD}\";
  1079. acquire::cdrom::${CD}/::umount \"mv ${CD} ${CD}-unmounted\";
  1080. acquire::cdrom::autodetect 0;" > rootdir/etc/apt/apt.conf.d/00cdrom
  1081. echo -n "$1" > ${CD}/.disk/info
  1082. if [ ! -d aptarchive/dists ]; then
  1083. msgdie 'Flat file archive cdroms can not be created currently'
  1084. return 1
  1085. fi
  1086. mv aptarchive/dists "$CD"
  1087. ln -s "$(readlink -f ./incoming)" $CD/pool
  1088. find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
  1089. # start with an unmounted disk
  1090. mv "${CD}" "${CD}-unmounted"
  1091. # we don't want the disk to be modifiable
  1092. addtrap 'prefix' "chmod -f -R +w $PWD/rootdir/media/cdrom/dists/ $PWD/rootdir/media/cdrom-unmounted/dists/ || true;"
  1093. chmod -R 555 rootdir/media/cdrom-unmounted/dists
  1094. }
  1095. downloadfile() {
  1096. local PROTO="${1%%:*}"
  1097. if ! apthelper -o Debug::Acquire::${PROTO}=1 -o Debug::pkgAcquire::Worker=1 \
  1098. download-file "$1" "$2" "$3" 2>&1 ; then
  1099. return 1
  1100. fi
  1101. # only if the file exists the download was successful
  1102. if [ -r "$2" ]; then
  1103. return 0
  1104. else
  1105. return 1
  1106. fi
  1107. }
  1108. checkdiff() {
  1109. local DIFFTEXT="$(command diff -u "$@" 2>&1 | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  1110. if [ -n "$DIFFTEXT" ]; then
  1111. echo >&2
  1112. echo >&2 "$DIFFTEXT"
  1113. return 1
  1114. else
  1115. return 0
  1116. fi
  1117. }
  1118. testfileequal() {
  1119. local MSG='Test for correctness of file'
  1120. if [ "$1" = '--nomsg' ]; then
  1121. MSG=''
  1122. shift
  1123. fi
  1124. local FILE="$1"
  1125. shift
  1126. if [ -n "$MSG" ]; then
  1127. msgtest "$MSG" "$FILE"
  1128. fi
  1129. if [ -z "$*" ]; then
  1130. echo -n "" | checkdiff - $FILE && msgpass || msgfail
  1131. else
  1132. echo "$*" | checkdiff - $FILE && msgpass || msgfail
  1133. fi
  1134. }
  1135. testempty() {
  1136. msgtest "Test for no output of" "$*"
  1137. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testempty.comparefile"
  1138. if "$@" >$COMPAREFILE 2>&1 && test ! -s $COMPAREFILE; then
  1139. msgpass
  1140. else
  1141. echo
  1142. cat $COMPAREFILE
  1143. msgfail
  1144. fi
  1145. aptautotest 'testempty' "$@"
  1146. }
  1147. testequal() {
  1148. local MSG='Test of equality of'
  1149. if [ "$1" = '--nomsg' ]; then
  1150. MSG=''
  1151. shift
  1152. fi
  1153. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
  1154. echo "$1" > $COMPAREFILE
  1155. shift
  1156. if [ -n "$MSG" ]; then
  1157. msgtest "$MSG" "$*"
  1158. fi
  1159. "$@" 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1160. aptautotest 'testequal' "$@"
  1161. }
  1162. testequalor2() {
  1163. local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
  1164. local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
  1165. local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
  1166. echo "$1" > $COMPAREFILE1
  1167. echo "$2" > $COMPAREFILE2
  1168. shift 2
  1169. msgtest "Test for equality OR of" "$*"
  1170. "$@" >$COMPAREAGAINST 2>&1 || true
  1171. if checkdiff $COMPAREFILE1 $COMPAREAGAINST >/dev/null 2>&1 || \
  1172. checkdiff $COMPAREFILE2 $COMPAREAGAINST >/dev/null 2>&1
  1173. then
  1174. msgpass
  1175. else
  1176. echo -n "\n${CINFO}Diff against OR 1${CNORMAL}"
  1177. checkdiff $COMPAREFILE1 $COMPAREAGAINST || true
  1178. echo -n "${CINFO}Diff against OR 2${CNORMAL}"
  1179. checkdiff $COMPAREFILE2 $COMPAREAGAINST || true
  1180. msgfail
  1181. fi
  1182. aptautotest 'testequalor2' "$@"
  1183. }
  1184. testshowvirtual() {
  1185. local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
  1186. local PACKAGE="$1"
  1187. shift
  1188. while [ -n "$1" ]; do
  1189. VIRTUAL="${VIRTUAL}
  1190. N: Can't select versions from package '$1' as it is purely virtual"
  1191. PACKAGE="${PACKAGE} $1"
  1192. shift
  1193. done
  1194. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  1195. VIRTUAL="${VIRTUAL}
  1196. N: No packages found"
  1197. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
  1198. local ARCH="$(getarchitecture 'native')"
  1199. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
  1200. aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1201. }
  1202. testnopackage() {
  1203. msgtest "Test for non-existent packages" "apt-cache show $*"
  1204. local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
  1205. if [ -n "$SHOWPKG" ]; then
  1206. echo >&2
  1207. echo >&2 "$SHOWPKG"
  1208. msgfail
  1209. else
  1210. msgpass
  1211. fi
  1212. }
  1213. testdpkgstatus() {
  1214. local STATE="$1"
  1215. local NR="$2"
  1216. shift 2
  1217. msgtest "Test that $NR package(s) are in state $STATE with" "dpkg -l $*"
  1218. local PKGS="$(dpkg -l "$@" 2>/dev/null | grep "^${STATE}" | wc -l)"
  1219. if [ "$PKGS" != $NR ]; then
  1220. echo >&2 $PKGS
  1221. dpkg -l "$@" | grep '^[a-z]' >&2 || true
  1222. msgfail
  1223. else
  1224. msgpass
  1225. fi
  1226. }
  1227. testdpkginstalled() {
  1228. testdpkgstatus 'ii' "$#" "$@"
  1229. }
  1230. testdpkgnotinstalled() {
  1231. testdpkgstatus 'ii' '0' "$@"
  1232. }
  1233. testmarkedauto() {
  1234. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
  1235. if [ -n "$1" ]; then
  1236. msgtest 'Test for correctly marked as auto-installed' "$*"
  1237. while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
  1238. else
  1239. msgtest 'Test for correctly marked as auto-installed' 'no package'
  1240. echo -n > $COMPAREFILE
  1241. fi
  1242. aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1243. }
  1244. testmarkedmanual() {
  1245. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedmanual.comparefile"
  1246. if [ -n "$1" ]; then
  1247. msgtest 'Test for correctly marked as manually installed' "$*"
  1248. while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
  1249. else
  1250. msgtest 'Test for correctly marked as manually installed' 'no package'
  1251. echo -n > $COMPAREFILE
  1252. fi
  1253. aptmark showmanual 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1254. }
  1255. msgfailoutput() {
  1256. local MSG="$1"
  1257. local OUTPUT="$2"
  1258. shift 2
  1259. echo >&2
  1260. if [ "$1" = 'grep' ]; then
  1261. while [ -n "$2" ]; do shift; done
  1262. echo "#### Complete file: $1 ####"
  1263. cat >&2 "$1" || true
  1264. echo '#### grep output ####'
  1265. elif [ "$1" = 'test' ]; then
  1266. # doesn't support ! or non-file flags
  1267. msgfailoutputstatfile() {
  1268. local FILEFLAGS='^-[bcdefgGhkLOprsStuwx]$'
  1269. if expr match "$1" "$FILEFLAGS" >/dev/null; then
  1270. echo "#### stat(2) of file: $2 ####"
  1271. stat "$2" || true
  1272. fi
  1273. }
  1274. msgfailoutputstatfile "$2" "$3"
  1275. while [ -n "$5" ] && [ "$4" = '-o' -o "$4" = '-a' ]; do
  1276. shift 3
  1277. msgfailoutputstatfile "$2" "$3"
  1278. done
  1279. echo '#### test output ####'
  1280. fi
  1281. cat >&2 $OUTPUT
  1282. msgfail "$MSG"
  1283. }
  1284. testsuccess() {
  1285. if [ "$1" = '--nomsg' ]; then
  1286. shift
  1287. else
  1288. msgtest 'Test for successful execution of' "$*"
  1289. fi
  1290. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output"
  1291. if "$@" >${OUTPUT} 2>&1; then
  1292. if expr match "$1" '^apt.*' >/dev/null; then
  1293. if grep -q -E ' runtime error: ' "$OUTPUT"; then
  1294. msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
  1295. elif grep -E '^[WE]: ' "$OUTPUT" > "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" 2>&1; then
  1296. if [ "$IGNORE_PTY_NOT_MOUNTED" = '1' ]; then
  1297. if echo 'E: Can not write log (Is /dev/pts mounted?) - posix_openpt (2: No such file or directory)' \
  1298. | cmp - "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" >/dev/null 2>&1; then
  1299. msgpass
  1300. else
  1301. msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
  1302. fi
  1303. else
  1304. msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
  1305. fi
  1306. else
  1307. msgpass
  1308. fi
  1309. else
  1310. msgpass
  1311. fi
  1312. else
  1313. local EXITCODE=$?
  1314. msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
  1315. fi
  1316. aptautotest 'testsuccess' "$@"
  1317. }
  1318. testwarning() {
  1319. if [ "$1" = '--nomsg' ]; then
  1320. shift
  1321. else
  1322. msgtest 'Test for successful execution with warnings of' "$*"
  1323. fi
  1324. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output"
  1325. if "$@" >${OUTPUT} 2>&1; then
  1326. if expr match "$1" '^apt.*' >/dev/null; then
  1327. if grep -q -E ' runtime error: ' "$OUTPUT"; then
  1328. msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
  1329. elif grep -q -E '^E: ' "$OUTPUT"; then
  1330. msgfailoutput 'successful run, but output contains errors' "$OUTPUT" "$@"
  1331. elif ! grep -q -E '^W: ' "$OUTPUT"; then
  1332. msgfailoutput 'successful run, but output contains no warnings' "$OUTPUT" "$@"
  1333. else
  1334. msgpass
  1335. fi
  1336. else
  1337. msgpass
  1338. fi
  1339. else
  1340. local EXITCODE=$?
  1341. echo >&2
  1342. cat >&2 $OUTPUT
  1343. msgfail "exitcode $EXITCODE"
  1344. fi
  1345. aptautotest 'testwarning' "$@"
  1346. }
  1347. testfailure() {
  1348. if [ "$1" = '--nomsg' ]; then
  1349. shift
  1350. else
  1351. msgtest 'Test for failure in execution of' "$*"
  1352. fi
  1353. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
  1354. if "$@" >${OUTPUT} 2>&1; then
  1355. local EXITCODE=$?
  1356. msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
  1357. else
  1358. local EXITCODE=$?
  1359. if expr match "$1" '^apt.*' >/dev/null; then
  1360. if [ "$1" = 'aptkey' ]; then
  1361. if grep -q -E " Can't check signature: " "$OUTPUT" || \
  1362. grep -q -E " BAD signature from " "$OUTPUT"; then
  1363. msgpass
  1364. else
  1365. msgfailoutput "run failed with exitcode ${EXITCODE}, but no signature error" "$OUTPUT" "$@"
  1366. fi
  1367. else
  1368. if grep -q -E ' runtime error: ' "$OUTPUT"; then
  1369. msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
  1370. elif grep -q -E '==ERROR' "$OUTPUT"; then
  1371. msgfailoutput 'compiler sanitizers reported errors' "$OUTPUT" "$@"
  1372. elif ! grep -q -E '^E: ' "$OUTPUT"; then
  1373. msgfailoutput "run failed with exitcode ${EXITCODE}, but with no errors" "$OUTPUT" "$@"
  1374. else
  1375. msgpass
  1376. fi
  1377. fi
  1378. else
  1379. msgpass
  1380. fi
  1381. fi
  1382. aptautotest 'testfailure' "$@"
  1383. }
  1384. testsuccessequal() {
  1385. local CMP="$1"
  1386. shift
  1387. testsuccess "$@"
  1388. testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output" "$CMP"
  1389. }
  1390. testwarningequal() {
  1391. local CMP="$1"
  1392. shift
  1393. testwarning "$@"
  1394. testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output" "$CMP"
  1395. }
  1396. testfailureequal() {
  1397. local CMP="$1"
  1398. shift
  1399. testfailure "$@"
  1400. testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output" "$CMP"
  1401. }
  1402. testfailuremsg() {
  1403. local CMP="$1"
  1404. shift
  1405. testfailure "$@"
  1406. msgtest 'Check that the output of the previous failed command has expected' 'failures and warnings'
  1407. grep '^\(W\|E\):' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output" > "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailureequal.output" 2>&1 || true
  1408. if echo "$CMP" | checkdiff - "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailureequal.output"; then
  1409. msgpass
  1410. else
  1411. echo '### Complete output ###'
  1412. cat "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
  1413. msgfail
  1414. fi
  1415. }
  1416. testfilestats() {
  1417. msgtest "Test that file $1 has $2 $3" "$4"
  1418. if [ "$4" "$3" "$(stat --format "$2" "$1")" ]; then
  1419. msgpass
  1420. else
  1421. echo >&2
  1422. ls -ld >&2 "$1" || true
  1423. echo -n >&2 "stat(1) reports for $2: "
  1424. stat --format "$2" "$1" || true
  1425. msgfail
  1426. fi
  1427. }
  1428. testaccessrights() {
  1429. testfilestats "$1" '%a' '=' "$2"
  1430. }
  1431. testwebserverlaststatuscode() {
  1432. local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
  1433. local STATUS='downloaded/webserverstatus-statusfile.log'
  1434. rm -f "$DOWNLOG" "$STATUS"
  1435. msgtest 'Test last status code from the webserver was' "$1"
  1436. if downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG" && [ "$(cat "$STATUS")" = "$1" ]; then
  1437. msgpass
  1438. else
  1439. echo >&2
  1440. if [ -n "$2" ]; then
  1441. shift
  1442. echo >&2 '#### Additionally provided output files contain:'
  1443. cat >&2 "$@"
  1444. fi
  1445. echo >&2 '#### Download log of the status code:'
  1446. cat >&2 "$DOWNLOG"
  1447. msgfail "Status was $(cat "$STATUS")"
  1448. fi
  1449. }
  1450. pause() {
  1451. echo "STOPPED execution. Press enter to continue"
  1452. local IGNORE
  1453. read IGNORE
  1454. }
  1455. listcurrentlistsdirectory() {
  1456. {
  1457. find rootdir/var/lib/apt/lists -maxdepth 1 -type d | while read line; do
  1458. stat --format '%U:%G:%a:%n' "$line"
  1459. done
  1460. find rootdir/var/lib/apt/lists -maxdepth 1 \! -type d | while read line; do
  1461. stat --format '%U:%G:%a:%s:%y:%n' "$line"
  1462. done
  1463. } | sort
  1464. }
  1465. ### convenience hacks ###
  1466. mkdir() {
  1467. # creating some directories by hand is a tedious task, so make it look simple
  1468. if [ "$*" = '-p rootdir/var/lib/apt/lists' ] || [ "$*" = "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" ] ||
  1469. [ "$*" = '-p rootdir/var/lib/apt/lists/partial' ] || [ "$*" = "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" ]; then
  1470. # only the last directory created by mkdir is effected by the -m !
  1471. command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt"
  1472. command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"
  1473. command mkdir -m 700 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
  1474. touch "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/lock"
  1475. if [ "$(id -u)" = '0' ]; then
  1476. chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
  1477. fi
  1478. else
  1479. command mkdir "$@"
  1480. fi
  1481. }
  1482. ### The following tests are run by most test methods automatically to check
  1483. ### general things about commands executed without writing the test every time.
  1484. aptautotest() {
  1485. local TESTCALL="$1"
  1486. local CMD="$2"
  1487. local FIRSTOPT="$3"
  1488. local AUTOTEST="aptautotest_$(basename "$CMD" | tr -d '-')_$(echo "$FIRSTOPT" | tr -d '-')"
  1489. if command -v $AUTOTEST >/dev/null; then
  1490. shift 3
  1491. # save and restore the *.output files from other tests
  1492. # as we might otherwise override them in these automatic tests
  1493. rm -rf ${TMPWORKINGDIRECTORY}/rootdir/tmp-before
  1494. mv ${TMPWORKINGDIRECTORY}/rootdir/tmp ${TMPWORKINGDIRECTORY}/rootdir/tmp-before
  1495. mkdir ${TMPWORKINGDIRECTORY}/rootdir/tmp
  1496. $AUTOTEST "$TESTCALL" "$@"
  1497. rm -rf ${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest
  1498. mv ${TMPWORKINGDIRECTORY}/rootdir/tmp ${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest
  1499. mv ${TMPWORKINGDIRECTORY}/rootdir/tmp-before ${TMPWORKINGDIRECTORY}/rootdir/tmp
  1500. fi
  1501. }
  1502. aptautotest_aptget_update() {
  1503. local TESTCALL="$1"
  1504. while [ -n "$2" ]; do
  1505. if [ "$2" = '--print-uris' ]; then return; fi # simulation mode
  1506. shift
  1507. done
  1508. if ! test -d "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"; then return; fi
  1509. testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
  1510. testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
  1511. # all copied files are properly chmodded
  1512. for file in $(find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" -type f ! -name 'lock'); do
  1513. testfilestats "$file" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:644"
  1514. done
  1515. if [ "$TESTCALL" = 'testsuccess' ]; then
  1516. # failure cases can retain partial files and such
  1517. testempty find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" -mindepth 1 ! \( -name 'lock' -o -name '*.FAILED' \)
  1518. fi
  1519. }
  1520. aptautotest_apt_update() { aptautotest_aptget_update "$@"; }
  1521. aptautotest_aptcdrom_add() { aptautotest_aptget_update "$@"; }
  1522. testaptautotestnodpkgwarning() {
  1523. local TESTCALL="$1"
  1524. while [ -n "$2" ]; do
  1525. if expr match "$2" '^-[a-z]*s' >/dev/null 2>&1; then return; fi # simulation mode
  1526. if expr match "$2" '^-dy\?' >/dev/null 2>&1; then return; fi # download-only mode
  1527. shift
  1528. done
  1529. testfailure grep '^dpkg: warning:.*ignor.*' "${TMPWORKINGDIRECTORY}/rootdir/tmp-before/${TESTCALL}.output"
  1530. }
  1531. aptautotest_aptget_install() { testaptautotestnodpkgwarning "$@"; }
  1532. aptautotest_aptget_remove() { testaptautotestnodpkgwarning "$@"; }
  1533. aptautotest_aptget_purge() { testaptautotestnodpkgwarning "$@"; }
  1534. aptautotest_apt_install() { testaptautotestnodpkgwarning "$@"; }
  1535. aptautotest_apt_remove() { testaptautotestnodpkgwarning "$@"; }
  1536. aptautotest_apt_purge() { testaptautotestnodpkgwarning "$@"; }