python:samba:tests: Fix code spelling
[samba.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 run does not "
58                            "support XATTR")
59         os.unlink(tempf)
60
61     def test_set_and_get_native(self):
62         if not samba.xattr_native.is_xattr_supported():
63             raise SkipTest()
64         tempf = self._tmpfilename()
65         reftxt = b"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.assertEqual(text, reftxt)
71         except IOError:
72             raise SkipTest("the filesystem where the tests are run does not "
73                            "support XATTR")
74         os.unlink(tempf)
75
76     def test_set_xattr_tdb(self):
77         tempf = self._tmpfilename()
78         eadb_path = self._eadbpath()
79         ntacl = xattr.NTACL()
80         ntacl.version = 1
81         open(tempf, 'w').write("empty")
82         try:
83             samba.xattr_tdb.wrap_setxattr(eadb_path,
84                                           tempf, "user.unittests", ndr_pack(ntacl))
85         finally:
86             os.unlink(tempf)
87         os.unlink(eadb_path)
88
89     def test_set_tdb_not_open(self):
90         tempf = self._tmpfilename()
91         ntacl = xattr.NTACL()
92         ntacl.version = 1
93         open(tempf, 'w').write("empty")
94         try:
95             self.assertRaises(IOError, samba.xattr_tdb.wrap_setxattr,
96                               os.path.join("nonexistent", "eadb.tdb"), tempf,
97                               "user.unittests", ndr_pack(ntacl))
98         finally:
99             os.unlink(tempf)
100
101     def test_set_and_get_tdb(self):
102         tempf = self._tmpfilename()
103         eadb_path = self._eadbpath()
104         reftxt = b"this is a test"
105         open(tempf, 'w').write("empty")
106         try:
107             samba.xattr_tdb.wrap_setxattr(eadb_path, tempf, "user.unittests",
108                                           reftxt)
109             text = samba.xattr_tdb.wrap_getxattr(eadb_path, tempf,
110                                                  "user.unittests")
111             self.assertEqual(text, reftxt)
112         finally:
113             os.unlink(tempf)
114         os.unlink(eadb_path)
115
116     def test_set_posix_eadb(self):
117         tempf = self._tmpfilename()
118         eadb_path = self._eadbpath()
119         ntacl = xattr.NTACL()
120         ntacl.version = 1
121         open(tempf, 'w').write("empty")
122         try:
123             samba.posix_eadb.wrap_setxattr(eadb_path,
124                                            tempf, "user.unittests", ndr_pack(ntacl))
125         finally:
126             os.unlink(tempf)
127         os.unlink(eadb_path)
128
129     def test_set_and_get_posix_eadb(self):
130         tempf = self._tmpfilename()
131         eadb_path = self._eadbpath()
132         reftxt = b"this is a test"
133         open(tempf, 'w').write("empty")
134         try:
135             samba.posix_eadb.wrap_setxattr(eadb_path, tempf, "user.unittests",
136                                            reftxt)
137             text = samba.posix_eadb.wrap_getxattr(eadb_path, tempf,
138                                                   "user.unittests")
139             self.assertEqual(text, reftxt)
140         finally:
141             os.unlink(tempf)
142         os.unlink(eadb_path)
143
144
145 class TestCopyTreeWithXattrs(TestCaseInTempDir):
146
147     def test_simple(self):
148         os.chdir(self.tempdir)
149         os.mkdir("a")
150         os.mkdir("a/b")
151         os.mkdir("a/b/c")
152         f = open('a/b/c/d', 'w')
153         try:
154             f.write("foo")
155         finally:
156             f.close()
157         copytree_with_xattrs("a", "b")
158         shutil.rmtree("a")
159         shutil.rmtree("b")