querying.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. from lettuce import *
  2. import subprocess
  3. import re
  4. # This script provides querying functionality
  5. # The most important step is
  6. #
  7. # query for <name> [type X] [class X] [to <addr>[:port]] should have rcode <rc>
  8. #
  9. # By default, it will send queries to 127.0.0.1:47806 unless specified
  10. # otherwise. The rcode is always checked. If the result is not NO_ANSWER,
  11. # the result will be stored in last_query_result, which can then be inspected
  12. # more closely, for instance with the step
  13. #
  14. # last query should have <property> <value>
  15. #
  16. #
  17. # define a class to easily access different parts
  18. # We may consider using our full library for this, but for now
  19. # simply store several parts of the response as text values in
  20. # this structure
  21. #
  22. # The following attributes are 'parsed' from the response, all as strings,
  23. # and end up as direct attributes of the QueryResult object:
  24. # opcode, rcode, id, flags, qdcount, ancount, nscount, adcount
  25. # (flags is one string with all flags)
  26. #
  27. # this will set 'rcode' as the result code, we 'define' one additional
  28. # rcode, "NO_ANSWER", if the dig process returned an error code itself
  29. # In this case none of the other attributes will be set.
  30. #
  31. # The different sections will be lists of strings, one for each RR in the
  32. # section. The question section will start with ';', as per dig output
  33. #
  34. # See server_from_sqlite3.feature for various examples to perform queries
  35. class QueryResult(object):
  36. status_re = re.compile("opcode: ([A-Z])+, status: ([A-Z]+), id: ([0-9]+)")
  37. flags_re = re.compile("flags: ([a-z ]+); QUERY: ([0-9]+), ANSWER: " +
  38. "([0-9]+), AUTHORITY: ([0-9]+), ADDITIONAL: ([0-9]+)")
  39. def __init__(self, name, qtype, qclass, address, port):
  40. args = [ 'dig', '+tries=1', '@' + address, '-p', str(port) ]
  41. if qtype is not None:
  42. args.append('-t')
  43. args.append(str(qtype))
  44. if qclass is not None:
  45. args.append('-c')
  46. args.append(str(qclass))
  47. args.append(name)
  48. dig_process = subprocess.Popen(args, 1, None, None, subprocess.PIPE,
  49. None)
  50. result = dig_process.wait()
  51. if result != 0:
  52. self.rcode = "NO_ANSWER"
  53. else:
  54. self.rcode = None
  55. parsing = "HEADER"
  56. self.question_section = []
  57. self.answer_section = []
  58. self.authority_section = []
  59. self.additional_section = []
  60. self.line_handler = self.parse_header
  61. for out in dig_process.stdout:
  62. self.line_handler(out)
  63. def parse_header(self, line):
  64. status_match = self.status_re.search(line)
  65. flags_match = self.flags_re.search(line)
  66. if status_match is not None:
  67. self.opcode = status_match.group(1)
  68. self.rcode = status_match.group(2)
  69. elif flags_match is not None:
  70. self.flags = flags_match.group(1)
  71. self.qdcount = flags_match.group(2)
  72. self.ancount = flags_match.group(3)
  73. self.nscount = flags_match.group(4)
  74. self.adcount = flags_match.group(5)
  75. elif line == ";; QUESTION SECTION:\n":
  76. self.line_handler = self.parse_question
  77. def parse_question(self, line):
  78. if line == ";; ANSWER SECTION:\n":
  79. self.line_handler = self.parse_answer
  80. elif line != "\n":
  81. self.question_section.append(line)
  82. def parse_answer(self, line):
  83. if line == ";; AUTHORITY SECTION:\n":
  84. self.line_handler = self.parse_authority
  85. elif line != "\n":
  86. self.answer_section.append(line)
  87. def parse_authority(self, line):
  88. if line == ";; ADDITIONAL SECTION:\n":
  89. self.line_handler = self.parse_additional
  90. elif line != "\n":
  91. self.additional_section.append(line)
  92. def parse_authority(self, line):
  93. if line.startswith(";; Query time"):
  94. self.line_handler = self.parse_footer
  95. elif line != "\n":
  96. self.additional_section.append(line)
  97. def parse_footer(self, line):
  98. pass
  99. @step('A query for ([\w.]+) (?:type ([A-Z]+) )?(?:class ([A-Z]+) )?' +
  100. '(?:to ([^:]+)(?::([0-9]+))? )?should have rcode ([\w.]+)')
  101. def query(step, query_name, qtype, qclass, addr, port, rcode):
  102. if qtype is None:
  103. qtype = "A"
  104. if qclass is None:
  105. qclass = "IN"
  106. if addr is None:
  107. addr = "127.0.0.1"
  108. if port is None:
  109. port = 47806
  110. query_result = QueryResult(query_name, qtype, qclass, addr, port)
  111. assert query_result.rcode == rcode,\
  112. "Expected: " + rcode + ", got " + query_result.rcode
  113. world.last_query_result = query_result
  114. @step('The SOA serial for ([\w.]+) should be ([0-9]+)')
  115. def query_soa(step, query_name, serial):
  116. query_result = QueryResult(query_name, "SOA", "IN", "127.0.0.1", "47806")
  117. assert "NOERROR" == query_result.rcode,\
  118. "Got " + query_result.rcode + ", expected NOERROR"
  119. assert len(query_result.answer_section) == 1,\
  120. "Too few or too many answers in SOA response"
  121. soa_parts = query_result.answer_section[0].split()
  122. assert serial == soa_parts[6],\
  123. "Got SOA serial " + soa_parts[6] + ", expected " + serial
  124. @step('last query should have (\S+) (.+)')
  125. def check_last_query(step, item, value):
  126. assert world.last_query_result is not None
  127. assert item in world.last_query_result.__dict__
  128. lq_val = world.last_query_result.__dict__[item]
  129. assert str(value) == str(lq_val),\
  130. "Got: " + str(lq_val) + ", expected: " + str(value)