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