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