framework 65 KB

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