chiark / gitweb /
use 'python2' everywhere since fdroidserver has not been tested with 3.x
[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`; 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/build-tools
30     mkdir $1/build-tools/19.0.2
31     touch $1/build-tools/19.0.2/aapt
32 }
33
34 create_test_dir() {
35     test -e $WORKSPACE/.testfiles || mkdir $WORKSPACE/.testfiles
36     mktemp --directory --tmpdir=$WORKSPACE/.testfiles
37 }
38
39 create_test_file() {
40     test -e $WORKSPACE/.testfiles || mkdir $WORKSPACE/.testfiles
41     mktemp --tmpdir=$WORKSPACE/.testfiles
42 }
43
44 #------------------------------------------------------------------------------#
45 # "main"
46
47 if [ $1 = "-h" ] || [ $1 = "--help" ]; then
48     set +x
49     echo "Usage: $0 '/path/to/folder/with/apks'"
50     exit 1
51 fi
52
53 if [ -z $ANDROID_HOME ]; then
54     echo "ANDROID_HOME must be set with the path to the Android SDK, i.e.: "
55     echo "  export ANDROID_HOME=/opt/android-sdk"
56     exit 1
57 fi
58
59 if [ -z $1 ]; then
60     APKDIR=`pwd`
61 else
62     APKDIR=$1
63 fi
64
65 if [ -z $WORKSPACE ]; then
66     WORKSPACE=`dirname $(pwd)`
67     echo "Setting Workspace to $WORKSPACE"
68 fi
69
70 # allow the location of the script to be overridden
71 if [ -z $fdroid ]; then
72     fdroid="$WORKSPACE/fdroid"
73 fi
74
75 # allow the location of aapt to be overridden
76 if [ -z $aapt ]; then
77     aapt=`ls -1 $ANDROID_HOME/build-tools/*/aapt | sort | tail -1`
78 fi
79
80 # allow the location of python to be overridden
81 if [ -z $python ]; then
82     python=python2
83 fi
84
85
86 #------------------------------------------------------------------------------#
87 echo_header "create a source tarball and use that to build a repo"
88
89 cd $WORKSPACE
90 $python setup.py sdist
91
92 REPOROOT=`create_test_dir`
93 cd $REPOROOT
94 tar xzf `ls -1 $WORKSPACE/dist/fdroidserver-*.tar.gz | sort -n | tail -1`
95 cd $REPOROOT/fdroidserver-*/fdroidserver/getsig
96 ./make.sh
97 cd $REPOROOT
98 ./fdroidserver-*/fdroid init
99 copy_apks_into_repo $REPOROOT
100 ./fdroidserver-*/fdroid update --create-metadata
101
102
103 #------------------------------------------------------------------------------#
104 echo_header "test config checks of local_copy_dir"
105
106 REPOROOT=`create_test_dir`
107 cd $REPOROOT
108 $fdroid init
109 $fdroid update --create-metadata
110 $fdroid server update --local-copy-dir=/tmp/fdroid
111
112 # now test the errors work
113 set +e
114 $fdroid server update --local-copy-dir=thisisnotanabsolutepath
115 if [ $? -eq 0 ]; then
116     echo "This should have failed because thisisnotanabsolutepath is not an absolute path!"
117     exit 1
118 else
119     echo "testing absolute path checker passed"
120 fi
121 $fdroid server update --local-copy-dir=/tmp/IReallyDoubtThisPathExistsasdfasdf
122 if [ $? -eq 0 ]; then
123     echo "This should have failed because the path does not end with 'fdroid'!"
124     exit 1
125 else
126     echo "testing dirname exists checker passed"
127 fi
128 $fdroid server update --local-copy-dir=/tmp/IReallyDoubtThisPathExistsasdfasdf/fdroid
129 if [ $? -eq 0 ]; then
130     echo "This should have failed because the dirname path does not exist!"
131     exit 1
132 else
133     echo "testing dirname exists checker passed"
134 fi
135 set -e
136
137
138 #------------------------------------------------------------------------------#
139 echo_header "setup a new repo from scratch using ANDROID_HOME and do a local sync"
140
141 REPOROOT=`create_test_dir`
142 cd $REPOROOT
143 $fdroid init
144 copy_apks_into_repo $REPOROOT
145 $fdroid update --create-metadata
146 grep -F '<application id=' repo/index.xml
147
148 LOCALCOPYDIR=`create_test_dir`/fdroid
149 $fdroid server update --local-copy-dir=$LOCALCOPYDIR
150 NEWREPOROOT=`create_test_dir`
151 cd $NEWREPOROOT
152 $fdroid init
153 $fdroid server update --local-copy-dir=$LOCALCOPYDIR --sync-from-local-copy-dir
154
155
156 #------------------------------------------------------------------------------#
157 # check that --android-home fails when dir does not exist or is not a dir
158
159 REPOROOT=`create_test_dir`
160 KEYSTORE=$REPOROOT/keystore.jks
161 cd $REPOROOT
162 set +e
163 $fdroid init --keystore $KEYSTORE --android-home /opt/fakeandroidhome
164 if [ $? -eq 0 ]; then
165     echo "This should have failed because /opt/fakeandroidhome does not exist!"
166     exit 1
167 else
168     echo "testing android-home path checker passed"
169 fi
170 TESTFILE=`create_test_file`
171 $fdroid init --keystore $KEYSTORE --android-home $TESTFILE
172 if [ $? -eq 0 ]; then
173     echo "This should have failed because $TESTFILE is a file not a dir!"
174     exit 1
175 else
176     echo "testing android-home not-dir checker passed"
177 fi
178 set -e
179
180
181 #------------------------------------------------------------------------------#
182 echo_header "check that fake android home passes `fdroid init`"
183
184 REPOROOT=`create_test_dir`
185 FAKE_ANDROID_HOME=`create_test_dir`
186 create_fake_android_home $FAKE_ANDROID_HOME
187 KEYSTORE=$REPOROOT/keystore.jks
188 cd $REPOROOT
189 $fdroid init --keystore $KEYSTORE --android-home $FAKE_ANDROID_HOME
190
191
192 #------------------------------------------------------------------------------#
193 echo_header "check that 'fdroid init' fails when build-tools cannot be found"
194
195 REPOROOT=`create_test_dir`
196 FAKE_ANDROID_HOME=`create_test_dir`
197 create_fake_android_home $FAKE_ANDROID_HOME
198 rm -f $FAKE_ANDROID_HOME/build-tools/*/aapt
199 KEYSTORE=$REPOROOT/keystore.jks
200 cd $REPOROOT
201 set +e
202 $fdroid init --keystore $KEYSTORE --android-home $FAKE_ANDROID_HOME
203 [ $? -eq 0 ] && exit 1
204 set -e
205
206
207 #------------------------------------------------------------------------------#
208 echo_header "check that --android-home overrides ANDROID_HOME"
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 set +e
217 grep $FAKE_ANDROID_HOME $REPOROOT/config.py
218 if [ $? -ne 0 ]; then
219     echo "the value set in --android-home '$FAKE_ANDROID_HOME' should override ANDROID_HOME '$ANDROID_HOME'"
220     exit 1
221 fi
222 set -e
223
224
225 #------------------------------------------------------------------------------#
226 # In this case, ANDROID_HOME is set to a fake, non-working version that will
227 # be detected by fdroid as an Android SDK install.  It should use the path set
228 # by --android-home over the one in ANDROID_HOME, therefore if it uses the one
229 # in ANDROID_HOME, it won't work because it is a fake one.  Only
230 # --android-home provides a working one.
231 echo_header "setup a new repo from scratch with keystore and android-home set on cmd line"
232
233 REPOROOT=`create_test_dir`
234 KEYSTORE=$REPOROOT/keystore.jks
235 FAKE_ANDROID_HOME=`create_test_dir`
236 create_fake_android_home $FAKE_ANDROID_HOME
237 STORED_ANDROID_HOME=$ANDROID_HOME
238 unset ANDROID_HOME
239 echo "ANDROID_HOME: $ANDROID_HOME"
240 cd $REPOROOT
241 $fdroid init --keystore $KEYSTORE --android-home $STORED_ANDROID_HOME --no-prompt
242 test -e $KEYSTORE
243 copy_apks_into_repo $REPOROOT
244 $fdroid update --create-metadata
245 grep -F '<application id=' repo/index.xml
246 test -e repo/index.xml
247 test -e repo/index.jar
248 export ANDROID_HOME=$STORED_ANDROID_HOME
249
250
251 #------------------------------------------------------------------------------#
252 echo_header "setup new repo from scratch using ANDROID_HOME, putting APKs in repo first"
253
254 REPOROOT=`create_test_dir`
255 cd $REPOROOT
256 mkdir repo
257 copy_apks_into_repo $REPOROOT
258 $fdroid init
259 $fdroid update --create-metadata
260 grep -F '<application id=' repo/index.xml
261
262
263 #------------------------------------------------------------------------------#
264 echo_header "setup a new repo from scratch and generate a keystore"
265
266 REPOROOT=`create_test_dir`
267 KEYSTORE=$REPOROOT/keystore.jks
268 cd $REPOROOT
269 $fdroid init --keystore $KEYSTORE
270 test -e $KEYSTORE
271 copy_apks_into_repo $REPOROOT
272 $fdroid update --create-metadata
273 test -e repo/index.xml
274 test -e repo/index.jar
275 grep -F '<application id=' repo/index.xml
276
277
278 #------------------------------------------------------------------------------#
279 echo_header "setup a new repo from scratch, generate a keystore, then add APK and update"
280
281 REPOROOT=`create_test_dir`
282 KEYSTORE=$REPOROOT/keystore.jks
283 cd $REPOROOT
284 $fdroid init --keystore $KEYSTORE
285 test -e $KEYSTORE
286 copy_apks_into_repo $REPOROOT
287 $fdroid update --create-metadata
288 test -e repo/index.xml
289 test -e repo/index.jar
290 grep -F '<application id=' repo/index.xml
291 cp $WORKSPACE/tests/urzip.apk $REPOROOT/
292 $fdroid update --create-metadata
293 test -e repo/index.xml
294 test -e repo/index.jar
295 grep -F '<application id=' repo/index.xml
296
297
298 #------------------------------------------------------------------------------#
299 echo_header "setup a new repo from scratch with a HSM/smartcard"
300
301 REPOROOT=`create_test_dir`
302 cd $REPOROOT
303 $fdroid init --keystore NONE
304 test -e opensc-fdroid.cfg
305 test ! -e NONE
306
307
308 echo SUCCESS