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