framework 68 KB

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