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