framework 52 KB

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