s4-tests: register new unit tests
[Samba/eduardoll.git] / source4 / scripting / python / samba / tests / ntacls.py
blob0add258a3e4c44e0ea5214c54baf13146ab34a70
1 #!/usr/bin/python
3 # Unix SMB/CIFS implementation. Tests for ntacls manipulation
4 # Copyright (C) Matthieu Patou <mat@matws.net> 2009-2010
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.
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.
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/>.
20 from samba.ntacls import setntacl, getntacl, XattrBackendError
21 from samba.dcerpc import xattr, security
22 from samba.param import LoadParm
23 from unittest import TestCase
24 import random
25 import os
27 class NtaclsTests(TestCase):
29 def test_setntacl(self):
30 random.seed()
31 lp=LoadParm()
32 path=None
33 try:
34 path=os.environ['SELFTEST_PREFIX']
35 except:
36 self.assertTrue(path!=None, "SELFTEST_PREFIX env not set")
37 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)"
38 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
39 ntacl=xattr.NTACL()
40 ntacl.version = 1
41 open(tempf, 'w').write("empty")
42 lp.set("posix:eadb",os.path.join(path,"eadbtest.tdb"))
43 setntacl(lp,tempf,acl,"S-1-5-21-2212615479-2695158682-2101375467")
44 os.unlink(tempf)
46 def test_setntacl_getntacl(self):
47 random.seed()
48 lp=LoadParm()
49 path=None
50 try:
51 path=os.environ['SELFTEST_PREFIX']
52 except:
53 self.assertTrue(path!=None, "SELFTEST_PREFIX env not set")
54 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)"
55 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
56 ntacl=xattr.NTACL()
57 ntacl.version = 1
58 open(tempf, 'w').write("empty")
59 lp.set("posix:eadb",os.path.join(path,"eadbtest.tdb"))
60 setntacl(lp,tempf,acl,"S-1-5-21-2212615479-2695158682-2101375467")
61 facl=getntacl(lp,tempf)
62 anysid=security.dom_sid(security.SID_NT_SELF)
63 self.assertEquals(facl.info.as_sddl(anysid),acl)
64 os.unlink(tempf)
66 def test_setntacl_getntacl_param(self):
67 random.seed()
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 path=None
71 try:
72 path=os.environ['SELFTEST_PREFIX']
73 except:
74 self.assertTrue(path!=None, "SELFTEST_PREFIX env not set")
75 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
76 ntacl=xattr.NTACL()
77 ntacl.version = 1
78 open(tempf, 'w').write("empty")
79 setntacl(lp,tempf,acl,"S-1-5-21-2212615479-2695158682-2101375467","tdb",os.path.join(path,"eadbtest.tdb"))
80 facl=getntacl(lp,tempf,"tdb",os.path.join(path,"eadbtest.tdb"))
81 domsid=security.dom_sid(security.SID_NT_SELF)
82 self.assertEquals(facl.info.as_sddl(domsid),acl)
83 os.unlink(tempf)
85 def test_setntacl_invalidbackend(self):
86 random.seed()
87 lp=LoadParm()
88 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)"
89 path=None
90 try:
91 path=os.environ['SELFTEST_PREFIX']
92 except:
93 self.assertTrue(path!=None, "SELFTEST_PREFIX env not set")
94 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
95 ntacl=xattr.NTACL()
96 ntacl.version = 1
97 open(tempf, 'w').write("empty")
98 self.assertRaises(XattrBackendError,setntacl,lp,tempf,acl,"S-1-5-21-2212615479-2695158682-2101375467","ttdb",os.path.join(path,"eadbtest.tdb"))
100 def test_setntacl_forcenative(self):
101 if os.getuid() != 0:
102 random.seed()
103 lp=LoadParm()
104 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)"
105 path=None
106 try:
107 path=os.environ['SELFTEST_PREFIX']
108 except:
109 self.assertTrue(path!=None, "SELFTEST_PREFIX env not set")
110 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
111 ntacl=xattr.NTACL()
112 ntacl.version = 1
113 open(tempf, 'w').write("empty")
114 lp.set("posix:eadb",os.path.join(path,"eadbtest.tdb"))
115 self.assertRaises(Exception,setntacl,lp,tempf,acl,"S-1-5-21-2212615479-2695158682-2101375467","native")
116 os.unlink(tempf)
117 else:
118 print "Running test as root, test skipped"