Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a73aa42

Browse files
committed
minor enhancements - #311
1 parent 0953ce5 commit a73aa42

1 file changed

Lines changed: 81 additions & 57 deletions

File tree

extra/shutils/regressiontest.py

Lines changed: 81 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,93 +8,117 @@
88
import re
99
import smtplib
1010
import subprocess
11+
import sys
1112
import time
1213

13-
from email.mime.text import MIMEText
1414
from email.mime.multipart import MIMEMultipart
15+
from email.mime.text import MIMEText
16+
17+
sys.path.append("../../")
18+
19+
from lib.core.revision import getRevisionNumber
1520

1621
TIME = time.strftime("%H:%M:%S %d-%m-%Y", time.gmtime())
1722
SQLMAP_HOME = "/opt/sqlmap"
23+
REVISION = getRevisionNumber()
1824

1925
SMTP_SERVER = "127.0.0.1"
2026
SMTP_PORT = 25
2127
SMTP_TIMEOUT = 30
2228
2329
24-
SUBJECT = "Regression test results on %s" % TIME
30+
SUBJECT = "Regression test results on %s using revision %s" % (TIME, REVISION)
2531
CONTENT = ""
2632
TEST_COUNTS = []
2733
ATTACHMENTS = {}
2834

29-
command_line = "cd %s && python sqlmap.py --live-test" % SQLMAP_HOME
30-
proc = subprocess.Popen(command_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
35+
def prepare_email(content):
36+
msg = MIMEMultipart()
37+
msg["Subject"] = SUBJECT
38+
msg["From"] = FROM
39+
msg["To"] = TO
3140

32-
proc.wait()
33-
stdout, stderr = proc.communicate()
41+
msg.attach(MIMEText(content))
3442

35-
failed_tests = re.findall("running live test case: (.+?) \((\d+)\/\d+\)[\r]*\n.+test failed (at parsing item \"(.+)\" )?\- scan folder: (\/.+) \- traceback: (.*?)( - SQL injection not detected)?[\r]*\n", stdout, re.M)
43+
return msg
3644

37-
for failed_test in failed_tests:
38-
title = failed_test[0]
39-
test_count = int(failed_test[1])
40-
parse = failed_test[3] if failed_test[3] else None
41-
output_folder = failed_test[4]
42-
traceback = False if failed_test[5] == "False" else bool(failed_test[5])
43-
detected = False if failed_test[6] else True
45+
def send_email(msg):
46+
try:
47+
s = smtplib.SMTP(host=SMTP_SERVER, port=SMTP_PORT, timeout=SMTP_TIMEOUT)
48+
#s.set_debuglevel(1)
49+
s.sendmail(FROM, TO, msg.as_string())
50+
s.quit()
51+
# Catch all for SMTP exceptions
52+
except smtplib.SMTPException, e:
53+
print "Failure to send email: %s" % str(e)
4454

45-
TEST_COUNTS.append(test_count)
55+
def main():
56+
command_line = "cd %s && python sqlmap.py --live-test" % SQLMAP_HOME
57+
proc = subprocess.Popen(command_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
4658

47-
console_output_fd = codecs.open(os.path.join(output_folder, "console_output"), "rb", "utf8")
48-
console_output = console_output_fd.read()
49-
console_output_fd.close()
59+
proc.wait()
60+
stdout, stderr = proc.communicate()
5061

51-
ATTACHMENTS[test_count] = str(console_output)
62+
if stderr:
63+
msg = prepare_email("Execution of regression test failed with error: %s" % stderr)
64+
send_email(msg)
65+
sys.exit(1)
5266

53-
log_fd = codecs.open(os.path.join(output_folder, "debiandev", "log"), "rb", "utf8")
54-
log = log_fd.read()
55-
log_fd.close()
67+
failed_tests = re.findall("running live test case: (.+?) \((\d+)\/\d+\)[\r]*\n.+test failed (at parsing item \"(.+)\" )?\- scan folder: (\/.+) \- traceback: (.*?)( - SQL injection not detected)?[\r]*\n", stdout, re.M)
5668

57-
if traceback:
58-
traceback_fd = codecs.open(os.path.join(output_folder, "traceback"), "rb", "utf8")
59-
traceback = traceback_fd.read()
60-
traceback_fd.close()
69+
for failed_test in failed_tests:
70+
title = failed_test[0]
71+
test_count = int(failed_test[1])
72+
parse = failed_test[3] if failed_test[3] else None
73+
output_folder = failed_test[4]
74+
traceback = False if failed_test[5] == "False" else bool(failed_test[5])
75+
detected = False if failed_test[6] else True
6176

62-
CONTENT += "Failed test case '%s'" % title
77+
TEST_COUNTS.append(test_count)
6378

64-
if parse:
65-
CONTENT += " at parsing: %s:\n\n" % parse
66-
CONTENT += "### Log file:\n\n"
67-
CONTENT += "%s\n" % log
68-
elif not detected:
69-
CONTENT += " - SQL injection not detected\n\n"
79+
console_output_fd = codecs.open(os.path.join(output_folder, "console_output"), "rb", "utf8")
80+
console_output = console_output_fd.read()
81+
console_output_fd.close()
7082

71-
if traceback:
72-
CONTENT += "### Traceback:\n\n"
73-
CONTENT += "%s\n" % str(traceback)
83+
ATTACHMENTS[test_count] = str(console_output)
7484

75-
CONTENT += "#######################################################################\n\n"
85+
log_fd = codecs.open(os.path.join(output_folder, "debiandev", "log"), "rb", "utf8")
86+
log = log_fd.read()
87+
log_fd.close()
7688

77-
if CONTENT:
78-
SUBJECT += " (%s)" % ", ".join("#%d" % count for count in TEST_COUNTS)
79-
CONTENT += "\n\nRegression test finished at %s" % time.strftime("%H:%M:%S %d-%m-%Y", time.gmtime())
89+
if traceback:
90+
traceback_fd = codecs.open(os.path.join(output_folder, "traceback"), "rb", "utf8")
91+
traceback = traceback_fd.read()
92+
traceback_fd.close()
8093

81-
msg = MIMEMultipart()
82-
msg["Subject"] = SUBJECT
83-
msg["From"] = FROM
84-
msg["To"] = TO
94+
CONTENT += "Failed test case '%s'" % title
8595

86-
msg.attach(MIMEText(CONTENT))
96+
if parse:
97+
CONTENT += " at parsing: %s:\n\n" % parse
98+
CONTENT += "### Log file:\n\n"
99+
CONTENT += "%s\n" % log
100+
elif not detected:
101+
CONTENT += " - SQL injection not detected\n\n"
87102

88-
for test_count, attachment in ATTACHMENTS.items():
89-
attachment = MIMEText(attachment)
90-
attachment.add_header("Content-Disposition", "attachment", filename="test_case_%d_console_output.txt" % test_count)
91-
msg.attach(attachment)
103+
if traceback:
104+
CONTENT += "### Traceback:\n\n"
105+
CONTENT += "%s\n" % str(traceback)
106+
107+
CONTENT += "#######################################################################\n\n"
108+
109+
if CONTENT:
110+
SUBJECT += " (%s)" % ", ".join("#%d" % count for count in TEST_COUNTS)
111+
CONTENT += "\n\nRegression test finished at %s" % time.strftime("%H:%M:%S %d-%m-%Y", time.gmtime())
112+
113+
msg = prepare_email(CONTENT)
114+
115+
for test_count, attachment in ATTACHMENTS.items():
116+
attachment = MIMEText(attachment)
117+
attachment.add_header("Content-Disposition", "attachment", filename="test_case_%d_console_output.txt" % test_count)
118+
msg.attach(attachment)
119+
120+
send_email(msg)
121+
122+
if __name__ == "__main__":
123+
main()
92124

93-
try:
94-
s = smtplib.SMTP(host=SMTP_SERVER, port=SMTP_PORT, timeout=SMTP_TIMEOUT)
95-
#s.set_debuglevel(1)
96-
s.sendmail(FROM, TO, msg.as_string())
97-
s.quit()
98-
# Catch all for SMTP exceptions
99-
except smtplib.SMTPException, e:
100-
print "Failure to send email: %s" % str(e)

0 commit comments

Comments
 (0)