framework 51 KB

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