Move python modules from source4/scripting/python/ to python/.
[Samba.git] / python / samba / tests / xattr.py
blob89add2845663a06ea6657fa2482d73c59233b72d
1 # Unix SMB/CIFS implementation. Tests for xattr manipulation
2 # Copyright (C) Matthieu Patou <mat@matws.net> 2009
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 """Tests for samba.xattr_native and samba.xattr_tdb."""
20 import samba.xattr_native, samba.xattr_tdb
21 from samba.xattr import copytree_with_xattrs
22 from samba.dcerpc import xattr
23 from samba.ndr import ndr_pack
24 from samba.tests import (
25 TestCase,
26 TestCaseInTempDir,
27 TestSkipped,
29 import random
30 import shutil
31 import os
33 class XattrTests(TestCase):
35 def _tmpfilename(self):
36 random.seed()
37 path = os.environ['SELFTEST_PREFIX']
38 return os.path.join(path, "pytests"+str(int(100000*random.random())))
40 def _eadbpath(self):
41 return os.path.join(os.environ['SELFTEST_PREFIX'], "eadb.tdb")
43 def test_set_xattr_native(self):
44 if not samba.xattr_native.is_xattr_supported():
45 raise TestSkipped()
46 ntacl = xattr.NTACL()
47 ntacl.version = 1
48 tempf = self._tmpfilename()
49 open(tempf, 'w').write("empty")
50 try:
51 samba.xattr_native.wrap_setxattr(tempf, "user.unittests",
52 ndr_pack(ntacl))
53 except IOError:
54 raise TestSkipped("the filesystem where the tests are runned do not support XATTR")
55 os.unlink(tempf)
57 def test_set_and_get_native(self):
58 if not samba.xattr_native.is_xattr_supported():
59 raise TestSkipped()
60 tempf = self._tmpfilename()
61 reftxt = "this is a test"
62 open(tempf, 'w').write("empty")
63 try:
64 samba.xattr_native.wrap_setxattr(tempf, "user.unittests", reftxt)
65 text = samba.xattr_native.wrap_getxattr(tempf, "user.unittests")
66 self.assertEquals(text, reftxt)
67 except IOError:
68 raise TestSkipped("the filesystem where the tests are runned do not support XATTR")
69 os.unlink(tempf)
71 def test_set_xattr_tdb(self):
72 tempf = self._tmpfilename()
73 eadb_path = self._eadbpath()
74 ntacl = xattr.NTACL()
75 ntacl.version = 1
76 open(tempf, 'w').write("empty")
77 try:
78 samba.xattr_tdb.wrap_setxattr(eadb_path,
79 tempf, "user.unittests", ndr_pack(ntacl))
80 finally:
81 os.unlink(tempf)
82 os.unlink(eadb_path)
84 def test_set_tdb_not_open(self):
85 tempf = self._tmpfilename()
86 ntacl = xattr.NTACL()
87 ntacl.version = 1
88 open(tempf, 'w').write("empty")
89 try:
90 self.assertRaises(IOError, samba.xattr_tdb.wrap_setxattr,
91 os.path.join("nonexistent", "eadb.tdb"), tempf,
92 "user.unittests", ndr_pack(ntacl))
93 finally:
94 os.unlink(tempf)
96 def test_set_and_get_tdb(self):
97 tempf = self._tmpfilename()
98 eadb_path = self._eadbpath()
99 reftxt = "this is a test"
100 open(tempf, 'w').write("empty")
101 try:
102 samba.xattr_tdb.wrap_setxattr(eadb_path, tempf, "user.unittests",
103 reftxt)
104 text = samba.xattr_tdb.wrap_getxattr(eadb_path, tempf,
105 "user.unittests")
106 self.assertEquals(text, reftxt)
107 finally:
108 os.unlink(tempf)
109 os.unlink(eadb_path)
112 class TestCopyTreeWithXattrs(TestCaseInTempDir):
114 def test_simple(self):
115 os.chdir(self.tempdir)
116 os.mkdir("a")
117 os.mkdir("a/b")
118 os.mkdir("a/b/c")
119 f = open('a/b/c/d', 'w')
120 try:
121 f.write("foo")
122 finally:
123 f.close()
124 copytree_with_xattrs("a", "b")
125 shutil.rmtree("a")
126 shutil.rmtree("b")