-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.sh
executable file
·412 lines (343 loc) · 14 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/bin/bash
if [ $# -ne 5 ]; then
echo "Usage: run.sh <opt> <explore_mode> <apk_path> <sdk_api> <abi_arch>"
echo " opt: @TODO: description"
echo " * prepare @TODO: description"
echo " * ripper @TODO: description"
echo " * createSnapshot @TODO: description"
echo " * log @TODO: description"
echo " * logf @TODO: description"
echo " * tasklist @TODO: description"
echo " * shutdown @TODO: description"
echo " explore_mode: @TODO: description"
echo " * random @TODO: description"
echo " * systematic @TODO: description"
echo " sdk_api: @TODO: description"
echo " * 10 @TODO: description"
echo " * 11 @TODO: description"
echo " * 12 @TODO: description"
echo " * ... @TODO: description"
echo " abi_arch: @TODO: description"
echo " * x86 @TODO: description"
echo " * armeabi-v7a @TODO: description"
echo ""
echo "Example: run.sh prepare systematic apks/tomdroid-0.7.5.apk 10 armeabi-v7a"
fi
# prepare | ripper | createSnapshot | log | logf | tasklist | shutdown
OPT=$1
# random | systematic
EXPLORE=$2
#APKPATH=apks/tomdroid-0.7.5.apk
APKPATH=$3
# API
API=${4:-16}
# ARCH
ARCH=${5:-x86}
ARM_ON=0
EMU_ARCH="x86"
if [[ ${ARCH} == *"arm"* ]]; then
ARM_ON=1
EMU_ARCH="arm"
fi
#System Path
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
JAVA_CMD=/usr/bin/java
JARSIGNER_CMD=$(which jarsigner)
ANDROID_HOME=${ANDROID_HOME_SDK}
EMULATORPATH=${ANDROID_HOME}/tools
PLATFORMPATH=${ANDROID_HOME}/platform-tools
BUILDTOOLS=${ANDROID_HOME}/build-tools/${ANDROID_BUILD_TOOLS_VERSION}
TOOLDIR=${DIR}
#AVD Path
EMULATORCMD=emulator64-${EMU_ARCH}
EMULATORTASK=${EMULATORPATH}/${EMULATORCMD}
# AVD_NAME="gui-ripper_${ARCH}"
AVD_NAME="android-${API}_${ARCH}"
ANDROIDCONF=/root/.android
SNAPSHOTPATH=$ANDROIDCONF/avd/${AVD_NAME}.avd/snapshots.img
# App Information
# i.e = APPPACKAGE=org.tomdroid
APPPACKAGE=`aapt dump badging $APKPATH | grep package | awk -F"'" '{print $2}'`
# i.e = CLASSPACKAGE=org.tomdroid.ui.Tomdroid
CLASSPACKAGE=`aapt dump badging $APKPATH | grep launchable-activity | awk -F"'" '{print $2}'`
DEVICEPATH=/data/data/$APPPACKAGE/files
echo "Testing APK $APKPATH package=$APPPACKAGE class=$CLASSPACKAGE"
#Output Experiment Path
EXPPATH=$TOOLDIR/output-exp
COVERAGEPATH=$EXPPATH/coverage
FILESPATH=$EXPPATH/files
RESTOREPATH=$EXPPATH/restore
SCREENSHOTSPATH=$EXPPATH/screenshots
#Tool Information
ROOT_DIR=$TOOLDIR
BATCHPATH=$ROOT_DIR/batch
DATAPATH=$ROOT_DIR/data
TOOLSPATH=$ROOT_DIR/tools
SMALIPATH=$TOOLSPATH/smali
TESTPACKAGE=it.unina.androidripper
RIPPERPATH=/data/data/$TESTPACKAGE/files
if [ "${EXPLORE}" == "random" ]; then
TESTCLASS=guitree.NomadEngine
SPLIT_CODE=1
else
TESTCLASS=guitree.GuiTreeEngine
SPLIT_CODE=0
fi
# ifeq ($EXPLORE,random)
# TESTCLASS=guitree.NomadEngine
# else
# TESTCLASS=guitree.GuiTreeEngine
# endif
## Set the number of events to a high number, and then invoke this script with a timeout of 1 hour.
NUM_EVENTS=50000000
################################################################################
################################################################################
################################################################################
################################################################################
case ${OPT} in
################################################################################
prepare)
echo "prepare: Running firstBoot..."
# AVD:
# * x86: ERROR WITH android-10 and x86
# echo no | android create avd -n ${AVD_NAME} -t android-${API} -c 1024M -b x86 --snapshot
# * armeabi-v7a:
# echo no | android create avd -f -n ${AVD_NAME} -t android-${API} --abi default/armeabi-v7a --sdcard 1024M
# problems with "|". eval split in two diferent comands
# AVD_CMD="android create avd -n ${AVD_NAME} -t android-${API} --sdcard 1024M --abi default/${ARCH} --snapshot &"
# echo -n " * AVD = echo no | ${AVD_CMD} : "
# echo no | eval ${AVD_CMD}
if [[ ${ARCH} == *"arm"* ]]; then
echo " * AVD = \"echo no | android create avd -n ${AVD_NAME} -t android-${API} --sdcard 1024M --abi default/${ARCH} --snapshot &\""
echo no | android create avd -n ${AVD_NAME} -t android-${API} --sdcard 1024M --abi default/${ARCH} --snapshot &
wait $! # waiting for avd
[[ $? -ne 0 ]] && echo "ERROR" && exit 1 || echo "OK"
else
echo -n " - Copying emulator template '${AVD_NAME}'... "
cp -r ${ANDROTEST_AVD_TEMPLATES_DOCKER_PATH}/${AVD_NAME}.* ${ANDROID_AVD_HOME}
dir1_ls=$(ls ${ANDROTEST_AVD_TEMPLATES_DOCKER_PATH} | grep ${AVD_NAME})
dir1=$(ls ${ANDROTEST_AVD_TEMPLATES_DOCKER_PATH} | grep ${AVD_NAME} | wc -l)
dir1_ls=$(ls ${ANDROID_AVD_HOME} | grep ${AVD_NAME})
dir2=$(ls ${ANDROID_AVD_HOME} | grep ${AVD_NAME} | wc -l)
if [[ ${dir1} -ne ${dir2} ]] ; then
echo "ERROR: dir1: ${dir1} vs dir2: ${dir2}";
echo ${dir1_ls}
ls ${ANDROTEST_AVD_TEMPLATES_DOCKER_PATH} | grep ${AVD_NAME};
echo " dir2 (ANDROID_AVD_HOME): ";
echo ${dir2_ls}
exit 1;
else
echo "OK";
fi
fi
EMU_OPTS=""
[[ ${DEBUG} -eq 1 ]] && EMU_OPTS="${EMU_OPTS} -debug-all"
[[ ${DEBUG} -eq 1 ]] && EMU_OPTS="${EMU_OPTS} -verbose"
[[ ${SHOWEMU} -eq 0 ]] && EMU_OPTS="${EMU_OPTS} -no-window"
EMU_OPTS="${EMU_OPTS} -avd ${AVD_NAME}"
EMU_OPTS="${EMU_OPTS} -partition-size 512"
EMU_OPTS="${EMU_OPTS} -snapshot ${SNAPSHOTPATH}"
####
EMU_OPTS="${EMU_OPTS} -no-snapshot-load"
####
EMU_OPTS="${EMU_OPTS} -wipe-data"
[[ ${SHOWEMU} -eq 0 ]] && EMU_OPTS="${EMU_OPTS} -no-window"
[[ ${ARM_ON} -eq 1 ]] && EMU_OPTS="${EMU_OPTS} -gpu off -qemu"
OTHER_OPTS=""
EMU_LOG_FILE="emu_${API}_${ARCH}_AS${AVD_SCRATCH}_${EMULATORCMD}_${OPT}.log"
EMU_LOG_PATH="${EMU_LOG_DIR}${EMU_LOG_FILE}"
[[ ${DEBUG} -eq 1 ]] && OTHER_OPTS="${OTHER_OPTS} &> ${EMU_LOG_PATH}" || OTHER_OPTS="${OTHER_OPTS} > /dev/null"
[[ ${FREEZE} -eq 0 ]] && OTHER_OPTS="${OTHER_OPTS} &"
EMU_CMD="${EMULATORTASK} ${EMU_OPTS} ${OTHER_OPTS}"
echo " * EMU = ${EMU_CMD}"
eval ${EMU_CMD}
${ROOT_DIR}/waitForEmu.sh
# ;;
#
################################################################################
# installerAPK)
echo Running Installer APK
rm -rf $EXPPATH/*
mkdir -p $COVERAGEPATH
mkdir -p $RESTOREPATH
mkdir -p $FILESPATH
mkdir -p $SCREENSHOTSPATH
rm -rf $TOOLSPATH/diet
rm -rf $DATAPATH/*.apk
rm -rf $DATAPATH/preferences.xml
rm -rf $SMALIPATH/build/*
echo "Installing the packages on the virtual device..."
$PLATFORMPATH/adb kill-server
sleep 5
$PLATFORMPATH/adb devices
sleep 5
$JAVA_CMD -jar $TOOLSPATH/Retarget.jar $SMALIPATH/AndroidManifest.xml $APPPACKAGE
$JAVA_CMD -jar $TOOLSPATH/apktool.jar b $TOOLSPATH/smali $DATAPATH/crawler.apk
$JARSIGNER_CMD -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore $ANDROIDCONF/debug.keystore -storepass android -keypass android $DATAPATH/crawler.apk androiddebugkey
$BUILDTOOLS/zipalign 4 $DATAPATH/crawler.apk $DATAPATH/ripper.apk
mkdir $DATAPATH/temp
cp $APKPATH $DATAPATH/APP.apk
unzip $DATAPATH/APP.apk -d $DATAPATH/temp
rm -rf $DATAPATH/temp/META-INF
rm $DATAPATH/APP.apk
cd $DATAPATH/temp; zip -r $DATAPATH/APP.apk *
cd $DATAPATH
rm -rf $DATAPATH/temp
$JARSIGNER_CMD -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore $ANDROIDCONF/debug.keystore -storepass android -keypass android $DATAPATH/APP.apk androiddebugkey
$BUILDTOOLS/zipalign 4 $DATAPATH/APP.apk $DATAPATH/AUT.apk
$PLATFORMPATH/adb install -r $DATAPATH/AUT.apk
$PLATFORMPATH/adb install -r $DATAPATH/ripper.apk > $EXPPATH/building.txt
$PLATFORMPATH/adb shell mkdir $DEVICEPATH
$PLATFORMPATH/adb shell chmod 777 $DEVICEPATH
$PLATFORMPATH/adb shell mkdir $RIPPERPATH
$PLATFORMPATH/adb shell chmod 777 $RIPPERPATH
cp $DATAPATH/preferences_$EXPLORE.xml $DATAPATH/preferences.xml
$JAVA_CMD -jar $TOOLSPATH/PreferenceEditor.jar $DATAPATH/preferences.xml retarget $APPPACKAGE $CLASSPACKAGE
$PLATFORMPATH/adb push $DATAPATH/preferences.xml $RIPPERPATH/preferences.xml
sleep 5
$JAVA_CMD -jar $TOOLSPATH/BuildControl.jar $EXPPATH/building.txt
rm -rf $DATAPATH/*.apk
echo "Deploy Completed"
sleep 15
echo "Preparation successful. Shutting down emulator"
adb emu kill
# sleep 5
# # $PLATFORMPATH/adb kill-server
# sleep 5
;;
################################################################################
createSnapshot)
${EMULATORTASK} -avd ${AVD_NAME} -partition-size 512 -snapshot $SNAPSHOTPATH &
$ROOT_DIR/waitForEmu.sh
$PLATFORMPATH/adb shell chmod 777 $RIPPERPATH
$PLATFORMPATH/adb shell rm $RIPPERPATH/*
;;
################################################################################
ripper)
echo Running Ripper
extCounter=1
#echo $extCounter
echo "** Start Random Ripping"
while true; do
$PLATFORMPATH/adb start-server
###
### EMU
###
EMU_OPTS=""
[[ ${DEBUG} -eq 1 ]] && EMU_OPTS="${EMU_OPTS} -debug-all"
[[ ${DEBUG} -eq 1 ]] && EMU_OPTS="${EMU_OPTS} -verbose"
[[ ${SHOWEMU} -eq 0 ]] && EMU_OPTS="${EMU_OPTS} -no-window"
EMU_OPTS="${EMU_OPTS} -avd ${AVD_NAME}"
EMU_OPTS="${EMU_OPTS} -partition-size 512"
EMU_OPTS="${EMU_OPTS} -snapshot ${SNAPSHOTPATH}"
####
EMU_OPTS="${EMU_OPTS} -no-snapshot-save"
####
[[ ${SHOWEMU} -eq 0 ]] && EMU_OPTS="${EMU_OPTS} -no-window"
[[ ${ARM_ON} -eq 1 ]] && EMU_OPTS="${EMU_OPTS} -gpu off -qemu"
OTHER_OPTS=""
EMU_LOG_DIR="${DIR}/"
EMU_LOG_FILE="emu_${API}_${ARCH}_AS${AVD_SCRATCH}_${EMULATORCMD}_${OPT}.log"
EMU_LOG_PATH="${EMU_LOG_DIR}${EMU_LOG_FILE}"
[[ ${DEBUG} -eq 1 ]] && OTHER_OPTS="${OTHER_OPTS} &> ${EMU_LOG_PATH}" || OTHER_OPTS="${OTHER_OPTS} > /dev/null"
[[ ${FREEZE} -eq 0 ]] && OTHER_OPTS="${OTHER_OPTS} &"
EMU_CMD="${EMULATORTASK} ${EMU_OPTS} ${OTHER_OPTS}"
echo " * EMU = ${EMU_CMD}"
eval ${EMU_CMD}
${ROOT_DIR}/waitForEmu.sh
###
### EMU
###
mkdir -p $EXPPATH
$PLATFORMPATH/adb logcat >> $EXPPATH/log-all.txt &
$PLATFORMPATH/adb logcat androidripper:i AndroidRuntime:e *:s >> $EXPPATH/log-filtered.txt &
sleep 5
mkdir -p $FILESPATH/$extCounter
mkdir -p $RESTOREPATH/$extCounter
mkdir -p $SCREENSHOTSPATH/$extCounter
mkdir -p $COVERAGEPATH/$extCounter
echo "Playing Session $extCounter"
echo "Playing Session $extCounter" >> $EXPPATH/test.txt
echo ""
$JAVA_CMD -jar $TOOLSPATH/PreferenceEditor.jar $DATAPATH/preferences.xml randomize $NUM_EVENTS >> $EXPPATH/test.txt
echo "changing prefs"
$PLATFORMPATH/adb shell rm $RIPPERPATH/*
$PLATFORMPATH/adb push $DATAPATH/preferences.xml $RIPPERPATH/preferences.xml
echo "unlock device"
$PLATFORMPATH/adb shell input keyevent 82
sleep 15
echo "running instr"
# NOTE: instrument apk (if you have emma in the classpath it is enough to change coverage false to true)
$PLATFORMPATH/adb shell am instrument -w -e coverage false -e class $TESTPACKAGE.$TESTCLASS $TESTPACKAGE/android.test.InstrumentationTestRunner >> $EXPPATH/test.txt
if `ls $FILESPATH/$extCounter/*.xml &> /dev/null` ; then \
mv $FILESPATH/$extCounter/*.xml $RESTOREPATH/$extCounter/; \
mv $FILESPATH/$extCounter/*.bak $RESTOREPATH/$extCounter/; \
mv $FILESPATH/$extCounter/*.obj $RESTOREPATH/$extCounter/; \
mv $FILESPATH/$extCounter/*.txt $RESTOREPATH/$extCounter/; \
fi
$PLATFORMPATH/adb pull $DEVICEPATH $FILESPATH/$extCounter
echo "copy coverage for run" $extCounter
adb shell am broadcast -a edu.gatech.m3.emma.COLLECT_COVERAGE
adb pull /mnt/sdcard/coverage.ec $COVERAGEPATH/$extCounter/coverage.ec
if [ -f $FILESPATH/*.jpg ] ; then \
cp $FILESPATH/*.jpg $SCREENSHOTSPATH
fi
if [ -f $FILESPATH/activities.xml ] ; then \
# Activity split
#cd $TOOLSPATH
if [ "$2" == "random" ]; then \
SPLIT_CODE=$extCounter
fi
$JAVA_CMD -jar $TOOLSPATH/GuiTSplitter.jar $FILESPATH $SPLIT_CODE
$JAVA_CMD -jar $TOOLSPATH/ActTSplitter.jar $FILESPATH $SPLIT_CODE
$JAVA_CMD -jar $TOOLSPATH/TasklistDiet.jar $FILESPATH $DATAPATH/preferences.xml
fi
$PLATFORMPATH/adb emu kill
sleep 5
rm -rf $EXPPATH/temp.txt
echo "End of Session $extCounter"
echo "End of Session $extCounter" >> $EXPPATH/test.txt
extCounter=$((extCounter+1))
if [ -f $FILESPATH/$extCounter/closed.txt ] ; then \
echo "** Crawling finished"; \
$PLATFORMPATH/adb kill-server; \
exit
fi
if [ ! -f $FILESPATH/$extCounter/parameters.obj ] ; then \
cp $RESTOREPATH/$extCounter/parameters.obj $FILESPATH/$extCounter/parameters.obj
fi
if [ ! -f $FILESPATH/$extCounter/tasklist.xml ] ; then \
cp $RESTOREPATH/$extCounter/tasklist.xml $FILESPATH/$extCounter/tasklist.xml
fi
$PLATFORMPATH/adb push $FILESPATH/$extCounter $DEVICEPATH
done
;;
################################################################################
log)
$PLATFORMPATH/adb logcat >> $EXPPATH/log.txt &
;;
################################################################################
logf)
$PLATFORMPATH/adb logcat androidripper:i AndroidRuntime:e *:s >> $EXPPATH/log.txt &
;;
################################################################################
close)
$PLATFORMPATH/adb kill-server
;;
################################################################################
tasklist)
adb push $RESTOREPATH $DEVICEPATH
if ls $TOOLSPATH/diet/tasklistdiet_bkp.xml $> /dev/null; then \
cp $TOOLSPATH/diet/tasklistdiet_bkp.xml $TOOLSPATH/diet/tasklist_diet.xml; \
fi
;;
################################################################################
shutdown)
adb emu kill
;;
################################################################################
*)
echo "No target specified. Targets in order: prepare <strategy> <apk>, ripper <strategy> <apk>, etc"
echo "strategy = random or systematic"
;;
esac