1 # Unix SMB/CIFS implementation.
2 # Copyright Volker Lendecke <vl@samba.org> 2012
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.
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.
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/>.
18 """Tests for samba.samba3.libsmb."""
20 from samba
.samba3
import libsmb_samba_internal
as libsmb
21 from samba
.dcerpc
import security
22 from samba
import NTSTATUSError
,ntstatus
23 from samba
.ntstatus
import NT_STATUS_DELETE_PENDING
24 from samba
.credentials
import SMB_ENCRYPTION_REQUIRED
25 import samba
.tests
.libsmb
31 class LibsmbTestCase(samba
.tests
.libsmb
.LibsmbTests
):
33 class OpenClose(threading
.Thread
):
35 def __init__(self
, conn
, filename
, num_ops
):
36 threading
.Thread
.__init
__(self
)
38 self
.filename
= filename
39 self
.num_ops
= num_ops
45 for i
in range(self
.num_ops
):
46 f
= c
.create(self
.filename
, CreateDisposition
=3,
47 DesiredAccess
=security
.SEC_STD_DELETE
)
48 c
.delete_on_close(f
, True)
51 self
.exc
= sys
.exc_info()
53 def test_OpenClose(self
):
66 t
= LibsmbTestCase
.OpenClose(c
, "test" + str(i
), 10)
75 raise t
.exc
[0](t
.exc
[1])
77 def test_SMB3EncryptionRequired(self
):
78 test_dir
= 'testing_%d' % random
.randint(0, 0xFFFF)
80 self
.creds
.set_smb_encryption(SMB_ENCRYPTION_REQUIRED
)
82 c
= libsmb
.Conn(self
.server_ip
, "tmp", self
.lp
, self
.creds
)
87 def test_SMB1EncryptionRequired(self
):
88 test_dir
= 'testing_%d' % random
.randint(0, 0xFFFF)
90 self
.creds
.set_smb_encryption(SMB_ENCRYPTION_REQUIRED
)
102 def test_RenameDstDelOnClose(self
):
104 dstdir
= "\\dst-subdir"
106 c1
= libsmb
.Conn(self
.server_ip
, "tmp", self
.lp
, self
.creds
)
107 c2
= libsmb
.Conn(self
.server_ip
, "tmp", self
.lp
, self
.creds
)
115 dnum
= c1
.create(dstdir
, DesiredAccess
=security
.SEC_STD_DELETE
)
116 c1
.delete_on_close(dnum
,1)
117 c2
.savefile("\\src.txt", b
"Content")
119 with self
.assertRaises(NTSTATUSError
) as cm
:
120 c2
.rename("\\src.txt", dstdir
+ "\\dst.txt")
121 if (cm
.exception
.args
[0] != NT_STATUS_DELETE_PENDING
):
122 raise AssertionError("Rename must fail with DELETE_PENDING")
124 c1
.delete_on_close(dnum
,0)
129 c1
.unlink("\\src.txt")
133 def test_libsmb_CreateContexts(self
):
134 c
= libsmb
.Conn(self
.server_ip
, "tmp", self
.lp
, self
.creds
)
135 cc_in
= [(libsmb
.SMB2_CREATE_TAG_MXAC
, b
'')]
136 fnum
,cr
,cc
= c
.create_ex("",CreateContexts
=cc_in
)
138 cr
['file_attributes'] & libsmb
.FILE_ATTRIBUTE_DIRECTORY
,
139 libsmb
.FILE_ATTRIBUTE_DIRECTORY
)
140 self
.assertEqual(cc
[0][0],libsmb
.SMB2_CREATE_TAG_MXAC
)
141 self
.assertEqual(len(cc
[0][1]),8)
144 def test_libsmb_TortureCaseSensitivity(self
):
145 testdir
= "test_libsmb_torture_case_sensitivity"
147 filepath
= testdir
+ "/" + filename
149 c
= libsmb
.Conn(self
.server_ip
, "tmp", self
.lp
, self
.creds
)
159 # Now check for all possible upper-/lowercase combinations:
165 dircases
= [testdir
, testdir
, testdir
.upper(), testdir
.upper()]
166 filecases
= [filename
, filename
.upper(), filename
, filename
.upper()]
167 tcases
= [{'dir':dir, 'file':file} for dir,file in zip(dircases
,filecases
)]
170 testpath
= tcase
['dir'] + "/" + tcase
['file']
172 # Create the testfile
173 h
= c
.create(filepath
,
174 DesiredAccess
=security
.SEC_FILE_ALL
,
175 CreateDisposition
=libsmb
.FILE_OPEN_IF
)
182 ls
= [f
['name'] for f
in c
.list(tcase
['dir'], mask
=tcase
['file'])]
183 self
.assertIn(filename
, ls
, msg
='When searching for "%s" not found in "%s"' % (tcase
['file'], tcase
['dir']))
186 c
.rename(testpath
, tcase
['dir'] + "/tmp")
187 c
.rename(tcase
['dir'] + "/TMP", filepath
)
196 def test_libsmb_TortureDirCaseSensitive(self
):
197 c
= libsmb
.Conn(self
.server_ip
, "lowercase", self
.lp
, self
.creds
)
200 ret
= c
.chkpath("SubDir/b")
205 def test_libsmb_shadow_depot(self
):
206 c
= libsmb
.Conn(self
.server_ip
, "shadow_depot", self
.lp
, self
.creds
)
208 fnum
=c
.create("x:y",CreateDisposition
=libsmb
.FILE_CREATE
)
213 # "c" might have crashed, get a new connection
214 c1
= libsmb
.Conn(self
.server_ip
, "shadow_depot", self
.lp
, self
.creds
)
218 def test_gencache_pollution_bz15481(self
):
219 c
= libsmb
.Conn(self
.server_ip
, "tmp", self
.lp
, self
.creds
)
220 fh
= c
.create("file",
221 DesiredAccess
=security
.SEC_STD_DELETE
,
222 CreateDisposition
=libsmb
.FILE_CREATE
)
224 # prime the gencache File->file
225 fh_upper
= c
.create("File",
226 DesiredAccess
=security
.SEC_FILE_READ_ATTRIBUTE
,
227 CreateDisposition
=libsmb
.FILE_OPEN
)
230 c
.delete_on_close(fh
, 1)
233 fh
= c
.create("File",
234 DesiredAccess
=security
.SEC_STD_DELETE
,
235 CreateDisposition
=libsmb
.FILE_CREATE
)
237 directory
= c
.list("\\", "File")
239 c
.delete_on_close(fh
, 1)
242 # Without the bugfix for 15481 we get 'file' not 'File'
243 self
.assertEqual(directory
[0]['name'], 'File')
245 def test_stream_close_with_full_information(self
):
246 c
= libsmb
.Conn(self
.server_ip
, "streams_xattr", self
.lp
, self
.creds
)
249 c
.deltree("teststreams")
253 c
.mkdir("teststreams")
254 fh
= c
.create("teststreams\\stream_full_close_info.txt:Stream",
255 DesiredAccess
=security
.SEC_STD_DELETE
,
256 CreateDisposition
=libsmb
.FILE_CREATE
)
257 c
.delete_on_close(fh
, 1)
260 c
.close(fh
, libsmb
.SMB2_CLOSE_FLAGS_FULL_INFORMATION
)
264 c
.deltree("teststreams")
266 if __name__
== "__main__":