framework 57 KB

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