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.
 
 
 
 
 
 

2033 lines
66 KiB

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