framework 49 KB

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