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.
 
 
 
 
 
 

2157 lines
71 KiB

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