framework 53 KB

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