framework 49 KB

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