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.
 
 
 
 
 
 

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