framework 56 KB

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