chiark / gitweb /
Merge branch 'master' into 'master'
[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 ./gendocs.sh -o html --email admin@f-droid.org fdroid "F-Droid Server Manual"
119
120
121 #------------------------------------------------------------------------------#
122 echo_header "test metadata checks"
123
124 REPOROOT=`create_test_dir`
125 cd $REPOROOT
126
127 touch config.py
128 mkdir repo
129 cp $WORKSPACE/tests/urzip.apk $REPOROOT/repo/
130
131 set +e
132 $fdroid build
133 if [ $? -eq 0 ]; then
134     echo "This should have failed because there is no metadata!"
135     exit 1
136 else
137     echo "testing metadata checks passed"
138 fi
139 set -e
140
141 mkdir $REPOROOT/metadata/
142 cp $WORKSPACE/tests/metadata/org.smssecure.smssecure.txt $REPOROOT/metadata/
143 $fdroid readmeta
144
145 # now make a fake duplicate
146 touch $REPOROOT/metadata/org.smssecure.smssecure.yaml
147
148 set +e
149 $fdroid readmeta
150 if [ $? -eq 0 ]; then
151     echo "This should have failed because there is a duplicate metadata file!"
152     exit 1
153 else
154     echo "testing duplicate metadata checks passed"
155 fi
156 set -e
157
158
159 #------------------------------------------------------------------------------#
160 echo_header "ensure commands that don't need the JDK work without a JDK configed"
161
162 REPOROOT=`create_test_dir`
163 cd $REPOROOT
164 mkdir repo
165 mkdir metadata
166 echo "License:GPL" >> metadata/fake.txt
167 echo "Summary:Yup still fake" >> metadata/fake.txt
168 echo "Categories:Internet" >> metadata/fake.txt
169 echo "Description:" >> metadata/fake.txt
170 echo "this is fake" >> metadata/fake.txt
171 echo "." >> metadata/fake.txt
172
173 # fake that no JDKs are available
174 echo 'java_paths = {}' > config.py
175
176 LOCAL_COPY_DIR=`create_test_dir`/fdroid
177 mkdir -p $LOCAL_COPY_DIR/repo
178 echo "local_copy_dir = '$LOCAL_COPY_DIR'" >> config.py
179
180 $fdroid checkupdates
181 $fdroid gpgsign
182 $fdroid lint
183 $fdroid readmeta
184 $fdroid rewritemeta fake
185 $fdroid server update
186 $fdroid scanner
187
188 # run these to get their output, but the are not setup, so don't fail
189 $fdroid build || true
190 $fdroid import || true
191 $fdroid install || true
192
193
194 #------------------------------------------------------------------------------#
195 echo_header "create a source tarball and use that to build a repo"
196
197 cd $WORKSPACE
198 $python setup.py sdist
199
200 REPOROOT=`create_test_dir`
201 cd $REPOROOT
202 tar xzf `ls -1 $WORKSPACE/dist/fdroidserver-*.tar.gz | sort -n | tail -1`
203 cd $REPOROOT
204 ./fdroidserver-*/fdroid init
205 copy_apks_into_repo $REPOROOT
206 ./fdroidserver-*/fdroid update --create-metadata --verbose
207
208
209 #------------------------------------------------------------------------------#
210 echo_header "test config checks of local_copy_dir"
211
212 REPOROOT=`create_test_dir`
213 cd $REPOROOT
214 $fdroid init
215 $fdroid update --create-metadata --verbose
216 $fdroid readmeta
217 $fdroid server update --local-copy-dir=/tmp/fdroid
218
219 # now test the errors work
220 set +e
221 $fdroid server update --local-copy-dir=thisisnotanabsolutepath
222 if [ $? -eq 0 ]; then
223     echo "This should have failed because thisisnotanabsolutepath is not an absolute path!"
224     exit 1
225 else
226     echo "testing absolute path checker passed"
227 fi
228 $fdroid server update --local-copy-dir=/tmp/IReallyDoubtThisPathExistsasdfasdf
229 if [ $? -eq 0 ]; then
230     echo "This should have failed because the path does not end with 'fdroid'!"
231     exit 1
232 else
233     echo "testing dirname exists checker passed"
234 fi
235 $fdroid server update --local-copy-dir=/tmp/IReallyDoubtThisPathExistsasdfasdf/fdroid
236 if [ $? -eq 0 ]; then
237     echo "This should have failed because the dirname path does not exist!"
238     exit 1
239 else
240     echo "testing dirname exists checker passed"
241 fi
242 set -e
243
244
245 #------------------------------------------------------------------------------#
246 echo_header "setup a new repo from scratch using ANDROID_HOME and do a local sync"
247
248 REPOROOT=`create_test_dir`
249 cd $REPOROOT
250 $fdroid init
251 copy_apks_into_repo $REPOROOT
252 $fdroid update --create-metadata --verbose
253 $fdroid readmeta
254 grep -F '<application id=' repo/index.xml > /dev/null
255
256 LOCALCOPYDIR=`create_test_dir`/fdroid
257 $fdroid server update --local-copy-dir=$LOCALCOPYDIR
258 NEWREPOROOT=`create_test_dir`
259 cd $NEWREPOROOT
260 $fdroid init
261 $fdroid server update --local-copy-dir=$LOCALCOPYDIR --sync-from-local-copy-dir
262
263
264 #------------------------------------------------------------------------------#
265 # check that --android-home fails when dir does not exist or is not a dir
266
267 REPOROOT=`create_test_dir`
268 KEYSTORE=$REPOROOT/keystore.jks
269 cd $REPOROOT
270 set +e
271 $fdroid init --keystore $KEYSTORE --android-home /opt/fakeandroidhome
272 if [ $? -eq 0 ]; then
273     echo "This should have failed because /opt/fakeandroidhome does not exist!"
274     exit 1
275 else
276     echo "testing android-home path checker passed"
277 fi
278 TESTFILE=`create_test_file`
279 $fdroid init --keystore $KEYSTORE --android-home $TESTFILE
280 if [ $? -eq 0 ]; then
281     echo "This should have failed because $TESTFILE is a file not a dir!"
282     exit 1
283 else
284     echo "testing android-home not-dir checker passed"
285 fi
286 set -e
287
288
289 #------------------------------------------------------------------------------#
290 echo_header "check that fake android home passes 'fdroid init'"
291
292 REPOROOT=`create_test_dir`
293 FAKE_ANDROID_HOME=`create_test_dir`
294 create_fake_android_home $FAKE_ANDROID_HOME
295 KEYSTORE=$REPOROOT/keystore.jks
296 cd $REPOROOT
297 $fdroid init --keystore $KEYSTORE --android-home $FAKE_ANDROID_HOME
298
299
300 #------------------------------------------------------------------------------#
301 echo_header "check that 'fdroid init' fails when build-tools cannot be found"
302
303 if [ -e /usr/bin/aapt ]; then
304     echo "/usr/bin/aapt exists, not running test"
305 else
306     REPOROOT=`create_test_dir`
307     FAKE_ANDROID_HOME=`create_test_dir`
308     create_fake_android_home $FAKE_ANDROID_HOME
309     rm -f $FAKE_ANDROID_HOME/build-tools/*/aapt
310     KEYSTORE=$REPOROOT/keystore.jks
311     cd $REPOROOT
312     set +e
313     $fdroid init --keystore $KEYSTORE --android-home $FAKE_ANDROID_HOME
314     [ $? -eq 0 ] && exit 1
315     set -e
316 fi
317
318
319 #------------------------------------------------------------------------------#
320 echo_header "check that --android-home overrides ANDROID_HOME"
321
322 REPOROOT=`create_test_dir`
323 FAKE_ANDROID_HOME=`create_test_dir`
324 create_fake_android_home $FAKE_ANDROID_HOME
325 KEYSTORE=$REPOROOT/keystore.jks
326 cd $REPOROOT
327 $fdroid init --keystore $KEYSTORE --android-home $FAKE_ANDROID_HOME
328 set +e
329 grep $FAKE_ANDROID_HOME $REPOROOT/config.py
330 if [ $? -ne 0 ]; then
331     echo "the value set in --android-home '$FAKE_ANDROID_HOME' should override ANDROID_HOME '$ANDROID_HOME'"
332     exit 1
333 fi
334 set -e
335
336
337 #------------------------------------------------------------------------------#
338 # In this case, ANDROID_HOME is set to a fake, non-working version that will
339 # be detected by fdroid as an Android SDK install.  It should use the path set
340 # by --android-home over the one in ANDROID_HOME, therefore if it uses the one
341 # in ANDROID_HOME, it won't work because it is a fake one.  Only
342 # --android-home provides a working one.
343 echo_header "setup a new repo from scratch with keystore and android-home set on cmd line"
344
345 REPOROOT=`create_test_dir`
346 KEYSTORE=$REPOROOT/keystore.jks
347 FAKE_ANDROID_HOME=`create_test_dir`
348 create_fake_android_home $FAKE_ANDROID_HOME
349 STORED_ANDROID_HOME=$ANDROID_HOME
350 unset ANDROID_HOME
351 echo "ANDROID_HOME: $ANDROID_HOME"
352 cd $REPOROOT
353 $fdroid init --keystore $KEYSTORE --android-home $STORED_ANDROID_HOME --no-prompt
354 test -e $KEYSTORE
355 copy_apks_into_repo $REPOROOT
356 $fdroid update --create-metadata --verbose
357 $fdroid readmeta
358 grep -F '<application id=' repo/index.xml > /dev/null
359 test -e repo/index.xml
360 test -e repo/index.jar
361 export ANDROID_HOME=$STORED_ANDROID_HOME
362
363
364 #------------------------------------------------------------------------------#
365 echo_header "setup new repo from scratch using ANDROID_HOME, putting APKs in repo first"
366
367 REPOROOT=`create_test_dir`
368 cd $REPOROOT
369 mkdir repo
370 copy_apks_into_repo $REPOROOT
371 $fdroid init
372 $fdroid update --create-metadata --verbose
373 $fdroid readmeta
374 grep -F '<application id=' repo/index.xml > /dev/null
375
376
377 #------------------------------------------------------------------------------#
378 echo_header "setup a new repo from scratch and generate a keystore"
379
380 REPOROOT=`create_test_dir`
381 KEYSTORE=$REPOROOT/keystore.jks
382 cd $REPOROOT
383 $fdroid init --keystore $KEYSTORE
384 test -e $KEYSTORE
385 copy_apks_into_repo $REPOROOT
386 $fdroid update --create-metadata --verbose
387 $fdroid readmeta
388 test -e repo/index.xml
389 test -e repo/index.jar
390 grep -F '<application id=' repo/index.xml > /dev/null
391
392
393 #------------------------------------------------------------------------------#
394 echo_header "setup a new repo manually and generate a keystore"
395
396 REPOROOT=`create_test_dir`
397 KEYSTORE=$REPOROOT/keystore.jks
398 cd $REPOROOT
399 touch config.py
400 cp $WORKSPACE/examples/fdroid-icon.png $REPOROOT/
401 ! test -e $KEYSTORE
402 set +e
403 $fdroid update
404 if [ $? -eq 0 ]; then
405     echo "This should have failed because this repo has no keystore!"
406     exit 1
407 else
408     echo '`fdroid update` prompted to add keystore'
409 fi
410 set -e
411 $fdroid update --create-key
412 test -e $KEYSTORE
413 copy_apks_into_repo $REPOROOT
414 $fdroid update --create-metadata --verbose
415 $fdroid readmeta
416 test -e repo/index.xml
417 test -e repo/index.jar
418 grep -F '<application id=' repo/index.xml > /dev/null
419
420
421 #------------------------------------------------------------------------------#
422 echo_header "setup a new repo from scratch, generate a keystore, then add APK and update"
423
424 REPOROOT=`create_test_dir`
425 KEYSTORE=$REPOROOT/keystore.jks
426 cd $REPOROOT
427 $fdroid init --keystore $KEYSTORE
428 test -e $KEYSTORE
429 copy_apks_into_repo $REPOROOT
430 $fdroid update --create-metadata --verbose
431 $fdroid readmeta
432 test -e repo/index.xml
433 test -e repo/index.jar
434 grep -F '<application id=' repo/index.xml > /dev/null
435 test -e $REPOROOT/repo/info.guardianproject.urzip_100.apk || \
436     cp $WORKSPACE/tests/urzip.apk $REPOROOT/repo/
437 $fdroid update --create-metadata --verbose
438 $fdroid readmeta
439 test -e repo/index.xml
440 test -e repo/index.jar
441 grep -F '<application id=' repo/index.xml > /dev/null
442
443
444 #------------------------------------------------------------------------------#
445 echo_header "setup a new repo from scratch with a HSM/smartcard"
446 REPOROOT=`create_test_dir`
447 cd $REPOROOT
448 $fdroid init --keystore NONE
449 test -e opensc-fdroid.cfg
450 test ! -e NONE
451
452
453 #------------------------------------------------------------------------------#
454 echo_header "setup a new repo with no keystore, add APK, and update"
455
456 REPOROOT=`create_test_dir`
457 KEYSTORE=$REPOROOT/keystore.jks
458 cd $REPOROOT
459 touch config.py
460 touch fdroid-icon.png
461 mkdir repo
462 cp $WORKSPACE/tests/urzip.apk $REPOROOT/repo/
463 set +e
464 $fdroid update --create-metadata --verbose
465 if [ $? -eq 0 ]; then
466     echo "This should have failed because this repo has no keystore!"
467     exit 1
468 else
469     echo '`fdroid update` prompted to add keystore'
470 fi
471 set -e
472
473 # now set up fake, non-working keystore setup
474 touch $KEYSTORE
475 echo "keystore = \"$KEYSTORE\"" >> config.py
476 echo 'repo_keyalias = "foo"' >> config.py
477 echo 'keystorepass = "foo"' >> config.py
478 echo 'keypass = "foo"' >> config.py
479 set +e
480 $fdroid update --create-metadata --verbose
481 if [ $? -eq 0 ]; then
482     echo "This should have failed because this repo has a bad/fake keystore!"
483     exit 1
484 else
485     echo '`fdroid update` prompted to add keystore'
486 fi
487 set -e
488
489
490 #------------------------------------------------------------------------------#
491 echo_header "setup a new repo with keystore with APK, update, then without key"
492
493 REPOROOT=`create_test_dir`
494 KEYSTORE=$REPOROOT/keystore.jks
495 cd $REPOROOT
496 $fdroid init --keystore $KEYSTORE
497 test -e $KEYSTORE
498 cp $WORKSPACE/tests/urzip.apk $REPOROOT/repo/
499 $fdroid update --create-metadata --verbose
500 $fdroid readmeta
501 test -e repo/index.xml
502 test -e repo/index.jar
503 grep -F '<application id=' repo/index.xml > /dev/null
504
505 # now set fake repo_keyalias
506 sed -i 's,^ *repo_keyalias.*,repo_keyalias = "fake",' $REPOROOT/config.py
507 set +e
508 $fdroid update
509 if [ $? -eq 0 ]; then
510     echo "This should have failed because this repo has a bad repo_keyalias!"
511     exit 1
512 else
513     echo '`fdroid update` prompted to add keystore'
514 fi
515 set -e
516
517 # try creating a new keystore, but fail because the old one is there
518 test -e $KEYSTORE
519 set +e
520 $fdroid update --create-key
521 if [ $? -eq 0 ]; then
522     echo "This should have failed because a keystore is already there!"
523     exit 1
524 else
525     echo '`fdroid update` complained about existing keystore'
526 fi
527 set -e
528
529 # now actually create the key with the existing settings
530 rm -f $KEYSTORE
531 ! test -e $KEYSTORE
532 $fdroid update --create-key
533 test -e $KEYSTORE
534
535
536 #------------------------------------------------------------------------------#
537
538 # remove this to prevent git conflicts and complaining
539 rm -rf $WORKSPACE/fdroidserver.egg-info/
540
541 echo SUCCESS