-
Notifications
You must be signed in to change notification settings - Fork 2
/
acvtool.py
247 lines (228 loc) · 12.8 KB
/
acvtool.py
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
import os
import sys
import yaml
import logging
import argparse
from logging import config as logging_config
from six.moves import input
from smiler import smiler
from smiler import reporter
from smiler.config import config
from pkg_resources import resource_stream
from smiler.granularity import Granularity
def setup_logging():
with open(config.logging_yaml) as f:
logging_config.dictConfig(yaml.safe_load(f.read()))
def run_actions(parser, args=None):
"""
This function parses the arguments using the provided parser and run
corresponding acvtool actions.
Args:
parser(argparse.ArgumentParser): ArgumentParser for this code
"""
if args is None:
args = parser.parse_args()
if args.subcmd in ["instrument", "install", "uninstall", "start", "stop", "snap", "report"] and args.device:
config.adb_path = "{} -s {}".format(config.adb_path, args.device)
if args.subcmd == "instrument":
if os.path.isdir(args.working_dir):
if not args.force:
print("The working directory exists and may contain data: {}".format(args.working_dir))
user_choice = input("Overwrite (y/n)? ")
if user_choice.lower() in ["y", "yes"]:
pass
elif user_choice.lower() in ["n", "no"]:
print("Aborting operation!")
return
else:
print("Your choice is not correct! Exiting!")
return
package, apk_path, pickle_path = smiler.instrument_apk(
apk_path=args.apk_path,
result_dir=args.working_dir,
dbg_start=args.dbg_start,
dbg_end=args.dbg_end,
installation=args.install or args.report,
granularity=args.granularity,
mem_stats=args.memstats,
keep_unpacked=args.keepsources,
ignore_filter=args.stubs)
if args.report:
smiler.start_instrumenting(package,
onstop=lambda: reporter.generate(
package,
pickle_path,
output_dir=config.default_report_dir,
granularity=args.granularity),
timeout=int(args.timeout))
elif args.subcmd == "install":
smiler.install(args.apk_path)
elif args.subcmd == "uninstall":
smiler.uninstall(args.package_name)
elif args.subcmd == "start":
onstop_report = None
if args.report:
onstop_report = lambda: reporter.generate(
args.package_name,
args.pickle_path,
output_dir=config.default_report_dir,
granularity=args.granularity)
smiler.start_instrumenting(
args.package_name,
args.q,
onstop=onstop_report,
timeout=int(args.timeout))
elif args.subcmd == "stop":
smiler.stop_instrumenting(args.package_name, int(args.timeout))
elif args.subcmd == "snap":
if args.repeat:
smiler.save_ec_and_screen(args.package_name, int(args.throttle), args.output_dir)
else:
smiler.snap(args.package_name, 1, args.output_dir)
elif args.subcmd == "report":
all_reps = not (args.xml or args.html or args.concise) # generate xml, html and concise reports by default
reporter.generate(
args.package_name,
args.pickle_path,
args.output_dir,
args.ec_dir,
xml=args.xml or all_reps, html=args.html or all_reps,
granularity=args.granularity, concise=args.concise or all_reps,
ignore_filter=args.stubs)
elif args.subcmd == "sign":
smiler.sign_align_apk(args.apk_path, "{0}.signed.apk".format(args.apk_path))
elif args.subcmd == "build":
smiler.build_dir(args.apktool_dir, args.result_dir, signature=args.s, installation=args.i)
else:
parser.print_usage()
return
def get_parser():
parser = argparse.ArgumentParser(prog='acvtool.py',
description='This tool is designed to measure code coverage of \
Android applications when its sources are not available.')
parser.add_argument('--version', action='version', version=str(config.version))
subparsers = parser.add_subparsers(dest='subcmd', metavar="<command>",
help="acvtool commands")
parser_instrument = subparsers.add_parser("instrument", help="Instruments an apk")
parser_instrument.add_argument("apk_path", metavar="<path_to_apk>",
help="Path to apk file")
parser_instrument.add_argument("--wd", metavar="<result_directory>",
default=config.default_working_dir,
dest="working_dir", required=False,
help="Path to the directory where the working data is stored")
parser_instrument.add_argument("--dbgstart", metavar="<methods_number>",
help="Troubleshooting purposes. The number of the first method to be \
instrumented. Only methods from DBGSTART to DBGEND will be instrumented.",
required=False, dest="dbg_start", type=int)
parser_instrument.add_argument("--dbgend", metavar="<methods_number>",
help="Troubleshooting purposes. The number of the last method to be \
instrumented. Only methods from DBGSTART to DBGEND will be instrumented.",
required=False, dest="dbg_end", type=int)
parser_instrument.add_argument("-f", "--force", action="store_true",
help="Working directory will be overwritten without asking.",
required=False)
parser_instrument.add_argument("-i", "--install", action="store_true",
help="Installs the application immidiately after instrumenting.")
parser_instrument.add_argument("-r", "--report", action="store_true",
help="Performs the whole testing cycle in a single session: \
instrument, start, report.")
parser_instrument.add_argument("-g", "--granularity", metavar="<granularity>",
help="Code coverage granularity [instruction or method].", choices=Granularity.granularities(),
default=Granularity.default)
parser_instrument.add_argument("-t", "--timeout", metavar="<timeout>", required=False,
help="Waiting time for coverage file preparing.",
default=config.default_onstop_timeout)
parser_instrument.add_argument("-ms", "--memstats", metavar="<memstats>", required=False,
help="How many bytes acvtool allocated for tracking.['single', 'verbose']",
choices=["single", "verbose"], default=None)
parser_instrument.add_argument("-d", "--device", metavar="<device>", required=False,
help="The name of adb device/emulator.", default=None, dest="device")
parser_instrument.add_argument("-k", "--keepsources", action="store_true",
help="Keep instrumented smali dir.")
parser_instrument.add_argument("-s", "--stubs", metavar="<stubs>", required=False,
help="Ignores list of stub methods. List methods signatures in txt files", default=None)
parser_install = subparsers.add_parser("install", help="Installs an apk")
parser_install.add_argument("apk_path", metavar="<path_to_apk>",
help="Path to apk file")
parser_install.add_argument("-d", "--device", metavar="<device>", required=False,
help="The name of adb device/emulator.", default=None, dest="device")
parser_uninstall = subparsers.add_parser("uninstall", help="Uninstalls an apk")
parser_uninstall.add_argument("package_name", metavar="<package>",
help="Package name")
parser_uninstall.add_argument("-d", "--device", metavar="<device>", required=False,
help="The name of adb device/emulator.", default=None, dest="device")
parser_start = subparsers.add_parser("start", help="Starts runtime coverage data collection")
parser_start.add_argument("package_name", metavar="<package.name>")
parser_start.add_argument("-q", "--q", action="store_true", required=False,
help="Releases the thread. Requires to call 'stop' command after the tests were completed.")
parser_start.add_argument("-r", "--report", action="store_true",
help="Generates a report immediately after the test is finished.")
parser_start.add_argument("-p", metavar="<pickle_file>", required=False,
dest="pickle_path", help="Path to pickle file")
parser_start.add_argument("-g", "--granularity", metavar="<granularity>",
help="Code coverage granularity [instruction or method].", choices=Granularity.granularities(),
default=Granularity.default)
parser_start.add_argument("-t", "--timeout", metavar="<timeout>", required=False,
help="Waiting time for coverage file preparing.",
default=config.default_onstop_timeout)
parser_start.add_argument("-d", "--device", metavar="<device>", required=False,
help="The name of adb device/emulator.", default=None, dest="device")
parser_stop = subparsers.add_parser("stop", help="Stops runtime coverage data collection")
parser_stop.add_argument("package_name", metavar="<package.name>")
parser_stop.add_argument("-t", "--timeout", metavar="<timeout>", required=False,
help="Waiting time for coverage file preparing.",
default=config.default_onstop_timeout)
parser_stop.add_argument("-d", "--device", metavar="<device>", required=False,
help="The name of adb device/emulator.", default=None, dest="device")
parser_snap = subparsers.add_parser("snap", help="Saves runtime current coverage data and screen")
parser_snap.add_argument("package_name", metavar="<package.name>")
parser_snap.add_argument("-d", "--device", metavar="<device>", required=False,
help="The name of adb device/emulator.", default=None, dest="device")
parser_snap.add_argument("-r", "--repeat", action="store_true",
help="Repeats coverage saving every 5 seconds.")
parser_snap.add_argument("-o", metavar="<output_dir>", required=False,
dest="output_dir", help="Output directory")
parser_snap.add_argument("-t", "--throttle", metavar="<throttle>", required=False,
dest="throttle", help="Delay between snaps.", default=config.throttle)
parser_report = subparsers.add_parser("report", help="Produces a report")
parser_report.add_argument("package_name", metavar="<package_name>",
help="Package name")
parser_report.add_argument("-p", metavar="<pickle_file>", required=True,
dest="pickle_path", help="Path to pickle file")
parser_report.add_argument("-o", metavar="<output_dir>", required=False,
dest="output_dir", help="Output directory",
default=config.default_report_dir)
parser_report.add_argument("-ec", metavar="<ec_dir>", required=False,
dest="ec_dir", help="The directory with the code coverage binary files preloaded from the emulator.")
parser_report.add_argument("-xml", "--xml", action="store_true",
help="Generate XML report.")
parser_report.add_argument("-html", "--html", action="store_true",
help="Generate HTML report.")
parser_report.add_argument("-g", "--granularity", metavar="<granularity>", dest="granularity",
help="Code coverage granularity [instruction or method].", choices=Granularity.granularities(),
default=Granularity.default)
parser_report.add_argument("-d", "--device", metavar="<device>", required=False,
help="The name of adb device/emulator.", default=None, dest="device")
parser_report.add_argument("-c", "--concise", action="store_true",
help="Short csv report contains coverage per single .ec file.")
parser_report.add_argument("-s", "--stubs", metavar="<stubs>", required=False,
help="Ignores list of stub methods. List methods signatures in txt files", default=None)
parser_sign = subparsers.add_parser("sign", help="Signs and alignes an apk.")
parser_sign.add_argument("apk_path", metavar="<apk_path>", help="An application's path")
parser_build = subparsers.add_parser("build", help="Builds a folder with an unpacked by apktool apk.")
parser_build.add_argument("apktool_dir", metavar="<apktool_dir>", help="The path to an unpacked apk.")
parser_build.add_argument("--rd", metavar="<result_dir>", required=False,
dest="result_dir",
default=config.default_working_dir,
help="Path to the directory where the working data is stored")
parser_build.add_argument("-s", "--s", action="store_true", help="Signs the apk produced.")
parser_build.add_argument("-i", "--i", action="store_true",
help="Installs the application immidiately after instrumenting.")
return parser
def main():
setup_logging()
parser = get_parser()
args = parser.parse_args()
run_actions(parser, args)
if __name__ == "__main__":
main()