framework 65 KB

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