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