-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As requested in #21, this verifies: * generated rulesets files match the XML files. (In some sense it's just a reimplementation of the `upstream/merge-rulesets.py` script, but would stop a zero-length ruleset from being deployed) * there are no extra nor missing signatures Fixes #21.
- Loading branch information
Showing
5 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import gzip | ||
import json | ||
import xml.etree.ElementTree | ||
from pathlib import Path | ||
|
||
LATEST_TIMESTAMP = Path("latest-rulesets-timestamp").read_text().strip() | ||
|
||
|
||
def load_xml_files(): | ||
xml_files = [] | ||
for ruleset in sorted(Path("rulesets").glob("*.xml")): | ||
tree = xml.etree.ElementTree.parse(ruleset) | ||
root = tree.getroot() | ||
# Rebuild the JSON | ||
expected = { | ||
"name": root.attrib["name"], | ||
"target": [root[0].attrib["host"]], | ||
"rule": [{"from": root[1].attrib["from"], "to": root[1].attrib["to"]}], | ||
} | ||
xml_files.append(expected) | ||
return xml_files | ||
|
||
|
||
def test_compressed_matches_xml(): | ||
# Read the contents of the gzipped file | ||
with gzip.open(f"default.rulesets.{LATEST_TIMESTAMP}.gz", "rb") as f: | ||
rulesets = json.load(f) | ||
xml_files = load_xml_files() | ||
|
||
assert rulesets["rulesets"] == xml_files | ||
|
||
|
||
def test_generated_matches_xml(): | ||
# Read the contents of the default file | ||
rulesets = json.loads(Path("rulesets/default.rulesets").read_text()) | ||
xml_files = load_xml_files() | ||
|
||
assert rulesets == xml_files | ||
|
||
|
||
def test_unique_signature(): | ||
""" | ||
For every default.rulesets.*.gz file, there should be a corresponding | ||
rulesets-signature.*.sha256 file with no extras. | ||
""" | ||
rulesets = [] | ||
for path in sorted(Path(".").glob("default.rulesets.*.gz")): | ||
rulesets.append(path.name.split(".")[2]) | ||
|
||
signatures = [] | ||
for path in sorted(Path(".").glob("rulesets-signature.*.sha256")): | ||
signatures.append(path.name.split(".")[1]) | ||
|
||
# Just verify it found *something* | ||
assert LATEST_TIMESTAMP in rulesets | ||
# Now check they're equal | ||
assert rulesets == signatures |