framework 65 KB

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