framework 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  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. mkdir -p usr/lib/apt/solvers usr/lib/apt/planners
  288. touch var/lib/dpkg/available
  289. ln -s "${METHODSDIR}" usr/lib/apt/methods
  290. ln -s "${APTDUMPSOLVER}" usr/lib/apt/solvers/dump
  291. ln -s "${APTDUMPSOLVER}" usr/lib/apt/planners/dump
  292. ln -s "${APTINTERNALSOLVER}" usr/lib/apt/solvers/apt
  293. ln -s "${APTINTERNALPLANNER}" usr/lib/apt/planners/apt
  294. echo "Dir::Bin::Solvers \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/solvers\";" >> ../aptconfig.conf
  295. echo "Dir::Bin::Planners \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/planners\";" >> ../aptconfig.conf
  296. # use the autoremove from the BUILDDIRECTORY if its there, otherwise
  297. # system
  298. if [ -e "${BUILDDIRECTORY}/../../debian/apt.conf.autoremove" ]; then
  299. ln -s "${BUILDDIRECTORY}/../../debian/apt.conf.autoremove" etc/apt/apt.conf.d/01autoremove
  300. else
  301. ln -s /etc/apt/apt.conf.d/01autoremove etc/apt/apt.conf.d/01autoremove
  302. fi
  303. cd ..
  304. local BASENAME="${0##*/}"
  305. local PACKAGESFILE="Packages-${BASENAME#*-}"
  306. if [ -f "${TESTDIRECTORY}/${PACKAGESFILE}" ]; then
  307. cp "${TESTDIRECTORY}/${PACKAGESFILE}" aptarchive/Packages
  308. fi
  309. local SOURCESSFILE="Sources-${BASENAME#*-}"
  310. if [ -f "${TESTDIRECTORY}/${SOURCESSFILE}" ]; then
  311. cp "${TESTDIRECTORY}/${SOURCESSFILE}" aptarchive/Sources
  312. fi
  313. find "$TESTDIRECTORY" \( -name '*.pub' -o -name '*.sec' \) -exec cp '{}' keys/ \;
  314. chmod 644 keys/*
  315. ln -s "${TMPWORKINGDIRECTORY}/keys/joesixpack.pub" rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  316. echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" >> aptconfig.conf
  317. echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
  318. echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
  319. echo "Dir::Bin::Methods \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/methods\";" >> aptconfig.conf
  320. # either store apt-key were we can access it, even if we run it as a different user
  321. #cp "${BUILDDIRECTORY}/apt-key" "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/"
  322. #chmod o+rx "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/apt-key"
  323. #echo "Dir::Bin::apt-key \"${TMPWORKINGDIRECTORY}/rootdir/usr/bin/apt-key\";" >> aptconfig.conf
  324. # destroys coverage reporting though, so we disable changing user for the calling gpgv
  325. echo "Dir::Bin::apt-key \"${BUILDDIRECTORY}/apt-key\";" >> aptconfig.conf
  326. if [ "$(id -u)" = '0' ]; then
  327. echo 'Binary::gpgv::APT::Sandbox::User "root";' >> aptconfig.conf
  328. # same for the solver executables
  329. echo 'APT::Solver::RunAsUser "root";' >> aptconfig.conf
  330. echo 'APT::Planner::RunAsUser "root";' >> aptconfig.conf
  331. fi
  332. cat > "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" <<EOF
  333. #!/bin/sh
  334. set -e
  335. if [ -r '${TMPWORKINGDIRECTORY}/noopchroot.so' ]; then
  336. if [ -n "\$LD_LIBRARY_PATH" ]; then
  337. export LD_LIBRARY_PATH='${TMPWORKINGDIRECTORY}:'"\${LD_LIBRARY_PATH}"
  338. else
  339. export LD_LIBRARY_PATH='${TMPWORKINGDIRECTORY}'
  340. fi
  341. if [ -n "\$LD_PRELOAD" ]; then
  342. export LD_PRELOAD="noopchroot.so \${LD_PRELOAD}"
  343. else
  344. export LD_PRELOAD="noopchroot.so"
  345. fi
  346. fi
  347. EOF
  348. cp "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg"
  349. cat >> "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" <<EOF
  350. exec fakeroot '${DPKG:-dpkg}' --root='${TMPWORKINGDIRECTORY}/rootdir' \\
  351. --log='${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log' \\
  352. --force-not-root --force-bad-path "\$@"
  353. EOF
  354. cat >> "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg" <<EOF
  355. exec fakeroot gdb --quiet -ex run '${DPKG:-dpkg}' --args '${DPKG:-dpkg}' --root='${TMPWORKINGDIRECTORY}/rootdir' \\
  356. --log='${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log' \\
  357. --force-not-root --force-bad-path "\$@"
  358. EOF
  359. chmod +x "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg"
  360. echo "Dir::Bin::dpkg \"${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg\";" > rootdir/etc/apt/apt.conf.d/99dpkg
  361. {
  362. if ! command dpkg --assert-multi-arch >/dev/null 2>&1; then
  363. echo "DPKG::options:: \"--force-architecture\";" # Added to test multiarch before dpkg is ready for it…
  364. fi
  365. echo 'quiet "0";'
  366. echo 'quiet::NoUpdate "true";'
  367. echo 'quiet::NoStatistic "true";'
  368. # too distracting for users, but helpful to detect changes
  369. echo 'Acquire::Progress::Ignore::ShowErrorText "true";'
  370. echo 'Acquire::Progress::Diffpercent "true";'
  371. # in testcases, it can appear as if localhost has a rotation setup,
  372. # hide this as we can't really deal with it properly
  373. echo 'Acquire::Failure::ShowIP "false";'
  374. # fakeroot can't fake everything, so disabled in production but good for tests
  375. echo 'APT::Sandbox::Verify "true";'
  376. } >> aptconfig.conf
  377. cp "${TESTDIRECTORY}/apt.pem" "${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem"
  378. if [ "$(id -u)" = '0' ]; then
  379. chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem"
  380. fi
  381. echo "Acquire::https::CaInfo \"${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem\";" > rootdir/etc/apt/apt.conf.d/99https
  382. echo "Apt::Cmd::Disable-Script-Warning \"1\";" > rootdir/etc/apt/apt.conf.d/apt-binary
  383. export APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=no
  384. echo 'Acquire::Connect::AddrConfig "false";' > rootdir/etc/apt/apt.conf.d/connect-addrconfig
  385. configcompression '.' 'gz' #'bz2' 'lzma' 'xz'
  386. confighashes 'SHA256' # these are tests, not security best-practices
  387. # create some files in /tmp and look at user/group to get what this means
  388. TEST_DEFAULT_USER="$(id -un)"
  389. if [ "$(uname)" = 'GNU/kFreeBSD' ]; then
  390. TEST_DEFAULT_GROUP='root'
  391. else
  392. TEST_DEFAULT_GROUP="$(id -gn)"
  393. fi
  394. # cleanup the environment a bit
  395. # prefer our apt binaries over the system apt binaries
  396. export PATH="${BUILDDIRECTORY}:${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
  397. export LC_ALL=C.UTF-8
  398. unset LANGUAGE APT_CONFIG
  399. unset GREP_OPTIONS DEB_BUILD_PROFILES
  400. unset http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy
  401. # If gpgv supports --weak-digest, pass it to make sure we can disable SHA1
  402. if aptkey verify --weak-digest SHA1 --help 2>/dev/null >/dev/null; then
  403. echo 'Acquire::gpgv::Options { "--weak-digest"; "sha1"; };' > rootdir/etc/apt/apt.conf.d/no-sha1
  404. fi
  405. # most tests just need one signed Release file, not both
  406. export APT_DONT_SIGN='Release.gpg'
  407. if [ -r "${TESTDIRECTORY}/extra-environment" ]; then
  408. . "${TESTDIRECTORY}/extra-environment"
  409. fi
  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. logcurrentarchivedirectory() {
  1789. find "${TMPWORKINGDIRECTORY}/aptarchive/dists" -type f | while read line; do
  1790. stat --format '%U:%G:%a:%n' "$line"
  1791. done | sort > "${TMPWORKINGDIRECTORY}/rootdir/var/log/aptgetupdate.before.lst"
  1792. }
  1793. listcurrentlistsdirectory() {
  1794. {
  1795. find rootdir/var/lib/apt/lists -maxdepth 1 -type d | while read line; do
  1796. stat --format '%U:%G:%a:%n' "$line"
  1797. done
  1798. find rootdir/var/lib/apt/lists -maxdepth 1 \! -type d | while read line; do
  1799. stat --format '%U:%G:%a:%s:%y:%n' "$line"
  1800. done
  1801. } | sort
  1802. }
  1803. forallsupportedcompressors() {
  1804. rm -f "${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/00force-compressor"
  1805. for COMP in $(aptconfig dump 'APT::Compressor' --format '%f%n' | cut -d':' -f 5 | uniq); do
  1806. if [ -z "$COMP" -o "$COMP" = '.' ]; then continue; fi
  1807. "$@" "$COMP"
  1808. done
  1809. }
  1810. ### convenience hacks ###
  1811. mkdir() {
  1812. # creating some directories by hand is a tedious task, so make it look simple
  1813. local PARAMS="$*"
  1814. if [ "$PARAMS" != "${PARAMS#*rootdir/var/lib/apt/lists}" ]; then
  1815. # only the last directory created by mkdir is effected by the -m !
  1816. command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt"
  1817. command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"
  1818. command mkdir -m 700 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
  1819. touch "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/lock"
  1820. if [ "$(id -u)" = '0' ]; then
  1821. chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
  1822. fi
  1823. else
  1824. command mkdir "$@"
  1825. fi
  1826. }
  1827. ### The following tests are run by most test methods automatically to check
  1828. ### general things about commands executed without writing the test every time.
  1829. aptautotest() {
  1830. local TESTCALL="$1"
  1831. local CMD="$2"
  1832. local FIRSTOPT="$3"
  1833. local AUTOTEST="aptautotest_$(echo "${CMD##*/}_${FIRSTOPT}" | tr -d -c 'A-za-z0-9')"
  1834. if command -v $AUTOTEST >/dev/null; then
  1835. shift 3
  1836. # save and restore the *.output files from other tests
  1837. # as we might otherwise override them in these automatic tests
  1838. rm -rf "${TMPWORKINGDIRECTORY}/rootdir/tmp-before"
  1839. mv "${TMPWORKINGDIRECTORY}/rootdir/tmp" "${TMPWORKINGDIRECTORY}/rootdir/tmp-before"
  1840. mkdir "${TMPWORKINGDIRECTORY}/rootdir/tmp"
  1841. $AUTOTEST "$TESTCALL" "$@"
  1842. rm -rf "${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest"
  1843. mv "${TMPWORKINGDIRECTORY}/rootdir/tmp" "${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest"
  1844. mv "${TMPWORKINGDIRECTORY}/rootdir/tmp-before" "${TMPWORKINGDIRECTORY}/rootdir/tmp"
  1845. fi
  1846. }
  1847. aptautotest_aptget_update() {
  1848. local TESTCALL="$1"
  1849. while [ -n "$2" ]; do
  1850. if [ "$2" = '--print-uris' ]; then return; fi # simulation mode
  1851. shift
  1852. done
  1853. if ! test -d "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"; then return; fi
  1854. testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
  1855. testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
  1856. # all copied files are properly chmodded
  1857. local backupIFS="$IFS"
  1858. IFS="$(printf "\n\b")"
  1859. for file in $(find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" -type f ! -name 'lock'); do
  1860. testfilestats "$file" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:644"
  1861. done
  1862. IFS="$backupIFS"
  1863. if [ "$TESTCALL" = 'testsuccess' ]; then
  1864. # failure cases can retain partial files and such
  1865. testempty find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" -mindepth 1 ! \( -name 'lock' -o -name '*.FAILED' \)
  1866. fi
  1867. if [ -s "${TMPWORKINGDIRECTORY}/rootdir/var/log/aptgetupdate.before.lst" ]; then
  1868. testfileequal "${TMPWORKINGDIRECTORY}/rootdir/var/log/aptgetupdate.before.lst" \
  1869. "$(find "${TMPWORKINGDIRECTORY}/aptarchive/dists" -type f | while read line; do stat --format '%U:%G:%a:%n' "$line"; done | sort)"
  1870. fi
  1871. }
  1872. aptautotest_apt_update() { aptautotest_aptget_update "$@"; }
  1873. aptautotest_aptcdrom_add() { aptautotest_aptget_update "$@"; }
  1874. testaptautotestnodpkgwarning() {
  1875. local TESTCALL="$1"
  1876. while [ -n "$2" ]; do
  1877. if expr match "$2" '^-[a-z]*s' >/dev/null 2>&1; then return; fi # simulation mode
  1878. if expr match "$2" '^-dy\?' >/dev/null 2>&1; then return; fi # download-only mode
  1879. shift
  1880. done
  1881. testfailure grep '^dpkg: warning:.*ignor.*' "${TMPWORKINGDIRECTORY}/rootdir/tmp-before/${TESTCALL}.output"
  1882. }
  1883. aptautotest_aptget_install() { testaptautotestnodpkgwarning "$@"; }
  1884. aptautotest_aptget_remove() { testaptautotestnodpkgwarning "$@"; }
  1885. aptautotest_aptget_purge() { testaptautotestnodpkgwarning "$@"; }
  1886. aptautotest_apt_install() { testaptautotestnodpkgwarning "$@"; }
  1887. aptautotest_apt_remove() { testaptautotestnodpkgwarning "$@"; }
  1888. aptautotest_apt_purge() { testaptautotestnodpkgwarning "$@"; }