samba.tests.blackbox.samba_tool_drs: Remove unused import.
[samba.git] / source4 / scripting / python / samba / tests / xattr.py
1 #!/usr/bin/env python
2
3 # Unix SMB/CIFS implementation. Tests for xattr manipulation
4 # Copyright (C) Matthieu Patou <mat@matws.net> 2009
5 #
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.
10 #
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.
15 #
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/>.
18 #
19
20 """Tests for samba.xattr_native and samba.xattr_tdb."""
21
22 import samba.xattr_native, samba.xattr_tdb
23 from samba.xattr import copytree_with_xattrs
24 from samba.dcerpc import xattr
25 from samba.ndr import ndr_pack
26 from samba.tests import (
27     TestCase,
28     TestCaseInTempDir,
29     TestSkipped,
30     )
31 import random
32 import shutil
33 import os
34
35 class XattrTests(TestCase):
36
37     def _tmpfilename(self):
38         random.seed()
39         path = os.environ['SELFTEST_PREFIX']
40         return os.path.join(path, "pytests"+str(int(100000*random.random())))
41
42     def _eadbpath(self):
43         return os.path.join(os.environ['SELFTEST_PREFIX'], "eadb.tdb")
44
45     def test_set_xattr_native(self):
46         if not samba.xattr_native.is_xattr_supported():
47             raise TestSkipped()
48         ntacl = xattr.NTACL()
49         ntacl.version = 1
50         tempf = self._tmpfilename()
51         open(tempf, 'w').write("empty")
52         try:
53             samba.xattr_native.wrap_setxattr(tempf, "user.unittests", 
54                 ndr_pack(ntacl))
55         except IOError:
56             raise TestSkipped("the filesystem where the tests are runned do not support XATTR")
57         os.unlink(tempf)
58
59     def test_set_and_get_native(self):
60         if not samba.xattr_native.is_xattr_supported():
61             raise TestSkipped()
62         tempf = self._tmpfilename()
63         reftxt = "this is a test"
64         open(tempf, 'w').write("empty")
65         try:
66             samba.xattr_native.wrap_setxattr(tempf, "user.unittests", reftxt)
67             text = samba.xattr_native.wrap_getxattr(tempf, "user.unittests")
68             self.assertEquals(text, reftxt)
69         except IOError:
70             raise TestSkipped("the filesystem where the tests are runned do not support XATTR")
71         os.unlink(tempf)
72
73     def test_set_xattr_tdb(self):
74         tempf = self._tmpfilename()
75         eadb_path = self._eadbpath()
76         ntacl = xattr.NTACL()
77         ntacl.version = 1
78         open(tempf, 'w').write("empty")
79         try:
80             samba.xattr_tdb.wrap_setxattr(eadb_path,
81                 tempf, "user.unittests", ndr_pack(ntacl))
82         finally:
83             os.unlink(tempf)
84         os.unlink(eadb_path)
85
86     def test_set_tdb_not_open(self):
87         tempf = self._tmpfilename()
88         ntacl = xattr.NTACL()
89         ntacl.version = 1
90         open(tempf, 'w').write("empty")
91         try:
92             self.assertRaises(IOError, samba.xattr_tdb.wrap_setxattr,
93                     os.path.join("nonexistent", "eadb.tdb"), tempf,
94                     "user.unittests", ndr_pack(ntacl))
95         finally:
96             os.unlink(tempf)
97
98     def test_set_and_get_tdb(self):
99         tempf = self._tmpfilename()
100         eadb_path = self._eadbpath()
101         reftxt = "this is a test"
102         open(tempf, 'w').write("empty")
103         try:
104             samba.xattr_tdb.wrap_setxattr(eadb_path, tempf, "user.unittests",
105                 reftxt)
106             text = samba.xattr_tdb.wrap_getxattr(eadb_path, tempf,
107                 "user.unittests")
108             self.assertEquals(text, reftxt)
109         finally:
110             os.unlink(tempf)
111         os.unlink(eadb_path)
112
113
114 class TestCopyTreeWithXattrs(TestCaseInTempDir):
115
116     def test_simple(self):
117         os.chdir(self.tempdir)
118         os.mkdir("a")
119         os.mkdir("a/b")
120         os.mkdir("a/b/c")
121         f = open('a/b/c/d', 'w')
122         try:
123             f.write("foo")
124         finally:
125             f.close()
126         copytree_with_xattrs("a", "b")
127         shutil.rmtree("a")
128         shutil.rmtree("b")