framework 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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 NM
  483. if [ "$(echo "$NAME" | cut -c 1-3)" = 'lib' ]; then
  484. NM="$(echo "$NAME" | cut -c 1-4)"
  485. else
  486. NM="$(echo "$NAME" | cut -c 1)"
  487. fi
  488. local ARCH="$2"
  489. local VERSION="$3"
  490. local RELEASE="${4:-unstable}"
  491. local DEPENDENCIES="$5"
  492. local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  493. If you find such a package installed on your system,
  494. something went horribly wrong! They are autogenerated
  495. und used only by testcases and surf no other propose…"}"
  496. local SECTION="${7:-others}"
  497. local PRIORITY="${8:-optional}"
  498. local FILE_TREE="$9"
  499. local COMPRESS_TYPE="${10:-gzip}"
  500. local DISTSECTION
  501. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  502. DISTSECTION="main"
  503. else
  504. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  505. fi
  506. local BUILDDIR=${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}
  507. msgtest "Build source package in version ${VERSION} for ${RELEASE} in ${DISTSECTION}" "$NAME"
  508. mkdir -p $BUILDDIR/debian/source
  509. echo "* most suckless software product ever" > ${BUILDDIR}/FEATURES
  510. echo "#!/bin/sh
  511. echo '$NAME says \"Hello!\"'" > ${BUILDDIR}/${NAME}
  512. echo "Copyleft by Joe Sixpack $(date +%Y)" > ${BUILDDIR}/debian/copyright
  513. echo "$NAME ($VERSION) $RELEASE; urgency=low
  514. * Initial release
  515. -- Joe Sixpack <joe@example.org> $(date -R)" > ${BUILDDIR}/debian/changelog
  516. echo "Source: $NAME
  517. Section: $SECTION
  518. Priority: $PRIORITY
  519. Maintainer: Joe Sixpack <joe@example.org>
  520. Standards-Version: 3.9.3" > ${BUILDDIR}/debian/control
  521. local BUILDDEPS="$(echo "$DEPENDENCIES" | grep '^Build-')"
  522. test -z "$BUILDDEPS" || echo "$BUILDDEPS" >> ${BUILDDIR}/debian/control
  523. echo "
  524. Package: $NAME" >> ${BUILDDIR}/debian/control
  525. if [ "$ARCH" = 'all' ]; then
  526. echo "Architecture: all" >> ${BUILDDIR}/debian/control
  527. else
  528. echo "Architecture: any" >> ${BUILDDIR}/debian/control
  529. fi
  530. local DEPS="$(echo "$DEPENDENCIES" | grep -v '^Build-')"
  531. test -z "$DEPS" || echo "$DEPS" >> ${BUILDDIR}/debian/control
  532. echo "Description: $DESCRIPTION" >> ${BUILDDIR}/debian/control
  533. echo '3.0 (native)' > ${BUILDDIR}/debian/source/format
  534. cd ${BUILDDIR}/..
  535. testsuccess --nomsg dpkg-source -b ${NAME}-${VERSION}
  536. cd - >/dev/null
  537. sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' ${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output \
  538. | while read SRC; do
  539. echo "pool/${SRC}" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist
  540. # if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
  541. # aptkey --keyring ./keys/joesixpack.pub --secret-keyring ./keys/joesixpack.sec --quiet --readonly \
  542. # adv --yes --default-key 'Joe Sixpack' \
  543. # --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  544. # mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  545. # fi
  546. done
  547. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  548. msgtest "Build binary package for ${RELEASE} in ${SECTION}" "$NAME"
  549. rm -rf ${BUILDDIR}/debian/tmp
  550. mkdir -p ${BUILDDIR}/debian/tmp/DEBIAN ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME} ${BUILDDIR}/debian/tmp/usr/bin
  551. cp ${BUILDDIR}/debian/copyright ${BUILDDIR}/debian/changelog ${BUILDDIR}/FEATURES ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}
  552. cp ${BUILDDIR}/${NAME} ${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}
  553. if [ -n "$FILE_TREE" ]; then
  554. cp -ar "$FILE_TREE" ${BUILDDIR}/debian/tmp
  555. fi
  556. (cd ${BUILDDIR}; dpkg-gencontrol -DArchitecture=$arch)
  557. (cd ${BUILDDIR}/debian/tmp; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
  558. local LOG="${BUILDDIR}/../${NAME}_${VERSION}_${arch}.dpkg-deb.log"
  559. # ensure the right permissions as dpkg-deb insists
  560. chmod 755 ${BUILDDIR}/debian/tmp/DEBIAN
  561. testsuccess --nomsg dpkg-deb -Z${COMPRESS_TYPE} --build ${BUILDDIR}/debian/tmp ${BUILDDIR}/..
  562. echo "pool/${NAME}_${VERSION}_${arch}.deb" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist
  563. done
  564. local CHANGEPATH="${BUILDDIR}/../${DISTSECTION}/${NM}/${NAME}/${NAME}_${VERSION}"
  565. mkdir -p $CHANGEPATH
  566. cp ${BUILDDIR}/debian/changelog $CHANGEPATH
  567. rm -rf "${BUILDDIR}"
  568. msgdone "info"
  569. }
  570. buildpackage() {
  571. local BUILDDIR=$1
  572. local RELEASE=$2
  573. local SECTION=$3
  574. local ARCH=$(getarchitecture $4)
  575. local PKGNAME="$(echo "$BUILDDIR" | grep -o '[^/]*$')"
  576. local BUILDLOG="$(readlink -f "${BUILDDIR}/../${PKGNAME}_${RELEASE}_${SECTION}.dpkg-bp.log")"
  577. msgtest "Build package for ${RELEASE} in ${SECTION}" "$PKGNAME"
  578. cd $BUILDDIR
  579. if [ "$ARCH" = "all" ]; then
  580. ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
  581. fi
  582. testsuccess --nomsg dpkg-buildpackage -uc -us -a$ARCH
  583. cp ${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output $BUILDLOG
  584. local PKGS="$(grep '^dpkg-deb: building package' $BUILDLOG | cut -d'/' -f 2 | sed -e "s#'\.##")"
  585. local SRCS="$(grep '^dpkg-source: info: building' $BUILDLOG | grep -o '[a-z0-9._+~-]*$')"
  586. cd - > /dev/null
  587. for PKG in $PKGS; do
  588. echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
  589. done
  590. for SRC in $SRCS; do
  591. echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
  592. done
  593. }
  594. buildaptarchive() {
  595. if [ -d incoming ]; then
  596. buildaptarchivefromincoming "$@"
  597. else
  598. buildaptarchivefromfiles "$@"
  599. fi
  600. }
  601. createaptftparchiveconfig() {
  602. local COMPRESSORS="$(cut -d' ' -f 1 ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | tr '\n' ' ')"
  603. local COMPRESSORS="${COMPRESSORS%* }"
  604. local ARCHS="$(getarchitectures)"
  605. echo -n 'Dir {
  606. ArchiveDir "' >> ftparchive.conf
  607. echo -n $(readlink -f .) >> ftparchive.conf
  608. echo -n '";
  609. CacheDir "' >> ftparchive.conf
  610. echo -n $(readlink -f ..) >> ftparchive.conf
  611. echo -n '";
  612. FileListDir "' >> ftparchive.conf
  613. echo -n $(readlink -f pool/) >> ftparchive.conf
  614. echo -n '";
  615. };
  616. Default {
  617. Packages::Compress "'"$COMPRESSORS"'";
  618. Sources::Compress "'"$COMPRESSORS"'";
  619. Contents::Compress "'"$COMPRESSORS"'";
  620. Translation::Compress "'"$COMPRESSORS"'";
  621. LongDescription "false";
  622. };
  623. TreeDefault {
  624. Directory "pool/";
  625. SrcDirectory "pool/";
  626. };
  627. APT {
  628. FTPArchive {
  629. Release {
  630. Origin "joesixpack";
  631. Label "apttestcases";
  632. Suite "unstable";
  633. Description "repository with dummy packages";
  634. Architectures "' >> ftparchive.conf
  635. echo -n "$ARCHS" >> ftparchive.conf
  636. echo 'source";
  637. };
  638. };
  639. };' >> ftparchive.conf
  640. for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
  641. echo -n 'tree "dists/' >> ftparchive.conf
  642. echo -n "$DIST" >> ftparchive.conf
  643. echo -n '" {
  644. Architectures "' >> ftparchive.conf
  645. echo -n "$ARCHS" >> ftparchive.conf
  646. echo -n 'source";
  647. FileList "' >> ftparchive.conf
  648. echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
  649. echo -n '";
  650. SourceFileList "' >> ftparchive.conf
  651. echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
  652. echo -n '";
  653. Sections "' >> ftparchive.conf
  654. 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
  655. echo '";
  656. };' >> ftparchive.conf
  657. done
  658. }
  659. buildaptftparchivedirectorystructure() {
  660. local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
  661. for DIST in $DISTS; do
  662. local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
  663. for SECTION in $SECTIONS; do
  664. local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
  665. for ARCH in $ARCHS; do
  666. mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
  667. done
  668. mkdir -p dists/${DIST}/${SECTION}/source
  669. mkdir -p dists/${DIST}/${SECTION}/i18n
  670. done
  671. done
  672. }
  673. insertpackage() {
  674. local RELEASES="$1"
  675. local NAME="$2"
  676. local ARCH="$3"
  677. local VERSION="$4"
  678. local DEPENDENCIES="$5"
  679. local PRIORITY="${6:-optional}"
  680. local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASES}
  681. If you find such a package installed on your system,
  682. something went horribly wrong! They are autogenerated
  683. und used only by testcases and surf no other propose…"}"
  684. local ARCHS=""
  685. for RELEASE in $(printf '%s' "$RELEASES" | tr ',' '\n'); do
  686. if [ "$RELEASE" = 'installed' ]; then
  687. insertinstalledpackage "$2" "$3" "$4" "$5" "$6" "$7"
  688. continue
  689. fi
  690. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  691. if [ "$arch" = 'all' -o "$arch" = 'none' ]; then
  692. ARCHS="$(getarchitectures)"
  693. else
  694. ARCHS="$arch"
  695. fi
  696. for BUILDARCH in $ARCHS; do
  697. local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
  698. mkdir -p $PPATH
  699. local FILE="${PPATH}/Packages"
  700. echo "Package: $NAME
  701. Priority: $PRIORITY
  702. Section: other
  703. Installed-Size: 42
  704. Maintainer: Joe Sixpack <joe@example.org>" >> $FILE
  705. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  706. echo "Version: $VERSION
  707. Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
  708. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  709. echo "Description: $(printf '%s' "$DESCRIPTION" | head -n 1)" >> $FILE
  710. echo "Description-md5: $(printf '%s' "$DESCRIPTION" | md5sum | cut -d' ' -f 1)" >> $FILE
  711. echo >> $FILE
  712. done
  713. done
  714. mkdir -p aptarchive/dists/${RELEASE}/main/source aptarchive/dists/${RELEASE}/main/i18n
  715. touch aptarchive/dists/${RELEASE}/main/source/Sources
  716. echo "Package: $NAME
  717. Description-md5: $(printf '%s' "$DESCRIPTION" | md5sum | cut -d' ' -f 1)
  718. Description-en: $DESCRIPTION
  719. " >> aptarchive/dists/${RELEASE}/main/i18n/Translation-en
  720. done
  721. }
  722. insertsource() {
  723. local RELEASE="$1"
  724. local NAME="$2"
  725. local ARCH="$3"
  726. local VERSION="$4"
  727. local DEPENDENCIES="$5"
  728. local ARCHS=""
  729. local SPATH="aptarchive/dists/${RELEASE}/main/source"
  730. mkdir -p $SPATH
  731. local FILE="${SPATH}/Sources"
  732. echo "Package: $NAME
  733. Binary: $NAME
  734. Version: $VERSION
  735. Maintainer: Joe Sixpack <joe@example.org>
  736. Architecture: $ARCH" >> $FILE
  737. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  738. echo "Files:
  739. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.dsc
  740. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.tar.gz
  741. " >> $FILE
  742. }
  743. insertinstalledpackage() {
  744. local NAME="$1"
  745. local ARCH="$2"
  746. local VERSION="$3"
  747. local DEPENDENCIES="$4"
  748. local PRIORITY="${5:-optional}"
  749. local STATUS="${6:-install ok installed}"
  750. local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/installed
  751. If you find such a package installed on your system,
  752. something went horribly wrong! They are autogenerated
  753. und used only by testcases and surf no other propose…"}"
  754. local FILE='rootdir/var/lib/dpkg/status'
  755. local INFO='rootdir/var/lib/dpkg/info'
  756. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  757. echo "Package: $NAME
  758. Status: $STATUS
  759. Priority: $PRIORITY
  760. Section: other
  761. Installed-Size: 42
  762. Maintainer: Joe Sixpack <joe@example.org>
  763. Version: $VERSION" >> $FILE
  764. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  765. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  766. echo "Description: $DESCRIPTION" >> $FILE
  767. echo >> $FILE
  768. if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
  769. echo -n > ${INFO}/${NAME}:${arch}.list
  770. else
  771. echo -n > ${INFO}/${NAME}.list
  772. fi
  773. done
  774. }
  775. buildaptarchivefromincoming() {
  776. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
  777. cd aptarchive
  778. [ -e pool ] || ln -s ../incoming pool
  779. [ -e ftparchive.conf ] || createaptftparchiveconfig
  780. [ -e dists ] || buildaptftparchivedirectorystructure
  781. msgninfo "\tGenerate Packages, Sources and Contents files… "
  782. testsuccess aptftparchive generate ftparchive.conf
  783. cd - > /dev/null
  784. msgdone "info"
  785. generatereleasefiles "$@"
  786. }
  787. buildaptarchivefromfiles() {
  788. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
  789. local DIR='aptarchive'
  790. if [ -d "${DIR}/dists" ]; then DIR="${DIR}/dists"; fi
  791. find "$DIR" -name 'Packages' -o -name 'Sources' -o -name 'Translation-*' | while read line; do
  792. msgninfo "\t${line} file… "
  793. compressfile "$line" "$1"
  794. msgdone "info"
  795. done
  796. generatereleasefiles "$@"
  797. }
  798. compressfile() {
  799. cat ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | while read compressor extension command; do
  800. if [ "$compressor" = '.' ]; then
  801. if [ -n "$2" ]; then
  802. touch -d "$2" "$1"
  803. fi
  804. continue
  805. fi
  806. cat "$1" | $command > "${1}.${extension}"
  807. if [ -n "$2" ]; then
  808. touch -d "$2" "${1}.${extension}"
  809. fi
  810. done
  811. }
  812. # can be overridden by testcases for their pleasure
  813. getcodenamefromsuite() {
  814. case "$1" in
  815. unstable) echo 'sid';;
  816. *) echo -n "$1";;
  817. esac
  818. }
  819. getreleaseversionfromsuite() { true; }
  820. getlabelfromsuite() { true; }
  821. getoriginfromsuite() { true; }
  822. generatereleasefiles() {
  823. # $1 is the Date header and $2 is the ValidUntil header to be set
  824. # both should be given in notation date/touch can understand
  825. msgninfo "\tGenerate Release files… "
  826. if [ -e aptarchive/dists ]; then
  827. for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
  828. local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
  829. local CODENAME="$(getcodenamefromsuite $SUITE)"
  830. local VERSION="$(getreleaseversionfromsuite $SUITE)"
  831. local LABEL="$(getlabelfromsuite $SUITE)"
  832. local ORIGIN="$(getoriginfromsuite $SUITE)"
  833. if [ -n "$VERSION" ]; then
  834. VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
  835. fi
  836. if [ -n "$LABEL" ]; then
  837. LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
  838. fi
  839. if [ -n "$ORIGIN" ]; then
  840. ORIGIN="-o APT::FTPArchive::Release::Origin=${ORIGIN}"
  841. fi
  842. aptftparchive -qq release $dir \
  843. -o APT::FTPArchive::Release::Suite="${SUITE}" \
  844. -o APT::FTPArchive::Release::Codename="${CODENAME}" \
  845. ${LABEL} \
  846. ${ORIGIN} \
  847. ${VERSION} \
  848. | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
  849. if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
  850. sed -i '/^Date: / a\
  851. NotAutomatic: yes' $dir/Release
  852. fi
  853. if [ -n "$1" -a "$1" != "now" ]; then
  854. sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
  855. fi
  856. if [ -n "$2" ]; then
  857. sed -i "/^Date: / a\
  858. Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
  859. fi
  860. done
  861. else
  862. aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
  863. fi
  864. if [ -n "$1" -a "$1" != "now" ]; then
  865. for release in $(find ./aptarchive -name 'Release'); do
  866. touch -d "$1" $release
  867. done
  868. fi
  869. msgdone "info"
  870. }
  871. setupdistsaptarchive() {
  872. local APTARCHIVE=$(readlink -f ./aptarchive)
  873. rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
  874. rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
  875. for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
  876. SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
  877. msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
  878. echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
  879. echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
  880. msgdone "info"
  881. done
  882. }
  883. setupflataptarchive() {
  884. local APTARCHIVE=$(readlink -f ./aptarchive)
  885. if [ -f ${APTARCHIVE}/Packages ]; then
  886. msgninfo "\tadd deb sources.list line… "
  887. echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  888. msgdone "info"
  889. else
  890. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  891. fi
  892. if [ -f ${APTARCHIVE}/Sources ]; then
  893. msgninfo "\tadd deb-src sources.list line… "
  894. echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  895. msgdone "info"
  896. else
  897. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  898. fi
  899. }
  900. setupaptarchive() {
  901. local NOUPDATE=0
  902. if [ "$1" = '--no-update' ]; then
  903. NOUPDATE=1
  904. shift
  905. fi
  906. buildaptarchive "$@"
  907. if [ -e aptarchive/dists ]; then
  908. setupdistsaptarchive
  909. else
  910. setupflataptarchive
  911. fi
  912. signreleasefiles 'Joe Sixpack'
  913. if [ "1" != "$NOUPDATE" ]; then
  914. testsuccess aptget update -o Debug::pkgAcquire::Worker=true -o Debug::Acquire::gpgv=true
  915. fi
  916. }
  917. signreleasefiles() {
  918. local SIGNER="${1:-Joe Sixpack}"
  919. local REPODIR="${2:-aptarchive}"
  920. local KEY="keys/$(echo "$SIGNER" | tr 'A-Z' 'a-z' | sed 's# ##g')"
  921. local GPG="aptkey --quiet --keyring ${KEY}.pub --secret-keyring ${KEY}.sec --readonly adv --batch --yes"
  922. msgninfo "\tSign archive with $SIGNER key $KEY… "
  923. local REXKEY='keys/rexexpired'
  924. local SECEXPIREBAK="${REXKEY}.sec.bak"
  925. local PUBEXPIREBAK="${REXKEY}.pub.bak"
  926. if [ "${SIGNER}" = 'Rex Expired' ]; then
  927. # the key is expired, so gpg doesn't allow to sign with and the --faked-system-time
  928. # option doesn't exist anymore (and using faketime would add a new obscure dependency)
  929. # therefore we 'temporary' make the key not expired and restore a backup after signing
  930. cp ${REXKEY}.sec $SECEXPIREBAK
  931. cp ${REXKEY}.pub $PUBEXPIREBAK
  932. local SECUNEXPIRED="${REXKEY}.sec.unexpired"
  933. local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
  934. if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
  935. cp $SECUNEXPIRED ${REXKEY}.sec
  936. cp $PUBUNEXPIRED ${REXKEY}.pub
  937. else
  938. if ! printf "expire\n1w\nsave\n" | $GPG --default-key "$SIGNER" --command-fd 0 --edit-key "${SIGNER}" >setexpire.gpg 2>&1; then
  939. cat setexpire.gpg
  940. exit 1
  941. fi
  942. cp ${REXKEY}.sec $SECUNEXPIRED
  943. cp ${REXKEY}.pub $PUBUNEXPIRED
  944. fi
  945. fi
  946. for RELEASE in $(find ${REPODIR}/ -name Release); do
  947. $GPG --default-key "$SIGNER" --armor --detach-sign --sign --output ${RELEASE}.gpg ${RELEASE}
  948. local INRELEASE="$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')"
  949. $GPG --default-key "$SIGNER" --clearsign --output $INRELEASE $RELEASE
  950. # we might have set a specific date for the Release file, so copy it
  951. touch -d "$(stat --format "%y" ${RELEASE})" ${RELEASE}.gpg ${INRELEASE}
  952. done
  953. if [ -f "$SECEXPIREBAK" ] && [ -f "$PUBEXPIREBAK" ]; then
  954. mv -f $SECEXPIREBAK ${REXKEY}.sec
  955. mv -f $PUBEXPIREBAK ${REXKEY}.pub
  956. fi
  957. msgdone "info"
  958. }
  959. redatereleasefiles() {
  960. local DATE="$(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')"
  961. for release in $(find aptarchive/ -name 'Release'); do
  962. sed -i "s/^Date: .*$/Date: ${DATE}/" $release
  963. touch -d "$DATE" $release
  964. done
  965. signreleasefiles "${2:-Joe Sixpack}"
  966. }
  967. webserverconfig() {
  968. local WEBSERVER="${3:-http://localhost:8080}"
  969. local NOCHECK=false
  970. if [ "$1" = '--no-check' ]; then
  971. NOCHECK=true
  972. shift
  973. fi
  974. local DOWNLOG='rootdir/tmp/download-testfile.log'
  975. local STATUS='downloaded/webserverconfig.status'
  976. rm -f "$STATUS" "$DOWNLOG"
  977. local URI
  978. if [ -n "$2" ]; then
  979. msgtest "Set webserver config option '${1}' to" "$2"
  980. URI="${WEBSERVER}/_config/set/${1}/${2}"
  981. else
  982. msgtest 'Clear webserver config option' "${1}"
  983. URI="${WEBSERVER}/_config/clear/${1}"
  984. fi
  985. if downloadfile "$URI" "$STATUS" > "$DOWNLOG"; then
  986. msgpass
  987. else
  988. cat "$DOWNLOG" "$STATUS" || true
  989. msgfail
  990. fi
  991. $NOCHECK || testwebserverlaststatuscode '200'
  992. }
  993. rewritesourceslist() {
  994. local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive")"
  995. for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
  996. sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#http://localhost:8080/#${1}#" -e "s#https://localhost:4433/#${1}#"
  997. done
  998. }
  999. # wait for up to 10s for a pid file to appear to avoid possible race
  1000. # when a helper is started and dosn't write the PID quick enough
  1001. waitforpidfile() {
  1002. local PIDFILE="$1"
  1003. for i in $(seq 10); do
  1004. if test -s "$PIDFILE"; then
  1005. return 0
  1006. fi
  1007. sleep 1
  1008. done
  1009. msgdie "waiting for $PIDFILE failed"
  1010. return 1
  1011. }
  1012. changetowebserver() {
  1013. if [ "$1" != '--no-rewrite' ]; then
  1014. rewritesourceslist 'http://localhost:8080/'
  1015. else
  1016. shift
  1017. fi
  1018. if test -x ${APTWEBSERVERBINDIR}/aptwebserver; then
  1019. cd aptarchive
  1020. local LOG="webserver.log"
  1021. if ! aptwebserver -o aptwebserver::fork=1 "$@" >$LOG 2>&1 ; then
  1022. cat $LOG
  1023. false
  1024. fi
  1025. waitforpidfile aptwebserver.pid
  1026. local PID="$(cat aptwebserver.pid)"
  1027. if [ -z "$PID" ]; then
  1028. msgdie 'Could not fork aptwebserver successfully'
  1029. fi
  1030. addtrap "kill $PID;"
  1031. cd - > /dev/null
  1032. else
  1033. msgdie 'You have to build aptwerbserver or install a webserver'
  1034. fi
  1035. }
  1036. changetohttpswebserver() {
  1037. if ! which stunnel4 >/dev/null; then
  1038. msgdie 'You need to install stunnel4 for https testcases'
  1039. fi
  1040. if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
  1041. changetowebserver --no-rewrite "$@"
  1042. fi
  1043. echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
  1044. cert = ${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem
  1045. output = /dev/null
  1046. [https]
  1047. accept = 4433
  1048. connect = 8080
  1049. " > ${TMPWORKINGDIRECTORY}/stunnel.conf
  1050. stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
  1051. waitforpidfile "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid"
  1052. local PID="$(cat ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid)"
  1053. if [ -z "$PID" ]; then
  1054. msgdie 'Could not fork stunnel4 successfully'
  1055. fi
  1056. addtrap 'prefix' "kill ${PID};"
  1057. rewritesourceslist 'https://localhost:4433/'
  1058. }
  1059. changetocdrom() {
  1060. mkdir -p rootdir/media/cdrom/.disk
  1061. local CD="$(readlink -f rootdir/media/cdrom)"
  1062. echo "acquire::cdrom::mount \"${CD}\";
  1063. acquire::cdrom::${CD}/::mount \"mv ${CD}-unmounted ${CD}\";
  1064. acquire::cdrom::${CD}/::umount \"mv ${CD} ${CD}-unmounted\";
  1065. acquire::cdrom::autodetect 0;" > rootdir/etc/apt/apt.conf.d/00cdrom
  1066. echo -n "$1" > ${CD}/.disk/info
  1067. if [ ! -d aptarchive/dists ]; then
  1068. msgdie 'Flat file archive cdroms can not be created currently'
  1069. return 1
  1070. fi
  1071. mv aptarchive/dists "$CD"
  1072. ln -s "$(readlink -f ./incoming)" $CD/pool
  1073. find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
  1074. # start with an unmounted disk
  1075. mv "${CD}" "${CD}-unmounted"
  1076. # we don't want the disk to be modifiable
  1077. addtrap 'prefix' "chmod -f -R +w $PWD/rootdir/media/cdrom/dists/ $PWD/rootdir/media/cdrom-unmounted/dists/ || true;"
  1078. chmod -R 555 rootdir/media/cdrom-unmounted/dists
  1079. }
  1080. downloadfile() {
  1081. local PROTO="${1%%:*}"
  1082. if ! apthelper -o Debug::Acquire::${PROTO}=1 -o Debug::pkgAcquire::Worker=1 \
  1083. download-file "$1" "$2" "$3" 2>&1 ; then
  1084. return 1
  1085. fi
  1086. # only if the file exists the download was successful
  1087. if [ -r "$2" ]; then
  1088. return 0
  1089. else
  1090. return 1
  1091. fi
  1092. }
  1093. checkdiff() {
  1094. local DIFFTEXT="$(command diff -u "$@" 2>&1 | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  1095. if [ -n "$DIFFTEXT" ]; then
  1096. echo >&2
  1097. echo >&2 "$DIFFTEXT"
  1098. return 1
  1099. else
  1100. return 0
  1101. fi
  1102. }
  1103. testfileequal() {
  1104. local FILE="$1"
  1105. shift
  1106. msgtest "Test for correctness of file" "$FILE"
  1107. if [ -z "$*" ]; then
  1108. echo -n "" | checkdiff - $FILE && msgpass || msgfail
  1109. else
  1110. echo "$*" | checkdiff - $FILE && msgpass || msgfail
  1111. fi
  1112. }
  1113. testempty() {
  1114. msgtest "Test for no output of" "$*"
  1115. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testempty.comparefile"
  1116. if "$@" >$COMPAREFILE 2>&1 && test ! -s $COMPAREFILE; then
  1117. msgpass
  1118. else
  1119. echo
  1120. cat $COMPAREFILE
  1121. msgfail
  1122. fi
  1123. aptautotest 'testempty' "$@"
  1124. }
  1125. testequal() {
  1126. local MSG='Test of equality of'
  1127. if [ "$1" = '--nomsg' ]; then
  1128. MSG=''
  1129. shift
  1130. fi
  1131. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
  1132. echo "$1" > $COMPAREFILE
  1133. shift
  1134. if [ -n "$MSG" ]; then
  1135. msgtest "$MSG" "$*"
  1136. fi
  1137. "$@" 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1138. aptautotest 'testequal' "$@"
  1139. }
  1140. testequalor2() {
  1141. local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
  1142. local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
  1143. local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
  1144. echo "$1" > $COMPAREFILE1
  1145. echo "$2" > $COMPAREFILE2
  1146. shift 2
  1147. msgtest "Test for equality OR of" "$*"
  1148. "$@" >$COMPAREAGAINST 2>&1 || true
  1149. if checkdiff $COMPAREFILE1 $COMPAREAGAINST >/dev/null 2>&1 || \
  1150. checkdiff $COMPAREFILE2 $COMPAREAGAINST >/dev/null 2>&1
  1151. then
  1152. msgpass
  1153. else
  1154. echo -n "\n${CINFO}Diff against OR 1${CNORMAL}"
  1155. checkdiff $COMPAREFILE1 $COMPAREAGAINST || true
  1156. echo -n "${CINFO}Diff against OR 2${CNORMAL}"
  1157. checkdiff $COMPAREFILE2 $COMPAREAGAINST || true
  1158. msgfail
  1159. fi
  1160. aptautotest 'testequalor2' "$@"
  1161. }
  1162. testshowvirtual() {
  1163. local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
  1164. local PACKAGE="$1"
  1165. shift
  1166. while [ -n "$1" ]; do
  1167. VIRTUAL="${VIRTUAL}
  1168. N: Can't select versions from package '$1' as it is purely virtual"
  1169. PACKAGE="${PACKAGE} $1"
  1170. shift
  1171. done
  1172. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  1173. VIRTUAL="${VIRTUAL}
  1174. N: No packages found"
  1175. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
  1176. local ARCH="$(getarchitecture 'native')"
  1177. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
  1178. aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1179. }
  1180. testnopackage() {
  1181. msgtest "Test for non-existent packages" "apt-cache show $*"
  1182. local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
  1183. if [ -n "$SHOWPKG" ]; then
  1184. echo >&2
  1185. echo >&2 "$SHOWPKG"
  1186. msgfail
  1187. else
  1188. msgpass
  1189. fi
  1190. }
  1191. testdpkgstatus() {
  1192. local STATE="$1"
  1193. local NR="$2"
  1194. shift 2
  1195. msgtest "Test that $NR package(s) are in state $STATE with" "dpkg -l $*"
  1196. local PKGS="$(dpkg -l "$@" 2>/dev/null | grep "^${STATE}" | wc -l)"
  1197. if [ "$PKGS" != $NR ]; then
  1198. echo >&2 $PKGS
  1199. dpkg -l "$@" | grep '^[a-z]' >&2
  1200. msgfail
  1201. else
  1202. msgpass
  1203. fi
  1204. }
  1205. testdpkginstalled() {
  1206. testdpkgstatus 'ii' "$#" "$@"
  1207. }
  1208. testdpkgnotinstalled() {
  1209. testdpkgstatus 'ii' '0' "$@"
  1210. }
  1211. testmarkedauto() {
  1212. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
  1213. if [ -n "$1" ]; then
  1214. msgtest 'Test for correctly marked as auto-installed' "$*"
  1215. while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
  1216. else
  1217. msgtest 'Test for correctly marked as auto-installed' 'no package'
  1218. echo -n > $COMPAREFILE
  1219. fi
  1220. aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1221. }
  1222. msgfailoutput() {
  1223. local MSG="$1"
  1224. local OUTPUT="$2"
  1225. shift 2
  1226. echo >&2
  1227. if [ "$1" = 'grep' ]; then
  1228. while [ -n "$2" ]; do shift; done
  1229. echo "#### Complete file: $1 ####"
  1230. cat >&2 "$1" || true
  1231. echo '#### grep output ####'
  1232. elif [ "$1" = 'test' ]; then
  1233. # doesn't support ! or non-file flags
  1234. msgfailoutputstatfile() {
  1235. local FILEFLAGS='^-[bcdefgGhkLOprsStuwx]$'
  1236. if expr match "$1" "$FILEFLAGS" >/dev/null; then
  1237. echo "#### stat(2) of file: $2 ####"
  1238. stat "$2" || true
  1239. fi
  1240. }
  1241. msgfailoutputstatfile "$2" "$3"
  1242. while [ -n "$5" ] && [ "$4" = '-o' -o "$4" = '-a' ]; do
  1243. shift 3
  1244. msgfailoutputstatfile "$2" "$3"
  1245. done
  1246. echo '#### test output ####'
  1247. fi
  1248. cat >&2 $OUTPUT
  1249. msgfail "$MSG"
  1250. }
  1251. testsuccess() {
  1252. if [ "$1" = '--nomsg' ]; then
  1253. shift
  1254. else
  1255. msgtest 'Test for successful execution of' "$*"
  1256. fi
  1257. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output"
  1258. if "$@" >${OUTPUT} 2>&1; then
  1259. if expr match "$1" '^apt.*' >/dev/null; then
  1260. if grep -q -E ' runtime error: ' "$OUTPUT"; then
  1261. msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
  1262. elif grep -q -E '^[WE]: ' "$OUTPUT"; then
  1263. msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
  1264. else
  1265. msgpass
  1266. fi
  1267. else
  1268. msgpass
  1269. fi
  1270. else
  1271. local EXITCODE=$?
  1272. msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
  1273. fi
  1274. aptautotest 'testsuccess' "$@"
  1275. }
  1276. testwarning() {
  1277. if [ "$1" = '--nomsg' ]; then
  1278. shift
  1279. else
  1280. msgtest 'Test for successful execution with warnings of' "$*"
  1281. fi
  1282. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output"
  1283. if "$@" >${OUTPUT} 2>&1; then
  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 'successful run, but output contains errors' "$OUTPUT" "$@"
  1289. elif ! grep -q -E '^W: ' "$OUTPUT"; then
  1290. msgfailoutput 'successful run, but output contains no warnings' "$OUTPUT" "$@"
  1291. else
  1292. msgpass
  1293. fi
  1294. else
  1295. msgpass
  1296. fi
  1297. else
  1298. local EXITCODE=$?
  1299. echo >&2
  1300. cat >&2 $OUTPUT
  1301. msgfail "exitcode $EXITCODE"
  1302. fi
  1303. aptautotest 'testwarning' "$@"
  1304. }
  1305. testfailure() {
  1306. if [ "$1" = '--nomsg' ]; then
  1307. shift
  1308. else
  1309. msgtest 'Test for failure in execution of' "$*"
  1310. fi
  1311. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
  1312. if "$@" >${OUTPUT} 2>&1; then
  1313. local EXITCODE=$?
  1314. msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
  1315. else
  1316. local EXITCODE=$?
  1317. if expr match "$1" '^apt.*' >/dev/null; then
  1318. if grep -q -E ' runtime error: ' "$OUTPUT"; then
  1319. msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
  1320. elif ! grep -q -E '^E: ' "$OUTPUT"; then
  1321. msgfailoutput "run failed with exitcode ${EXITCODE}, but with no errors" "$OUTPUT" "$@"
  1322. else
  1323. msgpass
  1324. fi
  1325. else
  1326. msgpass
  1327. fi
  1328. fi
  1329. aptautotest 'testfailure' "$@"
  1330. }
  1331. testsuccessequal() {
  1332. local CMP="$1"
  1333. shift
  1334. testsuccess "$@"
  1335. testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output" "$CMP"
  1336. }
  1337. testwarningequal() {
  1338. local CMP="$1"
  1339. shift
  1340. testwarning "$@"
  1341. testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output" "$CMP"
  1342. }
  1343. testfailureequal() {
  1344. local CMP="$1"
  1345. shift
  1346. testfailure "$@"
  1347. testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output" "$CMP"
  1348. }
  1349. testfailuremsg() {
  1350. local CMP="$1"
  1351. shift
  1352. testfailure "$@"
  1353. msgtest 'Check that the output of the previous failed command has expected' 'failures and warnings'
  1354. grep '^\(W\|E\):' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output" > "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailureequal.output" 2>&1 || true
  1355. if echo "$CMP" | checkdiff - "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailureequal.output"; then
  1356. msgpass
  1357. else
  1358. echo '### Complete output ###'
  1359. cat "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
  1360. msgfail
  1361. fi
  1362. }
  1363. testfilestats() {
  1364. msgtest "Test that file $1 has $2 $3" "$4"
  1365. if [ "$4" "$3" "$(stat --format "$2" "$1")" ]; then
  1366. msgpass
  1367. else
  1368. echo >&2
  1369. ls -ld >&2 "$1" || true
  1370. echo -n >&2 "stat(1) reports for $2: "
  1371. stat --format "$2" "$1" || true
  1372. msgfail
  1373. fi
  1374. }
  1375. testaccessrights() {
  1376. testfilestats "$1" '%a' '=' "$2"
  1377. }
  1378. testwebserverlaststatuscode() {
  1379. local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
  1380. local STATUS='downloaded/webserverstatus-statusfile.log'
  1381. rm -f "$DOWNLOG" "$STATUS"
  1382. msgtest 'Test last status code from the webserver was' "$1"
  1383. if downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG" && [ "$(cat "$STATUS")" = "$1" ]; then
  1384. msgpass
  1385. else
  1386. echo >&2
  1387. if [ -n "$2" ]; then
  1388. shift
  1389. echo >&2 '#### Additionally provided output files contain:'
  1390. cat >&2 "$@"
  1391. fi
  1392. echo >&2 '#### Download log of the status code:'
  1393. cat >&2 "$DOWNLOG"
  1394. msgfail "Status was $(cat "$STATUS")"
  1395. fi
  1396. }
  1397. pause() {
  1398. echo "STOPPED execution. Press enter to continue"
  1399. local IGNORE
  1400. read IGNORE
  1401. }
  1402. listcurrentlistsdirectory() {
  1403. {
  1404. find rootdir/var/lib/apt/lists -maxdepth 1 -type d | while read line; do
  1405. stat --format '%U:%G:%a:%n' "$line"
  1406. done
  1407. find rootdir/var/lib/apt/lists -maxdepth 1 \! -type d | while read line; do
  1408. stat --format '%U:%G:%a:%s:%y:%n' "$line"
  1409. done
  1410. } | sort
  1411. }
  1412. ### convinience hacks ###
  1413. mkdir() {
  1414. # creating some directories by hand is a tedious task, so make it look simple
  1415. if [ "$*" = '-p rootdir/var/lib/apt/lists' ] || [ "$*" = "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" ] ||
  1416. [ "$*" = '-p rootdir/var/lib/apt/lists/partial' ] || [ "$*" = "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" ]; then
  1417. # only the last directory created by mkdir is effected by the -m !
  1418. command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt"
  1419. command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"
  1420. command mkdir -m 700 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
  1421. touch "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/lock"
  1422. if [ "$(id -u)" = '0' ]; then
  1423. chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
  1424. fi
  1425. else
  1426. command mkdir "$@"
  1427. fi
  1428. }
  1429. ### The following tests are run by most test methods automatically to check
  1430. ### general things about commands executed without writing the test every time.
  1431. aptautotest() {
  1432. local TESTCALL="$1"
  1433. local CMD="$2"
  1434. local FIRSTOPT="$3"
  1435. local AUTOTEST="aptautotest_$(basename "$CMD" | tr -d '-')_$(echo "$FIRSTOPT" | tr -d '-')"
  1436. if command -v $AUTOTEST >/dev/null; then
  1437. shift 3
  1438. # save and restore the *.output files from other tests
  1439. # as we might otherwise override them in these automatic tests
  1440. rm -rf ${TMPWORKINGDIRECTORY}/rootdir/tmp-before
  1441. mv ${TMPWORKINGDIRECTORY}/rootdir/tmp ${TMPWORKINGDIRECTORY}/rootdir/tmp-before
  1442. mkdir ${TMPWORKINGDIRECTORY}/rootdir/tmp
  1443. $AUTOTEST "$TESTCALL" "$@"
  1444. rm -rf ${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest
  1445. mv ${TMPWORKINGDIRECTORY}/rootdir/tmp ${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest
  1446. mv ${TMPWORKINGDIRECTORY}/rootdir/tmp-before ${TMPWORKINGDIRECTORY}/rootdir/tmp
  1447. fi
  1448. }
  1449. aptautotest_aptget_update() {
  1450. local TESTCALL="$1"
  1451. while [ -n "$2" ]; do
  1452. if [ "$2" = '--print-uris' ]; then return; fi # simulation mode
  1453. shift
  1454. done
  1455. if ! test -d "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"; then return; fi
  1456. testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
  1457. testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
  1458. # all copied files are properly chmodded
  1459. for file in $(find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" -type f ! -name 'lock'); do
  1460. testfilestats "$file" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:644"
  1461. done
  1462. if [ "$TESTCALL" = 'testsuccess' ]; then
  1463. # failure cases can retain partial files and such
  1464. testempty find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" -mindepth 1 ! \( -name 'lock' -o -name '*.FAILED' \)
  1465. fi
  1466. }
  1467. aptautotest_apt_update() { aptautotest_aptget_update "$@"; }
  1468. aptautotest_aptcdrom_add() { aptautotest_aptget_update "$@"; }
  1469. testaptautotestnodpkgwarning() {
  1470. local TESTCALL="$1"
  1471. while [ -n "$2" ]; do
  1472. if expr match "$2" '^-[a-z]*s' >/dev/null 2>&1; then return; fi # simulation mode
  1473. if expr match "$2" '^-dy\?' >/dev/null 2>&1; then return; fi # download-only mode
  1474. shift
  1475. done
  1476. testfailure grep '^dpkg: warning:.*ignor.*' "${TMPWORKINGDIRECTORY}/rootdir/tmp-before/${TESTCALL}.output"
  1477. }
  1478. aptautotest_aptget_install() { testaptautotestnodpkgwarning "$@"; }
  1479. aptautotest_aptget_remove() { testaptautotestnodpkgwarning "$@"; }
  1480. aptautotest_aptget_purge() { testaptautotestnodpkgwarning "$@"; }
  1481. aptautotest_apt_install() { testaptautotestnodpkgwarning "$@"; }
  1482. aptautotest_apt_remove() { testaptautotestnodpkgwarning "$@"; }
  1483. aptautotest_apt_purge() { testaptautotestnodpkgwarning "$@"; }