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.
 
 
 
 
 
 

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