pyxattr: Simplify tests.
[Samba.git] / source4 / scripting / python / samba / tests / xattr.py
blob7854f84c8a286289ddf27976f3db600df2c5f265
1 #!/usr/bin/python
3 # Unix SMB/CIFS implementation. Tests for xattr manipulation
4 # Copyright (C) Matthieu Patou <mat@matws.net> 2009
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.
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.
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/>.
20 import samba.xattr_native, samba.xattr_tdb
21 from samba.dcerpc import xattr
22 from samba.ndr import ndr_pack
23 from testtools import TestCase, TestSkipped
24 import random
25 import os
27 class XattrTests(TestCase):
29 def test_set_xattr_native(self):
30 if not samba.xattr_native.is_xattr_supported():
31 raise TestSkipped()
32 random.seed()
33 path = os.environ['SELFTEST_PREFIX']
34 tempf = os.path.join(path,"pytests"+str(int(100000*random.random())))
35 ntacl = xattr.NTACL()
36 ntacl.version = 1
37 open(tempf, 'w').write("empty")
38 try:
39 samba.xattr_native.wrap_setxattr(tempf, "user.unittests",
40 ndr_pack(ntacl))
41 except IOError:
42 raise TestSkipped("the filesystem where the tests are runned do not support XATTR")
43 os.unlink(tempf)
45 def test_set_and_get_native(self):
46 if not samba.xattr_native.is_xattr_supported():
47 raise TestSkipped()
48 random.seed()
49 path = os.environ['SELFTEST_PREFIX']
50 tempf = os.path.join(path,"pytests"+str(int(100000*random.random())))
51 reftxt = "this is a test"
52 open(tempf, 'w').write("empty")
53 try:
54 samba.xattr_native.wrap_setxattr(tempf, "user.unittests", reftxt)
55 text = samba.xattr_native.wrap_getxattr(tempf, "user.unittests")
56 self.assertEquals(text, reftxt)
57 except IOError:
58 raise TestSkipped("the filesystem where the tests are runned do not support XATTR")
59 os.unlink(tempf)
61 def test_set_xattr_tdb(self):
62 path = os.environ['SELFTEST_PREFIX']
63 random.seed()
64 tempf = os.path.join(path, "pytests"+str(int(100000*random.random())))
65 ntacl = xattr.NTACL()
66 ntacl.version = 1
67 open(tempf, 'w').write("empty")
68 try:
69 samba.xattr_tdb.wrap_setxattr(os.path.join(path, "eadb.tdb"),
70 tempf, "user.unittests", ndr_pack(ntacl))
71 finally:
72 os.unlink(tempf)
73 os.unlink(os.path.join(path, "eadb.tdb"))
75 def test_set_tdb_not_open(self):
76 path = os.environ['SELFTEST_PREFIX']
77 random.seed()
78 tempf = os.path.join(path, "pytests"+str(int(100000*random.random())))
79 ntacl = xattr.NTACL()
80 ntacl.version = 1
81 open(tempf, 'w').write("empty")
82 try:
83 self.assertRaises(IOError, samba.xattr_tdb.wrap_setxattr,
84 os.path.join(path, "nonexistent","eadb.tdb"), tempf,
85 "user.unittests", ndr_pack(ntacl))
86 finally:
87 os.unlink(tempf)
89 def test_set_and_get_tdb(self):
90 path = os.environ['SELFTEST_PREFIX']
91 random.seed()
92 tempf = os.path.join(path, "pytests"+str(int(100000*random.random())))
93 reftxt = "this is a test"
94 open(tempf, 'w').write("empty")
95 try:
96 samba.xattr_tdb.wrap_setxattr(os.path.join(path, "eadb.tdb"),
97 tempf, "user.unittests", reftxt)
98 text = samba.xattr_tdb.wrap_getxattr(
99 os.path.join(path, "eadb.tdb"), tempf, "user.unittests")
100 self.assertEquals(text, reftxt)
101 finally:
102 os.unlink(tempf)
103 os.unlink(os.path.join(path, "eadb.tdb"))