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.
 
 
 
 
 
 

1275 lines
42 KiB

  1. #!/bin/sh -- # no runable script, just for vi
  2. EXIT_CODE=0
  3. # we all like colorful messages
  4. if [ "$MSGCOLOR" != 'NO' ]; then
  5. if [ ! -t 1 ]; then # but check that we output to a terminal
  6. export MSGCOLOR='NO'
  7. fi
  8. fi
  9. if [ "$MSGCOLOR" != 'NO' ]; then
  10. CERROR="\033[1;31m" # red
  11. CWARNING="\033[1;33m" # yellow
  12. CMSG="\033[1;32m" # green
  13. CINFO="\033[1;96m" # light blue
  14. CDEBUG="\033[1;94m" # blue
  15. CNORMAL="\033[0;39m" # default system console color
  16. CDONE="\033[1;32m" # green
  17. CPASS="\033[1;32m" # green
  18. CFAIL="\033[1;31m" # red
  19. CCMD="\033[1;35m" # pink
  20. fi
  21. msgdie() { printf "${CERROR}E: $1${CNORMAL}\n" >&2; exit 1; }
  22. msgwarn() { printf "${CWARNING}W: $1${CNORMAL}\n" >&2; }
  23. msgmsg() { printf "${CMSG}$1${CNORMAL}\n"; }
  24. msginfo() { printf "${CINFO}I: $1${CNORMAL}\n"; }
  25. msgdebug() { printf "${CDEBUG}D: $1${CNORMAL}\n"; }
  26. msgdone() { printf "${CDONE}DONE${CNORMAL}\n"; }
  27. msgnwarn() { printf "${CWARNING}W: $1${CNORMAL}" >&2; }
  28. msgnmsg() { printf "${CMSG}$1${CNORMAL}"; }
  29. msgninfo() { printf "${CINFO}I: $1${CNORMAL}"; }
  30. msgndebug() { printf "${CDEBUG}D: $1${CNORMAL}"; }
  31. msgtest() {
  32. while [ -n "$1" ]; do
  33. printf "${CINFO}$1${CCMD} "
  34. printf -- "$(echo "$2" | sed -e 's#^apt\([cgfs]\)#apt-\1#')${CINFO} "
  35. shift
  36. if [ -n "$1" ]; then shift; else break; fi
  37. done
  38. printf "…${CNORMAL} "
  39. }
  40. msgpass() { printf "${CPASS}PASS${CNORMAL}\n"; }
  41. msgskip() { printf "${CWARNING}SKIP${CNORMAL}\n" >&2; }
  42. msgfail() {
  43. if [ $# -gt 0 ]; then printf "${CFAIL}FAIL: $*${CNORMAL}\n" >&2;
  44. else printf "${CFAIL}FAIL${CNORMAL}\n" >&2; fi
  45. EXIT_CODE=$((EXIT_CODE+1));
  46. }
  47. # enable / disable Debugging
  48. MSGLEVEL=${MSGLEVEL:-3}
  49. if [ $MSGLEVEL -le 0 ]; then
  50. msgdie() { true; }
  51. fi
  52. if [ $MSGLEVEL -le 1 ]; then
  53. msgwarn() { true; }
  54. msgnwarn() { true; }
  55. fi
  56. if [ $MSGLEVEL -le 2 ]; then
  57. msgmsg() { true; }
  58. msgnmsg() { true; }
  59. msgtest() { true; }
  60. msgpass() { printf " ${CPASS}P${CNORMAL}"; }
  61. msgskip() { printf " ${CWARNING}S${CNORMAL}" >&2; }
  62. if [ -n "$CFAIL" ]; then
  63. msgfail() { printf " ${CFAIL}FAIL${CNORMAL}" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
  64. else
  65. msgfail() { printf " ###FAILED###" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
  66. fi
  67. fi
  68. if [ $MSGLEVEL -le 3 ]; then
  69. msginfo() { true; }
  70. msgninfo() { true; }
  71. fi
  72. if [ $MSGLEVEL -le 4 ]; then
  73. msgdebug() { true; }
  74. msgndebug() { true; }
  75. fi
  76. msgdone() {
  77. if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
  78. [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
  79. [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
  80. [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
  81. [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
  82. true;
  83. else
  84. printf "${CDONE}DONE${CNORMAL}\n";
  85. fi
  86. }
  87. getaptconfig() {
  88. if [ -f ./aptconfig.conf ]; then
  89. echo "./aptconfig.conf"
  90. elif [ -f ../aptconfig.conf ]; then
  91. echo "../aptconfig.conf"
  92. fi
  93. }
  94. runapt() {
  95. msgdebug "Executing: ${CCMD}$*${CDEBUG} "
  96. local CMD="$1"
  97. shift
  98. case $CMD in
  99. sh|aptitude|*/*) ;;
  100. *) CMD="${BUILDDIRECTORY}/$CMD";;
  101. esac
  102. MALLOC_PERTURB_=21 MALLOC_CHECK_=2 APT_CONFIG="$(getaptconfig)" LD_LIBRARY_PATH=${BUILDDIRECTORY} $CMD "$@"
  103. }
  104. aptconfig() { runapt apt-config "$@"; }
  105. aptcache() { runapt apt-cache "$@"; }
  106. aptcdrom() { runapt apt-cdrom "$@"; }
  107. aptget() { runapt apt-get "$@"; }
  108. aptftparchive() { runapt apt-ftparchive "$@"; }
  109. aptkey() { runapt apt-key "$@"; }
  110. aptmark() { runapt apt-mark "$@"; }
  111. aptsortpkgs() { runapt apt-sortpkgs "$@"; }
  112. apt() { runapt apt "$@"; }
  113. apthelper() { runapt "${APTHELPERBINDIR}/apt-helper" "$@"; }
  114. aptwebserver() { runapt "${APTWEBSERVERBINDIR}/aptwebserver" "$@"; }
  115. aptitude() { runapt aptitude "$@"; }
  116. aptextracttemplates() { runapt apt-extracttemplates "$@"; }
  117. aptinternalsolver() { runapt "${APTINTERNALSOLVER}" "$@"; }
  118. dpkg() {
  119. command dpkg --root=${TMPWORKINGDIRECTORY}/rootdir --force-not-root --force-bad-path --log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log "$@"
  120. }
  121. dpkgcheckbuilddeps() {
  122. command dpkg-checkbuilddeps --admindir=${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg "$@"
  123. }
  124. gdb() {
  125. echo "gdb: run »$*«"
  126. CMD="$1"
  127. shift
  128. APT_CONFIG=aptconfig.conf LD_LIBRARY_PATH=${LIBRARYPATH} command gdb ${BUILDDIRECTORY}/$CMD --args ${BUILDDIRECTORY}/$CMD "$@"
  129. }
  130. gpg() {
  131. # see apt-key for the whole trickery. Setup is done in setupenvironment
  132. command gpg --ignore-time-conflict --no-options --no-default-keyring \
  133. --homedir "${TMPWORKINGDIRECTORY}/gnupghome" \
  134. --no-auto-check-trustdb --trust-model always \
  135. "$@"
  136. }
  137. exitwithstatus() {
  138. # error if we about to overflow, but ...
  139. # "255 failures ought to be enough for everybody"
  140. if [ $EXIT_CODE -gt 255 ]; then
  141. msgdie "Total failure count $EXIT_CODE too big"
  142. fi
  143. exit $((EXIT_CODE <= 255 ? EXIT_CODE : 255));
  144. }
  145. shellsetedetector() {
  146. local exit_status=$?
  147. if [ "$exit_status" != '0' ]; then
  148. printf >&2 "${CERROR}E: Looks like the testcases ended prematurely with exitcode: ${exit_status}${CNORMAL}\n"
  149. if [ "$EXIT_CODE" = '0' ]; then
  150. EXIT_CODE="$exit_status"
  151. fi
  152. fi
  153. }
  154. addtrap() {
  155. if [ "$1" = 'prefix' ]; then
  156. CURRENTTRAP="$2 $CURRENTTRAP"
  157. else
  158. CURRENTTRAP="$CURRENTTRAP $1"
  159. fi
  160. trap "shellsetedetector; $CURRENTTRAP exitwithstatus;" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  161. }
  162. setupenvironment() {
  163. TMPWORKINGDIRECTORY=$(mktemp -d)
  164. TESTDIRECTORY=$(readlink -f $(dirname $0))
  165. msgninfo "Preparing environment for ${CCMD}$(basename $0)${CINFO} in ${TMPWORKINGDIRECTORY}… "
  166. # allow overriding the default BUILDDIR location
  167. BUILDDIRECTORY=${APT_INTEGRATION_TESTS_BUILD_DIR:-"${TESTDIRECTORY}/../../build/bin"}
  168. LIBRARYPATH=${APT_INTEGRATION_TESTS_LIBRARY_PATH:-"${BUILDDIRECTORY}"}
  169. METHODSDIR=${APT_INTEGRATION_TESTS_METHODS_DIR:-"${BUILDDIRECTORY}/methods"}
  170. APTHELPERBINDIR=${APT_INTEGRATION_TESTS_LIBEXEC_DIR:-"${BUILDDIRECTORY}"}
  171. APTWEBSERVERBINDIR=${APT_INTEGRATION_TESTS_WEBSERVER_BIN_DIR:-"${BUILDDIRECTORY}"}
  172. APTINTERNALSOLVER=${APT_INTEGRATION_TESTS_INTERNAL_SOLVER:-"${BUILDDIRECTORY}/apt-internal-solver"}
  173. test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
  174. # -----
  175. addtrap "cd /; rm -rf $TMPWORKINGDIRECTORY;"
  176. cd $TMPWORKINGDIRECTORY
  177. mkdir rootdir aptarchive keys
  178. cd rootdir
  179. mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d
  180. mkdir -p var/cache var/lib/apt var/log tmp
  181. mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
  182. touch var/lib/dpkg/available
  183. mkdir -p usr/lib/apt
  184. ln -s ${METHODSDIR} usr/lib/apt/methods
  185. if [ "$BUILDDIRECTORY" = "$LIBRARYPATH" ]; then
  186. mkdir -p usr/lib/apt/solvers
  187. ln -s "${BUILDDIRECTORY}/apt-dump-solver" usr/lib/apt/solvers/dump
  188. ln -s "${BUILDDIRECTORY}/apt-internal-solver" usr/lib/apt/solvers/apt
  189. echo "Dir::Bin::Solvers \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/solvers\";" > etc/apt/apt.conf.d/externalsolver.conf
  190. fi
  191. # use the autoremove from the BUILDDIRECTORY if its there, otherwise
  192. # system
  193. if [ -e ${BUILDDIRECTORY}/../../debian/apt.conf.autoremove ]; then
  194. ln -s ${BUILDDIRECTORY}/../../debian/apt.conf.autoremove etc/apt/apt.conf.d/01autoremove
  195. else
  196. ln -s /etc/apt/apt.conf.d/01autoremove etc/apt/apt.conf.d/01autoremove
  197. fi
  198. cd ..
  199. local PACKAGESFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Packages-/' -e 's/^skip-/Packages-/')
  200. if [ -f "${TESTDIRECTORY}/${PACKAGESFILE}" ]; then
  201. cp "${TESTDIRECTORY}/${PACKAGESFILE}" aptarchive/Packages
  202. fi
  203. local SOURCESSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Sources-/' -e 's/^skip-/Sources-/')
  204. if [ -f "${TESTDIRECTORY}/${SOURCESSFILE}" ]; then
  205. cp "${TESTDIRECTORY}/${SOURCESSFILE}" aptarchive/Sources
  206. fi
  207. cp $(find $TESTDIRECTORY -name '*.pub' -o -name '*.sec') keys/
  208. ln -s ${TMPWORKINGDIRECTORY}/keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  209. echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
  210. echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
  211. echo "Debug::NoLocking \"true\";" >> aptconfig.conf
  212. echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
  213. echo "Dir::Bin::Methods \"${METHODSDIR}\";" >> aptconfig.conf
  214. echo "Dir::Bin::dpkg \"fakeroot\";" >> aptconfig.conf
  215. echo "DPKG::options:: \"dpkg\";" >> aptconfig.conf
  216. echo "DPKG::options:: \"--root=${TMPWORKINGDIRECTORY}/rootdir\";" >> aptconfig.conf
  217. echo "DPKG::options:: \"--force-not-root\";" >> aptconfig.conf
  218. echo "DPKG::options:: \"--force-bad-path\";" >> aptconfig.conf
  219. if ! command dpkg --assert-multi-arch >/dev/null 2>&1; then
  220. echo "DPKG::options:: \"--force-architecture\";" >> aptconfig.conf # Added to test multiarch before dpkg is ready for it…
  221. fi
  222. echo "DPKG::options:: \"--log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log\";" >> aptconfig.conf
  223. echo 'quiet::NoUpdate "true";' >> aptconfig.conf
  224. echo "Acquire::https::CaInfo \"${TESTDIR}/apt.pem\";" > rootdir/etc/apt/apt.conf.d/99https
  225. echo "Apt::Cmd::Disable-Script-Warning \"1\";" > rootdir/etc/apt/apt.conf.d/apt-binary
  226. configcompression '.' 'gz' #'bz2' 'lzma' 'xz'
  227. # gpg needs a trustdb to function, but it can't be invalid (not even empty)
  228. # see also apt-key where this trickery comes from:
  229. local TRUSTDBDIR="${TMPWORKINGDIRECTORY}/gnupghome"
  230. mkdir "$TRUSTDBDIR"
  231. chmod 700 "$TRUSTDBDIR"
  232. # We also don't use a secret keyring, of course, but gpg panics and
  233. # implodes if there isn't one available - and writeable for imports
  234. local SECRETKEYRING="${TRUSTDBDIR}/secring.gpg"
  235. touch $SECRETKEYRING
  236. # now create the trustdb with an (empty) dummy keyring
  237. # newer gpg versions are fine without it, but play it safe for now
  238. gpg --quiet --check-trustdb --secret-keyring $SECRETKEYRING --keyring $SECRETKEYRING >/dev/null 2>&1
  239. # cleanup the environment a bit
  240. export PATH="${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
  241. export LC_ALL=C.UTF-8
  242. unset LANGUAGE APT_CONFIG
  243. unset GREP_OPTIONS DEB_BUILD_PROFILES
  244. msgdone "info"
  245. }
  246. getarchitecture() {
  247. if [ "$1" = "native" -o -z "$1" ]; then
  248. eval `aptconfig shell ARCH APT::Architecture`
  249. if [ -n "$ARCH" ]; then
  250. echo $ARCH
  251. else
  252. dpkg --print-architecture
  253. fi
  254. else
  255. echo $1
  256. fi
  257. }
  258. getarchitectures() {
  259. echo "$(aptconfig dump | grep APT::Architecture | cut -d'"' -f 2 | sed '/^$/ d' | sort | uniq | tr '\n' ' ')"
  260. }
  261. getarchitecturesfromcommalist() {
  262. echo "$1" | sed -e 's#,#\n#g' | sed -e "s/^native\$/$(getarchitecture 'native')/"
  263. }
  264. configarchitecture() {
  265. {
  266. echo "APT::Architecture \"$(getarchitecture $1)\";"
  267. while [ -n "$1" ]; do
  268. echo "APT::Architectures:: \"$(getarchitecture $1)\";"
  269. shift
  270. done
  271. } >rootdir/etc/apt/apt.conf.d/01multiarch.conf
  272. configdpkg
  273. }
  274. configdpkg() {
  275. if [ ! -e rootdir/var/lib/dpkg/status ]; then
  276. local STATUSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/status-/' -e 's/^skip-/status-/')
  277. if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
  278. cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
  279. else
  280. echo -n > rootdir/var/lib/dpkg/status
  281. fi
  282. fi
  283. rm -f rootdir/etc/apt/apt.conf.d/00foreigndpkg
  284. if command dpkg --assert-multi-arch >/dev/null 2>&1 ; then
  285. local ARCHS="$(getarchitectures)"
  286. if echo "$ARCHS" | grep -E -q '[^ ]+ [^ ]+'; then
  287. DPKGARCH="$(dpkg --print-architecture)"
  288. for ARCH in ${ARCHS}; do
  289. if [ "${ARCH}" != "${DPKGARCH}" ]; then
  290. if ! dpkg --add-architecture ${ARCH} >/dev/null 2>&1; then
  291. # old-style used e.g. in Ubuntu-P – and as it seems travis
  292. echo "DPKG::options:: \"--foreign-architecture\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
  293. echo "DPKG::options:: \"${ARCH}\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
  294. fi
  295. fi
  296. done
  297. if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
  298. # dpkg doesn't really check the version as long as it is fully installed,
  299. # but just to be sure we choose one above the required version
  300. insertinstalledpackage 'dpkg' "all" '1.16.2+fake'
  301. fi
  302. fi
  303. fi
  304. }
  305. configdpkgnoopchroot() {
  306. # create a library to noop chroot() and rewrite maintainer script executions
  307. # via execvp() as used by dpkg as we don't want our rootdir to be a fullblown
  308. # chroot directory dpkg could chroot into to execute the maintainer scripts
  309. msgtest 'Building library to preload to make maintainerscript work in' 'dpkg'
  310. cat << EOF > noopchroot.c
  311. #define _GNU_SOURCE
  312. #include <stdio.h>
  313. #include <stdlib.h>
  314. #include <string.h>
  315. #include <dlfcn.h>
  316. static char * chrootdir = NULL;
  317. int chroot(const char *path) {
  318. printf("WARNING: CHROOTing to %s was ignored!\n", path);
  319. free(chrootdir);
  320. chrootdir = strdup(path);
  321. return 0;
  322. }
  323. int execvp(const char *file, char *const argv[]) {
  324. static int (*func_execvp) (const char *, char * const []) = NULL;
  325. if (func_execvp == NULL)
  326. func_execvp = (int (*) (const char *, char * const [])) dlsym(RTLD_NEXT, "execvp");
  327. if (chrootdir == NULL || strncmp(file, "/var/lib/dpkg/", strlen("/var/lib/dpkg/")) != 0)
  328. return func_execvp(file, argv);
  329. printf("REWRITE execvp call %s into %s\n", file, chrootdir);
  330. char newfile[strlen(chrootdir) + strlen(file)];
  331. strcpy(newfile, chrootdir);
  332. strcat(newfile, file);
  333. return func_execvp(newfile, argv);
  334. }
  335. EOF
  336. testsuccess --nomsg gcc -fPIC -shared -o noopchroot.so noopchroot.c -ldl
  337. mkdir -p "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/"
  338. DPKG="${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg"
  339. echo "#!/bin/sh
  340. if [ -n \"\$LD_PRELOAD\" ]; then
  341. export LD_PRELOAD=\"${TMPWORKINGDIRECTORY}/noopchroot.so \${LD_PRELOAD}\"
  342. else
  343. export LD_PRELOAD=\"${TMPWORKINGDIRECTORY}/noopchroot.so\"
  344. fi
  345. dpkg \"\$@\"" > $DPKG
  346. chmod +x $DPKG
  347. sed -ie "s|^DPKG::options:: \"dpkg\";\$|DPKG::options:: \"$DPKG\";|" aptconfig.conf
  348. }
  349. configallowinsecurerepositories() {
  350. echo "Acquire::AllowInsecureRepositories \"$1\";" > rootdir/etc/apt/apt.conf.d/allow-insecure-repositories.conf
  351. }
  352. configcompression() {
  353. while [ -n "$1" ]; do
  354. case "$1" in
  355. '.') printf ".\t.\tcat\n";;
  356. 'gz') printf "gzip\tgz\tgzip\n";;
  357. 'bz2') printf "bzip2\tbz2\tbzip2\n";;
  358. 'lzma') printf "lzma\tlzma\txz --format=lzma\n";;
  359. 'xz') printf "xz\txz\txz\n";;
  360. *) printf "$1\t$1\t$1\n";;
  361. esac
  362. shift
  363. done > ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf
  364. }
  365. forcecompressor() {
  366. COMPRESSOR="$1"
  367. COMPRESSOR_CMD="$1"
  368. case $COMPRESSOR in
  369. gzip) COMPRESS='gz';;
  370. bzip2) COMPRESS='bz2';;
  371. lzma) COMPRESS='lzma';;
  372. xz) COMPRESS='xz';;
  373. *) msgdie "Compressor $COMPRESSOR is unknown to framework, so can't be forced by forcecompressor!";;
  374. esac
  375. local CONFFILE="${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/00force-compressor"
  376. echo "Acquire::CompressionTypes::Order { \"${COMPRESS}\"; };
  377. Dir::Bin::uncompressed \"/does/not/exist\";
  378. Dir::Bin::gzip \"/does/not/exist\";
  379. Dir::Bin::bzip2 \"/does/not/exist\";
  380. Dir::Bin::lzma \"/does/not/exist\";
  381. Dir::Bin::xz \"/does/not/exist\";" > "$CONFFILE"
  382. if [ -e "/bin/${COMPRESSOR}" ]; then
  383. echo "Dir::Bin::${COMPRESSOR} \"/bin/${COMPRESSOR}\";" >> "$CONFFILE"
  384. elif [ -e "/usr/bin/${COMPRESSOR}" ]; then
  385. echo "Dir::Bin::${COMPRESSOR} \"/usr/bin/${COMPRESSOR}\";" >> "$CONFFILE"
  386. elif [ "${COMPRESSOR}" = 'lzma' ]; then
  387. echo 'Dir::Bin::xz "/usr/bin/xz";' >> "$CONFFILE"
  388. COMPRESSOR_CMD='xz --format=lzma'
  389. else
  390. msgtest 'Test for availability of compressor' "${COMPRESSOR}"
  391. msgfail
  392. fi
  393. }
  394. setupsimplenativepackage() {
  395. local NAME="$1"
  396. local ARCH="$2"
  397. local VERSION="$3"
  398. local RELEASE="${4:-unstable}"
  399. local DEPENDENCIES="$5"
  400. local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  401. If you find such a package installed on your system,
  402. something went horribly wrong! They are autogenerated
  403. und used only by testcases and surf no other propose…"}"
  404. local SECTION="${7:-others}"
  405. local DISTSECTION
  406. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  407. DISTSECTION="main"
  408. else
  409. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  410. fi
  411. local BUILDDIR=incoming/${NAME}-${VERSION}
  412. mkdir -p ${BUILDDIR}/debian/source
  413. cd ${BUILDDIR}
  414. echo "* most suckless software product ever" > FEATURES
  415. test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright
  416. test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
  417. * Initial release
  418. -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog
  419. test -e debian/control || echo "Source: $NAME
  420. Section: $SECTION
  421. Priority: optional
  422. Maintainer: Joe Sixpack <joe@example.org>
  423. Build-Depends: debhelper (>= 7)
  424. Standards-Version: 3.9.1
  425. Package: $NAME" > debian/control
  426. if [ "$ARCH" = 'all' ]; then
  427. echo "Architecture: all" >> debian/control
  428. else
  429. echo "Architecture: any" >> debian/control
  430. fi
  431. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
  432. echo "Description: $DESCRIPTION" >> debian/control
  433. test -e debian/compat || echo "7" > debian/compat
  434. test -e debian/source/format || echo "3.0 (native)" > debian/source/format
  435. test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
  436. cd - > /dev/null
  437. }
  438. buildsimplenativepackage() {
  439. local NAME="$1"
  440. local ARCH="$2"
  441. local VERSION="$3"
  442. local RELEASE="${4:-unstable}"
  443. local DEPENDENCIES="$5"
  444. local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  445. If you find such a package installed on your system,
  446. something went horribly wrong! They are autogenerated
  447. und used only by testcases and surf no other propose…"}"
  448. local SECTION="${7:-others}"
  449. local PRIORITY="${8:-optional}"
  450. local FILE_TREE="$9"
  451. local COMPRESS_TYPE="${10:-gzip}"
  452. local DISTSECTION
  453. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  454. DISTSECTION="main"
  455. else
  456. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  457. fi
  458. local BUILDDIR=${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}
  459. msgtest "Build source package in version ${VERSION} for ${RELEASE} in ${DISTSECTION}" "$NAME"
  460. mkdir -p $BUILDDIR/debian/source
  461. echo "* most suckless software product ever" > ${BUILDDIR}/FEATURES
  462. echo "#!/bin/sh
  463. echo '$NAME says \"Hello!\"'" > ${BUILDDIR}/${NAME}
  464. echo "Copyleft by Joe Sixpack $(date +%Y)" > ${BUILDDIR}/debian/copyright
  465. echo "$NAME ($VERSION) $RELEASE; urgency=low
  466. * Initial release
  467. -- Joe Sixpack <joe@example.org> $(date -R)" > ${BUILDDIR}/debian/changelog
  468. echo "Source: $NAME
  469. Section: $SECTION
  470. Priority: $PRIORITY
  471. Maintainer: Joe Sixpack <joe@example.org>
  472. Standards-Version: 3.9.3" > ${BUILDDIR}/debian/control
  473. local BUILDDEPS="$(echo "$DEPENDENCIES" | grep '^Build-')"
  474. test -z "$BUILDDEPS" || echo "$BUILDDEPS" >> ${BUILDDIR}/debian/control
  475. echo "
  476. Package: $NAME" >> ${BUILDDIR}/debian/control
  477. if [ "$ARCH" = 'all' ]; then
  478. echo "Architecture: all" >> ${BUILDDIR}/debian/control
  479. else
  480. echo "Architecture: any" >> ${BUILDDIR}/debian/control
  481. fi
  482. local DEPS="$(echo "$DEPENDENCIES" | grep -v '^Build-')"
  483. test -z "$DEPS" || echo "$DEPS" >> ${BUILDDIR}/debian/control
  484. echo "Description: $DESCRIPTION" >> ${BUILDDIR}/debian/control
  485. echo '3.0 (native)' > ${BUILDDIR}/debian/source/format
  486. cd ${BUILDDIR}/..
  487. testsuccess --nomsg dpkg-source -b ${NAME}-${VERSION}
  488. cd - >/dev/null
  489. sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' ${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output \
  490. | while read SRC; do
  491. echo "pool/${SRC}" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist
  492. # if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
  493. # gpg --yes --secret-keyring ./keys/joesixpack.sec \
  494. # --keyring ./keys/joesixpack.pub --default-key 'Joe Sixpack' \
  495. # --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  496. # mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  497. # fi
  498. done
  499. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  500. msgtest "Build binary package for ${RELEASE} in ${SECTION}" "$NAME"
  501. rm -rf ${BUILDDIR}/debian/tmp
  502. mkdir -p ${BUILDDIR}/debian/tmp/DEBIAN ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME} ${BUILDDIR}/debian/tmp/usr/bin
  503. cp ${BUILDDIR}/debian/copyright ${BUILDDIR}/debian/changelog ${BUILDDIR}/FEATURES ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}
  504. cp ${BUILDDIR}/${NAME} ${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}
  505. if [ -n "$FILE_TREE" ]; then
  506. cp -ar "$FILE_TREE" ${BUILDDIR}/debian/tmp
  507. fi
  508. (cd ${BUILDDIR}; dpkg-gencontrol -DArchitecture=$arch)
  509. (cd ${BUILDDIR}/debian/tmp; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
  510. local LOG="${BUILDDIR}/../${NAME}_${VERSION}_${arch}.dpkg-deb.log"
  511. # ensure the right permissions as dpkg-deb ensists
  512. chmod 755 ${BUILDDIR}/debian/tmp/DEBIAN
  513. testsuccess --nomsg dpkg-deb -Z${COMPRESS_TYPE} --build ${BUILDDIR}/debian/tmp ${BUILDDIR}/..
  514. echo "pool/${NAME}_${VERSION}_${arch}.deb" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist
  515. done
  516. mkdir -p ${BUILDDIR}/../${NAME}_${VERSION}
  517. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}/
  518. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}.changelog
  519. rm -rf "${BUILDDIR}"
  520. msgdone "info"
  521. }
  522. buildpackage() {
  523. local BUILDDIR=$1
  524. local RELEASE=$2
  525. local SECTION=$3
  526. local ARCH=$(getarchitecture $4)
  527. local PKGNAME="$(echo "$BUILDDIR" | grep -o '[^/]*$')"
  528. local BUILDLOG="$(readlink -f "${BUILDDIR}/../${PKGNAME}_${RELEASE}_${SECTION}.dpkg-bp.log")"
  529. msgtest "Build package for ${RELEASE} in ${SECTION}" "$PKGNAME"
  530. cd $BUILDDIR
  531. if [ "$ARCH" = "all" ]; then
  532. ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
  533. fi
  534. testsuccess --nomsg dpkg-buildpackage -uc -us -a$ARCH
  535. cp ${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output $BUILDLOG
  536. local PKGS="$(grep '^dpkg-deb: building package' $BUILDLOG | cut -d'/' -f 2 | sed -e "s#'\.##")"
  537. local SRCS="$(grep '^dpkg-source: info: building' $BUILDLOG | grep -o '[a-z0-9._+~-]*$')"
  538. cd - > /dev/null
  539. for PKG in $PKGS; do
  540. echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
  541. done
  542. for SRC in $SRCS; do
  543. echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
  544. done
  545. }
  546. buildaptarchive() {
  547. if [ -d incoming ]; then
  548. buildaptarchivefromincoming "$@"
  549. else
  550. buildaptarchivefromfiles "$@"
  551. fi
  552. }
  553. createaptftparchiveconfig() {
  554. local COMPRESSORS="$(cut -d' ' -f 1 ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | tr '\n' ' ')"
  555. COMPRESSORS="${COMPRESSORS%* }"
  556. local ARCHS="$(find pool/ -name '*.deb' | grep -oE '_[a-z0-9-]+\.deb$' | sort | uniq | sed -e '/^_all.deb$/ d' -e 's#^_\([a-z0-9-]*\)\.deb$#\1#' | tr '\n' ' ')"
  557. if [ -z "$ARCHS" ]; then
  558. # the pool is empty, so we will operate on faked packages - let us use the configured archs
  559. ARCHS="$(getarchitectures)"
  560. fi
  561. echo -n 'Dir {
  562. ArchiveDir "' >> ftparchive.conf
  563. echo -n $(readlink -f .) >> ftparchive.conf
  564. echo -n '";
  565. CacheDir "' >> ftparchive.conf
  566. echo -n $(readlink -f ..) >> ftparchive.conf
  567. echo -n '";
  568. FileListDir "' >> ftparchive.conf
  569. echo -n $(readlink -f pool/) >> ftparchive.conf
  570. echo -n '";
  571. };
  572. Default {
  573. Packages::Compress "'"$COMPRESSORS"'";
  574. Sources::Compress "'"$COMPRESSORS"'";
  575. Contents::Compress "'"$COMPRESSORS"'";
  576. Translation::Compress "'"$COMPRESSORS"'";
  577. LongDescription "false";
  578. };
  579. TreeDefault {
  580. Directory "pool/";
  581. SrcDirectory "pool/";
  582. };
  583. APT {
  584. FTPArchive {
  585. Release {
  586. Origin "joesixpack";
  587. Label "apttestcases";
  588. Suite "unstable";
  589. Description "repository with dummy packages";
  590. Architectures "' >> ftparchive.conf
  591. echo -n "$ARCHS" >> ftparchive.conf
  592. echo 'source";
  593. };
  594. };
  595. };' >> ftparchive.conf
  596. for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
  597. echo -n 'tree "dists/' >> ftparchive.conf
  598. echo -n "$DIST" >> ftparchive.conf
  599. echo -n '" {
  600. Architectures "' >> ftparchive.conf
  601. echo -n "$ARCHS" >> ftparchive.conf
  602. echo -n 'source";
  603. FileList "' >> ftparchive.conf
  604. echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
  605. echo -n '";
  606. SourceFileList "' >> ftparchive.conf
  607. echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
  608. echo -n '";
  609. Sections "' >> ftparchive.conf
  610. echo -n "$(find ./pool/ -maxdepth 1 -name "${DIST}.*.pkglist" -type f | cut -d'/' -f 3 | cut -d'.' -f 2 | sort | uniq | tr '\n' ' ')" >> ftparchive.conf
  611. echo '";
  612. };' >> ftparchive.conf
  613. done
  614. }
  615. buildaptftparchivedirectorystructure() {
  616. local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
  617. for DIST in $DISTS; do
  618. local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
  619. for SECTION in $SECTIONS; do
  620. local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
  621. for ARCH in $ARCHS; do
  622. mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
  623. done
  624. mkdir -p dists/${DIST}/${SECTION}/source
  625. mkdir -p dists/${DIST}/${SECTION}/i18n
  626. done
  627. done
  628. }
  629. insertpackage() {
  630. local RELEASE="$1"
  631. local NAME="$2"
  632. local ARCH="$3"
  633. local VERSION="$4"
  634. local DEPENDENCIES="$5"
  635. local PRIORITY="${6:-optional}"
  636. local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  637. If you find such a package installed on your system,
  638. something went horribly wrong! They are autogenerated
  639. und used only by testcases and surf no other propose…"}"
  640. local ARCHS=""
  641. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  642. if [ "$arch" = 'all' -o "$arch" = 'none' ]; then
  643. ARCHS="$(getarchitectures)"
  644. else
  645. ARCHS="$arch"
  646. fi
  647. for BUILDARCH in $ARCHS; do
  648. local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
  649. mkdir -p $PPATH aptarchive/dists/${RELEASE}/main/source
  650. touch aptarchive/dists/${RELEASE}/main/source/Sources
  651. local FILE="${PPATH}/Packages"
  652. echo "Package: $NAME
  653. Priority: $PRIORITY
  654. Section: other
  655. Installed-Size: 42
  656. Maintainer: Joe Sixpack <joe@example.org>" >> $FILE
  657. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  658. echo "Version: $VERSION
  659. Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
  660. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  661. echo "Description: $DESCRIPTION" >> $FILE
  662. echo >> $FILE
  663. done
  664. done
  665. }
  666. insertsource() {
  667. local RELEASE="$1"
  668. local NAME="$2"
  669. local ARCH="$3"
  670. local VERSION="$4"
  671. local DEPENDENCIES="$5"
  672. local ARCHS=""
  673. local SPATH="aptarchive/dists/${RELEASE}/main/source"
  674. mkdir -p $SPATH
  675. local FILE="${SPATH}/Sources"
  676. echo "Package: $NAME
  677. Binary: $NAME
  678. Version: $VERSION
  679. Maintainer: Joe Sixpack <joe@example.org>
  680. Architecture: $ARCH" >> $FILE
  681. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  682. echo "Files:
  683. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.dsc
  684. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.tar.gz
  685. " >> $FILE
  686. }
  687. insertinstalledpackage() {
  688. local NAME="$1"
  689. local ARCH="$2"
  690. local VERSION="$3"
  691. local DEPENDENCIES="$4"
  692. local PRIORITY="${5:-optional}"
  693. local STATUS="${6:-install ok installed}"
  694. local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/installed
  695. If you find such a package installed on your system,
  696. something went horribly wrong! They are autogenerated
  697. und used only by testcases and surf no other propose…"}"
  698. local FILE='rootdir/var/lib/dpkg/status'
  699. local INFO='rootdir/var/lib/dpkg/info'
  700. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  701. echo "Package: $NAME
  702. Status: $STATUS
  703. Priority: $PRIORITY
  704. Section: other
  705. Installed-Size: 42
  706. Maintainer: Joe Sixpack <joe@example.org>
  707. Version: $VERSION" >> $FILE
  708. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  709. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  710. echo "Description: $DESCRIPTION" >> $FILE
  711. echo >> $FILE
  712. if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
  713. echo -n > ${INFO}/${NAME}:${arch}.list
  714. else
  715. echo -n > ${INFO}/${NAME}.list
  716. fi
  717. done
  718. }
  719. buildaptarchivefromincoming() {
  720. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
  721. cd aptarchive
  722. [ -e pool ] || ln -s ../incoming pool
  723. [ -e ftparchive.conf ] || createaptftparchiveconfig
  724. [ -e dists ] || buildaptftparchivedirectorystructure
  725. msgninfo "\tGenerate Packages, Sources and Contents files… "
  726. aptftparchive -qq generate ftparchive.conf
  727. cd - > /dev/null
  728. msgdone "info"
  729. generatereleasefiles
  730. }
  731. buildaptarchivefromfiles() {
  732. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
  733. find aptarchive -name 'Packages' -o -name 'Sources' | while read line; do
  734. msgninfo "\t${line} file… "
  735. compressfile "$line" "$1"
  736. msgdone "info"
  737. done
  738. generatereleasefiles "$@"
  739. }
  740. compressfile() {
  741. cat ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | while read compressor extension command; do
  742. if [ "$compressor" = '.' ]; then
  743. if [ -n "$2" ]; then
  744. touch -d "$2" "$1"
  745. fi
  746. continue
  747. fi
  748. cat "$1" | $command > "${1}.${extension}"
  749. if [ -n "$2" ]; then
  750. touch -d "$2" "${1}.${extension}"
  751. fi
  752. done
  753. }
  754. # can be overridden by testcases for their pleasure
  755. getcodenamefromsuite() {
  756. case "$1" in
  757. unstable) echo 'sid';;
  758. *) echo -n "$1";;
  759. esac
  760. }
  761. getreleaseversionfromsuite() { true; }
  762. getlabelfromsuite() { true; }
  763. generatereleasefiles() {
  764. # $1 is the Date header and $2 is the ValidUntil header to be set
  765. # both should be given in notation date/touch can understand
  766. msgninfo "\tGenerate Release files… "
  767. if [ -e aptarchive/dists ]; then
  768. for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
  769. local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
  770. local CODENAME="$(getcodenamefromsuite $SUITE)"
  771. local VERSION="$(getreleaseversionfromsuite $SUITE)"
  772. local LABEL="$(getlabelfromsuite $SUITE)"
  773. if [ -n "$VERSION" ]; then
  774. VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
  775. fi
  776. if [ -n "$LABEL" ]; then
  777. LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
  778. fi
  779. aptftparchive -qq release $dir \
  780. -o APT::FTPArchive::Release::Suite="${SUITE}" \
  781. -o APT::FTPArchive::Release::Codename="${CODENAME}" \
  782. ${LABEL} \
  783. ${VERSION} \
  784. | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
  785. if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
  786. sed -i '/^Date: / a\
  787. NotAutomatic: yes' $dir/Release
  788. fi
  789. if [ -n "$1" -a "$1" != "now" ]; then
  790. sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
  791. fi
  792. if [ -n "$2" ]; then
  793. sed -i "/^Date: / a\
  794. Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
  795. fi
  796. done
  797. else
  798. aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
  799. fi
  800. if [ -n "$1" -a "$1" != "now" ]; then
  801. for release in $(find ./aptarchive -name 'Release'); do
  802. touch -d "$1" $release
  803. done
  804. fi
  805. msgdone "info"
  806. }
  807. setupdistsaptarchive() {
  808. local APTARCHIVE=$(readlink -f ./aptarchive)
  809. rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
  810. rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
  811. for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
  812. SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
  813. msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
  814. echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
  815. echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
  816. msgdone "info"
  817. done
  818. }
  819. setupflataptarchive() {
  820. local APTARCHIVE=$(readlink -f ./aptarchive)
  821. if [ -f ${APTARCHIVE}/Packages ]; then
  822. msgninfo "\tadd deb sources.list line… "
  823. echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  824. msgdone "info"
  825. else
  826. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  827. fi
  828. if [ -f ${APTARCHIVE}/Sources ]; then
  829. msgninfo "\tadd deb-src sources.list line… "
  830. echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  831. msgdone "info"
  832. else
  833. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  834. fi
  835. }
  836. setupaptarchive() {
  837. buildaptarchive
  838. if [ -e aptarchive/dists ]; then
  839. setupdistsaptarchive
  840. else
  841. setupflataptarchive
  842. fi
  843. signreleasefiles
  844. if [ "$1" != '--no-update' ]; then
  845. msgninfo "\tSync APT's cache with the archive… "
  846. aptget update -qq
  847. msgdone "info"
  848. fi
  849. }
  850. signreleasefiles() {
  851. local SIGNER="${1:-Joe Sixpack}"
  852. local GPG="gpg --batch --yes"
  853. msgninfo "\tSign archive with $SIGNER key… "
  854. local REXKEY='keys/rexexpired'
  855. local SECEXPIREBAK="${REXKEY}.sec.bak"
  856. local PUBEXPIREBAK="${REXKEY}.pub.bak"
  857. if [ "${SIGNER}" = 'Rex Expired' ]; then
  858. # the key is expired, so gpg doesn't allow to sign with and the --faked-system-time
  859. # option doesn't exist anymore (and using faketime would add a new obscure dependency)
  860. # therefore we 'temporary' make the key not expired and restore a backup after signing
  861. cp ${REXKEY}.sec $SECEXPIREBAK
  862. cp ${REXKEY}.pub $PUBEXPIREBAK
  863. local SECUNEXPIRED="${REXKEY}.sec.unexpired"
  864. local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
  865. if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
  866. cp $SECUNEXPIRED ${REXKEY}.sec
  867. cp $PUBUNEXPIRED ${REXKEY}.pub
  868. else
  869. printf "expire\n1w\nsave\n" | $GPG --keyring ${REXKEY}.pub --secret-keyring ${REXKEY}.sec --command-fd 0 --edit-key "${SIGNER}" >/dev/null 2>&1 || true
  870. cp ${REXKEY}.sec $SECUNEXPIRED
  871. cp ${REXKEY}.pub $PUBUNEXPIRED
  872. fi
  873. fi
  874. for KEY in $(find keys/ -name '*.sec'); do
  875. GPG="$GPG --secret-keyring $KEY"
  876. done
  877. for KEY in $(find keys/ -name '*.pub'); do
  878. GPG="$GPG --keyring $KEY"
  879. done
  880. for RELEASE in $(find aptarchive/ -name Release); do
  881. $GPG --default-key "$SIGNER" --armor --detach-sign --sign --output ${RELEASE}.gpg ${RELEASE}
  882. local INRELEASE="$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')"
  883. $GPG --default-key "$SIGNER" --clearsign --output $INRELEASE $RELEASE
  884. # we might have set a specific date for the Release file, so copy it
  885. touch -d "$(stat --format "%y" ${RELEASE})" ${RELEASE}.gpg ${INRELEASE}
  886. done
  887. if [ -f "$SECEXPIREBAK" ] && [ -f "$PUBEXPIREBAK" ]; then
  888. mv -f $SECEXPIREBAK ${REXKEY}.sec
  889. mv -f $PUBEXPIREBAK ${REXKEY}.pub
  890. fi
  891. msgdone "info"
  892. }
  893. webserverconfig() {
  894. msgtest "Set webserver config option '${1}' to" "$2"
  895. local DOWNLOG='rootdir/tmp/download-testfile.log'
  896. local STATUS='rootdir/tmp/webserverconfig.status'
  897. rm -f "$STATUS" "$DOWNLOG"
  898. if downloadfile "http://localhost:8080/_config/set/${1}/${2}" "$STATUS" > "$DOWNLOG"; then
  899. msgpass
  900. else
  901. cat "$DOWNLOG" "$STATUS"
  902. msgfail
  903. fi
  904. testwebserverlaststatuscode '200'
  905. }
  906. rewritesourceslist() {
  907. local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive")"
  908. for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
  909. sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#http://localhost:8080/#${1}#" -e "s#http://localhost:4433/#${1}#"
  910. done
  911. }
  912. # wait for up to 10s for a pid file to appear to avoid possible race
  913. # when a helper is started and dosn't write the PID quick enough
  914. waitforpidfile() {
  915. local PIDFILE="$1"
  916. for i in $(seq 10); do
  917. if test -s "$PIDFILE"; then
  918. return 0
  919. fi
  920. sleep 1
  921. done
  922. msgdie "waiting for $PIDFILE failed"
  923. return 1
  924. }
  925. changetowebserver() {
  926. if [ "$1" != '--no-rewrite' ]; then
  927. rewritesourceslist 'http://localhost:8080/'
  928. else
  929. shift
  930. fi
  931. if test -x ${APTWEBSERVERBINDIR}/aptwebserver; then
  932. cd aptarchive
  933. local LOG="webserver.log"
  934. if ! aptwebserver -o aptwebserver::fork=1 "$@" >$LOG 2>&1 ; then
  935. cat $LOG
  936. false
  937. fi
  938. waitforpidfile aptwebserver.pid
  939. local PID="$(cat aptwebserver.pid)"
  940. if [ -z "$PID" ]; then
  941. msgdie 'Could not fork aptwebserver successfully'
  942. fi
  943. addtrap "kill $PID;"
  944. cd - > /dev/null
  945. else
  946. msgdie 'You have to build aptwerbserver or install a webserver'
  947. fi
  948. }
  949. changetohttpswebserver() {
  950. if ! which stunnel4 >/dev/null; then
  951. msgdie 'You need to install stunnel4 for https testcases'
  952. fi
  953. if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
  954. changetowebserver --no-rewrite "$@"
  955. fi
  956. echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
  957. cert = ${TESTDIRECTORY}/apt.pem
  958. output = /dev/null
  959. [https]
  960. accept = 4433
  961. connect = 8080
  962. " > ${TMPWORKINGDIRECTORY}/stunnel.conf
  963. stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
  964. waitforpidfile "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid"
  965. local PID="$(cat ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid)"
  966. if [ -z "$PID" ]; then
  967. msgdie 'Could not fork stunnel4 successfully'
  968. fi
  969. addtrap 'prefix' "kill ${PID};"
  970. rewritesourceslist 'https://localhost:4433/'
  971. }
  972. changetocdrom() {
  973. mkdir -p rootdir/media/cdrom/.disk
  974. local CD="$(readlink -f rootdir/media/cdrom)"
  975. echo "acquire::cdrom::mount \"${CD}\";
  976. acquire::cdrom::${CD}/::mount \"mv ${CD}-unmounted ${CD}\";
  977. acquire::cdrom::${CD}/::umount \"mv ${CD} ${CD}-unmounted\";
  978. acquire::cdrom::autodetect 0;" > rootdir/etc/apt/apt.conf.d/00cdrom
  979. echo -n "$1" > ${CD}/.disk/info
  980. if [ ! -d aptarchive/dists ]; then
  981. msgdie 'Flat file archive cdroms can not be created currently'
  982. return 1
  983. fi
  984. mv aptarchive/dists "$CD"
  985. ln -s "$(readlink -f ./incoming)" $CD/pool
  986. find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
  987. # start with an unmounted disk
  988. mv "${CD}" "${CD}-unmounted"
  989. # we don't want the disk to be modifiable
  990. addtrap 'prefix' "chmod -f -R +w $PWD/rootdir/media/cdrom/dists/ $PWD/rootdir/media/cdrom-unmounted/dists/ || true;"
  991. chmod -R -w rootdir/media/cdrom-unmounted/dists
  992. }
  993. downloadfile() {
  994. local PROTO="$(echo "$1" | cut -d':' -f 1 )"
  995. apthelper -o Debug::Acquire::${PROTO}=1 \
  996. download-file "$1" "$2" 2>&1 || true
  997. # only if the file exists the download was successful
  998. if [ -e "$2" ]; then
  999. return 0
  1000. else
  1001. return 1
  1002. fi
  1003. }
  1004. checkdiff() {
  1005. local DIFFTEXT="$(command diff -u "$@" | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  1006. if [ -n "$DIFFTEXT" ]; then
  1007. echo >&2
  1008. echo >&2 "$DIFFTEXT"
  1009. return 1
  1010. else
  1011. return 0
  1012. fi
  1013. }
  1014. testfileequal() {
  1015. local FILE="$1"
  1016. shift
  1017. msgtest "Test for correctness of file" "$FILE"
  1018. if [ -z "$*" ]; then
  1019. echo -n "" | checkdiff $FILE - && msgpass || msgfail
  1020. else
  1021. echo "$*" | checkdiff $FILE - && msgpass || msgfail
  1022. fi
  1023. }
  1024. testempty() {
  1025. msgtest "Test for no output of" "$*"
  1026. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testempty.comparefile"
  1027. if $* >$COMPAREFILE 2>&1 && test ! -s $COMPAREFILE; then
  1028. msgpass
  1029. else
  1030. cat $COMPAREFILE
  1031. msgfail
  1032. fi
  1033. }
  1034. testequal() {
  1035. local MSG='Test of equality of'
  1036. if [ "$1" = '--nomsg' ]; then
  1037. MSG=''
  1038. shift
  1039. fi
  1040. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
  1041. echo "$1" > $COMPAREFILE
  1042. shift
  1043. if [ -n "$MSG" ]; then
  1044. msgtest "$MSG" "$*"
  1045. fi
  1046. "$@" 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1047. }
  1048. testequalor2() {
  1049. local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
  1050. local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
  1051. local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
  1052. echo "$1" > $COMPAREFILE1
  1053. echo "$2" > $COMPAREFILE2
  1054. shift 2
  1055. msgtest "Test for equality OR of" "$*"
  1056. $* >$COMPAREAGAINST 2>&1 || true
  1057. if checkdiff $COMPAREFILE1 $COMPAREAGAINST >/dev/null 2>&1 || \
  1058. checkdiff $COMPAREFILE2 $COMPAREAGAINST >/dev/null 2>&1
  1059. then
  1060. msgpass
  1061. else
  1062. echo -n "\n${CINFO}Diff against OR 1${CNORMAL}"
  1063. checkdiff $COMPAREFILE1 $COMPAREAGAINST || true
  1064. echo -n "${CINFO}Diff against OR 2${CNORMAL}"
  1065. checkdiff $COMPAREFILE2 $COMPAREAGAINST || true
  1066. msgfail
  1067. fi
  1068. }
  1069. testshowvirtual() {
  1070. local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
  1071. local PACKAGE="$1"
  1072. shift
  1073. while [ -n "$1" ]; do
  1074. VIRTUAL="${VIRTUAL}
  1075. N: Can't select versions from package '$1' as it is purely virtual"
  1076. PACKAGE="${PACKAGE} $1"
  1077. shift
  1078. done
  1079. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  1080. VIRTUAL="${VIRTUAL}
  1081. N: No packages found"
  1082. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
  1083. local ARCH="$(getarchitecture 'native')"
  1084. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
  1085. aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1086. }
  1087. testnopackage() {
  1088. msgtest "Test for non-existent packages" "apt-cache show $*"
  1089. local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
  1090. if [ -n "$SHOWPKG" ]; then
  1091. echo >&2
  1092. echo >&2 "$SHOWPKG"
  1093. msgfail
  1094. else
  1095. msgpass
  1096. fi
  1097. }
  1098. testdpkginstalled() {
  1099. msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
  1100. local PKGS="$(dpkg -l "$@" 2>/dev/null | grep '^i' | wc -l)"
  1101. if [ "$PKGS" != $# ]; then
  1102. echo >&2 $PKGS
  1103. dpkg -l "$@" | grep '^[a-z]' >&2
  1104. msgfail
  1105. else
  1106. msgpass
  1107. fi
  1108. }
  1109. testdpkgnotinstalled() {
  1110. msgtest "Test for correctly not-installed package(s) with" "dpkg -l $*"
  1111. local PKGS="$(dpkg -l "$@" 2> /dev/null | grep '^i' | wc -l)"
  1112. if [ "$PKGS" != 0 ]; then
  1113. echo
  1114. dpkg -l "$@" | grep '^[a-z]' >&2
  1115. msgfail
  1116. else
  1117. msgpass
  1118. fi
  1119. }
  1120. testmarkedauto() {
  1121. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
  1122. if [ -n "$1" ]; then
  1123. msgtest 'Test for correctly marked as auto-installed' "$*"
  1124. while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
  1125. else
  1126. msgtest 'Test for correctly marked as auto-installed' 'no package'
  1127. echo -n > $COMPAREFILE
  1128. fi
  1129. aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1130. }
  1131. testsuccess() {
  1132. if [ "$1" = '--nomsg' ]; then
  1133. shift
  1134. else
  1135. msgtest 'Test for successful execution of' "$*"
  1136. fi
  1137. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output"
  1138. if $@ >${OUTPUT} 2>&1; then
  1139. msgpass
  1140. else
  1141. echo >&2
  1142. cat >&2 $OUTPUT
  1143. msgfail
  1144. fi
  1145. }
  1146. testfailure() {
  1147. if [ "$1" = '--nomsg' ]; then
  1148. shift
  1149. else
  1150. msgtest 'Test for failure in execution of' "$*"
  1151. fi
  1152. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
  1153. if $@ >${OUTPUT} 2>&1; then
  1154. echo >&2
  1155. cat >&2 $OUTPUT
  1156. msgfail
  1157. else
  1158. msgpass
  1159. fi
  1160. }
  1161. testwebserverlaststatuscode() {
  1162. local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
  1163. local STATUS='rootdir/tmp/webserverstatus-statusfile.log'
  1164. rm -f "$DOWNLOG" "$STATUS"
  1165. msgtest 'Test last status code from the webserver was' "$1"
  1166. downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG"
  1167. if [ "$(cat "$STATUS")" = "$1" ]; then
  1168. msgpass
  1169. else
  1170. echo >&2
  1171. if [ -n "$2" ]; then
  1172. shift
  1173. echo >&2 '#### Additionally provided output files contain:'
  1174. cat >&2 "$@"
  1175. fi
  1176. echo >&2 '#### Download log of the status code:'
  1177. cat >&2 "$DOWNLOG"
  1178. msgfail "Status was $(cat "$STATUS")"
  1179. fi
  1180. }
  1181. pause() {
  1182. echo "STOPPED execution. Press enter to continue"
  1183. local IGNORE
  1184. read IGNORE
  1185. }