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