forked from zapbot/zap-mgmt-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzap-backup-test.py
executable file
·72 lines (60 loc) · 2.52 KB
/
zap-backup-test.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
#!/usr/bin/env python
# Time how long it takes to backup and recover the current ZAP session
# with a basic sanity check (counting the number of messages before and after recovery)
import datetime, time, sys, getopt
from pprint import pprint
from zapv2 import ZAPv2
def main(argv):
# -------------------------------------------------------------------------
# Default Configurations - use -z/-zap and -w/-wavsep for different IP addrs
# -------------------------------------------------------------------------
zapHostPort = 'http://localhost:8090'
try:
opts, args = getopt.getopt(argv,"z:",["zap="])
except getopt.GetoptError:
# TODO
print('zap-backup-test.py -z <ZAPhostPort>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('zap-backup-test.py -z <ZAPhostPort>')
sys.exit()
elif opt in ("-z", "--zap"):
zapHostPort = arg
zap = ZAPv2(proxies={'http': zapHostPort, 'https': zapHostPort})
# Count number of messages
old_mcount = zap.core.number_of_messages()
old_acount = zap.core.number_of_alerts()
print('Initial msg count: %s' % old_mcount)
print('Initial alert count: %s' % old_acount)
# Time backup
start_time = time.time()
zap.core.save_session(name='backup-test', overwrite='true')
backup_time = (time.time() - start_time)
print('Backed up: %s' % str(time.strftime('%H:%M:%S', time.gmtime(int(backup_time)))))
# Time new session
start_time = time.time()
zap.core.new_session(name='backup-empty', overwrite='true')
new_time = (time.time() - start_time)
print('New session: %s' % str(time.strftime('%H:%M:%S', time.gmtime(int(new_time)))))
# Sanity check new session
new_mcount = zap.core.number_of_messages()
if (new_mcount != '0'):
print('Unexpected empty count: %s' % new_mcount)
# Time restore
start_time = time.time()
zap.core.load_session(name='backup-test')
rec_time = (time.time() - start_time)
print('Loaded: %s' % str(time.strftime('%H:%M:%S', time.gmtime(int(rec_time)))))
rec_mcount = zap.core.number_of_messages()
rec_acount = zap.core.number_of_alerts()
if (old_mcount == rec_mcount):
print('PASS: msg counts match')
else:
print('FAIL: msg counts differ - original: %s recovered: %s' % old_mcount, rec_mcount)
if (old_acount == rec_acount):
print('PASS: alert counts match')
else:
print('FAIL: alert counts differ - original: %s recovered: %s' % old_acount, rec_acount)
if __name__ == "__main__":
main(sys.argv[1:])