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