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