PEP8: fix E401: multiple imports on one line
[kai/samba-autobuild/.git] / python / samba / tests / xattr.py
1 # Unix SMB/CIFS implementation. Tests for xattr manipulation
2 # Copyright (C) Matthieu Patou <mat@matws.net> 2009
3 #
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.
8 #
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.
13 #
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/>.
16 #
17
18 """Tests for samba.xattr_native and samba.xattr_tdb."""
19
20 import samba.xattr_native
21 import samba.xattr_tdb
22 import samba.posix_eadb
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     SkipTest,
28     TestCase,
29     TestCaseInTempDir,
30 )
31 import random
32 import shutil
33 import os
34
35
36 class XattrTests(TestCase):
37
38     def _tmpfilename(self):
39         random.seed()
40         path = os.environ['SELFTEST_PREFIX']
41         return os.path.join(path, "pytests" +str(int(100000 * random.random())))
42
43     def _eadbpath(self):
44         return os.path.join(os.environ['SELFTEST_PREFIX'], "eadb.tdb")
45
46     def test_set_xattr_native(self):
47         if not samba.xattr_native.is_xattr_supported():
48             raise SkipTest()
49         ntacl = xattr.NTACL()
50         ntacl.version = 1
51         tempf = self._tmpfilename()
52         open(tempf, 'w').write("empty")
53         try:
54             samba.xattr_native.wrap_setxattr(tempf, "user.unittests",
55                                              ndr_pack(ntacl))
56         except IOError:
57             raise SkipTest("the filesystem where the tests are runned do not support XATTR")
58         os.unlink(tempf)
59
60     def test_set_and_get_native(self):
61         if not samba.xattr_native.is_xattr_supported():
62             raise SkipTest()
63         tempf = self._tmpfilename()
64         reftxt = b"this is a test"
65         open(tempf, 'w').write("empty")
66         try:
67             samba.xattr_native.wrap_setxattr(tempf, "user.unittests", reftxt)
68             text = samba.xattr_native.wrap_getxattr(tempf, "user.unittests")
69             self.assertEquals(text, reftxt)
70         except IOError:
71             raise SkipTest("the filesystem where the tests are runned do not support XATTR")
72         os.unlink(tempf)
73
74     def test_set_xattr_tdb(self):
75         tempf = self._tmpfilename()
76         eadb_path = self._eadbpath()
77         ntacl = xattr.NTACL()
78         ntacl.version = 1
79         open(tempf, 'w').write("empty")
80         try:
81             samba.xattr_tdb.wrap_setxattr(eadb_path,
82                                           tempf, "user.unittests", ndr_pack(ntacl))
83         finally:
84             os.unlink(tempf)
85         os.unlink(eadb_path)
86
87     def test_set_tdb_not_open(self):
88         tempf = self._tmpfilename()
89         ntacl = xattr.NTACL()
90         ntacl.version = 1
91         open(tempf, 'w').write("empty")
92         try:
93             self.assertRaises(IOError, samba.xattr_tdb.wrap_setxattr,
94                               os.path.join("nonexistent", "eadb.tdb"), tempf,
95                               "user.unittests", ndr_pack(ntacl))
96         finally:
97             os.unlink(tempf)
98
99     def test_set_and_get_tdb(self):
100         tempf = self._tmpfilename()
101         eadb_path = self._eadbpath()
102         reftxt = b"this is a test"
103         open(tempf, 'w').write("empty")
104         try:
105             samba.xattr_tdb.wrap_setxattr(eadb_path, tempf, "user.unittests",
106                                           reftxt)
107             text = samba.xattr_tdb.wrap_getxattr(eadb_path, tempf,
108                                                  "user.unittests")
109             self.assertEquals(text, reftxt)
110         finally:
111             os.unlink(tempf)
112         os.unlink(eadb_path)
113
114     def test_set_posix_eadb(self):
115         tempf = self._tmpfilename()
116         eadb_path = self._eadbpath()
117         ntacl = xattr.NTACL()
118         ntacl.version = 1
119         open(tempf, 'w').write("empty")
120         try:
121             samba.posix_eadb.wrap_setxattr(eadb_path,
122                                            tempf, "user.unittests", ndr_pack(ntacl))
123         finally:
124             os.unlink(tempf)
125         os.unlink(eadb_path)
126
127     def test_set_and_get_posix_eadb(self):
128         tempf = self._tmpfilename()
129         eadb_path = self._eadbpath()
130         reftxt = b"this is a test"
131         open(tempf, 'w').write("empty")
132         try:
133             samba.posix_eadb.wrap_setxattr(eadb_path, tempf, "user.unittests",
134                                            reftxt)
135             text = samba.posix_eadb.wrap_getxattr(eadb_path, tempf,
136                                                   "user.unittests")
137             self.assertEquals(text, reftxt)
138         finally:
139             os.unlink(tempf)
140         os.unlink(eadb_path)
141
142
143 class TestCopyTreeWithXattrs(TestCaseInTempDir):
144
145     def test_simple(self):
146         os.chdir(self.tempdir)
147         os.mkdir("a")
148         os.mkdir("a/b")
149         os.mkdir("a/b/c")
150         f = open('a/b/c/d', 'w')
151         try:
152             f.write("foo")
153         finally:
154             f.close()
155         copytree_with_xattrs("a", "b")
156         shutil.rmtree("a")
157         shutil.rmtree("b")