pyxattr: Simplify tests.
[Samba/eduardoll.git] / source4 / scripting / python / samba / tests / xattr.py
blob15f2566ff16adc56629f6ad8f51ad618e9885a68
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
27 import sys
29 class XattrTests(TestCase):
31 def test_set_xattr_native(self):
32 if samba.xattr_native.is_xattr_supported():
33 random.seed()
34 path=os.environ['SELFTEST_PREFIX']
35 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
36 ntacl=xattr.NTACL()
37 ntacl.version = 1
38 open(tempf, 'w').write("empty")
39 try:
40 samba.xattr_native.wrap_setxattr(tempf,"user.unittests",ndr_pack(ntacl))
41 except IOError:
42 print >>sys.stderr, "WARNING: the filesystem where the tests are runned do not support XATTR, tests SKIPED"
43 os.unlink(tempf)
44 else:
45 print >>sys.stderr, "WARNING: the filesystem where the tests are runned do not support XATTR, tests SKIPED"
47 def test_set_and_get_native(self):
48 if samba.xattr_native.is_xattr_supported():
49 random.seed()
50 path = os.environ['SELFTEST_PREFIX']
51 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
52 reftxt="this is a test"
53 open(tempf, 'w').write("empty")
54 try:
55 samba.xattr_native.wrap_setxattr(tempf,"user.unittests",reftxt)
56 text = samba.xattr_native.wrap_getxattr(tempf,"user.unittests")
57 self.assertEquals(text,reftxt)
58 except IOError:
59 print >>sys.stderr,"WARNING: the filesystem where the tests are runned do not support XATTR, tests SKIPED"
60 os.unlink(tempf)
61 else:
62 print >>sys.stderr,"WARNING: the filesystem where the tests are runned do not support XATTR, tests SKIPED"
64 def test_set_xattr_tdb(self):
65 path=os.environ['SELFTEST_PREFIX']
66 eadb=tdb.Tdb(os.path.join(path,"eadb.tdb"), 50000, tdb.DEFAULT, os.O_CREAT|os.O_RDWR)
67 random.seed()
68 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
69 ntacl=xattr.NTACL()
70 ntacl.version = 1
71 open(tempf, 'w').write("empty")
72 samba.xattr_tdb.wrap_setxattr(os.path.join(path,"eadb.tdb"),tempf,"user.unittests",ndr_pack(ntacl))
73 os.unlink(tempf)
74 os.unlink(os.path.join(path,"eadb.tdb"))
76 def test_set_tdb_not_open(self):
77 path=os.environ['SELFTEST_PREFIX']
78 eadb=tdb.Tdb(os.path.join(path,"eadb.tdb"), 50000, tdb.DEFAULT, os.O_CREAT|os.O_RDWR)
79 random.seed()
80 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
81 ntacl=xattr.NTACL()
82 ntacl.version = 1
83 open(tempf, 'w').write("empty")
84 self.assertRaises(IOError,samba.xattr_tdb.wrap_setxattr,os.path.join(path,os.path.join("nonexistent","eadb.tdb")),tempf,"user.unittests",ndr_pack(ntacl))
85 os.unlink(tempf)
87 def test_set_and_get_tdb(self):
88 path=os.environ['SELFTEST_PREFIX']
89 random.seed()
90 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
91 reftxt="this is a test"
92 open(tempf, 'w').write("empty")
93 samba.xattr_tdb.wrap_setxattr(os.path.join(path,"eadb.tdb"),tempf,"user.unittests",reftxt)
94 text = samba.xattr_tdb.wrap_getxattr(os.path.join(path,"eadb.tdb"),tempf,"user.unittests")
95 self.assertEquals(text,reftxt)
96 os.unlink(tempf)
97 os.unlink(os.path.join(path,"eadb.tdb"))