8cdf613078a4babb795aff4f31d8d41c77771144
[metze/samba/wip.git] / source4 / scripting / python / samba / tests / ntacls.py
1 # Unix SMB/CIFS implementation. Tests for ntacls manipulation
2 # Copyright (C) Matthieu Patou <mat@matws.net> 2009-2010
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.ntacls."""
19
20 from samba.ntacls import setntacl, getntacl, XattrBackendError
21 from samba.dcerpc import xattr, security
22 from samba.param import LoadParm
23 from samba.tests import TestCase, TestSkipped
24 import random
25 import os
26
27 class NtaclsTests(TestCase):
28
29     def test_setntacl(self):
30         random.seed()
31         lp = LoadParm()
32         path = os.environ['SELFTEST_PREFIX']
33         acl = "O:S-1-5-21-2212615479-2695158682-2101375467-512G:S-1-5-21-2212615479-2695158682-2101375467-513D:(A;OICI;0x001f01ff;;;S-1-5-21-2212615479-2695158682-2101375467-512)"
34         tempf = os.path.join(path,"pytests"+str(int(100000*random.random())))
35         open(tempf, 'w').write("empty")
36         lp.set("posix:eadb",os.path.join(path,"eadbtest.tdb"))
37         setntacl(lp, tempf, acl, "S-1-5-21-2212615479-2695158682-2101375467")
38         os.unlink(tempf)
39
40     def test_setntacl_getntacl(self):
41         random.seed()
42         lp = LoadParm()
43         path = None
44         path = os.environ['SELFTEST_PREFIX']
45         acl = "O:S-1-5-21-2212615479-2695158682-2101375467-512G:S-1-5-21-2212615479-2695158682-2101375467-513D:(A;OICI;0x001f01ff;;;S-1-5-21-2212615479-2695158682-2101375467-512)"
46         tempf = os.path.join(path,"pytests"+str(int(100000*random.random())))
47         open(tempf, 'w').write("empty")
48         lp.set("posix:eadb",os.path.join(path,"eadbtest.tdb"))
49         setntacl(lp,tempf,acl,"S-1-5-21-2212615479-2695158682-2101375467")
50         facl = getntacl(lp,tempf)
51         anysid = security.dom_sid(security.SID_NT_SELF)
52         self.assertEquals(facl.as_sddl(anysid),acl)
53         os.unlink(tempf)
54
55     def test_setntacl_getntacl_param(self):
56         random.seed()
57         lp = LoadParm()
58         acl = "O:S-1-5-21-2212615479-2695158682-2101375467-512G:S-1-5-21-2212615479-2695158682-2101375467-513D:(A;OICI;0x001f01ff;;;S-1-5-21-2212615479-2695158682-2101375467-512)"
59         path = os.environ['SELFTEST_PREFIX']
60         tempf = os.path.join(path,"pytests"+str(int(100000*random.random())))
61         open(tempf, 'w').write("empty")
62         setntacl(lp,tempf,acl,"S-1-5-21-2212615479-2695158682-2101375467","tdb",os.path.join(path,"eadbtest.tdb"))
63         facl=getntacl(lp,tempf,"tdb",os.path.join(path,"eadbtest.tdb"))
64         domsid=security.dom_sid(security.SID_NT_SELF)
65         self.assertEquals(facl.as_sddl(domsid),acl)
66         os.unlink(tempf)
67
68     def test_setntacl_invalidbackend(self):
69         random.seed()
70         lp = LoadParm()
71         acl = "O:S-1-5-21-2212615479-2695158682-2101375467-512G:S-1-5-21-2212615479-2695158682-2101375467-513D:(A;OICI;0x001f01ff;;;S-1-5-21-2212615479-2695158682-2101375467-512)"
72         path = os.environ['SELFTEST_PREFIX']
73         tempf = os.path.join(path,"pytests"+str(int(100000*random.random())))
74         open(tempf, 'w').write("empty")
75         self.assertRaises(XattrBackendError, setntacl, lp, tempf, acl, "S-1-5-21-2212615479-2695158682-2101375467","ttdb", os.path.join(path,"eadbtest.tdb"))
76
77     def test_setntacl_forcenative(self):
78         if os.getuid() == 0:
79             raise TestSkipped("Running test as root, test skipped")
80         random.seed()
81         lp = LoadParm()
82         acl = "O:S-1-5-21-2212615479-2695158682-2101375467-512G:S-1-5-21-2212615479-2695158682-2101375467-513D:(A;OICI;0x001f01ff;;;S-1-5-21-2212615479-2695158682-2101375467-512)"
83         path = os.environ['SELFTEST_PREFIX']
84         tempf = os.path.join(path,"pytests"+str(int(100000*random.random())))
85         open(tempf, 'w').write("empty")
86         lp.set("posix:eadb", os.path.join(path,"eadbtest.tdb"))
87         self.assertRaises(Exception, setntacl, lp, tempf ,acl,
88             "S-1-5-21-2212615479-2695158682-2101375467","native")
89         os.unlink(tempf)