github: Fix build on Ubuntu 20.04 and add a job for 18.04
[strongswan.git] / scripts / test.sh
1 #!/bin/sh
2 # Build script for CI
3
4 build_botan()
5 {
6 # same revision used in the build recipe of the testing environment
7 BOTAN_REV=2.17.1
8 BOTAN_DIR=$DEPS_BUILD_DIR/botan
9
10 if test -d "$BOTAN_DIR"; then
11 return
12 fi
13
14 echo "$ build_botan()"
15
16 # if the leak detective is enabled we have to disable threading support
17 # (used for std::async) as that causes invalid frees somehow, the
18 # locking allocator causes a static leak via the first function that
19 # references it (e.g. crypter or hasher), so we disable that too
20 if test "$LEAK_DETECTIVE" = "yes"; then
21 BOTAN_CONFIG="--without-os-features=threads
22 --disable-modules=locking_allocator"
23 fi
24 # disable some larger modules we don't need for the tests
25 BOTAN_CONFIG="$BOTAN_CONFIG --disable-modules=pkcs11,tls,x509,xmss
26 --prefix=$DEPS_PREFIX"
27
28 git clone https://github.com/randombit/botan.git $BOTAN_DIR &&
29 cd $BOTAN_DIR &&
30 git checkout -qf $BOTAN_REV &&
31 python ./configure.py --amalgamation $BOTAN_CONFIG &&
32 make -j4 libs >/dev/null &&
33 sudo make install >/dev/null &&
34 sudo ldconfig || exit $?
35 cd -
36 }
37
38 build_wolfssl()
39 {
40 WOLFSSL_REV=v4.7.0-stable
41 WOLFSSL_DIR=$DEPS_BUILD_DIR/wolfssl
42
43 if test -d "$WOLFSSL_DIR"; then
44 return
45 fi
46
47 echo "$ build_wolfssl()"
48
49 WOLFSSL_CFLAGS="-DWOLFSSL_PUBLIC_MP -DWOLFSSL_DES_ECB -DHAVE_ECC_BRAINPOOL"
50 WOLFSSL_CONFIG="--prefix=$DEPS_PREFIX
51 --disable-crypttests --disable-examples
52 --enable-keygen --enable-rsapss --enable-aesccm
53 --enable-aesctr --enable-des3 --enable-camellia
54 --enable-curve25519 --enable-ed25519
55 --enable-curve448 --enable-ed448
56 --enable-sha3 --enable-shake256 --enable-ecccustcurves"
57
58 git clone https://github.com/wolfSSL/wolfssl.git $WOLFSSL_DIR &&
59 cd $WOLFSSL_DIR &&
60 git checkout -qf $WOLFSSL_REV &&
61 ./autogen.sh &&
62 ./configure C_EXTRA_FLAGS="$WOLFSSL_CFLAGS" $WOLFSSL_CONFIG &&
63 make -j4 >/dev/null &&
64 sudo make install >/dev/null &&
65 sudo ldconfig || exit $?
66 cd -
67 }
68
69 build_tss2()
70 {
71 TSS2_REV=2.4.3
72 TSS2_PKG=tpm2-tss-$TSS2_REV
73 TSS2_DIR=$DEPS_BUILD_DIR/$TSS2_PKG
74 TSS2_SRC=https://github.com/tpm2-software/tpm2-tss/releases/download/$TSS2_REV/$TSS2_PKG.tar.gz
75
76 if test -d "$TSS2_DIR"; then
77 return
78 fi
79
80 echo "$ build_tss2()"
81
82 curl -L $TSS2_SRC | tar xz -C $DEPS_BUILD_DIR &&
83 cd $TSS2_DIR &&
84 ./configure --prefix=$DEPS_PREFIX --disable-doxygen-doc &&
85 make -j4 >/dev/null &&
86 sudo make install >/dev/null &&
87 sudo ldconfig || exit $?
88 cd -
89 }
90
91 : ${BUILD_DIR=$PWD}
92 : ${DEPS_BUILD_DIR=$BUILD_DIR/..}
93 : ${DEPS_PREFIX=/usr/local}
94
95 if [ -e /etc/os-release ]; then
96 . /etc/os-release
97 elif [ -e /usr/lib/os-release ]; then
98 . /usr/lib/os-release
99 fi
100
101 TARGET=check
102
103 DEPS="libgmp-dev"
104
105 CFLAGS="-g -O2 -Wall -Wno-format -Wno-format-security -Wno-pointer-sign -Werror"
106
107 case "$TEST" in
108 default)
109 # should be the default, but lets make sure
110 CONFIG="--with-printf-hooks=glibc"
111 ;;
112 openssl*)
113 CONFIG="--disable-defaults --enable-pki --enable-openssl --enable-pem"
114 export TESTS_PLUGINS="test-vectors pem openssl!"
115 DEPS="libssl-dev"
116 ;;
117 gcrypt)
118 CONFIG="--disable-defaults --enable-pki --enable-gcrypt --enable-pkcs1"
119 export TESTS_PLUGINS="test-vectors pkcs1 gcrypt!"
120 if [ "$ID" = "ubuntu" -a "$VERSION_ID" = "20.04" ]; then
121 DEPS="libgcrypt20-dev"
122 else
123 DEPS="libgcrypt11-dev"
124 fi
125 ;;
126 botan)
127 CONFIG="--disable-defaults --enable-pki --enable-botan --enable-pem"
128 export TESTS_PLUGINS="test-vectors pem botan!"
129 DEPS=""
130 if test "$1" = "build-deps"; then
131 build_botan
132 fi
133 ;;
134 wolfssl)
135 CONFIG="--disable-defaults --enable-pki --enable-wolfssl --enable-pem"
136 export TESTS_PLUGINS="test-vectors pem wolfssl!"
137 # build with custom options to enable all the features the plugin supports
138 DEPS=""
139 if test "$1" = "build-deps"; then
140 build_wolfssl
141 fi
142 ;;
143 printf-builtin)
144 CONFIG="--with-printf-hooks=builtin"
145 ;;
146 all|coverage|sonarcloud)
147 CONFIG="--enable-all --disable-android-dns --disable-android-log
148 --disable-kernel-pfroute --disable-keychain
149 --disable-lock-profiler --disable-padlock --disable-fuzzing
150 --disable-osx-attr --disable-tkm --disable-uci
151 --disable-unwind-backtraces
152 --disable-svc --disable-dbghelp-backtraces --disable-socket-win
153 --disable-kernel-wfp --disable-kernel-iph --disable-winhttp
154 --disable-python-eggs-install"
155 # not enabled on the build server
156 CONFIG="$CONFIG --disable-af-alg"
157 if test "$TEST" != "coverage"; then
158 CONFIG="$CONFIG --disable-coverage"
159 else
160 # not actually required but configure checks for it
161 DEPS="$DEPS lcov"
162 fi
163 # Botan requires newer compilers, so disable it on Ubuntu 16.04
164 if [ "$ID" = "ubuntu" -a "$VERSION_ID" = "16.04" ]; then
165 CONFIG="$CONFIG --disable-botan"
166 fi
167 DEPS="$DEPS libcurl4-gnutls-dev libsoup2.4-dev libunbound-dev libldns-dev
168 libmysqlclient-dev libsqlite3-dev clearsilver-dev libfcgi-dev
169 libldap2-dev libpcsclite-dev libpam0g-dev binutils-dev libnm-dev
170 libgcrypt20-dev libjson-c-dev python3-pip libtspi-dev libsystemd-dev"
171 if [ "$ID" = "ubuntu" -a "$VERSION_ID" = "20.04" ]; then
172 DEPS="$DEPS libiptc-dev"
173 else
174 DEPS="$DEPS iptables-dev"
175 fi
176 PYDEPS="tox"
177 if test "$1" = "build-deps"; then
178 if [ "$ID" != "ubuntu" -o "$VERSION_ID" != "16.04" ]; then
179 build_botan
180 fi
181 build_wolfssl
182 build_tss2
183 fi
184 ;;
185 win*)
186 CONFIG="--disable-defaults --enable-svc --enable-ikev2
187 --enable-ikev1 --enable-static --enable-test-vectors --enable-nonce
188 --enable-constraints --enable-revocation --enable-pem --enable-pkcs1
189 --enable-pkcs8 --enable-x509 --enable-pubkey --enable-acert
190 --enable-eap-tnc --enable-eap-ttls --enable-eap-identity
191 --enable-updown --enable-ext-auth --enable-libipsec --enable-pkcs11
192 --enable-tnccs-20 --enable-imc-attestation --enable-imv-attestation
193 --enable-imc-os --enable-imv-os --enable-tnc-imv --enable-tnc-imc
194 --enable-pki --enable-swanctl --enable-socket-win
195 --enable-kernel-iph --enable-kernel-wfp --enable-winhttp"
196 # no make check for Windows binaries unless we run on a windows host
197 if test "$APPVEYOR" != "True"; then
198 TARGET=
199 else
200 CONFIG="$CONFIG --enable-openssl"
201 CFLAGS="$CFLAGS -I$OPENSSL_DIR/include"
202 LDFLAGS="-L$OPENSSL_DIR"
203 export LDFLAGS
204 fi
205 CFLAGS="$CFLAGS -mno-ms-bitfields"
206 DEPS="gcc-mingw-w64-base"
207 case "$TEST" in
208 win64)
209 CONFIG="--host=x86_64-w64-mingw32 $CONFIG --enable-dbghelp-backtraces"
210 DEPS="gcc-mingw-w64-x86-64 binutils-mingw-w64-x86-64 mingw-w64-x86-64-dev $DEPS"
211 CC="x86_64-w64-mingw32-gcc"
212 ;;
213 win32)
214 CONFIG="--host=i686-w64-mingw32 $CONFIG"
215 DEPS="gcc-mingw-w64-i686 binutils-mingw-w64-i686 mingw-w64-i686-dev $DEPS"
216 CC="i686-w64-mingw32-gcc"
217 ;;
218 esac
219 ;;
220 android)
221 if test "$1" = "deps"; then
222 git clone git://git.strongswan.org/android-ndk-boringssl.git -b ndk-static \
223 src/frontends/android/app/src/main/jni/openssl
224 fi
225 TARGET=distdir
226 ;;
227 macos)
228 # this causes a false positive in ip-packet.c since Xcode 8.3
229 CFLAGS="$CFLAGS -Wno-address-of-packed-member"
230 # use the same options as in the Homebrew Formula
231 CONFIG="--disable-defaults --enable-charon --enable-cmd --enable-constraints
232 --enable-curl --enable-eap-gtc --enable-eap-identity
233 --enable-eap-md5 --enable-eap-mschapv2 --enable-farp --enable-ikev1
234 --enable-ikev2 --enable-kernel-libipsec --enable-kernel-pfkey
235 --enable-kernel-pfroute --enable-nonce --enable-openssl
236 --enable-osx-attr --enable-pem --enable-pgp --enable-pkcs1
237 --enable-pkcs8 --enable-pki --enable-pubkey --enable-revocation
238 --enable-scepclient --enable-socket-default --enable-sshkey
239 --enable-stroke --enable-swanctl --enable-unity --enable-updown
240 --enable-x509 --enable-xauth-generic"
241 DEPS="automake autoconf libtool bison gettext openssl curl"
242 BREW_PREFIX=$(brew --prefix)
243 export PATH=$BREW_PREFIX/opt/bison/bin:$PATH
244 export ACLOCAL_PATH=$BREW_PREFIX/opt/gettext/share/aclocal:$ACLOCAL_PATH
245 for pkg in openssl curl
246 do
247 PKG_CONFIG_PATH=$BREW_PREFIX/opt/$pkg/lib/pkgconfig:$PKG_CONFIG_PATH
248 CPPFLAGS="-I$BREW_PREFIX/opt/$pkg/include $CPPFLAGS"
249 LDFLAGS="-L$BREW_PREFIX/opt/$pkg/lib $LDFLAGS"
250 done
251 export PKG_CONFIG_PATH
252 export CPPFLAGS
253 export LDFLAGS
254 ;;
255 freebsd)
256 # use the options of the FreeBSD port (including options), except smp,
257 # which requires a patch but is deprecated anyway, only using the builtin
258 # printf hooks
259 CONFIG="--enable-kernel-pfkey --enable-kernel-pfroute --disable-scripts
260 --disable-kernel-netlink --enable-openssl --enable-eap-identity
261 --enable-eap-md5 --enable-eap-tls --enable-eap-mschapv2
262 --enable-eap-peap --enable-eap-ttls --enable-md4 --enable-blowfish
263 --enable-addrblock --enable-whitelist --enable-cmd --enable-curl
264 --enable-eap-aka --enable-eap-aka-3gpp2 --enable-eap-dynamic
265 --enable-eap-radius --enable-eap-sim --enable-eap-sim-file
266 --enable-gcm --enable-ipseckey --enable-kernel-libipsec
267 --enable-load-tester --enable-ldap --enable-mediation
268 --enable-mysql --enable-sqlite --enable-tpm --enable-tss-tss2
269 --enable-unbound --enable-unity --enable-xauth-eap --enable-xauth-pam
270 --with-printf-hooks=builtin --enable-attr-sql --enable-sql
271 --enable-farp"
272 DEPS="git gmp openldap-client libxml2 mysql80-client sqlite3 unbound ldns tpm2-tss"
273 export GPERF=/usr/local/bin/gperf
274 export LEX=/usr/local/bin/flex
275 ;;
276 fuzzing)
277 CFLAGS="$CFLAGS -DNO_CHECK_MEMWIPE"
278 CONFIG="--enable-fuzzing --enable-static --disable-shared --disable-scripts
279 --enable-imc-test --enable-tnccs-20"
280 # don't run any of the unit tests
281 export TESTS_RUNNERS=
282 # prepare corpora
283 if test -z "$1"; then
284 if test -z "$FUZZING_CORPORA"; then
285 git clone --depth 1 https://github.com/strongswan/fuzzing-corpora.git fuzzing-corpora
286 export FUZZING_CORPORA=$BUILD_DIR/fuzzing-corpora
287 fi
288 # these are about the same as those on OSS-Fuzz (except for the
289 # symbolize options and strip_path_prefix)
290 export ASAN_OPTIONS=redzone=16:handle_sigill=1:strict_string_check=1:\
291 allocator_release_to_os_interval_ms=500:strict_memcmp=1:detect_container_overflow=1:\
292 coverage=0:allocator_may_return_null=1:use_sigaltstack=1:detect_stack_use_after_return=1:\
293 alloc_dealloc_mismatch=0:detect_leaks=1:print_scariness=1:max_uar_stack_size_log=16:\
294 handle_abort=1:check_malloc_usable_size=0:quarantine_size_mb=10:detect_odr_violation=0:\
295 symbolize=1:handle_segv=1:fast_unwind_on_fatal=0:external_symbolizer_path=/usr/bin/llvm-symbolizer-3.5
296 fi
297 ;;
298 nm|nm-no-glib)
299 DEPS="gnome-common libsecret-1-dev libgtk-3-dev libnm-dev libnma-dev"
300 if test "$TEST" = "nm"; then
301 DEPS="$DEPS libnm-glib-vpn-dev libnm-gtk-dev"
302 else
303 CONFIG="$CONFIG --without-libnm-glib"
304 fi
305 cd src/frontends/gnome
306 # don't run ./configure with ./autogen.sh
307 export NOCONFIGURE=1
308 ;;
309 dist)
310 TARGET=distcheck
311 ;;
312 apidoc)
313 DEPS="doxygen"
314 CONFIG="--disable-defaults"
315 TARGET=apidoc
316 ;;
317 lgtm)
318 DEPS="jq"
319
320 if test -z "$1"; then
321 base=$COMMIT_BASE
322 # after rebases or for new/duplicate branches, the passed base commit
323 # ID might not be valid
324 git rev-parse -q --verify $base^{commit}
325 if [ $? != 0 ]; then
326 # this will always compare against master, while via base we
327 # otherwise only contains "new" commits
328 base=$(git merge-base origin/master ${COMMIT_ID})
329 fi
330 base=$(git rev-parse $base)
331 project_id=1506185006272
332
333 echo "Starting code review for $COMMIT_ID (base $base) on lgtm.com"
334 git diff --binary $base > lgtm.patch || exit $?
335 curl -s -X POST --data-binary @lgtm.patch \
336 "https://lgtm.com/api/v1.0/codereviews/${project_id}?base=${base}&external-id=${BUILD_NUMBER}" \
337 -H 'Content-Type: application/octet-stream' \
338 -H 'Accept: application/json' \
339 -H "Authorization: Bearer ${LGTM_TOKEN}" > lgtm.res || exit $?
340 lgtm_check_url=$(jq -r '."task-result-url"' lgtm.res)
341 if [ -z "$lgtm_check_url" -o "$lgtm_check_url" = "null" ]; then
342 cat lgtm.res
343 exit 1
344 fi
345 lgtm_url=$(jq -r '."task-result"."results-url"' lgtm.res)
346 echo "Progress and full results: ${lgtm_url}"
347
348 echo -n "Waiting for completion: "
349 lgtm_status=pending
350 while [ "$lgtm_status" = "pending" ]; do
351 sleep 15
352 curl -s -X GET "${lgtm_check_url}" \
353 -H 'Accept: application/json' \
354 -H "Authorization: Bearer ${LGTM_TOKEN}" > lgtm.res
355 if [ $? != 0 ]; then
356 echo -n "-"
357 continue
358 fi
359 echo -n "."
360 lgtm_status=$(jq -r '.status' lgtm.res)
361 done
362 echo ""
363
364 if [ "$lgtm_status" != "success" ]; then
365 lgtm_message=$(jq -r '.["status-message"]' lgtm.res)
366 echo "Code review failed: ${lgtm_message}"
367 exit 1
368 fi
369 lgtm_new=$(jq -r '.languages[].new' lgtm.res | awk '{t+=$1} END {print t}')
370 lgtm_fixed=$(jq -r '.languages[].fixed' lgtm.res | awk '{t+=$1} END {print t}')
371 echo -n "Code review complete: "
372 printf "%b\n" "\e[1;31m${lgtm_new}\e[0m new alerts, \e[1;32m${lgtm_fixed}\e[0m fixed"
373 exit $lgtm_new
374 fi
375 ;;
376 *)
377 echo "$0: unknown test $TEST" >&2
378 exit 1
379 ;;
380 esac
381
382 case "$1" in
383 deps)
384 case "$OS_NAME" in
385 linux)
386 sudo apt-get update -qq && \
387 sudo apt-get install -qq bison flex gperf gettext $DEPS
388 ;;
389 macos)
390 brew update && \
391 brew install $DEPS
392 ;;
393 freebsd)
394 pkg install -y automake autoconf libtool pkgconf && \
395 pkg install -y bison flex gperf gettext $DEPS
396 ;;
397 esac
398 exit $?
399 ;;
400 pydeps)
401 test -z "$PYDEPS" || pip3 -q install --user $PYDEPS
402 exit $?
403 ;;
404 build-deps)
405 exit
406 ;;
407 *)
408 ;;
409 esac
410
411 CONFIG="$CONFIG
412 --disable-dependency-tracking
413 --enable-silent-rules
414 --enable-test-vectors
415 --enable-monolithic=${MONOLITHIC-no}
416 --enable-leak-detective=${LEAK_DETECTIVE-no}"
417
418 echo "$ ./autogen.sh"
419 ./autogen.sh || exit $?
420 echo "$ CC=$CC CFLAGS=\"$CFLAGS\" ./configure $CONFIG"
421 CC="$CC" CFLAGS="$CFLAGS" ./configure $CONFIG || exit $?
422
423 case "$TEST" in
424 apidoc)
425 exec 2>make.warnings
426 ;;
427 *)
428 ;;
429 esac
430
431 echo "$ make $TARGET"
432 case "$TEST" in
433 sonarcloud)
434 # without target, coverage is currently not supported anyway because
435 # sonarqube only supports gcov, not lcov
436 build-wrapper-linux-x86-64 --out-dir bw-output make -j4 || exit $?
437 ;;
438 *)
439 make -j4 $TARGET || exit $?
440 ;;
441 esac
442
443 case "$TEST" in
444 apidoc)
445 if test -s make.warnings; then
446 cat make.warnings
447 exit 1
448 fi
449 rm make.warnings
450 ;;
451 sonarcloud)
452 sonar-scanner \
453 -Dsonar.host.url=https://sonarcloud.io \
454 -Dsonar.projectKey=${SONAR_PROJECT} \
455 -Dsonar.organization=${SONAR_ORGANIZATION} \
456 -Dsonar.login=${SONAR_TOKEN} \
457 -Dsonar.projectVersion=$(git describe)+${BUILD_NUMBER} \
458 -Dsonar.sources=. \
459 -Dsonar.cfamily.threads=2 \
460 -Dsonar.cfamily.cache.enabled=true \
461 -Dsonar.cfamily.cache.path=$HOME/.sonar-cache \
462 -Dsonar.cfamily.build-wrapper-output=bw-output || exit $?
463 rm -r bw-output .scannerwork
464 ;;
465 android)
466 rm -r strongswan-*
467 cd src/frontends/android
468 echo "$ ./gradlew build"
469 NDK_CCACHE=ccache ./gradlew build || exit $?
470 ;;
471 *)
472 ;;
473 esac
474
475 # ensure there are no unignored build artifacts (or other changes) in the Git repo
476 unclean="$(git status --porcelain)"
477 if test -n "$unclean"; then
478 echo "Unignored build artifacts or other changes:"
479 echo "$unclean"
480 exit 1
481 fi