diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-18 05:52:22 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-18 05:52:22 +0000 |
commit | 38b7c80217c4e72b1d8988eb1e60bb6e77334114 (patch) | |
tree | 356e9fd3762877d07cde52d21e77070aeff7e789 /ansible_collections/microsoft/ad/tests/unit/plugins | |
parent | Adding upstream version 7.7.0+dfsg. (diff) | |
download | ansible-38b7c80217c4e72b1d8988eb1e60bb6e77334114.tar.xz ansible-38b7c80217c4e72b1d8988eb1e60bb6e77334114.zip |
Adding upstream version 9.4.0+dfsg.upstream/9.4.0+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ansible_collections/microsoft/ad/tests/unit/plugins')
-rw-r--r-- | ansible_collections/microsoft/ad/tests/unit/plugins/filter/test_ldap_converters.py | 152 |
1 files changed, 149 insertions, 3 deletions
diff --git a/ansible_collections/microsoft/ad/tests/unit/plugins/filter/test_ldap_converters.py b/ansible_collections/microsoft/ad/tests/unit/plugins/filter/test_ldap_converters.py index 923d30b31..362e76b4a 100644 --- a/ansible_collections/microsoft/ad/tests/unit/plugins/filter/test_ldap_converters.py +++ b/ansible_collections/microsoft/ad/tests/unit/plugins/filter/test_ldap_converters.py @@ -8,7 +8,13 @@ import uuid import pytest from ansible.errors import AnsibleFilterError -from ansible_collections.microsoft.ad.plugins.filter.ldap_converters import as_sid, as_guid, as_datetime +from ansible_collections.microsoft.ad.plugins.filter.ldap_converters import ( + as_sid, + as_guid, + as_datetime, + dn_escape, + parse_dn, +) @pytest.mark.parametrize("type", ["int", "str", "bytes"]) @@ -37,7 +43,10 @@ def test_as_datetime_with_format() -> None: def test_as_datetime_from_list() -> None: actual = as_datetime([133220025750000000, 133220025751000020]) - assert actual == ["2023-02-27T20:16:15.000000+0000", "2023-02-27T20:16:15.100002+0000"] + assert actual == [ + "2023-02-27T20:16:15.000000+0000", + "2023-02-27T20:16:15.100002+0000", + ] @pytest.mark.parametrize("type", ["str", "bytes"]) @@ -83,10 +92,147 @@ def test_as_sid_from_list() -> None: def test_as_sid_too_little_data_auth_count() -> None: - with pytest.raises(AnsibleFilterError, match="Raw SID bytes must be at least 8 bytes long"): + with pytest.raises( + AnsibleFilterError, match="Raw SID bytes must be at least 8 bytes long" + ): as_sid(b"\x00\x00\x00\x00") def test_as_sid_too_little_data_sub_authorities() -> None: with pytest.raises(AnsibleFilterError, match="Not enough data to unpack SID"): as_sid(b"\x01\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00") + + +@pytest.mark.parametrize( + "value, expected", + [ + ("Sue, Grabbit and Runn", "Sue\\, Grabbit and Runn"), + ("Before\rAfter", "Before\\0DAfter"), + ("Docs, Adatum", "Docs\\, Adatum"), + ("foo,bar", "foo\\,bar"), + ("foo+bar", "foo\\+bar"), + ('foo"bar', 'foo\\"bar'), + ("foo\\bar", "foo\\\\bar"), + ("foo<bar", "foo\\<bar"), + ("foo>bar", "foo\\>bar"), + ("foo;bar", "foo\\;bar"), + (" foo bar", "\\ foo bar"), + ("#foo bar", "\\#foo bar"), + ("# foo bar", "\\# foo bar"), + ("foo bar ", "foo bar\\ "), + ("foo bar ", "foo bar \\ "), + ("foo bar #", "foo bar #"), + ("foo\00bar", "foo\\00bar"), + ("foo\nbar", "foo\\0Abar"), + ("foo\rbar", "foo\\0Dbar"), + ("foo=bar", "foo\\3Dbar"), + ("foo/bar", "foo\\2Fbar"), + ], +) +def test_dn_escape(value: str, expected: str) -> None: + actual = dn_escape(value) + assert actual == expected + + +@pytest.mark.parametrize( + "value, expected", + [ + ( + "", + [], + ), + ( + "CN=foo", + [["CN", "foo"]], + ), + ( + r"CN=foo,DC=bar", + [["CN", "foo"], ["DC", "bar"]], + ), + ( + r"CN=foo, DC=bar", + [["CN", "foo"], ["DC", "bar"]], + ), + ( + r"CN=foo , DC=bar", + [["CN", "foo"], ["DC", "bar"]], + ), + ( + r"CN=foo , DC=bar", + [["CN", "foo"], ["DC", "bar"]], + ), + ( + r"UID=jsmith,DC=example,DC=net", + [["UID", "jsmith"], ["DC", "example"], ["DC", "net"]], + ), + ( + r"OU=Sales+CN=J. Smith,DC=example,DC=net", + [["OU", "Sales", "CN", "J. Smith"], ["DC", "example"], ["DC", "net"]], + ), + ( + r"OU=Sales + CN=J. Smith,DC=example,DC=net", + [["OU", "Sales", "CN", "J. Smith"], ["DC", "example"], ["DC", "net"]], + ), + ( + r"CN=James \"Jim\" Smith\, III,DC=example,DC=net", + [["CN", 'James "Jim" Smith, III'], ["DC", "example"], ["DC", "net"]], + ), + ( + r"CN=Before\0dAfter,DC=example,DC=net", + [["CN", "Before\rAfter"], ["DC", "example"], ["DC", "net"]], + ), + ( + r"1.3.6.1.4.1.1466.0=#FE04024869", + [["1.3.6.1.4.1.1466.0", "\udcfe\x04\x02Hi"]], + ), + ( + r"1.3.6.1.4.1.1466.0 = #FE04024869", + [["1.3.6.1.4.1.1466.0", "\udcfe\x04\x02Hi"]], + ), + ( + r"CN=Lu\C4\8Di\C4\87", + [["CN", "Lučić"]], + ), + ], +) +def test_parse_dn(value: str, expected: t.List[str]) -> None: + actual = parse_dn(value) + + assert actual == expected + + +def test_parse_dn_invalid_attribute_type() -> None: + expected = "Expecting attribute type in RDN entry from 'foo_invalid=test'" + with pytest.raises(AnsibleFilterError, match=expected): + parse_dn("foo_invalid=test") + + +def test_parse_dn_no_attribute_value() -> None: + expected = "Expecting attribute value in RDN entry from ''" + with pytest.raises(AnsibleFilterError, match=expected): + parse_dn("foo=") + + +def test_parse_dn_no_value_after_ava_delimiter() -> None: + expected = "Expecting attribute type in RDN entry from ''" + with pytest.raises(AnsibleFilterError, match=expected): + parse_dn("foo=bar+") + + +def test_parse_dn_unescaped_hash() -> None: + expected = "Found leading # for attribute value but does not match hexstring format at '#bar'" + with pytest.raises(AnsibleFilterError, match=expected): + parse_dn("foo=#bar") + + +@pytest.mark.parametrize("c", ["\00", '"', ";", "<", ">"]) +def test_parse_dn_unescaped_special_char(c: str) -> None: + expected = f"Found unescaped character '{c}' in attribute value at '{c}value'" + with pytest.raises(AnsibleFilterError, match=expected): + parse_dn(f"foo=test{c}value") + + +def test_parse_dn_invalid_attr_value_escape() -> None: + expected = r"Found invalid escape sequence in attribute value at '\\1z" + with pytest.raises(AnsibleFilterError, match=expected): + parse_dn("foo=bar \\1z") |