| 1 | from __future__ import print_function |
| 2 | |
| 3 | import argparse as ap |
| 4 | from xml.etree import ElementTree |
| 5 | |
| 6 | |
| 7 | NAMESPACES = dict( |
| 8 | xccdf_ns="http://scap.nist.gov/schema/scap/source/1.2", |
| 9 | profile_ns="http://checklists.nist.gov/xccdf/1.2", |
| 10 | ) |
| 11 | |
| 12 | |
| 13 | class RuleResult(object): |
| 14 | def __init__(self, element): |
| 15 | self.idref = element.attrib["idref"] |
| 16 | self.weight = element.attrib["weight"] |
| 17 | |
| 18 | result_element = element.find("{%s}result" % NAMESPACES["profile_ns"]) |
| 19 | self.result_text = result_element.text |
| 20 | |
| 21 | check_element = element.find("{%s}check" % NAMESPACES["profile_ns"]) |
| 22 | self.check_system = check_element.attrib["system"] |
| 23 | self.check_system = check_element.attrib["system"] |
| 24 | |
| 25 | check_content_ref_element = check_element.find( |
| 26 | "{%s}check-content-ref" % NAMESPACES["profile_ns"]) |
| 27 | self.content_ref_href = check_content_ref_element.attrib["href"] |
| 28 | self.content_ref_name = check_content_ref_element.attrib["name"] |
| 29 | |
| 30 | check_export_element = check_element.find( |
| 31 | "{%s}check-export" % NAMESPACES["profile_ns"]) |
| 32 | self.check_export_name = None |
| 33 | self.check_export_value_id = None |
| 34 | if check_export_element is not None: |
| 35 | self.check_export_name = check_export_element.attrib["export-name"] |
| 36 | self.check_export_value_id = check_export_element.attrib["value-id"] |
| 37 | |
| 38 | def __hash__(self): |
| 39 | return hash(frozenset(self.__dict__.items())) |
| 40 | |
| 41 | def __eq__(self, rhs): |
| 42 | return hash(self) == hash(rhs) |
| 43 | |
| 44 | |
| 45 | def fname_to_etree(fname): |
| 46 | input_tree = ElementTree.parse(fname) |
| 47 | return input_tree |
| 48 | |
| 49 | |
| 50 | def get_rule_results_from_etree(tree): |
| 51 | xpath_expr = ".//{%s}rule-result" % NAMESPACES["profile_ns"] |
| 52 | xccdfs = tree.findall(xpath_expr) |
| 53 | return xccdfs |
| 54 | |
| 55 | |
| 56 | def extract_results_from_file(fname): |
| 57 | tree = fname_to_etree(fname) |
| 58 | results = get_rule_results_from_etree(tree) |
| 59 | results = {RuleResult(r) for r in results} |
| 60 | return results |
| 61 | |
| 62 | |
| 63 | def make_parser(): |
| 64 | parser = ap.ArgumentParser() |
| 65 | parser.add_argument("first") |
| 66 | parser.add_argument("second") |
| 67 | return parser |
| 68 | |
| 69 | |
| 70 | if __name__ == "__main__": |
| 71 | parser = make_parser() |
| 72 | args = parser.parse_args() |
| 73 | first_results = extract_results_from_file(args.first) |
| 74 | second_results = extract_results_from_file(args.second) |
| 75 | assert len(first_results) == len(second_results), \ |
| 76 | "There is not the same number of results in the two input files" |
| 77 | for result in first_results: |
| 78 | assert result in second_results, \ |
| 79 | "There is a result in the first file that isn't in the second file" |