framework 65 KB

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