| 1 | import os |
| 2 | import uuid |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | from bonsai.active_directory import ACL, SID |
| 7 | from bonsai.active_directory.acl import ACE, ACEFlag, ACEType, ACERight, ACLRevision |
| 8 | |
| 9 | from conftest import test_ace |
| 10 | |
| 11 | ACE_BINARY_TEST_DATA = b"\x05\nH\x00\x07\x00\x00\x00\x03\x00\x00\x00\x01\xc9u\xc9\xealoK\x83\x19\xd6\x7fED\x95\x06\x14\xcc(H7\x14\xbcE\x9b\x07\xado\x01^_(\x01\x05\x00\x00\x00\x00\x00\x05\x15\x00\x00\x00\x15\xb3LK\xd2\xfb\x90s\xc2\xdf9\xb9\\\x04\x00\x00" |
| 12 | |
| 13 | |
| 14 | def test_ace_from_binary(): |
| 15 | """Test ACE's from_binary method.""" |
| 16 | with pytest.raises(TypeError): |
| 17 | _ = ACE.from_binary(0) |
| 18 | with pytest.raises(TypeError): |
| 19 | _ = ACE.from_binary("INVALID") |
| 20 | with pytest.raises(ValueError): |
| 21 | _ = ACE.from_binary(b"\x05\nH\x00\x07\x00\x00\x00") |
| 22 | ace = ACE.from_binary(ACE_BINARY_TEST_DATA) |
| 23 | assert ace.type == ACEType.ACCESS_ALLOWED_OBJECT |
| 24 | assert ace.type.is_object_type |
| 25 | assert ace.type.is_access_allowed |
| 26 | assert not ace.type.is_access_denied |
| 27 | assert ace.mask == 7 |
| 28 | assert ace.rights == { |
| 29 | ACERight.DS_CREATE_CHILD, |
| 30 | ACERight.DS_DELETE_CHILD, |
| 31 | ACERight.ACTRL_DS_LIST, |
| 32 | } |
| 33 | assert ace.size == len(ACE_BINARY_TEST_DATA) |
| 34 | assert ace.flags == {ACEFlag.INHERIT_ONLY, ACEFlag.CONTAINER_INHERIT} |
| 35 | assert ace.is_inherited is False |
| 36 | assert sum(ace.flags) == 10 |
| 37 | assert ace.trustee_sid == "S-1-5-21-1263317781-1938881490-3107577794-1116" |
| 38 | assert ace.object_type == uuid.UUID("c975c901-6cea-4b6f-8319-d67f45449506") |
| 39 | assert ace.inherited_object_type == uuid.UUID( |
| 40 | "4828cc14-1437-45bc-9b07-ad6f015e5f28" |
| 41 | ) |
| 42 | assert ace.application_data == b"" |
| 43 | |
| 44 | |
| 45 | def test_ace_to_binary(): |
| 46 | """Test ACE's from_binary method.""" |
| 47 | assert ACE.from_binary(ACE_BINARY_TEST_DATA).to_binary() == ACE_BINARY_TEST_DATA |
| 48 | |
| 49 | |
| 50 | def test_ace_set_access_rights(test_ace: ACE): |
| 51 | """Test setting ACE's access rights.""" |
| 52 | assert test_ace.rights == {ACERight.GENERIC_READ} |
| 53 | with pytest.raises(TypeError): |
| 54 | test_ace.set_access_rights(None) |
| 55 | with pytest.raises(TypeError): |
| 56 | test_ace.set_access_rights("GENERIC_READ") |
| 57 | with pytest.raises(TypeError): |
| 58 | test_ace.set_access_rights({ACERight.GENERIC_READ, 12}) |
| 59 | with pytest.raises(ValueError): |
| 60 | test_ace.set_access_rights(-12) |
| 61 | test_ace.set_access_rights(268435464) |
| 62 | assert test_ace.mask == 268435464 |
| 63 | assert test_ace.rights == {ACERight.GENERIC_ALL, ACERight.DS_SELF} |
| 64 | test_ace.mask = 0 |
| 65 | assert test_ace.rights == set() |
| 66 | new_rights = {ACERight.GENERIC_WRITE, ACERight.ACTRL_DS_LIST} |
| 67 | test_ace.rights = new_rights |
| 68 | assert test_ace.rights == new_rights |
| 69 | assert test_ace.mask == sum(new_rights) |
| 70 | |
| 71 | |
| 72 | def test_ace_set_application_data(test_ace: ACE): |
| 73 | """Test setting ACE's application data.""" |
| 74 | assert test_ace.application_data == b"" |
| 75 | with pytest.raises(TypeError): |
| 76 | test_ace.set_application_data("string") |
| 77 | test_ace.set_application_data(b"\x00\x10") |
| 78 | assert test_ace.application_data == b"\x00\x10" |
| 79 | test_ace.application_data = b"\x11\x10" |
| 80 | assert test_ace.application_data == b"\x11\x10" |
| 81 | |
| 82 | |
| 83 | def test_ace_set_flags(test_ace: ACE): |
| 84 | """Test setting ACE's flags.""" |
| 85 | assert test_ace.flags == {ACEFlag.INHERITED} |
| 86 | with pytest.raises(TypeError): |
| 87 | test_ace.set_flags("string") |
| 88 | with pytest.raises(TypeError): |
| 89 | test_ace.set_flags(42) |
| 90 | test_ace.set_flags({ACEFlag.INHERIT_ONLY}) |
| 91 | assert test_ace.flags == {ACEFlag.INHERIT_ONLY} |
| 92 | test_ace.flags = {ACEFlag.OBJECT_INHERIT, ACEFlag.SUCCESSFUL_ACCESS} |
| 93 | assert test_ace.flags == {ACEFlag.OBJECT_INHERIT, ACEFlag.SUCCESSFUL_ACCESS} |
| 94 | |
| 95 | |
| 96 | def test_ace_set_object_type(test_ace: ACE): |
| 97 | """Test setting ACE's object type.""" |
| 98 | assert test_ace.object_type is None |
| 99 | with pytest.raises(TypeError): |
| 100 | test_ace.set_object_type("string") |
| 101 | with pytest.raises(TypeError): |
| 102 | test_ace.set_object_type(42) |
| 103 | obj_id = uuid.UUID("c975c901-6cea-4b6f-8319-d67f45449506") |
| 104 | test_ace.set_object_type(obj_id) |
| 105 | assert test_ace.object_type == obj_id |
| 106 | test_ace.object_type = None |
| 107 | assert test_ace.object_type is None |
| 108 | |
| 109 | |
| 110 | def test_ace_set_inherited_object_type(test_ace: ACE): |
| 111 | """Test setting ACE's inherited object type.""" |
| 112 | assert test_ace.inherited_object_type == uuid.UUID( |
| 113 | "c975c901-6cea-4b6f-8319-d67f45449506" |
| 114 | ) |
| 115 | with pytest.raises(TypeError): |
| 116 | test_ace.set_inherited_object_type("string") |
| 117 | with pytest.raises(TypeError): |
| 118 | test_ace.set_inherited_object_type(42) |
| 119 | obj_id = uuid.UUID("c975c901-6cea-4b6f-8319-d67f45449507") |
| 120 | test_ace.set_inherited_object_type(obj_id) |
| 121 | assert test_ace.inherited_object_type == obj_id |
| 122 | test_ace.inherited_object_type = None |
| 123 | assert test_ace.inherited_object_type is None |
| 124 | |
| 125 | |
| 126 | def test_ace_set_type(test_ace: ACE): |
| 127 | """Test setting ACE's type.""" |
| 128 | assert test_ace.type == ACEType.ACCESS_ALLOWED |
| 129 | assert test_ace.type.is_access_allowed |
| 130 | with pytest.raises(TypeError): |
| 131 | test_ace.set_type("string") |
| 132 | with pytest.raises(TypeError): |
| 133 | test_ace.set_type(42) |
| 134 | test_ace.set_type(ACEType.ACCESS_DENIED) |
| 135 | assert test_ace.type == ACEType.ACCESS_DENIED |
| 136 | assert test_ace.type.is_access_denied |
| 137 | test_ace.type = ACEType.ACCESS_DENIED_CALLBACK |
| 138 | assert test_ace.type == ACEType.ACCESS_DENIED_CALLBACK |
| 139 | assert test_ace.type.is_access_denied |
| 140 | |
| 141 | |
| 142 | def test_ace_set_trustee_sid(test_ace: ACE): |
| 143 | """Test setting ACE's trustee SID.""" |
| 144 | assert test_ace.trustee_sid == SID("S-1-1-0") |
| 145 | with pytest.raises(TypeError): |
| 146 | test_ace.set_trustee_sid("string") |
| 147 | with pytest.raises(TypeError): |
| 148 | test_ace.set_trustee_sid(42) |
| 149 | test_ace.set_trustee_sid(SID("S-1-5-10")) |
| 150 | assert test_ace.trustee_sid == SID("S-1-5-10") |
| 151 | test_ace.trustee_sid = SID("S-1-5-11") |
| 152 | assert test_ace.trustee_sid == SID("S-1-5-11") |
| 153 | |
| 154 | |
| 155 | def test_eq(test_ace: ACE): |
| 156 | """Test ACE's __eq__ method.""" |
| 157 | assert not test_ace == 21 |
| 158 | other_ace = ACE( |
| 159 | ACEType.ACCESS_ALLOWED, |
| 160 | {ACEFlag.INHERITED}, |
| 161 | 0x10000000, |
| 162 | SID("S-1-1-0"), |
| 163 | None, |
| 164 | uuid.UUID("c975c901-6cea-4b6f-8319-d67f45449506"), |
| 165 | b"", |
| 166 | ) |
| 167 | assert not test_ace == other_ace |
| 168 | other_ace = ACE( |
| 169 | ACEType.ACCESS_ALLOWED, |
| 170 | {ACEFlag.INHERITED}, |
| 171 | 0x80000000, |
| 172 | SID("S-1-1-0"), |
| 173 | None, |
| 174 | uuid.UUID("c975c901-6cea-4b6f-8319-d67f45449506"), |
| 175 | b"\x01", |
| 176 | ) |
| 177 | assert not test_ace == other_ace |
| 178 | other_ace = ACE.from_binary(ACE_BINARY_TEST_DATA) |
| 179 | assert not test_ace == other_ace |
| 180 | other_ace = ACE( |
| 181 | ACEType.ACCESS_ALLOWED, |
| 182 | {ACEFlag.INHERITED}, |
| 183 | 0x80000000, |
| 184 | SID("S-1-1-0"), |
| 185 | None, |
| 186 | uuid.UUID("c975c901-6cea-4b6f-8319-d67f45449506"), |
| 187 | b"", |
| 188 | ) |
| 189 | assert test_ace == other_ace |
| 190 | |
| 191 | |
| 192 | def test_acl_from_binary(): |
| 193 | """Test ACL's from_binary method.""" |
| 194 | with pytest.raises(TypeError): |
| 195 | _ = ACL.from_binary(True) |
| 196 | with pytest.raises(TypeError): |
| 197 | _ = ACL.from_binary("INVALID") |
| 198 | with pytest.raises(ValueError): |
| 199 | _ = ACL.from_binary( |
| 200 | b"\x05\nH\x00\x07\x00\x00\x00\x03\x00\x00\x00\x01\xc9u\xc9\xealoK\x83\x19\xd6\x7fED\x95\x06\x14\xcc(H7\x14\xbcE\x9b\x07\xado\x01^_(" |
| 201 | ) |
| 202 | curdir = os.path.abspath(os.path.dirname(__file__)) |
| 203 | with open(os.path.join(curdir, "testenv", "acl-sample.bin"), "rb") as data: |
| 204 | input_data = data.read() |
| 205 | acl = ACL.from_binary(input_data) |
| 206 | assert acl.size == len(input_data) |
| 207 | assert acl.revision == ACLRevision.ACL_REVISION_DS |
| 208 | assert len(acl.aces) == 85 |
| 209 | assert all(isinstance(ace, ACE) for ace in acl.aces) |
| 210 | assert acl.aces[0].type == ACEType.ACCESS_ALLOWED_OBJECT |
| 211 | assert acl.aces[0].inherited_object_type == uuid.UUID( |
| 212 | "4828cc14-1437-45bc-9b07-ad6f015e5f28" |
| 213 | ) |
| 214 | assert acl.aces[-1].type == ACEType.ACCESS_ALLOWED |
| 215 | assert ( |
| 216 | acl.aces[0].trustee_sid == "S-1-5-21-1263317781-1938881490-3107577794-1116" |
| 217 | ) |
| 218 | assert acl.aces[-1].trustee_sid == "S-1-5-18" |
| 219 | |
| 220 | |
| 221 | def test_acl_to_binary(): |
| 222 | """Test ACL's from_binary method.""" |
| 223 | curdir = os.path.abspath(os.path.dirname(__file__)) |
| 224 | with open(os.path.join(curdir, "testenv", "acl-sample.bin"), "rb") as data: |
| 225 | input_data = data.read() |
| 226 | assert ACL.from_binary(input_data).to_binary() == input_data |
| 227 | |
| 228 | |
| 229 | def test_acl_set_aces(test_ace): |
| 230 | """Test setting ACL's aces list.""" |
| 231 | test_acl = ACL(ACLRevision.ACL_REVISION, []) |
| 232 | assert test_acl.aces == [] |
| 233 | with pytest.raises(TypeError): |
| 234 | test_acl.set_aces([1, 2, 3]) |
| 235 | with pytest.raises(TypeError): |
| 236 | test_acl.set_aces([test_ace, 3]) |
| 237 | test_acl.set_aces([test_ace]) |
| 238 | assert test_acl.aces == [test_ace] |
| 239 | test_acl.aces = [] |
| 240 | assert test_acl.aces == [] |
| 241 | |
| 242 | |
| 243 | def test_str(): |
| 244 | """Test __str__ method.""" |
| 245 | ace = ACE.from_binary(ACE_BINARY_TEST_DATA) |
| 246 | assert ( |
| 247 | str(ace) |
| 248 | == "(OA;CIIO;CCDCLC;c975c901-6cea-4b6f-8319-d67f45449506;4828cc14-1437-45bc-9b07-ad6f015e5f28;S-1-5-21-1263317781-1938881490-3107577794-1116)" |
| 249 | ) |