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