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