framework 52 KB

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