| 1 | import pytest |
| 2 | |
| 3 | from bonsai.active_directory import SID |
| 4 | |
| 5 | |
| 6 | def test_create_from_bytes_le(): |
| 7 | """Test create SID from binary.""" |
| 8 | with pytest.raises(ValueError): |
| 9 | _ = SID(bytes_le=b"NOT-A-SID") |
| 10 | with pytest.raises(ValueError): |
| 11 | _ = SID( |
| 12 | bytes_le=b"\x01\x05\x00\x00\x00\x05\x15\x00\x00\x00\xdc\xf4\xdc;\x83=+F\x82\xa6(\x00\x02\x00\x00" |
| 13 | ) |
| 14 | with pytest.raises(TypeError): |
| 15 | _ = SID(bytes_le=True) |
| 16 | with pytest.raises(TypeError): |
| 17 | _ = SID(bytes_le="S-1-1-1") |
| 18 | input_data = b"\x01\x05\x00\x00\x00\x00\x00\x05\x15\x00\x00\x00\xdc\xf4\xdc;\x83=+F\x82\x8b\xa6(\x00\x02\x00\x00" |
| 19 | expected_sid = "S-1-5-21-1004336348-1177238915-682003330-512" |
| 20 | sid = SID(bytes_le=input_data) |
| 21 | assert sid.revision == 1 |
| 22 | assert sid.identifier_authority == 5 |
| 23 | assert sid.subauthorities == (21, 1004336348, 1177238915, 682003330, 512) |
| 24 | assert sid == expected_sid |
| 25 | |
| 26 | |
| 27 | def test_create_from_str_rep(): |
| 28 | """Test create SID from string.""" |
| 29 | with pytest.raises(ValueError): |
| 30 | _ = SID("NOT-A-SID") |
| 31 | with pytest.raises(ValueError): |
| 32 | _ = SID("S--") |
| 33 | with pytest.raises(ValueError): |
| 34 | _ = SID(str_rep="S-1") |
| 35 | with pytest.raises(ValueError): |
| 36 | _ = SID("S-1-NOT-SID") |
| 37 | with pytest.raises(ValueError): |
| 38 | _ = SID("NOT") |
| 39 | with pytest.raises(TypeError): |
| 40 | _ = SID(str_rep=0) |
| 41 | with pytest.raises(TypeError): |
| 42 | _ = SID(b"S-1-1-1") |
| 43 | sid = SID("S-1-5-21-3623811015-3361044348-30300820-1013") |
| 44 | assert sid.revision == 1 |
| 45 | assert sid.identifier_authority == 5 |
| 46 | assert sid.subauthorities == (21, 3623811015, 3361044348, 30300820, 1013) |
| 47 | everyone_sid = SID("S-1-1-0") |
| 48 | assert everyone_sid.identifier_authority == 1 |
| 49 | assert everyone_sid.subauthorities == (0,) |
| 50 | |
| 51 | |
| 52 | def test_bytes_le(): |
| 53 | """Test bytes_le property.""" |
| 54 | expected_bytes = b"\x01\x05\x00\x00\x00\x00\x00\x05\x15\x00\x00\x00\xdc\xf4\xdc;\x83=+F\x82\x8b\xa6(\x00\x02\x00\x00" |
| 55 | test_sid = SID("S-1-5-21-1004336348-1177238915-682003330-512") |
| 56 | assert test_sid.bytes_le == expected_bytes |
| 57 | assert ( |
| 58 | SID(bytes_le=test_sid.bytes_le).identifier_authority |
| 59 | == test_sid.identifier_authority |
| 60 | ) |
| 61 | assert SID(bytes_le=test_sid.bytes_le).subauthorities == test_sid.subauthorities |
| 62 | |
| 63 | |
| 64 | def test_str(): |
| 65 | """Test __str__ method.""" |
| 66 | test_sid = "S-1-5-21-3623811015-3361044348-30300820-1013" |
| 67 | assert str(SID(test_sid)) == test_sid |
| 68 | test_sid = "S-1-1-0" |
| 69 | assert str(SID(test_sid)) == test_sid |
| 70 | |
| 71 | |
| 72 | def test_repr(): |
| 73 | """Test __repr__ method.""" |
| 74 | sid = SID("S-1-1-0") |
| 75 | assert SID.__name__ in repr(sid) |
| 76 | assert "S-1-1-0" in repr(sid) |
| 77 | |
| 78 | |
| 79 | def test_eq(): |
| 80 | """Test __eq__ method.""" |
| 81 | sid0 = SID("S-1-1-0") |
| 82 | sid1 = SID("S-1-1-0") |
| 83 | assert sid0 == sid1 |
| 84 | assert sid0 != True |
| 85 | assert sid0 == "S-1-1-0" |
| 86 | |
| 87 | |
| 88 | def test_hash(): |
| 89 | """Test __hash__ method.""" |
| 90 | test_sid = "S-1-5-21-3623811015-3361044348-30300820-1013" |
| 91 | sid0 = SID(test_sid) |
| 92 | assert hash(sid0) == hash(test_sid) |
| 93 | assert set((sid0, SID(test_sid))) == {sid0} |
| 94 | |
| 95 | |
| 96 | @pytest.mark.parametrize( |
| 97 | "sid, alias", |
| 98 | [ |
| 99 | (SID("S-1-1-0"), "WD"), |
| 100 | (SID("S-1-5-32-559"), "LU"), |
| 101 | (SID("S-1-5-21-3623811015-3361044348-30300820-500"), "LA"), |
| 102 | (SID("S-1-5-21-3623811015-3361044348-30300820-520"), "PA"), |
| 103 | (SID("S-1-5-21-3623811015-3361044348-30300820-1013"), None), |
| 104 | (SID("S-1-5-34-3623811015-3361044348-30300820-500"), None), |
| 105 | ], |
| 106 | ) |
| 107 | def test_sddl_alias(sid, alias): |
| 108 | """Test sddl_alias property.""" |
| 109 | assert sid.sddl_alias == alias |
| 110 | |
| 111 | |
| 112 | @pytest.mark.parametrize( |
| 113 | "sid", |
| 114 | [ |
| 115 | SID("S-1-1-0"), |
| 116 | SID("S-1-5-32-559"), |
| 117 | SID("S-1-5-34-3623811015-3361044348-30300820-500"), |
| 118 | ], |
| 119 | ) |
| 120 | def test_size(sid): |
| 121 | """Test size property.""" |
| 122 | assert sid.size == len(sid.bytes_le) |