Commit 7e2011e7 authored by Alexandre's avatar Alexandre

Store success's state in Request + refactor

Do not return any state from the print_result method. The success's
state should already be known when calling this method.
parent ec4f2138
......@@ -136,7 +136,6 @@ def get_next_domain(input_file):
return name, rtype
def print_result(connection, request, prefix=None, display_err=True):
ok = request.ok
dot = connection.dot
server = connection.server
rcode = request.rcode
......@@ -161,8 +160,6 @@ def print_result(connection, request, prefix=None, display_err=True):
except (UnicodeDecodeError, AttributeError):
pass # Sometimes, msg can be binary, or Latin-1
print("HTTP error %i: %s" % (rcode, msg), file=sys.stderr)
ok = False
return ok
def run_check_default(connection):
ok = True
......@@ -195,16 +192,13 @@ def run_check_default(connection):
else:
print(e, file=sys.stderr)
return False
request.check_response(connection.debug)
if not print_result(connection, request, prefix=test_name, display_err=False):
if level >= opts.mandatory_level:
print_result(connection, request, prefix=test_name, display_err=True)
ok = False
if connection.verbose:
print()
break
if level >= opts.mandatory_level:
ok = request.check_response(connection.debug)
print_result(connection, request, prefix=test_name, display_err=not ok)
if connection.verbose:
print()
if not ok:
break
return ok
def run_check_mime(connection, accept="application/dns-message", content_type="application/dns-message"):
......@@ -228,9 +222,8 @@ def run_check_mime(connection, accept="application/dns-message", content_type="a
except (homer.ConnectionException, homer.DOHException) as e:
print(e, file=sys.stderr)
return False
request.check_response(connection.debug)
if not print_result(connection, request, prefix=f"Test Header {', '.join(header)}"):
ok = False
ok = request.check_response(connection.debug)
print_result(connection, request, prefix=f"Test Header {', '.join(header)}")
default = "application/dns-message"
default_header = [f"Accept: {default}", f"Content-type: {default}"]
handle.setopt(pycurl.HTTPHEADER, default_header)
......@@ -479,8 +472,8 @@ def run_default(name, connection, opts):
error(e)
break
if not opts.multistreams:
if not print_result(connection, request):
ok = False
ok = request.success
print_result(connection, request)
if opts.tests > 1 and i == 0:
start2 = time.time()
if opts.delay is not None:
......
......@@ -20,7 +20,7 @@ class Request:
self.message = dns.message.make_query(qname, dns.rdatatype.from_text(qtype),
use_edns=use_edns, want_dnssec=want_dnssec, options=options)
self.message.flags |= dns.flags.AD # Ask for validation
self.ok = True
self.success = True # True by default, set to False as soon as an error is encountered
self.i = 0 # request's number on the connection (default to the first)
def trunc_data(self):
......@@ -42,18 +42,18 @@ class RequestDOT(Request):
# raising custom exception for each unexpected response might be a good idea
def check_response(self, debug=False):
if self.response is None:
self.success = False
raise homer.RequestDOTException("No reply received")
ok = self.ok
if not self.rcode:
self.ok = False
self.success = False
return False
if self.response.id != self.message.id:
self.response = "The ID in the answer does not match the one in the query"
if debug:
self.response += f'"(query id: {self.message.id}) (response id: {self.response.id})'
self.ok = False
self.success = False
return False
return self.ok
return self.success
def store_response(self, rcode, data, size):
self.rcode = True
......@@ -70,11 +70,10 @@ class RequestDOH(Request):
# raising custom exception for each unexpected response might be a good idea
def check_response(self, debug=False):
ok = self.ok
if self.rcode == 200:
if self.ctype != "application/dns-message":
self.response = "Content type of the response (\"%s\") invalid" % self.ctype
ok = False
self.success = False
else:
if not self.head:
try:
......@@ -85,12 +84,12 @@ class RequestDOH(Request):
self.response = "ERROR Not proper DNS data, trailing junk"
if debug:
self.response += " \"%s\"" % response
ok = False
self.success = False
except dns.name.BadLabelType: # Not DNS.
self.response = "ERROR Not proper DNS data (wrong path in the URL?)"
if debug:
self.response += " \"%s\"" % response[:100]
ok = False
self.success = False
else:
self.response = response
else:
......@@ -101,15 +100,14 @@ class RequestDOH(Request):
self.response = "ERROR Body length is not null"
if debug:
self.response += "\"%s\"" % data[:100]
ok = False
self.success = False
else:
ok = False
self.success = False
if self.response_size == 0:
self.response = "[No details]"
else:
self.response = self.response
self.ok = ok
return ok
return self.success
def create_request(qname, qtype='AAAA', use_edns=True, want_dnssec=False, no_ecs=True, dot=False, trunc=False):
if dot:
......
......@@ -82,7 +82,6 @@ def error(msg=None):
sys.exit(STATE_CRITICAL)
def print_result(connection, request, prefix=None, display_err=True):
ok = request.ok
dot = connection.dot
server = connection.server
rcode = request.rcode
......@@ -91,18 +90,16 @@ def print_result(connection, request, prefix=None, display_err=True):
if (dot and rcode) or (not dot and rcode == 200):
if not request.has_expected_str(opts.expect):
print("%s Cannot find \"%s\" in response" % (server, opts.expect))
ok = False
if ok and size is not None and size > 0:
print("%s OK - %s" % (server, "No error for %s/%s, %i bytes received" % (name, opts.rtype, size)))
elif ok:
print("%s OK - %s" % (server, "No error"))
else:
if size is not None and size > 0:
print("%s OK - %s" % (server, "No error for %s/%s, %i bytes received" % (name, opts.rtype, size)))
else:
print("%s OK - %s" % (server, "No error"))
else:
if not dot:
print("%s HTTP error - %i: %s" % (server, rcode, msg))
else:
print("%s Error - %i: %s" % (server, rcode, msg))
ok = False
return ok
def parse_opts_monitoring(me, opts):
name = None
......@@ -188,7 +185,10 @@ def run_default(name, connection, opts):
except (OpenSSL.SSL.Error, homer.DOHException) as e:
error(e)
return False
return print_result(connection, request)
ok = request.success and request.has_expected_str(opts.expect)
print_result(connection, request)
return ok
# Main program
if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment