framework 51 KB

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