framework 49 KB

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