framework 66 KB

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