framework 65 KB

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