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