1 # -*- coding: utf-8 -*-
2 # Unix SMB/CIFS implementation. Tests for smb manipulation
3 # Copyright (C) David Mulder <dmulder@suse.com> 2018
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 from samba import NTSTATUSError
24 from samba.ntstatus import (NT_STATUS_OBJECT_NAME_NOT_FOUND,
25 NT_STATUS_OBJECT_PATH_NOT_FOUND)
26 from samba.samba3 import libsmb_samba_internal
27 from samba.samba3 import param as s3param
29 PY3 = sys.version_info[0] == 3
30 addom = 'addom.samba.example.com/'
31 test_contents = 'abcd' * 256
32 utf_contents = u'Süßigkeiten Äpfel ' * 128
33 test_literal_bytes_embed_nulls = b'\xff\xfe\x14\x61\x00\x00\x62\x63\x64' * 256
34 binary_contents = b'\xff\xfe'
35 binary_contents = binary_contents + "Hello cruel world of python3".encode('utf8') * 128
36 test_dir = os.path.join(addom, 'testing_%d' % random.randint(0, 0xFFFF))
37 test_file = os.path.join(test_dir, 'testing').replace('/', '\\')
40 class SMBTests(samba.tests.TestCase):
42 super(SMBTests, self).setUp()
43 self.server = os.environ["SERVER"]
44 creds = self.insta_creds(template=self.get_credentials())
45 self.conn = smb.SMB(self.server,
47 lp=self.get_loadparm(),
50 # temporarily create a 2nd SMB connection for migrating the py-bindings
51 lp = s3param.get_context()
52 lp.load(os.getenv("SMB_CONF_PATH"))
53 self.smb_conn = libsmb_samba_internal.Conn(self.server, "sysvol",
56 self.conn.mkdir(test_dir)
59 super(SMBTests, self).tearDown()
61 self.conn.deltree(test_dir)
66 # check a basic listing returns the items we expect
67 ls = [f['name'] for f in self.conn.list(addom)]
68 self.assertIn('scripts', ls,
69 msg='"scripts" directory not found in sysvol')
70 self.assertIn('Policies', ls,
71 msg='"Policies" directory not found in sysvol')
72 self.assertNotIn('..', ls,
73 msg='Parent (..) found in directory listing')
74 self.assertNotIn('.', ls,
75 msg='Current dir (.) found in directory listing')
77 # using a '*' mask should be the same as using no mask
78 ls_wildcard = [f['name'] for f in self.conn.list(addom, "*")]
79 self.assertEqual(ls, ls_wildcard)
81 # applying a mask should only return items that match that mask
82 ls_pol = [f['name'] for f in self.conn.list(addom, "Pol*")]
83 expected = ["Policies"]
84 self.assertEqual(ls_pol, expected)
86 # each item in the listing is a has with expected keys
87 expected_keys = ['attrib', 'mtime', 'name', 'short_name', 'size']
88 for item in self.conn.list(addom):
89 for key in expected_keys:
90 self.assertIn(key, item,
91 msg="Key '%s' not in listing '%s'" % (key, item))
93 def test_deltree(self):
94 """The smb.deltree API should delete files and sub-dirs"""
95 # create some test sub-dirs
99 for subdir in ["subdir-X", "subdir-Y", "subdir-Z"]:
100 path = self.make_sysvol_path(cur_dir, subdir)
101 self.conn.mkdir(path)
102 dirpaths.append(path)
105 # create another empty dir just for kicks
106 path = self.make_sysvol_path(cur_dir, "another")
107 self.conn.mkdir(path)
108 empty_dirs.append(path)
110 # create some files in these directories
112 for subdir in dirpaths:
113 for i in range(1, 4):
114 contents = "I'm file {0} in dir {1}!".format(i, subdir)
115 path = self.make_sysvol_path(subdir, "file-{0}.txt".format(i))
116 self.conn.savefile(path, test_contents.encode('utf8'))
117 filepaths.append(path)
119 # sanity-check these dirs/files exist
120 for subdir in dirpaths + empty_dirs:
121 self.assertTrue(self.conn.chkpath(subdir),
122 "Failed to create {0}".format(subdir))
123 for path in filepaths:
124 self.assertTrue(self.file_exists(path),
125 "Failed to create {0}".format(path))
127 # try using deltree to remove a single empty directory
128 path = empty_dirs.pop(0)
129 self.conn.deltree(path)
130 self.assertFalse(self.conn.chkpath(path),
131 "Failed to delete {0}".format(path))
133 # try using deltree to remove a single file
134 path = filepaths.pop(0)
135 self.conn.deltree(path)
136 self.assertFalse(self.file_exists(path),
137 "Failed to delete {0}".format(path))
139 # delete the top-level dir
140 self.conn.deltree(test_dir)
142 # now check that all the dirs/files are no longer there
143 for subdir in dirpaths + empty_dirs:
144 self.assertFalse(self.conn.chkpath(subdir),
145 "Failed to delete {0}".format(subdir))
146 for path in filepaths:
147 self.assertFalse(self.file_exists(path),
148 "Failed to delete {0}".format(path))
150 def file_exists(self, filepath):
151 """Returns whether a regular file exists (by trying to open it)"""
153 self.conn.loadfile(filepath)
155 except NTSTATUSError as err:
156 if (err.args[0] == NT_STATUS_OBJECT_NAME_NOT_FOUND or
157 err.args[0] == NT_STATUS_OBJECT_PATH_NOT_FOUND):
163 def test_unlink(self):
165 The smb.unlink API should delete file
167 # create the test file
168 self.assertFalse(self.file_exists(test_file))
169 self.conn.savefile(test_file, binary_contents)
170 self.assertTrue(self.file_exists(test_file))
172 # delete it and check that it's gone
173 self.smb_conn.unlink(test_file)
174 self.assertFalse(self.file_exists(test_file))
176 def test_chkpath(self):
177 """Tests .chkpath determines whether or not a directory exists"""
179 self.assertTrue(self.conn.chkpath(test_dir))
181 # should return False for a non-existent directory
182 bad_dir = self.make_sysvol_path(test_dir, 'dont_exist')
183 self.assertFalse(self.conn.chkpath(bad_dir))
185 # should return False for files (because they're not directories)
186 self.conn.savefile(test_file, binary_contents)
187 self.assertFalse(self.conn.chkpath(test_file))
189 # check correct result after creating and then deleting a new dir
190 new_dir = self.make_sysvol_path(test_dir, 'test-new')
191 self.conn.mkdir(new_dir)
192 self.assertTrue(self.conn.chkpath(new_dir))
193 self.conn.rmdir(new_dir)
194 self.assertFalse(self.conn.chkpath(new_dir))
196 def test_save_load_text(self):
198 self.conn.savefile(test_file, test_contents.encode('utf8'))
200 contents = self.conn.loadfile(test_file)
201 self.assertEquals(contents.decode('utf8'), test_contents,
202 msg='contents of test file did not match what was written')
204 # check we can overwrite the file with new contents
205 new_contents = 'wxyz' * 128
206 self.conn.savefile(test_file, new_contents.encode('utf8'))
207 contents = self.conn.loadfile(test_file)
208 self.assertEquals(contents.decode('utf8'), new_contents,
209 msg='contents of test file did not match what was written')
211 # with python2 this will save/load str type (with embedded nulls)
212 # with python3 this will save/load bytes type
213 def test_save_load_string_bytes(self):
214 self.conn.savefile(test_file, test_literal_bytes_embed_nulls)
216 contents = self.conn.loadfile(test_file)
217 self.assertEquals(contents, test_literal_bytes_embed_nulls,
218 msg='contents of test file did not match what was written')
220 # python3 only this will save/load unicode
221 def test_save_load_utfcontents(self):
223 self.conn.savefile(test_file, utf_contents.encode('utf8'))
225 contents = self.conn.loadfile(test_file)
226 self.assertEquals(contents.decode('utf8'), utf_contents,
227 msg='contents of test file did not match what was written')
229 # with python2 this will save/load str type
230 # with python3 this will save/load bytes type
231 def test_save_binary_contents(self):
232 self.conn.savefile(test_file, binary_contents)
234 contents = self.conn.loadfile(test_file)
235 self.assertEquals(contents, binary_contents,
236 msg='contents of test file did not match what was written')
238 def make_sysvol_path(self, dirpath, filename):
239 # return the dir + filename as a sysvol path
240 return os.path.join(dirpath, filename).replace('/', '\\')