Add support for XML
This commit is contained in:
parent
022990a337
commit
24f7dbea02
5 changed files with 662 additions and 6 deletions
|
|
@ -5,6 +5,7 @@ import configparser
|
|||
import pytest
|
||||
import textwrap
|
||||
import yaml
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
import jinjaturtle.core as core
|
||||
from jinjaturtle.core import (
|
||||
|
|
@ -147,12 +148,15 @@ def test_formats_match_expected_extensions():
|
|||
"""
|
||||
toml_path = SAMPLES_DIR / "tom.toml"
|
||||
ini_path = SAMPLES_DIR / "php.ini"
|
||||
xml_path = SAMPLES_DIR / "ossec.xml"
|
||||
|
||||
fmt_toml, _ = parse_config(toml_path)
|
||||
fmt_ini, _ = parse_config(ini_path)
|
||||
fmt_xml, _ = parse_config(xml_path)
|
||||
|
||||
assert fmt_toml == "toml"
|
||||
assert fmt_ini == "ini"
|
||||
assert fmt_xml == "xml"
|
||||
|
||||
|
||||
def test_parse_config_toml_missing_tomllib(monkeypatch):
|
||||
|
|
@ -442,3 +446,210 @@ def test_fallback_str_representer_for_unknown_type():
|
|||
|
||||
# It should serialize without error, and the string form should appear.
|
||||
assert "weird-value" in dumped
|
||||
|
||||
|
||||
def test_xml_roundtrip_ossec_web_rules():
|
||||
xml_path = SAMPLES_DIR / "ossec.xml"
|
||||
assert xml_path.is_file(), f"Missing sample XML file: {xml_path}"
|
||||
|
||||
fmt, parsed = parse_config(xml_path)
|
||||
assert fmt == "xml"
|
||||
|
||||
flat_items = flatten_config(fmt, parsed)
|
||||
assert flat_items, "Expected at least one flattened item from XML sample"
|
||||
|
||||
defaults_yaml = generate_defaults_yaml("ossec", flat_items)
|
||||
defaults = yaml.safe_load(defaults_yaml)
|
||||
|
||||
# defaults should be a non-empty dict
|
||||
assert isinstance(defaults, dict)
|
||||
assert defaults, "Expected non-empty defaults for XML sample"
|
||||
|
||||
# all keys should be lowercase, start with prefix, and have no spaces
|
||||
for key in defaults:
|
||||
assert key.startswith("ossec_")
|
||||
assert key == key.lower()
|
||||
assert " " not in key
|
||||
|
||||
# Root <group name="web,accesslog,"> attribute should flatten to ossec_name
|
||||
assert defaults["ossec_name"] == "web,accesslog,"
|
||||
|
||||
# There should be at least one default for rule id="31100"
|
||||
id_keys = [k for k, v in defaults.items() if v == "31100"]
|
||||
assert id_keys, "Expected to find a default for rule id 31100"
|
||||
|
||||
# At least one of them should be the rule *id* attribute
|
||||
assert any(
|
||||
key.startswith("ossec_rule_") and key.endswith("_id") for key in id_keys
|
||||
), f"Expected at least one *_id var for value 31100, got: {id_keys}"
|
||||
|
||||
# Template generation (preserving comments)
|
||||
original_text = xml_path.read_text(encoding="utf-8")
|
||||
template = generate_template(fmt, parsed, "ossec", original_text=original_text)
|
||||
assert isinstance(template, str)
|
||||
assert template.strip(), "Template for XML sample should not be empty"
|
||||
|
||||
# Top-of-file and mid-file comments should be preserved
|
||||
assert "Official Web access rules for OSSEC." in template
|
||||
assert "Rules to ignore crawlers" in template
|
||||
|
||||
# Each default variable name should appear in the template as a Jinja placeholder
|
||||
for var_name in defaults:
|
||||
assert (
|
||||
var_name in template
|
||||
), f"Variable {var_name} not referenced in XML template"
|
||||
|
||||
|
||||
def test_generate_xml_template_from_text_edge_cases():
|
||||
"""
|
||||
Exercise XML text edge cases:
|
||||
- XML declaration and DOCTYPE in prolog
|
||||
- top-level and inner comments
|
||||
- repeated child elements (indexing)
|
||||
- attributes and text content
|
||||
"""
|
||||
text = textwrap.dedent(
|
||||
"""\
|
||||
<?xml version="1.0"?>
|
||||
<!-- top comment -->
|
||||
<!DOCTYPE something>
|
||||
<root attr="1">
|
||||
<!-- inner comment -->
|
||||
<child attr="2">text</child>
|
||||
<child>other</child>
|
||||
</root>
|
||||
"""
|
||||
)
|
||||
|
||||
tmpl = core._generate_xml_template_from_text("role", text)
|
||||
|
||||
# Prolog and comments preserved
|
||||
assert "<?xml version" in tmpl
|
||||
assert "top comment" in tmpl
|
||||
assert "inner comment" in tmpl
|
||||
|
||||
# Root attribute becomes a variable (path ("@attr",) -> role_attr)
|
||||
assert "role_attr" in tmpl
|
||||
|
||||
# Repeated <child> elements should be indexed in both attr and text
|
||||
assert "role_child_0_attr" in tmpl
|
||||
assert "role_child_0" in tmpl
|
||||
assert "role_child_1" in tmpl
|
||||
|
||||
|
||||
def test_generate_template_xml_type_error():
|
||||
"""
|
||||
Wrong type for XML in generate_template should raise TypeError.
|
||||
"""
|
||||
with pytest.raises(TypeError):
|
||||
generate_template("xml", parsed="not an element", role_prefix="role")
|
||||
|
||||
|
||||
def test_flatten_config_xml_type_error():
|
||||
"""
|
||||
Wrong type for XML in flatten_config should raise TypeError.
|
||||
"""
|
||||
with pytest.raises(TypeError):
|
||||
flatten_config("xml", parsed="not-an-element")
|
||||
|
||||
|
||||
def test_generate_template_xml_structural_fallback():
|
||||
"""
|
||||
When original_text is not provided for XML, generate_template should use
|
||||
the structural fallback path (ET.tostring + _generate_xml_template_from_text).
|
||||
"""
|
||||
xml_text = textwrap.dedent(
|
||||
"""\
|
||||
<root attr="1">
|
||||
<child>2</child>
|
||||
<node attr="x">text</node>
|
||||
</root>
|
||||
"""
|
||||
)
|
||||
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=False))
|
||||
root = ET.fromstring(xml_text, parser=parser)
|
||||
|
||||
tmpl = generate_template("xml", parsed=root, role_prefix="role")
|
||||
|
||||
# Root attribute path ("@attr",) -> role_attr
|
||||
assert "role_attr" in tmpl
|
||||
|
||||
# Simple child element text ("child",) -> role_child
|
||||
assert "role_child" in tmpl
|
||||
|
||||
# Element with both attr and text:
|
||||
# - attr -> ("node", "@attr") -> role_node_attr
|
||||
# - text -> ("node", "value") -> role_node_value
|
||||
assert "role_node_attr" in tmpl
|
||||
assert "role_node_value" in tmpl
|
||||
|
||||
|
||||
def test_split_xml_prolog_only_whitespace():
|
||||
"""
|
||||
Whitespace-only input: prolog is the whitespace, body is empty.
|
||||
Exercises the 'if i >= n: break' path.
|
||||
"""
|
||||
text = " \n\t"
|
||||
prolog, body = core._split_xml_prolog(text)
|
||||
assert prolog == text
|
||||
assert body == ""
|
||||
|
||||
|
||||
def test_split_xml_prolog_unterminated_declaration():
|
||||
"""
|
||||
Unterminated XML declaration should hit the 'end == -1' branch and
|
||||
treat the whole string as body.
|
||||
"""
|
||||
text = "<?xml version='1.0'"
|
||||
prolog, body = core._split_xml_prolog(text)
|
||||
assert prolog == ""
|
||||
assert body == text
|
||||
|
||||
|
||||
def test_split_xml_prolog_unterminated_comment():
|
||||
"""
|
||||
Unterminated comment should likewise hit its 'end == -1' branch.
|
||||
"""
|
||||
text = "<!-- no end"
|
||||
prolog, body = core._split_xml_prolog(text)
|
||||
assert prolog == ""
|
||||
assert body == text
|
||||
|
||||
|
||||
def test_split_xml_prolog_unterminated_doctype():
|
||||
"""
|
||||
Unterminated DOCTYPE should hit the DOCTYPE 'end == -1' branch.
|
||||
"""
|
||||
text = "<!DOCTYPE foo"
|
||||
prolog, body = core._split_xml_prolog(text)
|
||||
assert prolog == ""
|
||||
assert body == text
|
||||
|
||||
|
||||
def test_split_xml_prolog_unexpected_content():
|
||||
"""
|
||||
Non-XML content at the start should trigger the 'unexpected content'
|
||||
break and be returned entirely as body.
|
||||
"""
|
||||
text = "garbage<root/>"
|
||||
prolog, body = core._split_xml_prolog(text)
|
||||
assert prolog == ""
|
||||
assert body == text
|
||||
|
||||
|
||||
def test_flatten_xml_text_with_attributes_uses_value_suffix():
|
||||
"""
|
||||
When an element has both attributes and text, _flatten_xml should store
|
||||
the text at path + ('value',), not just path.
|
||||
"""
|
||||
xml_text = "<root><node attr='x'>text</node></root>"
|
||||
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=False))
|
||||
root = ET.fromstring(xml_text, parser=parser)
|
||||
|
||||
items = flatten_config("xml", root)
|
||||
|
||||
# Attribute path: ("node", "@attr") -> "x"
|
||||
assert (("node", "@attr"), "x") in items
|
||||
|
||||
# Text-with-attrs path: ("node", "value") -> "text"
|
||||
assert (("node", "value"), "text") in items
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue