Move python modules from source4/scripting/python/ to python/.
[samba.git] / 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 # Copyright (C) Andrew Bartlett 2012
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 """Tests for samba.ntacls."""
20
21 from samba.ntacls import setntacl, getntacl, XattrBackendError
22 from samba.dcerpc import xattr, security
23 from samba.param import LoadParm
24 from samba.tests import TestCaseInTempDir, TestSkipped
25 import random
26 import os
27
28 class NtaclsTests(TestCaseInTempDir):
29
30     def test_setntacl(self):
31         lp = LoadParm()
32         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)"
33         open(self.tempf, 'w').write("empty")
34         lp.set("posix:eadb",os.path.join(self.tempdir,"eadbtest.tdb"))
35         setntacl(lp, self.tempf, acl, "S-1-5-21-2212615479-2695158682-2101375467")
36         os.unlink(os.path.join(self.tempdir,"eadbtest.tdb"))
37
38     def test_setntacl_getntacl(self):
39         lp = LoadParm()
40         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)"
41         open(self.tempf, 'w').write("empty")
42         lp.set("posix:eadb",os.path.join(self.tempdir,"eadbtest.tdb"))
43         setntacl(lp,self.tempf,acl,"S-1-5-21-2212615479-2695158682-2101375467")
44         facl = getntacl(lp,self.tempf)
45         anysid = security.dom_sid(security.SID_NT_SELF)
46         self.assertEquals(facl.as_sddl(anysid),acl)
47         os.unlink(os.path.join(self.tempdir,"eadbtest.tdb"))
48
49     def test_setntacl_getntacl_param(self):
50         lp = LoadParm()
51         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)"
52         open(self.tempf, 'w').write("empty")
53         setntacl(lp,self.tempf,acl,"S-1-5-21-2212615479-2695158682-2101375467","tdb",os.path.join(self.tempdir,"eadbtest.tdb"))
54         facl=getntacl(lp,self.tempf,"tdb",os.path.join(self.tempdir,"eadbtest.tdb"))
55         domsid=security.dom_sid(security.SID_NT_SELF)
56         self.assertEquals(facl.as_sddl(domsid),acl)
57         os.unlink(os.path.join(self.tempdir,"eadbtest.tdb"))
58
59     def test_setntacl_invalidbackend(self):
60         lp = LoadParm()
61         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)"
62         open(self.tempf, 'w').write("empty")
63         self.assertRaises(XattrBackendError, setntacl, lp, self.tempf, acl, "S-1-5-21-2212615479-2695158682-2101375467","ttdb", os.path.join(self.tempdir,"eadbtest.tdb"))
64
65     def test_setntacl_forcenative(self):
66         if os.getuid() == 0:
67             raise TestSkipped("Running test as root, test skipped")
68         lp = LoadParm()
69         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)"
70         open(self.tempf, 'w').write("empty")
71         lp.set("posix:eadb", os.path.join(self.tempdir,"eadbtest.tdb"))
72         self.assertRaises(Exception, setntacl, lp, self.tempf ,acl,
73             "S-1-5-21-2212615479-2695158682-2101375467","native")
74
75
76     def setUp(self):
77         super(NtaclsTests, self).setUp()
78         self.tempf = os.path.join(self.tempdir, "test")
79         open(self.tempf, 'w').write("empty")
80
81     def tearDown(self):
82         os.unlink(self.tempf)
83         super(NtaclsTests, self).tearDown()