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