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