python: use '#!/usr/bin/env python' to cope with varying install locations
[kai/samba.git] / source4 / libcli / security / tests / bindings.py
1 #!/usr/bin/env python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
5 #
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.
10 #
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.
15 #
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/>.
18 #
19
20 import unittest
21 from samba.dcerpc import security
22
23 class SecurityTokenTests(unittest.TestCase):
24     def setUp(self):
25         self.token = security.token()
26
27     def test_is_system(self):
28         self.assertFalse(self.token.is_system())
29
30     def test_is_anonymous(self):
31         self.assertFalse(self.token.is_anonymous())
32
33     def test_has_builtin_administrators(self):
34         self.assertFalse(self.token.has_builtin_administrators())
35
36     def test_has_nt_authenticated_users(self):
37         self.assertFalse(self.token.has_nt_authenticated_users())
38
39     def test_has_priv(self):
40         self.assertFalse(self.token.has_privilege(security.SEC_PRIV_SHUTDOWN))
41
42     def test_set_priv(self):
43         self.assertFalse(self.token.has_privilege(security.SEC_PRIV_SHUTDOWN))
44         self.assertFalse(self.token.set_privilege(security.SEC_PRIV_SHUTDOWN))
45         self.assertTrue(self.token.has_privilege(security.SEC_PRIV_SHUTDOWN))
46
47
48 class SecurityDescriptorTests(unittest.TestCase):
49
50     def setUp(self):
51         self.descriptor = security.descriptor()
52
53     def test_from_sddl(self):
54         desc = security.descriptor.from_sddl("O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)", security.dom_sid("S-2-0-0"))
55         self.assertEquals(desc.group_sid, security.dom_sid('S-2-0-0-512'))
56         self.assertEquals(desc.owner_sid, security.dom_sid('S-1-5-32-548'))
57         self.assertEquals(desc.revision, 1)
58         self.assertEquals(desc.sacl, None)
59         self.assertEquals(desc.type, 0x8004)
60
61     def test_from_sddl_invalidsddl(self):
62         self.assertRaises(TypeError,security.descriptor.from_sddl, "foo",security.dom_sid("S-2-0-0"))
63
64     def test_from_sddl_invalidtype1(self):
65         self.assertRaises(TypeError,security.descriptor.from_sddl, security.dom_sid('S-2-0-0-512'),security.dom_sid("S-2-0-0"))
66
67     def test_from_sddl_invalidtype1(self):
68         sddl = "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
69         self.assertRaises(TypeError,security.descriptor.from_sddl, sddl,"S-2-0-0")
70
71     def test_as_sddl(self):
72         text = "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
73         dom = security.dom_sid("S-2-0-0")
74         desc1 = security.descriptor.from_sddl(text, dom)
75         desc2 = security.descriptor.from_sddl(desc1.as_sddl(dom), dom)
76         self.assertEquals(desc1.group_sid, desc2.group_sid)
77         self.assertEquals(desc1.owner_sid, desc2.owner_sid)
78         self.assertEquals(desc1.sacl, desc2.sacl)
79         self.assertEquals(desc1.type, desc2.type)
80
81     def test_as_sddl_invalid(self):
82         text = "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
83         dom = security.dom_sid("S-2-0-0")
84         desc1 = security.descriptor.from_sddl(text, dom)
85         self.assertRaises(TypeError, desc1.as_sddl,text)
86
87
88     def test_as_sddl_no_domainsid(self):
89         dom = security.dom_sid("S-2-0-0")
90         text = "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
91         desc1 = security.descriptor.from_sddl(text, dom)
92         desc2 = security.descriptor.from_sddl(desc1.as_sddl(), dom)
93         self.assertEquals(desc1.group_sid, desc2.group_sid)
94         self.assertEquals(desc1.owner_sid, desc2.owner_sid)
95         self.assertEquals(desc1.sacl, desc2.sacl)
96         self.assertEquals(desc1.type, desc2.type)
97
98     def test_domsid_nodomsid_as_sddl(self):
99         dom = security.dom_sid("S-2-0-0")
100         text = "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
101         desc1 = security.descriptor.from_sddl(text, dom)
102         self.assertNotEqual(desc1.as_sddl(), desc1.as_sddl(dom))
103
104     def test_split(self):
105         dom = security.dom_sid("S-2-0-7")
106         self.assertEquals((security.dom_sid("S-2-0"), 7), dom.split())
107
108
109 class DomSidTests(unittest.TestCase):
110     def test_parse_sid(self):
111         sid = security.dom_sid("S-1-5-21")
112         self.assertEquals("S-1-5-21", str(sid))
113
114     def test_sid_equal(self):
115         sid1 = security.dom_sid("S-1-5-21")
116         sid2 = security.dom_sid("S-1-5-21")
117         self.assertEquals(sid1, sid1)
118         self.assertEquals(sid1, sid2)
119
120     def test_random(self):
121         sid = security.random_sid()
122         self.assertTrue(str(sid).startswith("S-1-5-21-"))
123
124     def test_repr(self):
125         sid = security.random_sid()
126         self.assertTrue(repr(sid).startswith("dom_sid('S-1-5-21-"))
127
128
129 class PrivilegeTests(unittest.TestCase):
130     def test_privilege_name(self):
131         self.assertEquals("SeShutdownPrivilege", security.privilege_name(security.SEC_PRIV_SHUTDOWN))
132
133     def test_privilege_id(self):
134         self.assertEquals(security.SEC_PRIV_SHUTDOWN, security.privilege_id("SeShutdownPrivilege"))
135