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.
 
 
 
 
 
 

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