framework 52 KB

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