ca25afbdcd76a9e0a8ec69e7026e62bf6c3c6a00
[amitay/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, samba.xattr_tdb, samba.posix_eadb
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     SkipTest,
26     TestCase,
27     TestCaseInTempDir,
28 )
29 import random
30 import shutil
31 import os
32
33 class XattrTests(TestCase):
34
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())))
39
40     def _eadbpath(self):
41         return os.path.join(os.environ['SELFTEST_PREFIX'], "eadb.tdb")
42
43     def test_set_xattr_native(self):
44         if not samba.xattr_native.is_xattr_supported():
45             raise SkipTest()
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 SkipTest("the filesystem where the tests are runned do not support XATTR")
55         os.unlink(tempf)
56
57     def test_set_and_get_native(self):
58         if not samba.xattr_native.is_xattr_supported():
59             raise SkipTest()
60         tempf = self._tmpfilename()
61         reftxt = b"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 SkipTest("the filesystem where the tests are runned do not support XATTR")
69         os.unlink(tempf)
70
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)
83
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)
95
96     def test_set_and_get_tdb(self):
97         tempf = self._tmpfilename()
98         eadb_path = self._eadbpath()
99         reftxt = b"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)
110
111     def test_set_posix_eadb(self):
112         tempf = self._tmpfilename()
113         eadb_path = self._eadbpath()
114         ntacl = xattr.NTACL()
115         ntacl.version = 1
116         open(tempf, 'w').write("empty")
117         try:
118             samba.posix_eadb.wrap_setxattr(eadb_path,
119                                            tempf, "user.unittests", ndr_pack(ntacl))
120         finally:
121             os.unlink(tempf)
122         os.unlink(eadb_path)
123
124     def test_set_and_get_posix_eadb(self):
125         tempf = self._tmpfilename()
126         eadb_path = self._eadbpath()
127         reftxt = b"this is a test"
128         open(tempf, 'w').write("empty")
129         try:
130             samba.posix_eadb.wrap_setxattr(eadb_path, tempf, "user.unittests",
131                                            reftxt)
132             text = samba.posix_eadb.wrap_getxattr(eadb_path, tempf,
133                                                   "user.unittests")
134             self.assertEquals(text, reftxt)
135         finally:
136             os.unlink(tempf)
137         os.unlink(eadb_path)
138
139
140 class TestCopyTreeWithXattrs(TestCaseInTempDir):
141
142     def test_simple(self):
143         os.chdir(self.tempdir)
144         os.mkdir("a")
145         os.mkdir("a/b")
146         os.mkdir("a/b/c")
147         f = open('a/b/c/d', 'w')
148         try:
149             f.write("foo")
150         finally:
151             f.close()
152         copytree_with_xattrs("a", "b")
153         shutil.rmtree("a")
154         shutil.rmtree("b")