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