You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1985 lines
64 KiB

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