chiark / gitweb /
tests: allow running ./tests/run-tests
[fdroidserver.git] / tests / run-tests
1 #!/bin/bash
2
3 set -e # quit script on error
4 set -x # show each command as it is executed
5
6 echo_header() {
7     echo "=============================================================================="
8     echo $1
9 }
10
11 copy_apks_into_repo() {
12     set +x
13     for f in `find $APKDIR -name '*.apk' | grep -F -v -e unaligned -e unsigned -e badsig -e badcert`; do
14         name=$(basename $(dirname `dirname $f`))
15         apk=`$aapt dump badging "$f" | sed -n "s,^package: name='\(.*\)' versionCode='\([0-9][0-9]*\)' .*,\1_\2.apk,p"`
16         test $f -nt repo/$apk && rm -f repo/$apk  # delete existing if $f is newer
17         if [ ! -e repo/$apk ] && [ ! -e archive/$apk ]; then
18             echo "$f --> repo/$apk"
19             ln $f $1/repo/$apk || \
20                 rsync -axv $f $1/repo/$apk # rsync if hard link is not possible
21         fi
22     done
23     set -x
24 }
25
26 # keep this as an old version to test the automatic parsing of build-tools
27 # verion numbers in `fdroid init`
28 create_fake_android_home() {
29     mkdir $1/tools
30     mkdir $1/platform-tools
31     mkdir $1/build-tools
32     mkdir $1/build-tools/19.0.2
33     touch $1/build-tools/19.0.2/aapt
34 }
35
36 create_test_dir() {
37     test -e $WORKSPACE/.testfiles || mkdir $WORKSPACE/.testfiles
38     TMPDIR=$WORKSPACE/.testfiles  mktemp -d
39 }
40
41 create_test_file() {
42     test -e $WORKSPACE/.testfiles || mkdir $WORKSPACE/.testfiles
43     TMPDIR=$WORKSPACE/.testfiles  mktemp
44 }
45
46 #------------------------------------------------------------------------------#
47 # "main"
48
49 if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
50     set +x
51     echo "Usage: $0 '/path/to/folder/with/apks'"
52     exit 1
53 fi
54
55 if [ -z "$ANDROID_HOME" ]; then
56     echo "ANDROID_HOME must be set with the path to the Android SDK, i.e.: "
57     echo "  export ANDROID_HOME=/opt/android-sdk"
58     exit 1
59 fi
60
61 if [ -d tests ]; then
62     cd tests
63 fi
64
65 if [ -z "$1" ]; then
66     APKDIR=`pwd`
67 else
68     APKDIR=$1
69 fi
70
71 if [ -z $WORKSPACE ]; then
72     WORKSPACE=`dirname $(pwd)`
73     echo "Setting Workspace to $WORKSPACE"
74 fi
75
76 # allow the location of the script to be overridden
77 if [ -z $fdroid ]; then
78     fdroid="$WORKSPACE/fdroid"
79 fi
80
81 # allow the location of aapt to be overridden
82 if [ -z $aapt ]; then
83     aapt=`ls -1 $ANDROID_HOME/build-tools/*/aapt | sort | tail -1`
84 fi
85
86 # allow the location of python to be overridden
87 if [ -z $python ]; then
88     python=python2
89 fi
90
91
92 #------------------------------------------------------------------------------#
93 echo_header "run commit hooks"
94
95 cd $WORKSPACE
96 ./hooks/pre-commit
97
98
99 #------------------------------------------------------------------------------#
100 echo_header "test python getsig replacement"
101
102 cd $WORKSPACE/tests/getsig
103 ./make.sh
104 for testcase in $WORKSPACE/tests/*.TestCase; do
105     $testcase
106 done
107
108
109 #------------------------------------------------------------------------------#
110 echo_header "print fdroid version"
111
112 $fdroid --version
113
114
115 #------------------------------------------------------------------------------#
116 echo_header "build the TeX manual"
117
118 cd $WORKSPACE/docs
119 ./gendocs.sh -o html --email admin@f-droid.org fdroid "F-Droid Server Manual"
120
121
122 #------------------------------------------------------------------------------#
123 echo_header "test metadata checks"
124
125 REPOROOT=`create_test_dir`
126 cd $REPOROOT
127
128 touch config.py
129 mkdir repo
130 cp $WORKSPACE/tests/urzip.apk $REPOROOT/repo/
131
132 set +e
133 $fdroid build
134 if [ $? -eq 0 ]; then
135     echo "This should have failed because there is no metadata!"
136     exit 1
137 else
138     echo "testing metadata checks passed"
139 fi
140 set -e
141
142 mkdir $REPOROOT/metadata/
143 cp $WORKSPACE/tests/metadata/org.smssecure.smssecure.txt $REPOROOT/metadata/
144 $fdroid readmeta
145
146 # now make a fake duplicate
147 touch $REPOROOT/metadata/org.smssecure.smssecure.yaml
148
149 set +e
150 $fdroid readmeta
151 if [ $? -eq 0 ]; then
152     echo "This should have failed because there is a duplicate metadata file!"
153     exit 1
154 else
155     echo "testing duplicate metadata checks passed"
156 fi
157 set -e
158
159
160 #------------------------------------------------------------------------------#
161 echo_header "create a source tarball and use that to build a repo"
162
163 cd $WORKSPACE
164 $python setup.py sdist
165
166 REPOROOT=`create_test_dir`
167 cd $REPOROOT
168 tar xzf `ls -1 $WORKSPACE/dist/fdroidserver-*.tar.gz | sort -n | tail -1`
169 cd $REPOROOT
170 ./fdroidserver-*/fdroid init
171 copy_apks_into_repo $REPOROOT
172 ./fdroidserver-*/fdroid update --create-metadata
173
174
175 #------------------------------------------------------------------------------#
176 echo_header "test config checks of local_copy_dir"
177
178 REPOROOT=`create_test_dir`
179 cd $REPOROOT
180 $fdroid init
181 $fdroid update --create-metadata
182 $fdroid readmeta
183 $fdroid server update --local-copy-dir=/tmp/fdroid
184
185 # now test the errors work
186 set +e
187 $fdroid server update --local-copy-dir=thisisnotanabsolutepath
188 if [ $? -eq 0 ]; then
189     echo "This should have failed because thisisnotanabsolutepath is not an absolute path!"
190     exit 1
191 else
192     echo "testing absolute path checker passed"
193 fi
194 $fdroid server update --local-copy-dir=/tmp/IReallyDoubtThisPathExistsasdfasdf
195 if [ $? -eq 0 ]; then
196     echo "This should have failed because the path does not end with 'fdroid'!"
197     exit 1
198 else
199     echo "testing dirname exists checker passed"
200 fi
201 $fdroid server update --local-copy-dir=/tmp/IReallyDoubtThisPathExistsasdfasdf/fdroid
202 if [ $? -eq 0 ]; then
203     echo "This should have failed because the dirname path does not exist!"
204     exit 1
205 else
206     echo "testing dirname exists checker passed"
207 fi
208 set -e
209
210
211 #------------------------------------------------------------------------------#
212 echo_header "setup a new repo from scratch using ANDROID_HOME and do a local sync"
213
214 REPOROOT=`create_test_dir`
215 cd $REPOROOT
216 $fdroid init
217 copy_apks_into_repo $REPOROOT
218 $fdroid update --create-metadata
219 $fdroid readmeta
220 grep -F '<application id=' repo/index.xml > /dev/null
221
222 LOCALCOPYDIR=`create_test_dir`/fdroid
223 $fdroid server update --local-copy-dir=$LOCALCOPYDIR
224 NEWREPOROOT=`create_test_dir`
225 cd $NEWREPOROOT
226 $fdroid init
227 $fdroid server update --local-copy-dir=$LOCALCOPYDIR --sync-from-local-copy-dir
228
229
230 #------------------------------------------------------------------------------#
231 # check that --android-home fails when dir does not exist or is not a dir
232
233 REPOROOT=`create_test_dir`
234 KEYSTORE=$REPOROOT/keystore.jks
235 cd $REPOROOT
236 set +e
237 $fdroid init --keystore $KEYSTORE --android-home /opt/fakeandroidhome
238 if [ $? -eq 0 ]; then
239     echo "This should have failed because /opt/fakeandroidhome does not exist!"
240     exit 1
241 else
242     echo "testing android-home path checker passed"
243 fi
244 TESTFILE=`create_test_file`
245 $fdroid init --keystore $KEYSTORE --android-home $TESTFILE
246 if [ $? -eq 0 ]; then
247     echo "This should have failed because $TESTFILE is a file not a dir!"
248     exit 1
249 else
250     echo "testing android-home not-dir checker passed"
251 fi
252 set -e
253
254
255 #------------------------------------------------------------------------------#
256 echo_header "check that fake android home passes 'fdroid init'"
257
258 REPOROOT=`create_test_dir`
259 FAKE_ANDROID_HOME=`create_test_dir`
260 create_fake_android_home $FAKE_ANDROID_HOME
261 KEYSTORE=$REPOROOT/keystore.jks
262 cd $REPOROOT
263 $fdroid init --keystore $KEYSTORE --android-home $FAKE_ANDROID_HOME
264
265
266 #------------------------------------------------------------------------------#
267 echo_header "check that 'fdroid init' fails when build-tools cannot be found"
268
269 if [ -e /usr/bin/aapt ]; then
270     echo "/usr/bin/aapt exists, not running test"
271 else
272     REPOROOT=`create_test_dir`
273     FAKE_ANDROID_HOME=`create_test_dir`
274     create_fake_android_home $FAKE_ANDROID_HOME
275     rm -f $FAKE_ANDROID_HOME/build-tools/*/aapt
276     KEYSTORE=$REPOROOT/keystore.jks
277     cd $REPOROOT
278     set +e
279     $fdroid init --keystore $KEYSTORE --android-home $FAKE_ANDROID_HOME
280     [ $? -eq 0 ] && exit 1
281     set -e
282 fi
283
284
285 #------------------------------------------------------------------------------#
286 echo_header "check that --android-home overrides ANDROID_HOME"
287
288 REPOROOT=`create_test_dir`
289 FAKE_ANDROID_HOME=`create_test_dir`
290 create_fake_android_home $FAKE_ANDROID_HOME
291 KEYSTORE=$REPOROOT/keystore.jks
292 cd $REPOROOT
293 $fdroid init --keystore $KEYSTORE --android-home $FAKE_ANDROID_HOME
294 set +e
295 grep $FAKE_ANDROID_HOME $REPOROOT/config.py
296 if [ $? -ne 0 ]; then
297     echo "the value set in --android-home '$FAKE_ANDROID_HOME' should override ANDROID_HOME '$ANDROID_HOME'"
298     exit 1
299 fi
300 set -e
301
302
303 #------------------------------------------------------------------------------#
304 # In this case, ANDROID_HOME is set to a fake, non-working version that will
305 # be detected by fdroid as an Android SDK install.  It should use the path set
306 # by --android-home over the one in ANDROID_HOME, therefore if it uses the one
307 # in ANDROID_HOME, it won't work because it is a fake one.  Only
308 # --android-home provides a working one.
309 echo_header "setup a new repo from scratch with keystore and android-home set on cmd line"
310
311 REPOROOT=`create_test_dir`
312 KEYSTORE=$REPOROOT/keystore.jks
313 FAKE_ANDROID_HOME=`create_test_dir`
314 create_fake_android_home $FAKE_ANDROID_HOME
315 STORED_ANDROID_HOME=$ANDROID_HOME
316 unset ANDROID_HOME
317 echo "ANDROID_HOME: $ANDROID_HOME"
318 cd $REPOROOT
319 $fdroid init --keystore $KEYSTORE --android-home $STORED_ANDROID_HOME --no-prompt
320 test -e $KEYSTORE
321 copy_apks_into_repo $REPOROOT
322 $fdroid update --create-metadata
323 $fdroid readmeta
324 grep -F '<application id=' repo/index.xml > /dev/null
325 test -e repo/index.xml
326 test -e repo/index.jar
327 export ANDROID_HOME=$STORED_ANDROID_HOME
328
329
330 #------------------------------------------------------------------------------#
331 echo_header "setup new repo from scratch using ANDROID_HOME, putting APKs in repo first"
332
333 REPOROOT=`create_test_dir`
334 cd $REPOROOT
335 mkdir repo
336 copy_apks_into_repo $REPOROOT
337 $fdroid init
338 $fdroid update --create-metadata
339 $fdroid readmeta
340 grep -F '<application id=' repo/index.xml > /dev/null
341
342
343 #------------------------------------------------------------------------------#
344 echo_header "setup a new repo from scratch and generate a keystore"
345
346 REPOROOT=`create_test_dir`
347 KEYSTORE=$REPOROOT/keystore.jks
348 cd $REPOROOT
349 $fdroid init --keystore $KEYSTORE
350 test -e $KEYSTORE
351 copy_apks_into_repo $REPOROOT
352 $fdroid update --create-metadata
353 $fdroid readmeta
354 test -e repo/index.xml
355 test -e repo/index.jar
356 grep -F '<application id=' repo/index.xml > /dev/null
357
358
359 #------------------------------------------------------------------------------#
360 echo_header "setup a new repo manually and generate a keystore"
361
362 REPOROOT=`create_test_dir`
363 KEYSTORE=$REPOROOT/keystore.jks
364 cd $REPOROOT
365 touch config.py
366 cp $WORKSPACE/examples/fdroid-icon.png $REPOROOT/
367 ! test -e $KEYSTORE
368 set +e
369 $fdroid update
370 if [ $? -eq 0 ]; then
371     echo "This should have failed because this repo has no keystore!"
372     exit 1
373 else
374     echo '`fdroid update` prompted to add keystore'
375 fi
376 set -e
377 $fdroid update --create-key
378 test -e $KEYSTORE
379 copy_apks_into_repo $REPOROOT
380 $fdroid update --create-metadata
381 $fdroid readmeta
382 test -e repo/index.xml
383 test -e repo/index.jar
384 grep -F '<application id=' repo/index.xml > /dev/null
385
386
387 #------------------------------------------------------------------------------#
388 echo_header "setup a new repo from scratch, generate a keystore, then add APK and update"
389
390 REPOROOT=`create_test_dir`
391 KEYSTORE=$REPOROOT/keystore.jks
392 cd $REPOROOT
393 $fdroid init --keystore $KEYSTORE
394 test -e $KEYSTORE
395 copy_apks_into_repo $REPOROOT
396 $fdroid update --create-metadata
397 $fdroid readmeta
398 test -e repo/index.xml
399 test -e repo/index.jar
400 grep -F '<application id=' repo/index.xml > /dev/null
401 test -e $REPOROOT/repo/info.guardianproject.urzip_100.apk || \
402     cp $WORKSPACE/tests/urzip.apk $REPOROOT/repo/
403 $fdroid update --create-metadata
404 $fdroid readmeta
405 test -e repo/index.xml
406 test -e repo/index.jar
407 grep -F '<application id=' repo/index.xml > /dev/null
408
409
410 #------------------------------------------------------------------------------#
411 echo_header "setup a new repo from scratch with a HSM/smartcard"
412
413 REPOROOT=`create_test_dir`
414 cd $REPOROOT
415 $fdroid init --keystore NONE
416 test -e opensc-fdroid.cfg
417 test ! -e NONE
418
419
420 #------------------------------------------------------------------------------#
421 echo_header "setup a new repo with no keystore, add APK, and update"
422
423 REPOROOT=`create_test_dir`
424 KEYSTORE=$REPOROOT/keystore.jks
425 cd $REPOROOT
426 touch config.py
427 touch fdroid-icon.png
428 mkdir repo
429 cp $WORKSPACE/tests/urzip.apk $REPOROOT/repo/
430 set +e
431 $fdroid update --create-metadata
432 if [ $? -eq 0 ]; then
433     echo "This should have failed because this repo has no keystore!"
434     exit 1
435 else
436     echo '`fdroid update` prompted to add keystore'
437 fi
438 set -e
439
440 # now set up fake, non-working keystore setup
441 touch $KEYSTORE
442 echo "keystore = \"$KEYSTORE\"" >> config.py
443 echo 'repo_keyalias = "foo"' >> config.py
444 echo 'keystorepass = "foo"' >> config.py
445 echo 'keypass = "foo"' >> config.py
446 set +e
447 $fdroid update --create-metadata
448 if [ $? -eq 0 ]; then
449     echo "This should have failed because this repo has a bad/fake keystore!"
450     exit 1
451 else
452     echo '`fdroid update` prompted to add keystore'
453 fi
454 set -e
455
456
457 #------------------------------------------------------------------------------#
458 echo_header "setup a new repo with keystore with APK, update, then without key"
459
460 REPOROOT=`create_test_dir`
461 KEYSTORE=$REPOROOT/keystore.jks
462 cd $REPOROOT
463 $fdroid init --keystore $KEYSTORE
464 test -e $KEYSTORE
465 cp $WORKSPACE/tests/urzip.apk $REPOROOT/repo/
466 $fdroid update --create-metadata
467 $fdroid readmeta
468 test -e repo/index.xml
469 test -e repo/index.jar
470 grep -F '<application id=' repo/index.xml > /dev/null
471
472 # now set fake repo_keyalias
473 sed -i 's,^ *repo_keyalias.*,repo_keyalias = "fake",' $REPOROOT/config.py
474 set +e
475 $fdroid update
476 if [ $? -eq 0 ]; then
477     echo "This should have failed because this repo has a bad repo_keyalias!"
478     exit 1
479 else
480     echo '`fdroid update` prompted to add keystore'
481 fi
482 set -e
483
484 # try creating a new keystore, but fail because the old one is there
485 test -e $KEYSTORE
486 set +e
487 $fdroid update --create-key
488 if [ $? -eq 0 ]; then
489     echo "This should have failed because a keystore is already there!"
490     exit 1
491 else
492     echo '`fdroid update` complained about existing keystore'
493 fi
494 set -e
495
496 # now actually create the key with the existing settings
497 rm -f $KEYSTORE
498 ! test -e $KEYSTORE
499 $fdroid update --create-key
500 test -e $KEYSTORE
501
502
503 #------------------------------------------------------------------------------#
504
505 # remove this to prevent git conflicts and complaining
506 rm -rf $WORKSPACE/fdroidserver.egg-info/
507
508 echo SUCCESS