chiark / gitweb /
only test ./gendocs on GNU/Linux
[fdroidserver.git] / tests / run-tests
1 #!/bin/bash
2
3 set -e # quit script on error
4
5 echo_header() {
6     { echo -e "==============================================================================\n$1"; } 2>/dev/null
7 }
8
9 copy_apks_into_repo() {
10     set +x
11     for f in `find $APKDIR -name '*.apk' | grep -F -v -e unaligned -e unsigned -e badsig -e badcert`; do
12         name=$(basename $(dirname `dirname $f`))
13         apk=`$aapt dump badging "$f" | sed -n "s,^package: name='\(.*\)' versionCode='\([0-9][0-9]*\)' .*,\1_\2.apk,p"`
14         test $f -nt repo/$apk && rm -f repo/$apk  # delete existing if $f is newer
15         if [ ! -e repo/$apk ] && [ ! -e archive/$apk ]; then
16             echo "$f --> repo/$apk"
17             ln $f $1/repo/$apk || \
18                 rsync -axv $f $1/repo/$apk # rsync if hard link is not possible
19         fi
20     done
21     set -x
22 }
23
24 # keep this as an old version to test the automatic parsing of build-tools
25 # verion numbers in `fdroid init`
26 create_fake_android_home() {
27     mkdir $1/tools
28     mkdir $1/platform-tools
29     mkdir $1/build-tools
30     mkdir $1/build-tools/19.0.2
31     touch $1/build-tools/19.0.2/aapt
32 }
33
34 create_test_dir() {
35     test -e $WORKSPACE/.testfiles || mkdir $WORKSPACE/.testfiles
36     TMPDIR=$WORKSPACE/.testfiles  mktemp -d
37 }
38
39 create_test_file() {
40     test -e $WORKSPACE/.testfiles || mkdir $WORKSPACE/.testfiles
41     TMPDIR=$WORKSPACE/.testfiles  mktemp
42 }
43
44 #------------------------------------------------------------------------------#
45 # "main"
46
47 if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
48     set +x
49     echo "Usage: $0 '/path/to/folder/with/apks'"
50     exit 1
51 fi
52
53 if [ -z "$ANDROID_HOME" ]; then
54     echo "ANDROID_HOME must be set with the path to the Android SDK, i.e.: "
55     echo "  export ANDROID_HOME=/opt/android-sdk"
56     exit 1
57 fi
58
59 if [ -d tests ]; then
60     cd tests
61 fi
62
63 if [ -z "$1" ]; then
64     APKDIR=`pwd`
65 else
66     APKDIR=$1
67 fi
68
69 if [ -z $WORKSPACE ]; then
70     WORKSPACE=`dirname $(pwd)`
71     echo "Setting Workspace to $WORKSPACE"
72 fi
73
74 # allow the location of the script to be overridden
75 if [ -z $fdroid ]; then
76     fdroid="$WORKSPACE/fdroid"
77 fi
78
79 # allow the location of aapt to be overridden
80 if [ -z $aapt ]; then
81     aapt=`ls -1 $ANDROID_HOME/build-tools/*/aapt | sort | tail -1`
82 fi
83
84 # allow the location of python to be overridden
85 if [ -z $python ]; then
86     python=python2
87 fi
88
89 set -x # show each command as it is executed
90
91 #------------------------------------------------------------------------------#
92 echo_header "run commit hooks"
93
94 cd $WORKSPACE
95 ./hooks/pre-commit
96
97
98 #------------------------------------------------------------------------------#
99 echo_header "test python getsig replacement"
100
101 cd $WORKSPACE/tests/getsig
102 ./make.sh
103 for testcase in $WORKSPACE/tests/*.TestCase; do
104     $testcase
105 done
106
107
108 #------------------------------------------------------------------------------#
109 echo_header "print fdroid version"
110
111 $fdroid --version
112
113
114 #------------------------------------------------------------------------------#
115 echo_header "build the TeX manual"
116
117 cd $WORKSPACE/docs
118 # this is only ever generated officially on GNU/Linux
119 if [ `uname -s` == "Linux" ]; then
120     ./gendocs.sh -o html --email admin@f-droid.org fdroid "F-Droid Server Manual"
121 fi
122
123
124 #------------------------------------------------------------------------------#
125 echo_header "test metadata checks"
126
127 REPOROOT=`create_test_dir`
128 cd $REPOROOT
129
130 touch config.py
131 mkdir repo
132 cp $WORKSPACE/tests/urzip.apk $REPOROOT/repo/
133
134 set +e
135 $fdroid build
136 if [ $? -eq 0 ]; then
137     echo "This should have failed because there is no metadata!"
138     exit 1
139 else
140     echo "testing metadata checks passed"
141 fi
142 set -e
143
144 mkdir $REPOROOT/metadata/
145 cp $WORKSPACE/tests/metadata/org.smssecure.smssecure.txt $REPOROOT/metadata/
146 $fdroid readmeta
147
148 # now make a fake duplicate
149 touch $REPOROOT/metadata/org.smssecure.smssecure.yaml
150
151 set +e
152 $fdroid readmeta
153 if [ $? -eq 0 ]; then
154     echo "This should have failed because there is a duplicate metadata file!"
155     exit 1
156 else
157     echo "testing duplicate metadata checks passed"
158 fi
159 set -e
160
161
162 #------------------------------------------------------------------------------#
163 echo_header "ensure commands that don't need the JDK work without a JDK configed"
164
165 REPOROOT=`create_test_dir`
166 cd $REPOROOT
167 mkdir repo
168 mkdir metadata
169 echo "License:GPL" >> metadata/fake.txt
170 echo "Summary:Yup still fake" >> metadata/fake.txt
171 echo "Categories:Internet" >> metadata/fake.txt
172 echo "Description:" >> metadata/fake.txt
173 echo "this is fake" >> metadata/fake.txt
174 echo "." >> metadata/fake.txt
175
176 # fake that no JDKs are available
177 echo 'java_paths = {}' > config.py
178
179 LOCAL_COPY_DIR=`create_test_dir`/fdroid
180 mkdir -p $LOCAL_COPY_DIR/repo
181 echo "local_copy_dir = '$LOCAL_COPY_DIR'" >> config.py
182
183 $fdroid checkupdates
184 $fdroid gpgsign
185 $fdroid lint
186 $fdroid readmeta
187 $fdroid rewritemeta fake
188 $fdroid server update
189 $fdroid scanner
190
191 # run these to get their output, but the are not setup, so don't fail
192 $fdroid build || true
193 $fdroid import || true
194 $fdroid install || true
195
196
197 #------------------------------------------------------------------------------#
198 echo_header "create a source tarball and use that to build a repo"
199
200 cd $WORKSPACE
201 $python setup.py sdist
202
203 REPOROOT=`create_test_dir`
204 cd $REPOROOT
205 tar xzf `ls -1 $WORKSPACE/dist/fdroidserver-*.tar.gz | sort -n | tail -1`
206 cd $REPOROOT
207 ./fdroidserver-*/fdroid init
208 copy_apks_into_repo $REPOROOT
209 ./fdroidserver-*/fdroid update --create-metadata --verbose
210
211
212 #------------------------------------------------------------------------------#
213 echo_header "test config checks of local_copy_dir"
214
215 REPOROOT=`create_test_dir`
216 cd $REPOROOT
217 $fdroid init
218 $fdroid update --create-metadata --verbose
219 $fdroid readmeta
220 $fdroid server update --local-copy-dir=/tmp/fdroid
221
222 # now test the errors work
223 set +e
224 $fdroid server update --local-copy-dir=thisisnotanabsolutepath
225 if [ $? -eq 0 ]; then
226     echo "This should have failed because thisisnotanabsolutepath is not an absolute path!"
227     exit 1
228 else
229     echo "testing absolute path checker passed"
230 fi
231 $fdroid server update --local-copy-dir=/tmp/IReallyDoubtThisPathExistsasdfasdf
232 if [ $? -eq 0 ]; then
233     echo "This should have failed because the path does not end with 'fdroid'!"
234     exit 1
235 else
236     echo "testing dirname exists checker passed"
237 fi
238 $fdroid server update --local-copy-dir=/tmp/IReallyDoubtThisPathExistsasdfasdf/fdroid
239 if [ $? -eq 0 ]; then
240     echo "This should have failed because the dirname path does not exist!"
241     exit 1
242 else
243     echo "testing dirname exists checker passed"
244 fi
245 set -e
246
247
248 #------------------------------------------------------------------------------#
249 echo_header "setup a new repo from scratch using ANDROID_HOME and do a local sync"
250
251 REPOROOT=`create_test_dir`
252 cd $REPOROOT
253 $fdroid init
254 copy_apks_into_repo $REPOROOT
255 $fdroid update --create-metadata --verbose
256 $fdroid readmeta
257 grep -F '<application id=' repo/index.xml > /dev/null
258
259 LOCALCOPYDIR=`create_test_dir`/fdroid
260 $fdroid server update --local-copy-dir=$LOCALCOPYDIR
261 NEWREPOROOT=`create_test_dir`
262 cd $NEWREPOROOT
263 $fdroid init
264 $fdroid server update --local-copy-dir=$LOCALCOPYDIR --sync-from-local-copy-dir
265
266
267 #------------------------------------------------------------------------------#
268 # check that --android-home fails when dir does not exist or is not a dir
269
270 REPOROOT=`create_test_dir`
271 KEYSTORE=$REPOROOT/keystore.jks
272 cd $REPOROOT
273 set +e
274 $fdroid init --keystore $KEYSTORE --android-home /opt/fakeandroidhome
275 if [ $? -eq 0 ]; then
276     echo "This should have failed because /opt/fakeandroidhome does not exist!"
277     exit 1
278 else
279     echo "testing android-home path checker passed"
280 fi
281 TESTFILE=`create_test_file`
282 $fdroid init --keystore $KEYSTORE --android-home $TESTFILE
283 if [ $? -eq 0 ]; then
284     echo "This should have failed because $TESTFILE is a file not a dir!"
285     exit 1
286 else
287     echo "testing android-home not-dir checker passed"
288 fi
289 set -e
290
291
292 #------------------------------------------------------------------------------#
293 echo_header "check that fake android home passes 'fdroid init'"
294
295 REPOROOT=`create_test_dir`
296 FAKE_ANDROID_HOME=`create_test_dir`
297 create_fake_android_home $FAKE_ANDROID_HOME
298 KEYSTORE=$REPOROOT/keystore.jks
299 cd $REPOROOT
300 $fdroid init --keystore $KEYSTORE --android-home $FAKE_ANDROID_HOME
301
302
303 #------------------------------------------------------------------------------#
304 echo_header "check that 'fdroid init' fails when build-tools cannot be found"
305
306 if [ -e /usr/bin/aapt ]; then
307     echo "/usr/bin/aapt exists, not running test"
308 else
309     REPOROOT=`create_test_dir`
310     FAKE_ANDROID_HOME=`create_test_dir`
311     create_fake_android_home $FAKE_ANDROID_HOME
312     rm -f $FAKE_ANDROID_HOME/build-tools/*/aapt
313     KEYSTORE=$REPOROOT/keystore.jks
314     cd $REPOROOT
315     set +e
316     $fdroid init --keystore $KEYSTORE --android-home $FAKE_ANDROID_HOME
317     [ $? -eq 0 ] && exit 1
318     set -e
319 fi
320
321
322 #------------------------------------------------------------------------------#
323 echo_header "check that --android-home overrides ANDROID_HOME"
324
325 REPOROOT=`create_test_dir`
326 FAKE_ANDROID_HOME=`create_test_dir`
327 create_fake_android_home $FAKE_ANDROID_HOME
328 KEYSTORE=$REPOROOT/keystore.jks
329 cd $REPOROOT
330 $fdroid init --keystore $KEYSTORE --android-home $FAKE_ANDROID_HOME
331 set +e
332 grep $FAKE_ANDROID_HOME $REPOROOT/config.py
333 if [ $? -ne 0 ]; then
334     echo "the value set in --android-home '$FAKE_ANDROID_HOME' should override ANDROID_HOME '$ANDROID_HOME'"
335     exit 1
336 fi
337 set -e
338
339
340 #------------------------------------------------------------------------------#
341 # In this case, ANDROID_HOME is set to a fake, non-working version that will
342 # be detected by fdroid as an Android SDK install.  It should use the path set
343 # by --android-home over the one in ANDROID_HOME, therefore if it uses the one
344 # in ANDROID_HOME, it won't work because it is a fake one.  Only
345 # --android-home provides a working one.
346 echo_header "setup a new repo from scratch with keystore and android-home set on cmd line"
347
348 REPOROOT=`create_test_dir`
349 KEYSTORE=$REPOROOT/keystore.jks
350 FAKE_ANDROID_HOME=`create_test_dir`
351 create_fake_android_home $FAKE_ANDROID_HOME
352 STORED_ANDROID_HOME=$ANDROID_HOME
353 unset ANDROID_HOME
354 echo "ANDROID_HOME: $ANDROID_HOME"
355 cd $REPOROOT
356 $fdroid init --keystore $KEYSTORE --android-home $STORED_ANDROID_HOME --no-prompt
357 test -e $KEYSTORE
358 copy_apks_into_repo $REPOROOT
359 $fdroid update --create-metadata --verbose
360 $fdroid readmeta
361 grep -F '<application id=' repo/index.xml > /dev/null
362 test -e repo/index.xml
363 test -e repo/index.jar
364 export ANDROID_HOME=$STORED_ANDROID_HOME
365
366
367 #------------------------------------------------------------------------------#
368 echo_header "setup new repo from scratch using ANDROID_HOME, putting APKs in repo first"
369
370 REPOROOT=`create_test_dir`
371 cd $REPOROOT
372 mkdir repo
373 copy_apks_into_repo $REPOROOT
374 $fdroid init
375 $fdroid update --create-metadata --verbose
376 $fdroid readmeta
377 grep -F '<application id=' repo/index.xml > /dev/null
378
379
380 #------------------------------------------------------------------------------#
381 echo_header "setup a new repo from scratch and generate a keystore"
382
383 REPOROOT=`create_test_dir`
384 KEYSTORE=$REPOROOT/keystore.jks
385 cd $REPOROOT
386 $fdroid init --keystore $KEYSTORE
387 test -e $KEYSTORE
388 copy_apks_into_repo $REPOROOT
389 $fdroid update --create-metadata --verbose
390 $fdroid readmeta
391 test -e repo/index.xml
392 test -e repo/index.jar
393 grep -F '<application id=' repo/index.xml > /dev/null
394
395
396 #------------------------------------------------------------------------------#
397 echo_header "setup a new repo manually and generate a keystore"
398
399 REPOROOT=`create_test_dir`
400 KEYSTORE=$REPOROOT/keystore.jks
401 cd $REPOROOT
402 touch config.py
403 cp $WORKSPACE/examples/fdroid-icon.png $REPOROOT/
404 ! test -e $KEYSTORE
405 set +e
406 $fdroid update
407 if [ $? -eq 0 ]; then
408     echo "This should have failed because this repo has no keystore!"
409     exit 1
410 else
411     echo '`fdroid update` prompted to add keystore'
412 fi
413 set -e
414 $fdroid update --create-key
415 test -e $KEYSTORE
416 copy_apks_into_repo $REPOROOT
417 $fdroid update --create-metadata --verbose
418 $fdroid readmeta
419 test -e repo/index.xml
420 test -e repo/index.jar
421 grep -F '<application id=' repo/index.xml > /dev/null
422
423
424 #------------------------------------------------------------------------------#
425 echo_header "setup a new repo from scratch, generate a keystore, then add APK and update"
426
427 REPOROOT=`create_test_dir`
428 KEYSTORE=$REPOROOT/keystore.jks
429 cd $REPOROOT
430 $fdroid init --keystore $KEYSTORE
431 test -e $KEYSTORE
432 copy_apks_into_repo $REPOROOT
433 $fdroid update --create-metadata --verbose
434 $fdroid readmeta
435 test -e repo/index.xml
436 test -e repo/index.jar
437 grep -F '<application id=' repo/index.xml > /dev/null
438 test -e $REPOROOT/repo/info.guardianproject.urzip_100.apk || \
439     cp $WORKSPACE/tests/urzip.apk $REPOROOT/repo/
440 $fdroid update --create-metadata --verbose
441 $fdroid readmeta
442 test -e repo/index.xml
443 test -e repo/index.jar
444 grep -F '<application id=' repo/index.xml > /dev/null
445
446
447 #------------------------------------------------------------------------------#
448 echo_header "setup a new repo from scratch with a HSM/smartcard"
449 REPOROOT=`create_test_dir`
450 cd $REPOROOT
451 $fdroid init --keystore NONE
452 test -e opensc-fdroid.cfg
453 test ! -e NONE
454
455
456 #------------------------------------------------------------------------------#
457 echo_header "setup a new repo with no keystore, add APK, and update"
458
459 REPOROOT=`create_test_dir`
460 KEYSTORE=$REPOROOT/keystore.jks
461 cd $REPOROOT
462 touch config.py
463 touch fdroid-icon.png
464 mkdir repo
465 cp $WORKSPACE/tests/urzip.apk $REPOROOT/repo/
466 set +e
467 $fdroid update --create-metadata --verbose
468 if [ $? -eq 0 ]; then
469     echo "This should have failed because this repo has no keystore!"
470     exit 1
471 else
472     echo '`fdroid update` prompted to add keystore'
473 fi
474 set -e
475
476 # now set up fake, non-working keystore setup
477 touch $KEYSTORE
478 echo "keystore = \"$KEYSTORE\"" >> config.py
479 echo 'repo_keyalias = "foo"' >> config.py
480 echo 'keystorepass = "foo"' >> config.py
481 echo 'keypass = "foo"' >> config.py
482 set +e
483 $fdroid update --create-metadata --verbose
484 if [ $? -eq 0 ]; then
485     echo "This should have failed because this repo has a bad/fake keystore!"
486     exit 1
487 else
488     echo '`fdroid update` prompted to add keystore'
489 fi
490 set -e
491
492
493 #------------------------------------------------------------------------------#
494 echo_header "setup a new repo with keystore with APK, update, then without key"
495
496 REPOROOT=`create_test_dir`
497 KEYSTORE=$REPOROOT/keystore.jks
498 cd $REPOROOT
499 $fdroid init --keystore $KEYSTORE
500 test -e $KEYSTORE
501 cp $WORKSPACE/tests/urzip.apk $REPOROOT/repo/
502 $fdroid update --create-metadata --verbose
503 $fdroid readmeta
504 test -e repo/index.xml
505 test -e repo/index.jar
506 grep -F '<application id=' repo/index.xml > /dev/null
507
508 # now set fake repo_keyalias
509 sed -i 's,^ *repo_keyalias.*,repo_keyalias = "fake",' $REPOROOT/config.py
510 set +e
511 $fdroid update
512 if [ $? -eq 0 ]; then
513     echo "This should have failed because this repo has a bad repo_keyalias!"
514     exit 1
515 else
516     echo '`fdroid update` prompted to add keystore'
517 fi
518 set -e
519
520 # try creating a new keystore, but fail because the old one is there
521 test -e $KEYSTORE
522 set +e
523 $fdroid update --create-key
524 if [ $? -eq 0 ]; then
525     echo "This should have failed because a keystore is already there!"
526     exit 1
527 else
528     echo '`fdroid update` complained about existing keystore'
529 fi
530 set -e
531
532 # now actually create the key with the existing settings
533 rm -f $KEYSTORE
534 ! test -e $KEYSTORE
535 $fdroid update --create-key
536 test -e $KEYSTORE
537
538
539 #------------------------------------------------------------------------------#
540
541 # remove this to prevent git conflicts and complaining
542 rm -rf $WORKSPACE/fdroidserver.egg-info/
543
544 echo SUCCESS