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.
 
 
 
 
 
 

2150 lines
70 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 APT_CONFIG
  455. unset GREP_OPTIONS DEB_BUILD_PROFILES
  456. unset http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy
  457. # Make gcov shut up
  458. export GCOV_ERROR_FILE=/dev/null
  459. # If gpgv supports --weak-digest, pass it to make sure we can disable SHA1
  460. if aptkey verify --weak-digest SHA1 --help 2>/dev/null >/dev/null; then
  461. echo 'Acquire::gpgv::Options { "--weak-digest"; "sha1"; };' > rootdir/etc/apt/apt.conf.d/no-sha1
  462. fi
  463. # most tests just need one signed Release file, not both
  464. export APT_DONT_SIGN='Release.gpg'
  465. if [ -r "${TESTDIRECTORY}/extra-environment" ]; then
  466. . "${TESTDIRECTORY}/extra-environment"
  467. fi
  468. msgdone "info"
  469. }
  470. getarchitecture() {
  471. if [ "$1" = "native" -o -z "$1" ]; then
  472. eval `aptconfig shell ARCH APT::Architecture`
  473. if [ -n "$ARCH" ]; then
  474. echo $ARCH
  475. else
  476. dpkg --print-architecture
  477. fi
  478. else
  479. echo $1
  480. fi
  481. }
  482. getarchitectures() {
  483. aptconfig dump --no-empty --format '%v%n' APT::Architecture APT::Architectures | sort -u | tr '\n' ' '
  484. }
  485. getarchitecturesfromcommalist() {
  486. echo "$1" | sed -e 's#,#\n#g' | sed -e "s/^native\$/$(getarchitecture 'native')/"
  487. }
  488. configarchitecture() {
  489. {
  490. echo "APT::Architecture \"$(getarchitecture $1)\";"
  491. while [ -n "$1" ]; do
  492. echo "APT::Architectures:: \"$(getarchitecture $1)\";"
  493. shift
  494. done
  495. } >rootdir/etc/apt/apt.conf.d/01multiarch.conf
  496. configdpkg
  497. }
  498. configdpkg() {
  499. if [ ! -e rootdir/var/lib/dpkg/status ]; then
  500. local BASENAME="${0##*/}"
  501. local STATUSFILE="status-${BASENAME#*-}"
  502. if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
  503. cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
  504. # Add an empty line to the end if there is none
  505. if tail -1 rootdir/var/lib/dpkg/status | grep -q .; then
  506. echo >> rootdir/var/lib/dpkg/status
  507. fi
  508. else
  509. echo -n > rootdir/var/lib/dpkg/status
  510. fi
  511. fi
  512. rm -f rootdir/etc/apt/apt.conf.d/00foreigndpkg
  513. # make sure dpkg can detect itself as capable of it
  514. if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
  515. # dpkg doesn't really check the version as long as it is fully installed,
  516. # but just to be sure we choose one above the required version
  517. insertinstalledpackage 'dpkg' "all" '1.16.2+fake'
  518. fi
  519. if dpkg --assert-multi-arch >/dev/null 2>&1 ; then
  520. local ARCHS="$(getarchitectures)"
  521. local DPKGARCH="$(dpkg --print-architecture)"
  522. # this ensures that even if multi-arch isn't active in the view
  523. # of apt, given that dpkg can't be told which arch is native
  524. # the arch apt treats as native might be foreign for dpkg
  525. for ARCH in ${ARCHS}; do
  526. if [ "${ARCH}" != "${DPKGARCH}" ]; then
  527. if ! dpkg --add-architecture ${ARCH} >/dev/null 2>&1; then
  528. # old-style used e.g. in Ubuntu-P – and as it seems travis
  529. echo "DPKG::options:: \"--foreign-architecture\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
  530. echo "DPKG::options:: \"${ARCH}\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
  531. fi
  532. fi
  533. done
  534. fi
  535. }
  536. configdpkgnoopchroot() {
  537. # create a library to noop chroot() and rewrite maintainer script executions
  538. # via execvp() as used by dpkg as we don't want our rootdir to be a fullblown
  539. # chroot directory dpkg could chroot into to execute the maintainer scripts
  540. msgtest 'Building library to preload to make maintainerscript work in' 'dpkg'
  541. cat > noopchroot.c << EOF
  542. #define _GNU_SOURCE
  543. #include <stdio.h>
  544. #include <stdlib.h>
  545. #include <string.h>
  546. #include <dlfcn.h>
  547. static char * chrootdir = NULL;
  548. int chroot(const char *path) {
  549. printf("WARNING: CHROOTing to %s was ignored!\n", path);
  550. free(chrootdir);
  551. chrootdir = strdup(path);
  552. return 0;
  553. }
  554. int execvp(const char *file, char *const argv[]) {
  555. static int (*func_execvp) (const char *, char * const []) = NULL;
  556. if (func_execvp == NULL)
  557. func_execvp = (int (*) (const char *, char * const [])) dlsym(RTLD_NEXT, "execvp");
  558. if (chrootdir == NULL || strncmp(file, "/var/lib/dpkg/", strlen("/var/lib/dpkg/")) != 0)
  559. return func_execvp(file, argv);
  560. printf("REWRITE execvp call %s into %s\n", file, chrootdir);
  561. char *newfile;
  562. if (asprintf(&newfile, "%s%s", chrootdir, file) == -1) {
  563. perror("asprintf");
  564. return -1;
  565. }
  566. char const * const baseadmindir = "/var/lib/dpkg";
  567. char *admindir;
  568. if (asprintf(&admindir, "%s%s", chrootdir, baseadmindir) == -1) {
  569. perror("asprintf");
  570. return -1;
  571. }
  572. setenv("DPKG_ADMINDIR", admindir, 1);
  573. return func_execvp(newfile, argv);
  574. }
  575. EOF
  576. if cc -ldl 2>&1 | grep -q dl; then
  577. testempty --nomsg cc -Wall -Wextra -fPIC -shared -o noopchroot.so noopchroot.c
  578. else
  579. testempty --nomsg cc -Wall -Wextra -fPIC -shared -o noopchroot.so noopchroot.c -ldl
  580. fi
  581. }
  582. configcompression() {
  583. if [ "$1" = 'ALL' ]; then
  584. configcompression '.' $(aptconfig dump APT::Compressor --format '%t %v%n' | sed -n 's#^Extension \.\(.*\)$#\1#p')
  585. return
  586. fi
  587. local CMD='apthelper cat-file -C'
  588. while [ -n "$1" ]; do
  589. case "$1" in
  590. '.') printf ".\t.\tcat\n";;
  591. 'gz') printf "gzip\tgz\t$CMD $1\n";;
  592. 'bz2') printf "bzip2\tbz2\t$CMD $1\n";;
  593. 'zst') printf "zstd\tzst\t$CMD $1\n";;
  594. *) printf "$1\t$1\t$CMD $1\n";;
  595. esac
  596. shift
  597. done > "${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf"
  598. }
  599. confighashes() {
  600. {
  601. echo 'APT::FTPArchive {'
  602. {
  603. while [ -n "$1" ]; do
  604. printf "$1" | tr 'a-z' 'A-Z'
  605. printf "\t\"true\";\n"
  606. shift
  607. done
  608. for h in 'MD5' 'SHA1' 'SHA256' 'SHA512'; do
  609. printf "$h\t\"false\";\n"
  610. done
  611. } | awk '!x[$1]++'
  612. echo '};'
  613. } >> "${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/ftparchive-hashes.conf"
  614. }
  615. forcecompressor() {
  616. COMPRESSOR="$1"
  617. COMPRESS="$1"
  618. COMPRESSOR_CMD="apthelper cat-file -C $1"
  619. case $COMPRESSOR in
  620. gzip) COMPRESS='gz';;
  621. bzip2) COMPRESS='bz2';;
  622. zstd) COMPRESS='zst';;
  623. esac
  624. local CONFFILE="${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/00force-compressor"
  625. echo "Acquire::CompressionTypes::Order { \"${COMPRESS}\"; };
  626. Dir::Bin::uncompressed \"/does/not/exist\";" > "$CONFFILE"
  627. for COMP in $(aptconfig dump 'APT::Compressor' --format '%f%n' | cut -d':' -f 5 | uniq); do
  628. if [ -z "$COMP" -o "$COMP" = '.' -o "$COMP" = "$COMPRESSOR" ]; then continue; fi
  629. echo "Dir::Bin::${COMP} \"/does/not/exist\";" >> "$CONFFILE"
  630. echo "APT::Compressor::${COMP}::Name \"${COMP}-disabled\";" >> "$CONFFILE"
  631. done
  632. }
  633. _setupsimplenativepackage() {
  634. local NAME="$1"
  635. local ARCH="$2"
  636. local VERSION="$3"
  637. local RELEASE="${4:-unstable}"
  638. local DEPENDENCIES="$5"
  639. local DESCRIPTION="${6:-an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  640. If you find such a package installed on your system,
  641. something went horribly wrong! They are autogenerated
  642. und used only by testcases and serve no other purpose…}"
  643. local SECTION="${7:-others}"
  644. local PRIORITY="${8:-optional}"
  645. local FILE_TREE="$9"
  646. local COMPRESS_TYPE="${10:-gzip}"
  647. local DISTSECTION
  648. if [ "$SECTION" = "${SECTION#*/}" ]; then
  649. DISTSECTION="main"
  650. else
  651. DISTSECTION="${SECTION%/*}"
  652. fi
  653. local BUILDDIR="${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}"
  654. mkdir -p "$BUILDDIR/debian/source"
  655. echo "* most suckless software product ever" > "${BUILDDIR}/FEATURES"
  656. echo "#!/bin/sh
  657. echo '$NAME says \"Hello!\"'" > "${BUILDDIR}/${NAME}"
  658. echo "Copyleft by Joe Sixpack $(date -u +%Y)" > "${BUILDDIR}/debian/copyright"
  659. echo "$NAME ($VERSION) $RELEASE; urgency=low
  660. * Initial release
  661. -- Joe Sixpack <joe@example.org> $(date -u -R)" > "${BUILDDIR}/debian/changelog"
  662. {
  663. echo "Source: $NAME
  664. Priority: $PRIORITY
  665. Maintainer: Joe Sixpack <joe@example.org>
  666. Standards-Version: 4.3.1
  667. Rules-Requires-Root: no"
  668. if [ "$SECTION" != '<none>' ]; then
  669. echo "Section: $SECTION"
  670. fi
  671. local BUILDDEPS="$(printf "%b\n" "$DEPENDENCIES" | grep '^Build-')"
  672. test -z "$BUILDDEPS" || echo "$BUILDDEPS"
  673. echo "
  674. Package: $NAME"
  675. if [ "$ARCH" = 'all' ]; then
  676. echo "Architecture: all"
  677. else
  678. echo "Architecture: any"
  679. fi
  680. local DEPS="$(printf "%b\n" "$DEPENDENCIES" | grep -v '^Build-')"
  681. test -z "$DEPS" || echo "$DEPS"
  682. printf "%b\n" "Description: $DESCRIPTION"
  683. } > "${BUILDDIR}/debian/control"
  684. echo '3.0 (native)' > "${BUILDDIR}/debian/source/format"
  685. }
  686. make_tiny_rules() {
  687. local OUT="$1"
  688. if command -v gmake >/dev/null 2>&1; then
  689. [ -e ${TMPWORKINGDIRECTORY}/bin/make ] || ln -s $(command -v gmake) ${TMPWORKINGDIRECTORY}/bin/make
  690. echo "#!${TMPWORKINGDIRECTORY}/bin/make -f" > "$OUT"
  691. else
  692. echo '#!/usr/bin/make -f' > "$OUT"
  693. fi
  694. echo '%:' >> "$OUT"
  695. echo ' dh $@' >> "$OUT"
  696. }
  697. setupsimplenativepackage() {
  698. _setupsimplenativepackage "$@"
  699. local NAME="$1"
  700. local VERSION="$3"
  701. local BUILDDIR="${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}"
  702. test -e "${BUILDDIR}/debian/compat" || echo '10' > "${BUILDDIR}/debian/compat"
  703. test -e "${BUILDDIR}/debian/rules" || make_tiny_rules "${BUILDDIR}/debian/rules"
  704. }
  705. buildsimplenativepackage() {
  706. local NAME="$1"
  707. local ARCH="$2"
  708. local VERSION="$3"
  709. local RELEASE="${4:-unstable}"
  710. local DEPENDENCIES="$5"
  711. local DESCRIPTION="$6"
  712. local SECTION="${7:-others}"
  713. local PRIORITY="${8:-optional}"
  714. local FILE_TREE="$9"
  715. local COMPRESS_TYPE="${10:-gzip}"
  716. local DISTSECTION
  717. if [ "$SECTION" = "${SECTION#*/}" ]; then
  718. DISTSECTION="main"
  719. else
  720. DISTSECTION="${SECTION%/*}"
  721. fi
  722. local BUILDDIR="${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}"
  723. msgtest "Build source package in version ${VERSION} for ${RELEASE} in ${DISTSECTION}" "$NAME"
  724. _setupsimplenativepackage "$@"
  725. cd "${BUILDDIR}/.."
  726. testsuccess --nomsg dpkg-source -b ${NAME}-${VERSION}
  727. cd - >/dev/null
  728. sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output" \
  729. | while read SRC; do
  730. echo "pool/${SRC}" >> "${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist"
  731. # if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
  732. # aptkey --keyring ./keys/joesixpack.pub --secret-keyring ./keys/joesixpack.sec --quiet --readonly \
  733. # adv --yes --default-key 'Joe Sixpack' \
  734. # --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  735. # mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  736. # fi
  737. done
  738. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  739. msgtest "Build binary package for ${RELEASE} in ${SECTION}" "$NAME"
  740. rm -rf "${BUILDDIR}/debian/tmp"
  741. mkdir -p "${BUILDDIR}/debian/tmp/DEBIAN" "${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}" "${BUILDDIR}/debian/tmp/usr/bin"
  742. cp "${BUILDDIR}/debian/copyright" "${BUILDDIR}/debian/changelog" "${BUILDDIR}/FEATURES" "${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}"
  743. cp "${BUILDDIR}/${NAME}" "${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}"
  744. if [ -n "$FILE_TREE" ]; then
  745. cp -ar "$FILE_TREE" "${BUILDDIR}/debian/tmp"
  746. fi
  747. (cd "${BUILDDIR}"; dpkg-gencontrol -DArchitecture=$arch)
  748. (cd "${BUILDDIR}/debian/tmp"; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
  749. local LOG="${BUILDDIR}/../${NAME}_${VERSION}_${arch}.dpkg-deb.log"
  750. # ensure the right permissions as dpkg-deb insists
  751. chmod 755 "${BUILDDIR}/debian/tmp/DEBIAN"
  752. testsuccess --nomsg dpkg-deb -Z${COMPRESS_TYPE} --build "${BUILDDIR}/debian/tmp" "${BUILDDIR}/.."
  753. echo "pool/${NAME}_${VERSION}_${arch}.deb" >> "${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist"
  754. done
  755. local NM
  756. if [ "$(echo "$NAME" | cut -c 1-3)" = 'lib' ]; then
  757. NM="$(echo "$NAME" | cut -c 1-4)"
  758. else
  759. NM="$(echo "$NAME" | cut -c 1)"
  760. fi
  761. local CHANGEPATH="${BUILDDIR}/../${DISTSECTION}/${NM}/${NAME}/${NAME}_${VERSION}"
  762. mkdir -p "$CHANGEPATH"
  763. cp "${BUILDDIR}/debian/changelog" "$CHANGEPATH"
  764. rm -rf "${BUILDDIR}"
  765. msgdone "info"
  766. }
  767. buildpackage() {
  768. local BUILDDIR=$1
  769. local RELEASE=$2
  770. local SECTION=$3
  771. local ARCH=$(getarchitecture $4)
  772. local PKGNAME="$(echo "$BUILDDIR" | grep -o '[^/]*$')"
  773. local BUILDLOG="$(readlink -f "${BUILDDIR}/../${PKGNAME}_${RELEASE}_${SECTION}.dpkg-bp.log")"
  774. msgtest "Build package for ${RELEASE} in ${SECTION}" "$PKGNAME"
  775. cd "$BUILDDIR"
  776. if [ "$ARCH" = "all" ]; then
  777. ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
  778. fi
  779. testsuccess --nomsg dpkg-buildpackage -uc -us -a$ARCH -d
  780. cp "${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output" "$BUILDLOG"
  781. local PKGS="$(grep '^dpkg-deb: building package' "$BUILDLOG" | cut -d'/' -f 2 | sed -e "s#'\.##")"
  782. local SRCS="$(grep '^dpkg-source: info: building' "$BUILDLOG" | grep -o '[a-z0-9._+~-]*$')"
  783. cd - > /dev/null
  784. for PKG in $PKGS; do
  785. echo "pool/${PKG}" >> "${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist"
  786. done
  787. for SRC in $SRCS; do
  788. echo "pool/${SRC}" >> "${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist"
  789. done
  790. }
  791. buildaptarchive() {
  792. if [ -d incoming ]; then
  793. buildaptarchivefromincoming "$@"
  794. else
  795. buildaptarchivefromfiles "$@"
  796. fi
  797. }
  798. createaptftparchiveconfig() {
  799. local COMPRESSORS="$(cut -d' ' -f 1 "${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf" | tr '\n' ' ')"
  800. local COMPRESSORS="${COMPRESSORS%* }"
  801. local ARCHS="$(getarchitectures)"
  802. cat > ftparchive.conf <<EOF
  803. Dir {
  804. ArchiveDir "$(readlink -f .)";
  805. CacheDir "$(readlink -f ..)";
  806. FileListDir "$(readlink -f pool/)";
  807. };
  808. Default {
  809. Packages::Compress "$COMPRESSORS";
  810. Sources::Compress "$COMPRESSORS";
  811. Contents::Compress "$COMPRESSORS";
  812. Translation::Compress "$COMPRESSORS";
  813. LongDescription "false";
  814. };
  815. TreeDefault {
  816. Directory "pool/";
  817. SrcDirectory "pool/";
  818. };
  819. EOF
  820. for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
  821. cat <<EOF
  822. tree "dists/$DIST" {
  823. Architectures "$ARCHS all source";
  824. FileList "${DIST}.\$(SECTION).pkglist";
  825. SourceFileList "${DIST}.\$(SECTION).srclist";
  826. Sections "$(find ./pool/ -maxdepth 1 -name "${DIST}.*.pkglist" -type f | cut -d'/' -f 3 | cut -d'.' -f 2 | sort | uniq | tr '\n' ' ')";
  827. };
  828. EOF
  829. done >> ftparchive.conf
  830. }
  831. buildaptftparchivedirectorystructure() {
  832. local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
  833. for DIST in $DISTS; do
  834. local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
  835. for SECTION in $SECTIONS; do
  836. local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
  837. for ARCH in $ARCHS; do
  838. mkdir -p "dists/${DIST}/${SECTION}/binary-${ARCH}"
  839. done
  840. mkdir -p "dists/${DIST}/${SECTION}/source"
  841. mkdir -p "dists/${DIST}/${SECTION}/i18n"
  842. done
  843. done
  844. }
  845. insertpackage() {
  846. local RELEASES="$1"
  847. local NAME="$2"
  848. local ARCH="$3"
  849. local VERSION="$4"
  850. local DEPENDENCIES="$5"
  851. local PRIORITY="${6:-optional}"
  852. local DESCRIPTION="${7:-an autogenerated dummy ${NAME}=${VERSION}/${RELEASES}
  853. If you find such a package installed on your system,
  854. something went horribly wrong! They are autogenerated
  855. und used only by testcases and serve no other purpose…}"
  856. local SECTION="${8:-other}"
  857. if [ "$SECTION" = "${SECTION#*/}" ]; then
  858. DISTSECTION="main"
  859. else
  860. DISTSECTION="${SECTION%/*}"
  861. fi
  862. local ARCHS=""
  863. for RELEASE in $(printf '%s' "$RELEASES" | tr ',' '\n'); do
  864. if [ "$RELEASE" = 'installed' ]; then
  865. insertinstalledpackage "$2" "$3" "$4" "$5" "$6" "$7" "$8"
  866. continue
  867. fi
  868. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  869. if [ "$arch" = 'none' ]; then
  870. ARCHS="$(getarchitectures)"
  871. else
  872. ARCHS="$arch"
  873. fi
  874. for BUILDARCH in $ARCHS; do
  875. local PPATH="aptarchive/dists/${RELEASE}/${DISTSECTION}/binary-${BUILDARCH}"
  876. mkdir -p "$PPATH"
  877. {
  878. echo "Package: $NAME
  879. Priority: $PRIORITY
  880. Section: $SECTION
  881. Installed-Size: 42
  882. Maintainer: Joe Sixpack <joe@example.org>"
  883. test "$arch" = 'none' || echo "Architecture: $arch"
  884. echo "Version: $VERSION
  885. Filename: pool/${DISTSECTION}/${NAME}/${NAME}_${VERSION}_${arch}.deb"
  886. test -z "$DEPENDENCIES" || printf "%b\n" "$DEPENDENCIES"
  887. echo "Description: $(printf '%s' "$DESCRIPTION" | head -n 1)"
  888. echo "Description-md5: $(printf '%s' "$DESCRIPTION" | md5sum | cut -d' ' -f 1)"
  889. echo "SHA256: 0000000000000000000000000000000000000000000000000000000000000000"
  890. echo
  891. } >> "${PPATH}/Packages"
  892. done
  893. done
  894. mkdir -p "aptarchive/dists/${RELEASE}/${DISTSECTION}/source" "aptarchive/dists/${RELEASE}/${DISTSECTION}/i18n"
  895. touch "aptarchive/dists/${RELEASE}/${DISTSECTION}/source/Sources"
  896. echo "Package: $NAME
  897. Description-md5: $(printf '%s' "$DESCRIPTION" | md5sum | cut -d' ' -f 1)
  898. Description-en: $DESCRIPTION
  899. " >> "aptarchive/dists/${RELEASE}/${DISTSECTION}/i18n/Translation-en"
  900. done
  901. }
  902. insertsource() {
  903. local RELEASES="$1"
  904. local NAME="$2"
  905. local ARCH="$3"
  906. local VERSION="$4"
  907. local DEPENDENCIES="$5"
  908. local BINARY="${6:-$NAME}"
  909. local ARCHS=""
  910. for RELEASE in $(printf '%s' "$RELEASES" | tr ',' '\n'); do
  911. local SPATH="aptarchive/dists/${RELEASE}/main/source"
  912. mkdir -p $SPATH
  913. local FILE="${SPATH}/Sources"
  914. local DSCFILE="${NAME}_${VERSION}.dsc"
  915. local TARFILE="${NAME}_${VERSION}.tar.gz"
  916. echo "Package: $NAME
  917. Binary: $BINARY
  918. Version: $VERSION
  919. Maintainer: Joe Sixpack <joe@example.org>
  920. Architecture: $ARCH" >> $FILE
  921. test -z "$DEPENDENCIES" || printf "%b\n" "$DEPENDENCIES" >> "$FILE"
  922. echo "Files:
  923. $(echo -n "$DSCFILE" | md5sum | cut -d' ' -f 1) $(echo -n "$DSCFILE" | wc -c) "$DSCFILE"
  924. $(echo -n "$TARFILE" | md5sum | cut -d' ' -f 1) $(echo -n "$TARFILE" | wc -c) "$TARFILE"
  925. Checksums-Sha256:
  926. $(echo -n "$DSCFILE" | sha256sum | cut -d' ' -f 1) $(echo -n "$DSCFILE" | wc -c) "$DSCFILE"
  927. $(echo -n "$TARFILE" | sha256sum | cut -d' ' -f 1) $(echo -n "$TARFILE" | wc -c) "$TARFILE"
  928. " >> "$FILE"
  929. done
  930. }
  931. insertinstalledpackage() {
  932. local NAME="$1"
  933. local ARCH="$2"
  934. local VERSION="$3"
  935. local DEPENDENCIES="$4"
  936. local PRIORITY="${5:-optional}"
  937. local STATUS="${6:-install ok installed}"
  938. local DESCRIPTION="${7:-an autogenerated dummy ${NAME}=${VERSION}/installed
  939. If you find such a package installed on your system,
  940. something went horribly wrong! They are autogenerated
  941. und used only by testcases and serve no other purpose…}"
  942. local SECTION="${8:-other}"
  943. local FILE='rootdir/var/lib/dpkg/status'
  944. local INFO='rootdir/var/lib/dpkg/info'
  945. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  946. echo "Package: $NAME
  947. Status: $STATUS
  948. Priority: $PRIORITY
  949. Section: $SECTION
  950. Installed-Size: 42
  951. Maintainer: Joe Sixpack <joe@example.org>
  952. Version: $VERSION" >> "$FILE"
  953. test "$arch" = 'none' || echo "Architecture: $arch" >> "$FILE"
  954. test -z "$DEPENDENCIES" || printf "%b\n" "$DEPENDENCIES" >> "$FILE"
  955. printf "%b\n" "Description: $DESCRIPTION" >> "$FILE"
  956. echo >> "$FILE"
  957. if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
  958. echo -n > "${INFO}/${NAME}:${arch}.list"
  959. else
  960. echo -n > "${INFO}/${NAME}.list"
  961. fi
  962. done
  963. }
  964. buildaptarchivefromincoming() {
  965. msginfo "Build APT archive for ${CCMD}${0##*/}${CINFO} based on incoming packages…"
  966. cd aptarchive
  967. [ -e pool ] || ln -s ../incoming pool
  968. [ -e ftparchive.conf ] || createaptftparchiveconfig
  969. [ -e dists ] || buildaptftparchivedirectorystructure
  970. msgninfo "\tGenerate Packages, Sources and Contents files… "
  971. testsuccess aptftparchive generate ftparchive.conf
  972. cd - > /dev/null
  973. msgdone "info"
  974. generatereleasefiles "$@"
  975. }
  976. buildaptarchivefromfiles() {
  977. msginfo "Build APT archive for ${CCMD}${0##*/}${CINFO} based on prebuild files…"
  978. local DIR='aptarchive'
  979. if [ -d "${DIR}/dists" ]; then DIR="${DIR}/dists"; fi
  980. find "$DIR" -name 'Packages' -o -name 'Sources' -o -name 'Translation-*' | while read line; do
  981. msgninfo "\t${line} file… "
  982. compressfile "$line" "$1"
  983. msgdone "info"
  984. done
  985. generatereleasefiles "$@"
  986. }
  987. compressfile() {
  988. while read compressor extension command; do
  989. if [ "$compressor" = '.' ]; then
  990. if [ -n "$2" ]; then
  991. touch -d "$2" "$1"
  992. fi
  993. continue
  994. fi
  995. cat "$1" | $command > "${1}.${extension}"
  996. if [ -n "$2" ]; then
  997. touch -d "$2" "${1}.${extension}"
  998. fi
  999. done < "${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf"
  1000. }
  1001. # can be overridden by testcases for their pleasure
  1002. getcodenamefromsuite() {
  1003. case "$1" in
  1004. unstable) echo 'sid';;
  1005. *) echo -n "$1";;
  1006. esac
  1007. }
  1008. getreleaseversionfromsuite() { true; }
  1009. getlabelfromsuite() { true; }
  1010. getoriginfromsuite() { true; }
  1011. getarchitecturesfromreleasefile() { echo "all $(getarchitectures)"; }
  1012. getnotautomaticfromsuite() {
  1013. case "$1" in
  1014. experimental|experimental2) echo "yes";;
  1015. esac
  1016. }
  1017. getbutautomaticupgradesfromsuite() { true; }
  1018. aptftparchiverelease() {
  1019. aptftparchive -qq release "$@" | sed -e '/0 Release$/ d' # remove the self reference
  1020. }
  1021. generatereleasefiles() {
  1022. # $1 is the Date header and $2 is the ValidUntil header to be set
  1023. # both should be given in notation date/touch can understand
  1024. local DATE="$1"
  1025. local VALIDUNTIL="$2"
  1026. if [ -e aptarchive/dists ]; then
  1027. msgninfo "\tGenerate Release files for dists… "
  1028. for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
  1029. local ARCHITECTURES="$(getarchitecturesfromreleasefile "$dir")"
  1030. local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
  1031. local CODENAME="$(getcodenamefromsuite $SUITE)"
  1032. local VERSION="$(getreleaseversionfromsuite $SUITE)"
  1033. local LABEL="$(getlabelfromsuite $SUITE)"
  1034. local ORIGIN="$(getoriginfromsuite $SUITE)"
  1035. local NOTAUTOMATIC="$(getnotautomaticfromsuite $SUITE)"
  1036. local BUTAUTOMATICUPGRADES="$(getbutautomaticupgradesfromsuite $SUITE)"
  1037. aptftparchiverelease "$dir" \
  1038. -o APT::FTPArchive::Release::Suite="${SUITE}" \
  1039. -o APT::FTPArchive::Release::Codename="${CODENAME}" \
  1040. -o APT::FTPArchive::Release::Architectures="${ARCHITECTURES}" \
  1041. -o APT::FTPArchive::Release::Label="${LABEL}" \
  1042. -o APT::FTPArchive::Release::Origin="${ORIGIN}" \
  1043. -o APT::FTPArchive::Release::Version="${VERSION}" \
  1044. -o APT::FTPArchive::Release::NotAutomatic="${NOTAUTOMATIC}" \
  1045. -o APT::FTPArchive::Release::ButAutomaticUpgrades="${BUTAUTOMATICUPGRADES}" \
  1046. > "$dir/Release"
  1047. done
  1048. else
  1049. msgninfo "\tGenerate Release files for flat… "
  1050. aptftparchiverelease ./aptarchive > aptarchive/Release
  1051. fi
  1052. if [ -n "$DATE" -a "$DATE" != "now" ]; then
  1053. for release in $(find ./aptarchive -name 'Release'); do
  1054. sed -i "s/^Date: .*$/Date: $(date -u -d "$DATE" -R)/" "$release"
  1055. touch -d "$DATE" "$release"
  1056. done
  1057. fi
  1058. if [ -n "$VALIDUNTIL" ]; then
  1059. sed -i "/^Date: / a\
  1060. Valid-Until: $(date -u -d "$VALIDUNTIL" -R)" $(find ./aptarchive -name 'Release')
  1061. fi
  1062. msgdone "info"
  1063. }
  1064. setupdistsaptarchive() {
  1065. local APTARCHIVE="$(readlink -f ./aptarchive | sed 's# #%20#g')"
  1066. rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
  1067. rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
  1068. for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
  1069. SECTIONS=$(find "./aptarchive/dists/${DISTS}/" -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
  1070. msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
  1071. echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > "rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list"
  1072. echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > "rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list"
  1073. msgdone "info"
  1074. done
  1075. }
  1076. setupflataptarchive() {
  1077. local APTARCHIVE="$(readlink -f ./aptarchive)"
  1078. local APTARCHIVEURI="$(readlink -f ./aptarchive | sed 's# #%20#g')"
  1079. if [ -f "${APTARCHIVE}/Packages" ]; then
  1080. msgninfo "\tadd deb sources.list line… "
  1081. echo "deb file://$APTARCHIVEURI /" > 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list'
  1082. msgdone 'info'
  1083. else
  1084. rm -f 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list'
  1085. fi
  1086. if [ -f "${APTARCHIVE}/Sources" ]; then
  1087. msgninfo "\tadd deb-src sources.list line… "
  1088. echo "deb-src file://$APTARCHIVEURI /" > 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list'
  1089. msgdone 'info'
  1090. else
  1091. rm -f 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list'
  1092. fi
  1093. }
  1094. setupaptarchive() {
  1095. local NOUPDATE=0
  1096. if [ "$1" = '--no-update' ]; then
  1097. NOUPDATE=1
  1098. shift
  1099. fi
  1100. buildaptarchive "$@"
  1101. if [ -e aptarchive/dists ]; then
  1102. setupdistsaptarchive
  1103. else
  1104. setupflataptarchive
  1105. fi
  1106. signreleasefiles 'Joe Sixpack'
  1107. if [ "1" != "$NOUPDATE" ]; then
  1108. testsuccess aptget update -o Debug::pkgAcquire::Worker=true -o Debug::Acquire::gpgv=true
  1109. fi
  1110. }
  1111. killgpgagent() {
  1112. if [ -z "${TMPWORKINGDIRECTORY}" ]; then return; fi
  1113. local GPGHOME="${TMPWORKINGDIRECTORY}/signinghome"
  1114. if [ ! -e "${GPGHOME}" ]; then return; fi
  1115. # ensure the agent dies quickly as different versions have different suicide heuristics
  1116. GNUPGHOME="${GPGHOME}" gpgconf --kill gpg-agent >/dev/null 2>&1 || true
  1117. rm -rf "$GPGHOME"
  1118. }
  1119. dosigning() {
  1120. local KEY="$1"
  1121. shift
  1122. local GPGHOME="${TMPWORKINGDIRECTORY}/signinghome"
  1123. if [ -n "$APT_TEST_SIGNINGHOME" ]; then
  1124. GPGHOME="$APT_TEST_SIGNINGHOME"
  1125. else
  1126. if [ ! -e "$GPGHOME" ]; then
  1127. mkdir -p --mode=700 "${GPGHOME}"
  1128. addtrap 'prefix' 'killgpgagent;'
  1129. fi
  1130. fi
  1131. testsuccess aptkey --quiet --keyring ${KEY}.pub --secret-keyring ${KEY}.sec --readonly \
  1132. --homedir "${GPGHOME}" adv --batch --yes --digest-algo "${APT_TESTS_DIGEST_ALGO:-SHA512}" \
  1133. "$@"
  1134. }
  1135. signreleasefiles() {
  1136. local SIGNERS="${1:-Joe Sixpack}"
  1137. local REPODIR="${2:-aptarchive}"
  1138. if [ -n "$1" ]; then shift; fi
  1139. if [ -n "$1" ]; then shift; fi
  1140. local KEY="keys/$(echo "$SIGNERS" | tr 'A-Z' 'a-z' | tr -d ' ,')"
  1141. msgninfo "\tSign archive with $SIGNERS key $KEY… "
  1142. local REXKEY='keys/rexexpired'
  1143. local SECEXPIREBAK="${REXKEY}.sec.bak"
  1144. local PUBEXPIREBAK="${REXKEY}.pub.bak"
  1145. local SIGUSERS=""
  1146. while [ -n "${SIGNERS%%,*}" ]; do
  1147. local SIGNER="${SIGNERS%%,*}"
  1148. if [ "${SIGNERS}" = "${SIGNER}" ]; then
  1149. SIGNERS=""
  1150. fi
  1151. SIGNERS="${SIGNERS#*,}"
  1152. # FIXME: This should be the full name, but we can't encode the space properly currently
  1153. SIGUSERS="${SIGUSERS} -u ${SIGNER#* }"
  1154. if [ "${SIGNER}" = 'Rex Expired' ]; then
  1155. # the key is expired, so gpg doesn't allow one to sign with and the --faked-system-time
  1156. # option doesn't exist anymore (and using faketime would add a new obscure dependency)
  1157. # therefore we 'temporary' make the key not expired and restore a backup after signing
  1158. cp "${REXKEY}.sec" "$SECEXPIREBAK"
  1159. cp "${REXKEY}.pub" "$PUBEXPIREBAK"
  1160. local SECUNEXPIRED="${REXKEY}.sec.unexpired"
  1161. local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
  1162. if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
  1163. cp "$SECUNEXPIRED" "${REXKEY}.sec"
  1164. cp "$PUBUNEXPIRED" "${REXKEY}.pub"
  1165. else
  1166. if ! printf "expire\n1w\nsave\n" | aptkey --quiet --keyring "${REXKEY}.pub" --secret-keyring "${REXKEY}.sec" \
  1167. --readonly adv --batch --yes --digest-algo "${APT_TESTS_DIGEST_ALGO:-SHA512}" \
  1168. --default-key "$SIGNER" --command-fd 0 --edit-key "${SIGNER}" >setexpire.gpg 2>&1; then
  1169. cat setexpire.gpg
  1170. exit 1
  1171. fi
  1172. cp "${REXKEY}.sec" "$SECUNEXPIRED"
  1173. cp "${REXKEY}.pub" "$PUBUNEXPIRED"
  1174. fi
  1175. fi
  1176. if [ ! -e "${KEY}.pub" ]; then
  1177. local K="keys/$(echo "$SIGNER" | tr 'A-Z' 'a-z' | tr -d ' ,')"
  1178. cat "${K}.pub" >> "${KEY}.new.pub"
  1179. cat "${K}.sec" >> "${KEY}.new.sec"
  1180. fi
  1181. done
  1182. if [ ! -e "${KEY}.pub" ]; then
  1183. mv "${KEY}.new.pub" "${KEY}.pub"
  1184. mv "${KEY}.new.sec" "${KEY}.sec"
  1185. fi
  1186. for RELEASE in $(find "${REPODIR}/" -name Release); do
  1187. # we might have set a specific date for the Release file, so copy it
  1188. local DATE="$(stat --format "%y" "${RELEASE}")"
  1189. if [ "$APT_DONT_SIGN" = 'Release.gpg' ]; then
  1190. rm -f "${RELEASE}.gpg"
  1191. else
  1192. dosigning "$KEY" "$@" $SIGUSERS --armor --detach-sign --sign --output "${RELEASE}.gpg" "${RELEASE}"
  1193. touch -d "$DATE" "${RELEASE}.gpg"
  1194. fi
  1195. local INRELEASE="${RELEASE%/*}/InRelease"
  1196. if [ "$APT_DONT_SIGN" = 'InRelease' ]; then
  1197. rm -f "$INRELEASE"
  1198. else
  1199. dosigning "$KEY" "$@" $SIGUSERS --clearsign --output "$INRELEASE" "$RELEASE"
  1200. touch -d "$DATE" "${INRELEASE}"
  1201. fi
  1202. done
  1203. if [ -f "$SECEXPIREBAK" ] && [ -f "$PUBEXPIREBAK" ]; then
  1204. mv -f "$SECEXPIREBAK" "${REXKEY}.sec"
  1205. mv -f "$PUBEXPIREBAK" "${REXKEY}.pub"
  1206. fi
  1207. msgdone 'info'
  1208. }
  1209. redatereleasefiles() {
  1210. local DATE="$(date -u -d "$1" -R)"
  1211. for release in $(find aptarchive/ -name 'Release'); do
  1212. sed -i "s/^Date: .*$/Date: ${DATE}/" "$release"
  1213. touch -d "$DATE" "$release"
  1214. done
  1215. signreleasefiles "${2:-Joe Sixpack}"
  1216. }
  1217. webserverconfig() {
  1218. local WEBSERVER="${3:-http://localhost:${APTHTTPPORT}}"
  1219. local NOCHECK=false
  1220. if [ "$1" = '--no-check' ]; then
  1221. NOCHECK=true
  1222. shift
  1223. fi
  1224. local DOWNLOG="${TMPWORKINGDIRECTORY}/rootdir/tmp/download-testfile.log"
  1225. local STATUS="${TMPWORKINGDIRECTORY}/downloaded/webserverconfig.status"
  1226. rm -f "$STATUS" "$DOWNLOG"
  1227. # very very basic URI encoding
  1228. local URI
  1229. if [ -n "$2" ]; then
  1230. msgtest "Set webserver config option '${1}' to" "$2"
  1231. URI="${WEBSERVER}/_config/set/$(echo "${1}" | sed -e 's/\//%2f/g')/$(echo "${2}" | sed -e 's/\//%2f/g')"
  1232. else
  1233. msgtest 'Clear webserver config option' "${1}"
  1234. URI="${WEBSERVER}/_config/clear/$(echo "${1}" | sed -e 's/\//%2f/g')"
  1235. fi
  1236. if downloadfile "$URI" "$STATUS" > "$DOWNLOG"; then
  1237. msgpass
  1238. else
  1239. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/webserverconfig.output"
  1240. cat "$DOWNLOG" "$STATUS" >"$OUTPUT" 2>&1 || true
  1241. msgfailoutput '' "$OUTPUT"
  1242. fi
  1243. $NOCHECK || testwebserverlaststatuscode '200'
  1244. }
  1245. rewritesourceslist() {
  1246. local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive" | sed 's# #%20#g')"
  1247. local APTARCHIVE2="copy://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive" | sed 's# #%20#g')"
  1248. for LIST in $(find "${TMPWORKINGDIRECTORY}/rootdir/etc/apt/sources.list.d/" -name 'apt-test-*.list'); do
  1249. sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#$APTARCHIVE2#${1}#" \
  1250. -e "s#http://[^@]*@\?localhost:${APTHTTPPORT}/\?#${1}#" \
  1251. -e "s#https://[^@]*@\?localhost:${APTHTTPSPORT}/\?#${1}#"
  1252. done
  1253. }
  1254. # wait for up to 10s for a pid file to appear to avoid possible race
  1255. # when a helper is started and doesn't write the PID quick enough
  1256. waitforpidfile() {
  1257. local PIDFILE="$1"
  1258. for i in $(seq 10); do
  1259. if test -s "$PIDFILE"; then
  1260. return 0
  1261. fi
  1262. sleep 1
  1263. done
  1264. msgdie "waiting for $PIDFILE failed"
  1265. return 1
  1266. }
  1267. changetowebserver() {
  1268. local REWRITE='no'
  1269. if [ "$1" != '--no-rewrite' ]; then
  1270. REWRITE='yes'
  1271. else
  1272. shift
  1273. fi
  1274. if test -x "${APTTESTHELPERSBINDIR}/aptwebserver"; then
  1275. cd aptarchive
  1276. local LOG="webserver.log"
  1277. if ! aptwebserver --port 0 -o aptwebserver::fork=1 -o aptwebserver::portfile='aptwebserver.port' -o aptwebserver::logfiles="$(readlink -f .)/$LOG" "$@" >$LOG 2>&1 ; then
  1278. cat "$LOG"
  1279. false
  1280. fi
  1281. waitforpidfile aptwebserver.pid
  1282. local PID="$(cat aptwebserver.pid)"
  1283. if [ -z "$PID" ]; then
  1284. msgdie 'Could not fork aptwebserver successfully'
  1285. fi
  1286. addtrap "kill $PID;"
  1287. waitforpidfile aptwebserver.port
  1288. APTHTTPPORT="$(cat aptwebserver.port)"
  1289. if [ -z "$APTHTTPPORT" ]; then
  1290. msgdie 'Could not get port for aptwebserver successfully'
  1291. fi
  1292. cd - > /dev/null
  1293. else
  1294. msgdie 'You have to build apt from source to have test/interactive-helper/aptwebserver available for tests requiring a webserver'
  1295. fi
  1296. if [ "$REWRTE" != 'yes' ]; then
  1297. rewritesourceslist "http://localhost:${APTHTTPPORT}/"
  1298. fi
  1299. }
  1300. changetohttpswebserver() {
  1301. local stunnel4
  1302. if command -v stunnel4 >/dev/null 2>&1; then
  1303. stunnel4=stunnel4
  1304. elif command -v stunnel >/dev/null 2>&1; then
  1305. stunnel4=stunnel
  1306. else
  1307. msgdie 'You need to install stunnel4 for https testcases'
  1308. fi
  1309. if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
  1310. changetowebserver --no-rewrite "$@"
  1311. fi
  1312. echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
  1313. cert = ${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem
  1314. output = /dev/null
  1315. [https]
  1316. accept = 127.0.0.1:0
  1317. connect = $APTHTTPPORT
  1318. " > "${TMPWORKINGDIRECTORY}/stunnel.conf"
  1319. $stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
  1320. waitforpidfile "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid"
  1321. local PID="$(cat "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid")"
  1322. if [ -z "$PID" ]; then
  1323. msgdie 'Could not fork $stunnel4 successfully'
  1324. fi
  1325. addtrap 'prefix' "kill ${PID};"
  1326. APTHTTPSPORT="$(lsof -i -n | awk "/^$stunnel4 / && \$2 == \"${PID}\" {print \$9; exit; }" | cut -d':' -f 2)"
  1327. webserverconfig 'aptwebserver::port::https' "$APTHTTPSPORT" "https://localhost:${APTHTTPSPORT}"
  1328. rewritesourceslist "https://localhost:${APTHTTPSPORT}/"
  1329. }
  1330. changetocdrom() {
  1331. mkdir -p rootdir/media/cdrom/.disk
  1332. local CD="$(readlink -f rootdir/media/cdrom)"
  1333. cat > rootdir/etc/apt/apt.conf.d/00cdrom <<EOF
  1334. acquire::cdrom::mount "${CD}";
  1335. acquire::cdrom::"${CD}/"::mount "mv ${CD}-unmounted ${CD}";
  1336. acquire::cdrom::"${CD}/"::umount "mv ${CD} ${CD}-unmounted";
  1337. acquire::cdrom::autodetect 0;
  1338. EOF
  1339. echo -n "$1" > "${CD}/.disk/info"
  1340. if [ ! -d aptarchive/dists ]; then
  1341. msgdie 'Flat file archive cdroms can not be created currently'
  1342. return 1
  1343. fi
  1344. mv aptarchive/dists "$CD"
  1345. ln -s "$(readlink -f ./incoming)" "$CD/pool"
  1346. find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
  1347. # start with an unmounted disk
  1348. mv "${CD}" "${CD}-unmounted"
  1349. # we don't want the disk to be modifiable
  1350. addtrap 'prefix' "chmod -f -R +w '$(escape_shell "$PWD/rootdir/media/cdrom/dists/")' '$(escape_shell "$PWD/rootdir/media/cdrom-unmounted/dists/")' || true;"
  1351. chmod -R 555 rootdir/media/cdrom-unmounted/dists
  1352. }
  1353. downloadfile() {
  1354. local PROTO="${1%%:*}"
  1355. if ! apthelper -o Debug::Acquire::${PROTO}=1 -o Debug::pkgAcquire::Worker=1 \
  1356. download-file "$1" "$2" "$3" 2>&1 ; then
  1357. return 1
  1358. fi
  1359. # only if the file exists the download was successful
  1360. if [ -r "$2" ]; then
  1361. return 0
  1362. else
  1363. return 1
  1364. fi
  1365. }
  1366. cleanup_output() {
  1367. cat "$1" | sed \
  1368. -e '/gpgv: WARNING: This key is not suitable for signing in --compliance=gnupg mode/ d' \
  1369. -e '/^profiling:/ d' \
  1370. | sed -e '/\.\.\.profiling:/ {N;s#\.\.\.profiling:.*\n#...#g}' \
  1371. >"$2"
  1372. }
  1373. checkdiff() {
  1374. local TMPFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/checkdiff.1.tmp"
  1375. local TMPFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/checkdiff.2.tmp"
  1376. touch "$TMPFILE1" "$TMPFILE2"
  1377. cleanup_output "$1" "$TMPFILE1"
  1378. cleanup_output "$2" "$TMPFILE2"
  1379. local DIFFTEXT="$(command diff -u "$TMPFILE1" "$TMPFILE2" 2>&1 | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  1380. if [ -n "$DIFFTEXT" ]; then
  1381. echo >&2
  1382. echo >&2 "$DIFFTEXT"
  1383. return 1
  1384. else
  1385. return 0
  1386. fi
  1387. }
  1388. testoutputequal() {
  1389. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testoutputequal.output"
  1390. local COMPAREFILE="$1"
  1391. shift
  1392. if "$@" 2>&1 | checkdiff "$COMPAREFILE" - >"$OUTPUT" 2>&1; then
  1393. msgpass
  1394. else
  1395. echo "=== content of file we compared with (${COMPAREFILE}) ===" >>"${OUTPUT}"
  1396. cat "$COMPAREFILE" >>"${OUTPUT}"
  1397. msgfailoutput '' "$OUTPUT" "$@"
  1398. fi
  1399. }
  1400. testfileequal() {
  1401. msggroup 'testfileequal'
  1402. local MSG='Test for correctness of file'
  1403. if [ "$1" = '--nomsg' ]; then
  1404. MSG=''
  1405. shift
  1406. fi
  1407. local FILE="$1"
  1408. shift
  1409. if [ -n "$MSG" ]; then
  1410. msgtest "$MSG" "$FILE"
  1411. fi
  1412. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfileequal.output"
  1413. if [ -z "$*" ]; then
  1414. testoutputequal "$FILE" echo -n ''
  1415. else
  1416. testoutputequal "$FILE" echo "$*"
  1417. fi
  1418. msggroup
  1419. }
  1420. testempty() {
  1421. msggroup 'testempty'
  1422. if [ "$1" = '--nomsg' ]; then
  1423. shift
  1424. else
  1425. msgtest "Test for no output of" "$*"
  1426. fi
  1427. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testempty.comparefile"
  1428. if "$@" >"$COMPAREFILE" 2>&1 && test ! -s "$COMPAREFILE"; then
  1429. msgpass
  1430. else
  1431. msgfailoutput '' "$COMPAREFILE" "$@"
  1432. fi
  1433. aptautotest 'testempty' "$@"
  1434. msggroup
  1435. }
  1436. testwarningempty() {
  1437. testwarning "$@"
  1438. testfailure grep -v '^W:' "${ROOTDIR}/tmp/testwarning.output"
  1439. }
  1440. testnotempty() {
  1441. msggroup 'testnotempty'
  1442. msgtest "Test for some output of" "$*"
  1443. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnotempty.comparefile"
  1444. if ("$@" >"$COMPAREFILE" 2>&1 || true) && test -s "$COMPAREFILE"; then
  1445. msgpass
  1446. else
  1447. msgfailoutput '' "$COMPAREFILE" "$@"
  1448. fi
  1449. aptautotest 'testnotempty' "$@"
  1450. msggroup
  1451. }
  1452. testequal() {
  1453. msggroup 'testequal'
  1454. local MSG='Test of equality of'
  1455. if [ "$1" = '--nomsg' ]; then
  1456. MSG=''
  1457. shift
  1458. fi
  1459. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
  1460. echo "$1" > "$COMPAREFILE"
  1461. shift
  1462. if [ -n "$MSG" ]; then
  1463. msgtest "$MSG" "$*"
  1464. fi
  1465. testoutputequal "$COMPAREFILE" "$@"
  1466. aptautotest 'testequal' "$@"
  1467. msggroup
  1468. }
  1469. testequalor2() {
  1470. msggroup 'testequalor2'
  1471. local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
  1472. local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
  1473. local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
  1474. echo "$1" > "$COMPAREFILE1"
  1475. echo "$2" > "$COMPAREFILE2"
  1476. shift 2
  1477. msgtest "Test for equality OR of" "$*"
  1478. "$@" >"$COMPAREAGAINST" 2>&1 || true
  1479. if checkdiff "$COMPAREFILE1" "$COMPAREAGAINST" >/dev/null 2>&1 || \
  1480. checkdiff "$COMPAREFILE2" "$COMPAREAGAINST" >/dev/null 2>&1
  1481. then
  1482. msgpass
  1483. else
  1484. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.output"
  1485. echo -n "\n${CINFO}Diff against OR 1${CNORMAL}" >"$OUTPUT" 2>&1
  1486. checkdiff "$COMPAREFILE1" "$COMPAREAGAINST" >"$OUTPUT" 2>&1 || true
  1487. echo -n "${CINFO}Diff against OR 2${CNORMAL}" >"$OUTPUT" 2>&1
  1488. checkdiff "$COMPAREFILE2" "$COMPAREAGAINST" >"$OUTPUT" 2>&1 || true
  1489. msgfailoutput '' "$OUTPUT"
  1490. fi
  1491. aptautotest 'testequalor2' "$@"
  1492. msggroup
  1493. }
  1494. testshowvirtual() {
  1495. msggroup 'testshowvirtual'
  1496. local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
  1497. local PACKAGE="$1"
  1498. shift
  1499. while [ -n "$1" ]; do
  1500. VIRTUAL="${VIRTUAL}
  1501. N: Can't select versions from package '$1' as it is purely virtual"
  1502. PACKAGE="${PACKAGE} $1"
  1503. shift
  1504. done
  1505. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  1506. VIRTUAL="${VIRTUAL}
  1507. N: No packages found"
  1508. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
  1509. local ARCH="$(getarchitecture 'native')"
  1510. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' >"$COMPAREFILE"
  1511. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.output"
  1512. testoutputequal "$COMPAREFILE" aptcache show "$PACKAGE"
  1513. msggroup
  1514. }
  1515. testnopackage() {
  1516. msggroup 'testnopackage'
  1517. msgtest "Test for non-existent packages" "apt-cache show $*"
  1518. local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
  1519. if [ -n "$SHOWPKG" ]; then
  1520. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnopackage.output"
  1521. echo "$SHOWPKG" >"$OUTPUT"
  1522. msgfailoutput '' "$OUTPUT"
  1523. else
  1524. msgpass
  1525. fi
  1526. msggroup
  1527. }
  1528. testnosrcpackage() {
  1529. msggroup 'testnosrcpackage'
  1530. msgtest "Test for non-existent source packages" "apt-cache showsrc $*"
  1531. local SHOWPKG="$(aptcache showsrc "$@" 2>&1 | grep '^Package: ')"
  1532. if [ -n "$SHOWPKG" ]; then
  1533. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnosrcpackage.output"
  1534. echo "$SHOWPKG" >"$OUTPUT"
  1535. msgfailoutput '' "$OUTPUT"
  1536. else
  1537. msgpass
  1538. fi
  1539. msggroup
  1540. }
  1541. testdpkgstatus() {
  1542. msggroup 'testdpkgstatus'
  1543. local STATE="$1"
  1544. local NR="$2"
  1545. shift 2
  1546. msgtest "Test that $NR package(s) are in state $STATE with" "dpkg -l $*"
  1547. local PKGS="$(dpkg -l "$@" 2>/dev/null | grep "^${STATE}" | wc -l)"
  1548. if [ "$PKGS" != $NR ]; then
  1549. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnopackage.output"
  1550. echo "$PKGS" >"$OUTPUT"
  1551. dpkg -l "$@" | grep '^[a-z]' >"$OUTPUT" >&2 || true
  1552. msgfailoutput '' "$OUTPUT"
  1553. else
  1554. msgpass
  1555. fi
  1556. msggroup
  1557. }
  1558. testdpkginstalled() {
  1559. msggroup 'testdpkginstalled'
  1560. testdpkgstatus 'ii' "$#" "$@"
  1561. msggroup
  1562. }
  1563. testdpkgnotinstalled() {
  1564. msggroup 'testdpkgnotinstalled'
  1565. testdpkgstatus 'ii' '0' "$@"
  1566. msggroup
  1567. }
  1568. testmarkedauto() {
  1569. msggroup 'testmarkedauto'
  1570. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
  1571. if [ -n "$1" ]; then
  1572. msgtest 'Test for correctly marked as auto-installed' "$*"
  1573. while [ -n "$1" ]; do echo "$1"; shift; done | sort > "$COMPAREFILE"
  1574. else
  1575. msgtest 'Test for correctly marked as auto-installed' 'no package'
  1576. echo -n > "$COMPAREFILE"
  1577. fi
  1578. testoutputequal "$COMPAREFILE" aptmark showauto
  1579. msggroup
  1580. }
  1581. testmarkedmanual() {
  1582. msggroup 'testmarkedmanual'
  1583. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedmanual.comparefile"
  1584. if [ -n "$1" ]; then
  1585. msgtest 'Test for correctly marked as manually installed' "$*"
  1586. while [ -n "$1" ]; do echo "$1"; shift; done | sort > "$COMPAREFILE"
  1587. else
  1588. msgtest 'Test for correctly marked as manually installed' 'no package'
  1589. echo -n > "$COMPAREFILE"
  1590. fi
  1591. testoutputequal "$COMPAREFILE" aptmark showmanual
  1592. msggroup
  1593. }
  1594. catfile() {
  1595. if [ "${1##*.}" = 'deb' ]; then
  1596. stat >&2 "$1" || true
  1597. file >&2 "$1" || true
  1598. else
  1599. cat >&2 "$1" || true
  1600. fi
  1601. }
  1602. msgfailoutput() {
  1603. msgreportheader 'msgfailoutput'
  1604. local MSG="$1"
  1605. local OUTPUT="$2"
  1606. shift 2
  1607. local CMD="$1"
  1608. if [ "$1" = 'grep' -o "$1" = 'tail' -o "$1" = 'head' ]; then
  1609. echo >&2
  1610. while [ -n "$2" ]; do shift; done
  1611. echo "#### Complete file: $1 ####"
  1612. catfile "$1"
  1613. echo "#### $CMD output ####"
  1614. elif [ "$1" = 'test' ]; then
  1615. echo >&2
  1616. # doesn't support ! or non-file flags
  1617. msgfailoutputstatfile() {
  1618. local FILEFLAGS='^-[bcdefgGhkLOprsStuwx]$'
  1619. if expr match "$1" "$FILEFLAGS" >/dev/null; then
  1620. echo "#### stat(2) of file: $2 ####"
  1621. stat "$2" || true
  1622. if test -d "$2"; then
  1623. echo "#### The directory contains: $2 ####"
  1624. ls >&2 "$2" || true
  1625. elif test -e "$2"; then
  1626. echo "#### Complete file: $2 ####"
  1627. catfile "$2"
  1628. fi
  1629. fi
  1630. }
  1631. msgfailoutputstatfile "$2" "$3"
  1632. while [ -n "$5" ] && [ "$4" = '-o' -o "$4" = '-a' ]; do
  1633. shift 3
  1634. msgfailoutputstatfile "$2" "$3"
  1635. done
  1636. echo '#### test output ####'
  1637. elif [ "$1" = 'cmp' ]; then
  1638. echo >&2
  1639. while [ -n "$2" ]; do
  1640. echo "#### Complete file: $2 ####"
  1641. catfile "$2"
  1642. shift
  1643. done
  1644. echo '#### cmp output ####'
  1645. fi
  1646. catfile "$OUTPUT"
  1647. msgfail "$MSG"
  1648. }
  1649. testsuccesswithglobalerror() {
  1650. local TYPE="$1"
  1651. local ERRORS="$2"
  1652. shift 2
  1653. msggroup "$TYPE"
  1654. if [ "$1" = '--nomsg' ]; then
  1655. shift
  1656. else
  1657. msgtest 'Test for successful execution of' "$*"
  1658. fi
  1659. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/${TYPE}.output"
  1660. if "$@" >"${OUTPUT}" 2>&1; then
  1661. if expr match "$1" '^apt.*' >/dev/null; then
  1662. if grep -q -E ' runtime error: ' "$OUTPUT"; then
  1663. msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
  1664. elif grep -E "^[${ERRORS}]: " "$OUTPUT" > "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" 2>&1; then
  1665. if [ "$IGNORE_PTY_NOT_MOUNTED" = '1' ]; then
  1666. if echo 'E: Can not write log (Is /dev/pts mounted?) - posix_openpt (2: No such file or directory)' \
  1667. | cmp - "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" >/dev/null 2>&1; then
  1668. msgpass
  1669. else
  1670. msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
  1671. fi
  1672. else
  1673. msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
  1674. fi
  1675. elif [ "$TYPE" = 'testsuccesswithnotice' ]; then
  1676. if grep -q -E "^N: " "$OUTPUT"; then
  1677. msgpass
  1678. else
  1679. msgfailoutput 'successful run, but output had no notices' "$OUTPUT" "$@"
  1680. fi
  1681. else
  1682. msgpass
  1683. fi
  1684. else
  1685. msgpass
  1686. fi
  1687. else
  1688. local EXITCODE=$?
  1689. msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
  1690. fi
  1691. aptautotest "$TYPE" "$@"
  1692. msggroup
  1693. }
  1694. testsuccesswithnotice() {
  1695. testsuccesswithglobalerror 'testsuccesswithnotice' 'WE' "$@"
  1696. }
  1697. testsuccess() {
  1698. testsuccesswithglobalerror 'testsuccess' 'NWE' "$@"
  1699. }
  1700. testwarning() {
  1701. msggroup 'testwarning'
  1702. if [ "$1" = '--nomsg' ]; then
  1703. shift
  1704. else
  1705. msgtest 'Test for successful execution with warnings of' "$*"
  1706. fi
  1707. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output"
  1708. if "$@" >"${OUTPUT}" 2>&1; then
  1709. if expr match "$1" '^apt.*' >/dev/null; then
  1710. if grep -q -E ' runtime error: ' "$OUTPUT"; then
  1711. msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
  1712. elif grep -q -E '^E: ' "$OUTPUT"; then
  1713. msgfailoutput 'successful run, but output contains errors' "$OUTPUT" "$@"
  1714. elif ! grep -q -E '^W: ' "$OUTPUT"; then
  1715. msgfailoutput 'successful run, but output contains no warnings' "$OUTPUT" "$@"
  1716. else
  1717. msgpass
  1718. fi
  1719. else
  1720. msgpass
  1721. fi
  1722. else
  1723. local EXITCODE=$?
  1724. msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
  1725. fi
  1726. aptautotest 'testwarning' "$@"
  1727. msggroup
  1728. }
  1729. testfailure() {
  1730. msggroup 'testfailure'
  1731. if [ "$1" = '--nomsg' ]; then
  1732. shift
  1733. else
  1734. msgtest 'Test for failure in execution of' "$*"
  1735. fi
  1736. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
  1737. if "$@" >"${OUTPUT}" 2>&1; then
  1738. local EXITCODE=$?
  1739. msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
  1740. else
  1741. local EXITCODE=$?
  1742. if expr match "$1" '^apt.*' >/dev/null; then
  1743. if [ "$1" = 'aptkey' ]; then
  1744. if grep -q " Can't check signature:
  1745. BAD signature from
  1746. signature could not be verified" "$OUTPUT"; then
  1747. msgpass
  1748. else
  1749. msgfailoutput "run failed with exitcode ${EXITCODE}, but no signature error" "$OUTPUT" "$@"
  1750. fi
  1751. else
  1752. if grep -q -E ' runtime error: ' "$OUTPUT"; then
  1753. msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
  1754. elif grep -q -E '==ERROR' "$OUTPUT"; then
  1755. msgfailoutput 'compiler sanitizers reported errors' "$OUTPUT" "$@"
  1756. elif ! grep -q -E '^E: ' "$OUTPUT"; then
  1757. msgfailoutput "run failed with exitcode ${EXITCODE}, but with no errors" "$OUTPUT" "$@"
  1758. else
  1759. msgpass
  1760. fi
  1761. fi
  1762. else
  1763. msgpass
  1764. fi
  1765. fi
  1766. aptautotest 'testfailure' "$@"
  1767. msggroup
  1768. }
  1769. testreturnstateequal() {
  1770. local STATE="$1"
  1771. if [ "$STATE" = 'testsuccesswithglobalerror' ]; then
  1772. local STATE="$2"
  1773. local TYPE="$3"
  1774. shift 3
  1775. msggroup "${STATE}equal"
  1776. if [ "$1" != '--nomsg' ]; then
  1777. local CMP="$1"
  1778. shift
  1779. testsuccesswithglobalerror "$STATE" "$TYPE" "$@"
  1780. testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
  1781. else
  1782. local CMP="$2"
  1783. shift 2
  1784. testsuccesswithglobalerror "$STATE" "$TYPE" "$@"
  1785. testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
  1786. fi
  1787. else
  1788. msggroup "${STATE}equal"
  1789. if [ "$2" != '--nomsg' ]; then
  1790. local CMP="$2"
  1791. shift 2
  1792. "$STATE" "$@"
  1793. testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
  1794. else
  1795. local CMP="$3"
  1796. shift 3
  1797. "$STATE" --nomsg "$@"
  1798. testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
  1799. fi
  1800. fi
  1801. msggroup
  1802. }
  1803. testsuccessequal() {
  1804. # we compare output, so we know perfectly well about N:
  1805. testreturnstateequal 'testsuccesswithglobalerror' 'testsuccess' 'WE' "$@"
  1806. }
  1807. testwarningequal() {
  1808. testreturnstateequal 'testwarning' "$@"
  1809. }
  1810. testfailureequal() {
  1811. testreturnstateequal 'testfailure' "$@"
  1812. }
  1813. testfailuremsg() {
  1814. msggroup 'testfailuremsg'
  1815. local CMP="$1"
  1816. shift
  1817. testfailure "$@"
  1818. msgtest 'Check that the output of the previous failed command has expected' 'failures and warnings'
  1819. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailuremsg.comparefile"
  1820. grep '^\(W\|E\|N\):' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output" > "$COMPAREFILE" 2>&1 || true
  1821. testoutputequal "$COMPAREFILE" echo "$CMP"
  1822. msggroup
  1823. }
  1824. testwarningmsg() {
  1825. msggroup 'testwarningmsg'
  1826. local CMP="$1"
  1827. shift
  1828. testwarning "$@"
  1829. msgtest 'Check that the output of the previous warned command has expected' 'warnings'
  1830. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarningmsg.comparefile"
  1831. grep '^\(W\|E\|N\):' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output" > "$COMPAREFILE" 2>&1 || true
  1832. testoutputequal "$COMPAREFILE" echo "$CMP"
  1833. msggroup
  1834. }
  1835. testfilestats() {
  1836. msggroup 'testfilestats'
  1837. msgtest "Test that file $1 has $2 $3" "$4"
  1838. if [ "$4" "$3" "$(stat --format "$2" "$1")" ]; then
  1839. msgpass
  1840. else
  1841. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfilestats.output"
  1842. {
  1843. ls -ld "$1" || true
  1844. echo -n "stat(1) reports for $2: "
  1845. stat --format "$2" "$1" || true
  1846. } >"$OUTPUT" 2>&1
  1847. msgfailoutput '' "$OUTPUT"
  1848. fi
  1849. msggroup
  1850. }
  1851. testaccessrights() {
  1852. msggroup 'testaccessrights'
  1853. testfilestats "$1" '%a' '=' "$2"
  1854. msggroup
  1855. }
  1856. testwebserverlaststatuscode() {
  1857. msggroup 'testwebserverlaststatuscode'
  1858. local DOWNLOG="${TMPWORKINGDIRECTORY}/rootdir/tmp/webserverstatus-testfile.log"
  1859. local STATUS="${TMPWORKINGDIRECTORY}/downloaded/webserverstatus-statusfile.log"
  1860. rm -f "$DOWNLOG" "$STATUS"
  1861. msgtest 'Test last status code from the webserver was' "$1"
  1862. if downloadfile "http://localhost:${APTHTTPPORT}/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG" && [ "$(cat "$STATUS")" = "$1" ]; then
  1863. msgpass
  1864. else
  1865. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwebserverlaststatuscode.output"
  1866. {
  1867. if [ -n "$2" ]; then
  1868. shift
  1869. echo >&2 '#### Additionally provided output files contain:'
  1870. cat >&2 "$@"
  1871. fi
  1872. echo >&2 '#### Download log of the status code:'
  1873. cat >&2 "$DOWNLOG"
  1874. } >"$OUTPUT" 2>&1
  1875. msgfailoutput "Status was $(cat "$STATUS")" "$OUTPUT"
  1876. fi
  1877. msggroup
  1878. }
  1879. mapkeynametokeyid() {
  1880. while [ -n "$1" ]; do
  1881. case "$1" in
  1882. *Joe*|*Sixpack*|newarchive) echo '5A90D141DBAC8DAE';;
  1883. *Rex*|*Expired*) echo '4BC0A39C27CE74F9';;
  1884. *Marvin*|*Paranoid*) echo 'E8525D47528144E2';;
  1885. oldarchive) echo 'FDD2DB85F68C85A3';;
  1886. *) echo 'UNKNOWN KEY';;
  1887. esac
  1888. shift
  1889. done
  1890. }
  1891. testaptkeys() {
  1892. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/aptkeylist.output"
  1893. if ! aptkey list --with-colon 2>/dev/null | grep '^pub' | cut -d':' -f 5 > "$OUTPUT"; then
  1894. echo -n > "$OUTPUT"
  1895. fi
  1896. testfileequal "$OUTPUT" "$(mapkeynametokeyid "$@")"
  1897. }
  1898. pause() {
  1899. echo "STOPPED execution. Press enter to continue"
  1900. local IGNORE
  1901. read IGNORE
  1902. }
  1903. logcurrentarchivedirectory() {
  1904. find "${TMPWORKINGDIRECTORY}/aptarchive/dists" -type f | while read line; do
  1905. stat --format '%U:%G:%a:%n' "$line"
  1906. done | sort > "${TMPWORKINGDIRECTORY}/rootdir/var/log/aptgetupdate.before.lst"
  1907. }
  1908. listcurrentlistsdirectory() {
  1909. {
  1910. find rootdir/var/lib/apt/lists -maxdepth 1 -type d | while read line; do
  1911. stat --format '%U:%G:%a:%n' "$line"
  1912. done
  1913. find rootdir/var/lib/apt/lists -maxdepth 1 \! -type d | while read line; do
  1914. stat --format '%U:%G:%a:%s:%y:%n' "$line"
  1915. done
  1916. } | sort
  1917. }
  1918. forallsupportedcompressors() {
  1919. rm -f "${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/00force-compressor"
  1920. for COMP in $(aptconfig dump 'APT::Compressor' --format '%f%n' | cut -d':' -f 5 | uniq); do
  1921. if [ -z "$COMP" -o "$COMP" = '.' ]; then continue; fi
  1922. "$@" "$COMP"
  1923. done
  1924. }
  1925. breakfiles() {
  1926. while [ -n "$1" ]; do
  1927. mv -f "${1}" "${1}.bak"
  1928. testsuccess dd if=/dev/zero of="${1}" bs="$(stat -c %s "${1}.bak")" count=1
  1929. shift
  1930. done
  1931. }
  1932. unbreakfiles() {
  1933. while [ -n "$1" ]; do
  1934. mv -f "${1}.bak" "${1}"
  1935. shift
  1936. done
  1937. }
  1938. ### convenience hacks ###
  1939. mkdir() {
  1940. # creating some directories by hand is a tedious task, so make it look simple
  1941. local PARAMS="$*"
  1942. if [ "$PARAMS" != "${PARAMS#*rootdir/var/lib/apt/lists}" ]; then
  1943. # only the last directory created by mkdir is effected by the -m !
  1944. command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt"
  1945. command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"
  1946. command mkdir -m 700 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
  1947. command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/auxfiles"
  1948. touch "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/lock"
  1949. if [ "$(id -u)" = '0' ]; then
  1950. chown _apt:$(id -gn) "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
  1951. chown _apt:$(id -gn) "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/auxfiles"
  1952. fi
  1953. else
  1954. command mkdir "$@"
  1955. fi
  1956. }
  1957. ### The following tests are run by most test methods automatically to check
  1958. ### general things about commands executed without writing the test every time.
  1959. aptautotest() {
  1960. if [ $# -lt 3 ]; then return; fi
  1961. local TESTCALL="$1"
  1962. local CMD="$2"
  1963. local FIRSTOPT="$3"
  1964. shift 2
  1965. for i in "$@"; do
  1966. if ! expr match "$i" '^-' >/dev/null 2>&1; then
  1967. FIRSTOPT="$i"
  1968. break
  1969. fi
  1970. done
  1971. shift
  1972. local AUTOTEST="aptautotest_$(echo "${CMD##*/}_${FIRSTOPT}" | tr -d -c 'A-za-z0-9')"
  1973. if command -v $AUTOTEST >/dev/null; then
  1974. # save and restore the *.output files from other tests
  1975. # as we might otherwise override them in these automatic tests
  1976. rm -rf "${TMPWORKINGDIRECTORY}/rootdir/tmp-before"
  1977. mv "${TMPWORKINGDIRECTORY}/rootdir/tmp" "${TMPWORKINGDIRECTORY}/rootdir/tmp-before"
  1978. mkdir "${TMPWORKINGDIRECTORY}/rootdir/tmp"
  1979. $AUTOTEST "$TESTCALL" "$@"
  1980. rm -rf "${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest"
  1981. mv "${TMPWORKINGDIRECTORY}/rootdir/tmp" "${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest"
  1982. mv "${TMPWORKINGDIRECTORY}/rootdir/tmp-before" "${TMPWORKINGDIRECTORY}/rootdir/tmp"
  1983. fi
  1984. }
  1985. aptautotest_aptget_update() {
  1986. local TESTCALL="$1"
  1987. while [ -n "$2" ]; do
  1988. if [ "$2" = '--print-uris' ]; then return; fi # simulation mode
  1989. shift
  1990. done
  1991. if ! test -d "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"; then return; fi
  1992. testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
  1993. testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
  1994. (
  1995. cd /
  1996. # all copied files are properly chmodded
  1997. local backupIFS="$IFS"
  1998. IFS="$(printf "\n\b")"
  1999. find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" -type f ! -name 'lock' | while read file; do
  2000. testfilestats "$file" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:644"
  2001. done
  2002. IFS="$backupIFS"
  2003. if [ "$TESTCALL" = 'testsuccess' ]; then
  2004. # failure cases can retain partial files and such
  2005. testempty find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" -mindepth 1 ! \( -name 'lock' -o -name '*.FAILED' \)
  2006. fi
  2007. if [ -s "${TMPWORKINGDIRECTORY}/rootdir/var/log/aptgetupdate.before.lst" ]; then
  2008. testfileequal "${TMPWORKINGDIRECTORY}/rootdir/var/log/aptgetupdate.before.lst" \
  2009. "$(find "${TMPWORKINGDIRECTORY}/aptarchive/dists" -type f | while read line; do stat --format '%U:%G:%a:%n' "$line"; done | sort)"
  2010. fi
  2011. )
  2012. }
  2013. aptautotest_apt_update() { aptautotest_aptget_update "$@"; }
  2014. aptautotest_aptcdrom_add() { aptautotest_aptget_update "$@"; }
  2015. testaptautotestnodpkgwarning() {
  2016. local TESTCALL="$1"
  2017. while [ -n "$2" ]; do
  2018. if expr match "$2" '^-[a-z]*s' >/dev/null 2>&1; then return; fi # simulation mode
  2019. if expr match "$2" '^-dy\?' >/dev/null 2>&1; then return; fi # download-only mode
  2020. shift
  2021. done
  2022. testfailure grep '^dpkg: warning:.*\(ignor\|unknown\).*' "${TMPWORKINGDIRECTORY}/rootdir/tmp-before/${TESTCALL}.output"
  2023. }
  2024. aptautotest_aptget_install() { testaptautotestnodpkgwarning "$@"; }
  2025. aptautotest_aptget_remove() { testaptautotestnodpkgwarning "$@"; }
  2026. aptautotest_aptget_purge() { testaptautotestnodpkgwarning "$@"; }
  2027. aptautotest_apt_install() { testaptautotestnodpkgwarning "$@"; }
  2028. aptautotest_apt_remove() { testaptautotestnodpkgwarning "$@"; }
  2029. aptautotest_apt_purge() { testaptautotestnodpkgwarning "$@"; }
  2030. testaptmarknodefaultsections() {
  2031. testfailure grep '^Auto-Installed: 0$' "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/extended_states"
  2032. }
  2033. aptautotest_aptmark_auto() { testaptmarknodefaultsections "$@"; }
  2034. aptautotest_aptmark_manual() { testaptmarknodefaultsections "$@"; }
  2035. aptautotest_aptget_markauto() { testaptmarknodefaultsections "$@"; }
  2036. aptautotest_aptget_markmanual() { testaptmarknodefaultsections "$@"; }