s4-python: add more unit tests for xattr manipulation in python
[Samba/ekacnet.git] / source4 / scripting / python / samba / tests / xattr.py
blob9beff93933e4e78c743ac03680917663ac59d0ee
1 #!/usr/bin/python
3 # Unix SMB/CIFS implementation. Tests for xattr manipulation
4 # Copyright (C) Matthieu Patou <mat@matws.net> 2009
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 import samba.xattr_native, samba.xattr_tdb
21 from samba.dcerpc import xattr
22 from samba.ndr import ndr_pack, ndr_unpack
23 from unittest import TestCase
24 import random
25 import os
26 import tdb
28 class XattrTests(TestCase):
30 def test_set_xattr_native(self):
31 if samba.xattr_native.is_xattr_supported():
32 random.seed()
33 path=os.environ['SELFTEST_PREFIX']
34 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
35 ntacl=xattr.NTACL()
36 ntacl.version = 1
37 open(tempf, 'w').write("empty")
38 samba.xattr_native.wrap_setxattr(tempf,"user.unittests",ndr_pack(ntacl))
39 os.unlink(tempf)
41 def test_set_and_get_native(self):
42 if samba.xattr_native.is_xattr_supported():
43 random.seed()
44 path=os.environ['SELFTEST_PREFIX']
45 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
46 reftxt="this is a test"
47 open(tempf, 'w').write("empty")
48 samba.xattr_native.wrap_setxattr(tempf,"user.unittests",reftxt)
49 text = samba.xattr_native.wrap_getxattr(tempf,"user.unittests")
50 self.assertEquals(text,reftxt)
51 os.unlink(tempf)
53 def test_set_xattr_tdb(self):
54 path=os.environ['SELFTEST_PREFIX']
55 eadb=tdb.Tdb(os.path.join(path,"eadb.tdb"), 50000, tdb.DEFAULT, os.O_CREAT|os.O_RDWR)
56 random.seed()
57 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
58 ntacl=xattr.NTACL()
59 ntacl.version = 1
60 open(tempf, 'w').write("empty")
61 samba.xattr_tdb.wrap_setxattr(eadb,tempf,"user.unittests",ndr_pack(ntacl))
62 os.unlink(tempf)
63 os.unlink(os.path.join(path,"eadb.tdb"))
65 def test_set_and_get_tdb(self):
66 path=os.environ['SELFTEST_PREFIX']
67 eadb=tdb.Tdb(os.path.join(path,"eadb.tdb"), 50000, tdb.DEFAULT, os.O_CREAT|os.O_RDWR)
68 random.seed()
69 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
70 reftxt="this is a test"
71 open(tempf, 'w').write("empty")
72 samba.xattr_tdb.wrap_setxattr(eadb,tempf,"user.unittests",reftxt)
73 text = samba.xattr_tdb.wrap_getxattr(eadb,tempf,"user.unittests")
74 self.assertEquals(text,reftxt)
75 os.unlink(tempf)
76 os.unlink(os.path.join(path,"eadb.tdb"))