Fix of a bug in the security.descriptor.as_sddl() method
[ira/wip.git] / source4 / libcli / security / tests / bindings.py
1 #!/usr/bin/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     def setUp(self):
50         self.descriptor = security.descriptor()
51
52     def test_from_sddl(self):
53         desc = security.descriptor.from_sddl("O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)", security.dom_sid("S-2-0-0"))
54         self.assertEquals(desc.group_sid, security.dom_sid('S-2-0-0-512'))
55         self.assertEquals(desc.owner_sid, security.dom_sid('S-1-5-32-548'))
56         self.assertEquals(desc.revision, 1)
57         self.assertEquals(desc.sacl, None)
58         self.assertEquals(desc.type, 0x8004)
59
60     def test_as_sddl(self):
61         text = "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
62         dom = security.dom_sid("S-2-0-0")
63         desc1 = security.descriptor.from_sddl(text, dom)
64         desc2 = security.descriptor.from_sddl(desc1.as_sddl(dom), dom)
65         self.assertEquals(desc1.group_sid, desc2.group_sid)
66         self.assertEquals(desc1.owner_sid, desc2.owner_sid)
67         self.assertEquals(desc1.sacl, desc2.sacl)
68         self.assertEquals(desc1.type, desc2.type)
69
70     def test_as_sddl_no_domainsid(self):
71         dom = security.dom_sid("S-2-0-0")
72         text = "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
73         desc1 = security.descriptor.from_sddl(text, dom)
74         desc2 = security.descriptor.from_sddl(desc1.as_sddl(), dom)
75         self.assertEquals(desc1.group_sid, desc2.group_sid)
76         self.assertEquals(desc1.owner_sid, desc2.owner_sid)
77         self.assertEquals(desc1.sacl, desc2.sacl)
78         self.assertEquals(desc1.type, desc2.type)
79
80     def test_domsid_nodomsid_as_sddl(self):
81         dom = security.dom_sid("S-2-0-0")
82         text = "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
83         desc1 = security.descriptor.from_sddl(text, dom)
84         self.assertNotEqual(desc1.as_sddl(), desc1.as_sddl(dom))
85
86
87 class DomSidTests(unittest.TestCase):
88     def test_parse_sid(self):
89         sid = security.dom_sid("S-1-5-21")
90         self.assertEquals("S-1-5-21", str(sid))
91
92     def test_sid_equal(self):
93         sid1 = security.dom_sid("S-1-5-21")
94         sid2 = security.dom_sid("S-1-5-21")
95         self.assertEquals(sid1, sid1)
96         self.assertEquals(sid1, sid2)
97
98     def test_random(self):
99         sid = security.random_sid()
100         self.assertTrue(str(sid).startswith("S-1-5-21-"))
101
102     def test_repr(self):
103         sid = security.random_sid()
104         self.assertTrue(repr(sid).startswith("dom_sid('S-1-5-21-"))
105
106
107 class PrivilegeTests(unittest.TestCase):
108     def test_privilege_name(self):
109         self.assertEquals("SeShutdownPrivilege", security.privilege_name(security.SEC_PRIV_SHUTDOWN))
110
111     def test_privilege_id(self):
112         self.assertEquals(security.SEC_PRIV_SHUTDOWN, security.privilege_id("SeShutdownPrivilege"))
113