s4-tests: register new unit tests
[Samba/ekacnet.git] / source4 / scripting / python / samba / tests / xattr.py
blobfcc2a3a2e4214507362b92408254c0c8614bf68c
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=None
35 try:
36 path=os.environ['SELFTEST_PREFIX']
37 except:
38 self.assertTrue(path!=None, "SELFTEST_PREFIX env not set")
39 if path:
40 path=os.environ['SELFTEST_PREFIX']
41 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
42 ntacl=xattr.NTACL()
43 ntacl.version = 1
44 open(tempf, 'w').write("empty")
45 try:
46 samba.xattr_native.wrap_setxattr(tempf,"user.unittests",ndr_pack(ntacl))
47 except IOError:
48 print >>sys.stderr, "WARNING: the filesystem where the tests are runned do not support XATTR, tests SKIPED"
49 os.unlink(tempf)
50 else:
51 print >>sys.stderr, "WARNING: the filesystem where the tests are runned do not support XATTR, tests SKIPED"
54 def test_set_and_get_native(self):
55 if samba.xattr_native.is_xattr_supported():
56 random.seed()
57 path=None
58 try:
59 path=os.environ['SELFTEST_PREFIX']
60 except:
61 self.assertTrue(path!=None, "SELFTEST_PREFIX env not set")
62 if path:
63 path=os.environ['SELFTEST_PREFIX']
64 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
65 reftxt="this is a test"
66 open(tempf, 'w').write("empty")
67 try:
68 samba.xattr_native.wrap_setxattr(tempf,"user.unittests",reftxt)
69 text = samba.xattr_native.wrap_getxattr(tempf,"user.unittests")
70 self.assertEquals(text,reftxt)
71 except IOError:
72 print >>sys.stderr,"WARNING: the filesystem where the tests are runned do not support XATTR, tests SKIPED"
73 os.unlink(tempf)
74 else:
75 print >>sys.stderr,"WARNING: the filesystem where the tests are runned do not support XATTR, tests SKIPED"
77 def test_set_xattr_tdb(self):
78 path=None
79 try:
80 path=os.environ['SELFTEST_PREFIX']
81 except:
82 self.assertTrue(path!=None, "SELFTEST_PREFIX env not set")
83 if path:
84 eadb=tdb.Tdb(os.path.join(path,"eadb.tdb"), 50000, tdb.DEFAULT, os.O_CREAT|os.O_RDWR)
85 random.seed()
86 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
87 ntacl=xattr.NTACL()
88 ntacl.version = 1
89 open(tempf, 'w').write("empty")
90 samba.xattr_tdb.wrap_setxattr(os.path.join(path,"eadb.tdb"),tempf,"user.unittests",ndr_pack(ntacl))
91 os.unlink(tempf)
92 os.unlink(os.path.join(path,"eadb.tdb"))
94 def test_set_tdb_not_open(self):
95 path=None
96 try:
97 path=os.environ['SELFTEST_PREFIX']
98 except:
99 self.assertTrue(path!=None, "SELFTEST_PREFIX env not set")
100 if path:
101 eadb=tdb.Tdb(os.path.join(path,"eadb.tdb"), 50000, tdb.DEFAULT, os.O_CREAT|os.O_RDWR)
102 random.seed()
103 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
104 ntacl=xattr.NTACL()
105 ntacl.version = 1
106 open(tempf, 'w').write("empty")
107 self.assertRaises(IOError,samba.xattr_tdb.wrap_setxattr,os.path.join(path,os.path.join("nonexistent","eadb.tdb")),tempf,"user.unittests",ndr_pack(ntacl))
108 os.unlink(tempf)
110 def test_set_and_get_tdb(self):
111 path=None
112 try:
113 path=os.environ['SELFTEST_PREFIX']
114 except:
115 self.assertTrue(path!=None, "SELFTEST_PREFIX env not set")
116 if path:
117 random.seed()
118 tempf=os.path.join(path,"pytests"+str(int(100000*random.random())))
119 reftxt="this is a test"
120 open(tempf, 'w').write("empty")
121 samba.xattr_tdb.wrap_setxattr(os.path.join(path,"eadb.tdb"),tempf,"user.unittests",reftxt)
122 text = samba.xattr_tdb.wrap_getxattr(os.path.join(path,"eadb.tdb"),tempf,"user.unittests")
123 self.assertEquals(text,reftxt)
124 os.unlink(tempf)
125 os.unlink(os.path.join(path,"eadb.tdb"))