framework 66 KB

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