3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """Tests for samba.dcerpc.security."""
23 from samba
.dcerpc
import security
25 class SecurityTokenTests(samba
.tests
.TestCase
):
28 super(SecurityTokenTests
, self
).setUp()
29 self
.token
= security
.token()
31 def test_is_system(self
):
32 self
.assertFalse(self
.token
.is_system())
34 def test_is_anonymous(self
):
35 self
.assertFalse(self
.token
.is_anonymous())
37 def test_has_builtin_administrators(self
):
38 self
.assertFalse(self
.token
.has_builtin_administrators())
40 def test_has_nt_authenticated_users(self
):
41 self
.assertFalse(self
.token
.has_nt_authenticated_users())
43 def test_has_priv(self
):
44 self
.assertFalse(self
.token
.has_privilege(security
.SEC_PRIV_SHUTDOWN
))
46 def test_set_priv(self
):
47 self
.assertFalse(self
.token
.has_privilege(security
.SEC_PRIV_SHUTDOWN
))
48 self
.assertFalse(self
.token
.set_privilege(security
.SEC_PRIV_SHUTDOWN
))
49 self
.assertTrue(self
.token
.has_privilege(security
.SEC_PRIV_SHUTDOWN
))
52 class SecurityDescriptorTests(samba
.tests
.TestCase
):
55 super(SecurityDescriptorTests
, self
).setUp()
56 self
.descriptor
= security
.descriptor()
58 def test_from_sddl(self
):
59 desc
= security
.descriptor
.from_sddl("O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)", security
.dom_sid("S-2-0-0"))
60 self
.assertEquals(desc
.group_sid
, security
.dom_sid('S-2-0-0-512'))
61 self
.assertEquals(desc
.owner_sid
, security
.dom_sid('S-1-5-32-548'))
62 self
.assertEquals(desc
.revision
, 1)
63 self
.assertEquals(desc
.sacl
, None)
64 self
.assertEquals(desc
.type, 0x8004)
66 def test_from_sddl_invalidsddl(self
):
67 self
.assertRaises(TypeError,security
.descriptor
.from_sddl
, "foo",security
.dom_sid("S-2-0-0"))
69 def test_from_sddl_invalidtype1(self
):
70 self
.assertRaises(TypeError, security
.descriptor
.from_sddl
, security
.dom_sid('S-2-0-0-512'),security
.dom_sid("S-2-0-0"))
72 def test_from_sddl_invalidtype2(self
):
73 sddl
= "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
74 self
.assertRaises(TypeError, security
.descriptor
.from_sddl
, sddl
,
77 def test_as_sddl(self
):
78 text
= "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
79 dom
= security
.dom_sid("S-2-0-0")
80 desc1
= security
.descriptor
.from_sddl(text
, dom
)
81 desc2
= security
.descriptor
.from_sddl(desc1
.as_sddl(dom
), dom
)
82 self
.assertEquals(desc1
.group_sid
, desc2
.group_sid
)
83 self
.assertEquals(desc1
.owner_sid
, desc2
.owner_sid
)
84 self
.assertEquals(desc1
.sacl
, desc2
.sacl
)
85 self
.assertEquals(desc1
.type, desc2
.type)
87 def test_as_sddl_invalid(self
):
88 text
= "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
89 dom
= security
.dom_sid("S-2-0-0")
90 desc1
= security
.descriptor
.from_sddl(text
, dom
)
91 self
.assertRaises(TypeError, desc1
.as_sddl
,text
)
94 def test_as_sddl_no_domainsid(self
):
95 dom
= security
.dom_sid("S-2-0-0")
96 text
= "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
97 desc1
= security
.descriptor
.from_sddl(text
, dom
)
98 desc2
= security
.descriptor
.from_sddl(desc1
.as_sddl(), dom
)
99 self
.assertEquals(desc1
.group_sid
, desc2
.group_sid
)
100 self
.assertEquals(desc1
.owner_sid
, desc2
.owner_sid
)
101 self
.assertEquals(desc1
.sacl
, desc2
.sacl
)
102 self
.assertEquals(desc1
.type, desc2
.type)
104 def test_domsid_nodomsid_as_sddl(self
):
105 dom
= security
.dom_sid("S-2-0-0")
106 text
= "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
107 desc1
= security
.descriptor
.from_sddl(text
, dom
)
108 self
.assertNotEqual(desc1
.as_sddl(), desc1
.as_sddl(dom
))
110 def test_split(self
):
111 dom
= security
.dom_sid("S-2-0-7")
112 self
.assertEquals((security
.dom_sid("S-2-0"), 7), dom
.split())
115 class DomSidTests(samba
.tests
.TestCase
):
117 def test_parse_sid(self
):
118 sid
= security
.dom_sid("S-1-5-21")
119 self
.assertEquals("S-1-5-21", str(sid
))
121 def test_sid_equal(self
):
122 sid1
= security
.dom_sid("S-1-5-21")
123 sid2
= security
.dom_sid("S-1-5-21")
124 self
.assertEquals(sid1
, sid1
)
125 self
.assertEquals(sid1
, sid2
)
127 def test_random(self
):
128 sid
= security
.random_sid()
129 self
.assertTrue(str(sid
).startswith("S-1-5-21-"))
132 sid
= security
.random_sid()
133 self
.assertTrue(repr(sid
).startswith("dom_sid('S-1-5-21-"))
136 class PrivilegeTests(samba
.tests
.TestCase
):
138 def test_privilege_name(self
):
139 self
.assertEquals("SeShutdownPrivilege",
140 security
.privilege_name(security
.SEC_PRIV_SHUTDOWN
))
142 def test_privilege_id(self
):
143 self
.assertEquals(security
.SEC_PRIV_SHUTDOWN
,
144 security
.privilege_id("SeShutdownPrivilege"))