Debian Codegraph @

tests/test_sftp_big.py

raw
1 # Copyright (C) 2003-2009 Robey Pointer <[email protected]>
2 #
3 # This file is part of paramiko.
4 #
5 # Paramiko is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation; either version 2.1 of the License, or (at your option)
8 # any later version.
9 #
10 # Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 """
20 some unit tests to make sure sftp works well with large files.
21
22 a real actual sftp server is contacted, and a new folder is created there to
23 do test file operations in (so no existing files will be harmed).
24 """
25
26 import random
27 import struct
28 import sys
29 import time
30
31 from paramiko.common import o660
32
33 from ._util import slow, wait_until
34
35
36 @slow
37 class TestBigSFTP:
38 def test_lots_of_files(self, sftp):
39 """
40 create a bunch of files over the same session.
41 """
42 numfiles = 100
43 try:
44 for i in range(numfiles):
45 target = f"{sftp.FOLDER}/file{i}.txt"
46 with sftp.open(target, "w", 1) as f:
47 f.write(f"this is file #{i}.\n")
48 sftp.chmod(target, o660)
49
50 # now make sure every file is there, by creating a list of filenmes
51 # and reading them in random order.
52 numlist = list(range(numfiles))
53 while len(numlist) > 0:
54 r = numlist[random.randint(0, len(numlist) - 1)]
55 with sftp.open(f"{sftp.FOLDER}/file{r}.txt") as f:
56 assert f.readline() == f"this is file #{r}.\n"
57 numlist.remove(r)
58 finally:
59 for i in range(numfiles):
60 try:
61 sftp.remove(f"{sftp.FOLDER}/file{i}.txt")
62 except:
63 pass
64
65 def test_big_file(self, sftp):
66 """
67 write a 1MB file with no buffering.
68 """
69 kblob = 1024 * b"x"
70 start = time.time()
71 try:
72 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "w") as f:
73 for n in range(1024):
74 f.write(kblob)
75 if n % 128 == 0:
76 sys.stderr.write(".")
77 sys.stderr.write(" ")
78
79 assert (
80 sftp.stat(f"{sftp.FOLDER}/hongry.txt").st_size == 1024 * 1024
81 )
82 end = time.time()
83 sys.stderr.write(f"{round(end - start)}s")
84
85 start = time.time()
86 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "r") as f:
87 for n in range(1024):
88 data = f.read(1024)
89 assert data == kblob
90
91 end = time.time()
92 sys.stderr.write(f"{round(end - start)}s")
93 finally:
94 sftp.remove(f"{sftp.FOLDER}/hongry.txt")
95
96 def test_big_file_pipelined(self, sftp):
97 """
98 write a 1MB file, with no linefeeds, using pipelining.
99 """
100 kblob = bytes().join([struct.pack(">H", n) for n in range(512)])
101 start = time.time()
102 try:
103 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "wb") as f:
104 f.set_pipelined(True)
105 for n in range(1024):
106 f.write(kblob)
107 if n % 128 == 0:
108 sys.stderr.write(".")
109 sys.stderr.write(" ")
110
111 assert (
112 sftp.stat(f"{sftp.FOLDER}/hongry.txt").st_size == 1024 * 1024
113 )
114 end = time.time()
115 sys.stderr.write(f"{round(end - start)}s")
116
117 start = time.time()
118 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "rb") as f:
119 file_size = f.stat().st_size
120 f.prefetch(file_size)
121
122 # read on odd boundaries to make sure the bytes aren't getting
123 # scrambled
124 n = 0
125 k2blob = kblob + kblob
126 chunk = 629
127 size = 1024 * 1024
128 while n < size:
129 if n + chunk > size:
130 chunk = size - n
131 data = f.read(chunk)
132 offset = n % 1024
133 assert data == k2blob[offset : offset + chunk]
134 n += chunk
135
136 end = time.time()
137 sys.stderr.write(f"{round(end - start)}s")
138 finally:
139 sftp.remove(f"{sftp.FOLDER}/hongry.txt")
140
141 def test_prefetch_seek(self, sftp):
142 kblob = bytes().join([struct.pack(">H", n) for n in range(512)])
143 try:
144 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "wb") as f:
145 f.set_pipelined(True)
146 for n in range(1024):
147 f.write(kblob)
148 if n % 128 == 0:
149 sys.stderr.write(".")
150 sys.stderr.write(" ")
151
152 assert (
153 sftp.stat(f"{sftp.FOLDER}/hongry.txt").st_size == 1024 * 1024
154 )
155
156 start = time.time()
157 k2blob = kblob + kblob
158 chunk = 793
159 for i in range(10):
160 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "rb") as f:
161 file_size = f.stat().st_size
162 f.prefetch(file_size)
163 base_offset = (512 * 1024) + 17 * random.randint(
164 1000, 2000
165 )
166 offsets = [base_offset + j * chunk for j in range(100)]
167 # randomly seek around and read them out
168 for j in range(100):
169 offset = offsets[random.randint(0, len(offsets) - 1)]
170 offsets.remove(offset)
171 f.seek(offset)
172 data = f.read(chunk)
173 n_offset = offset % 1024
174 assert data == k2blob[n_offset : n_offset + chunk]
175 offset += chunk
176 end = time.time()
177 sys.stderr.write(f"{round(end - start)}s")
178 finally:
179 sftp.remove(f"{sftp.FOLDER}/hongry.txt")
180
181 def test_readv_seek(self, sftp):
182 kblob = bytes().join([struct.pack(">H", n) for n in range(512)])
183 try:
184 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "wb") as f:
185 f.set_pipelined(True)
186 for n in range(1024):
187 f.write(kblob)
188 if n % 128 == 0:
189 sys.stderr.write(".")
190 sys.stderr.write(" ")
191
192 assert (
193 sftp.stat(f"{sftp.FOLDER}/hongry.txt").st_size == 1024 * 1024
194 )
195
196 start = time.time()
197 k2blob = kblob + kblob
198 chunk = 793
199 for i in range(10):
200 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "rb") as f:
201 base_offset = (512 * 1024) + 17 * random.randint(
202 1000, 2000
203 )
204 # make a bunch of offsets and put them in random order
205 offsets = [base_offset + j * chunk for j in range(100)]
206 readv_list = []
207 for j in range(100):
208 o = offsets[random.randint(0, len(offsets) - 1)]
209 offsets.remove(o)
210 readv_list.append((o, chunk))
211 ret = f.readv(readv_list)
212 for i in range(len(readv_list)):
213 offset = readv_list[i][0]
214 n_offset = offset % 1024
215 assert next(ret) == k2blob[n_offset : n_offset + chunk]
216 end = time.time()
217 sys.stderr.write(f"{round(end - start)}s")
218 finally:
219 sftp.remove(f"{sftp.FOLDER}/hongry.txt")
220
221 def test_lots_of_prefetching(self, sftp):
222 """
223 prefetch a 1MB file a bunch of times, discarding the file object
224 without using it, to verify that paramiko doesn't get confused.
225 """
226 kblob = 1024 * b"x"
227 try:
228 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "w") as f:
229 f.set_pipelined(True)
230 for n in range(1024):
231 f.write(kblob)
232 if n % 128 == 0:
233 sys.stderr.write(".")
234 sys.stderr.write(" ")
235
236 assert (
237 sftp.stat(f"{sftp.FOLDER}/hongry.txt").st_size == 1024 * 1024
238 )
239
240 for i in range(10):
241 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "r") as f:
242 file_size = f.stat().st_size
243 f.prefetch(file_size)
244 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "r") as f:
245 file_size = f.stat().st_size
246 f.prefetch(file_size)
247 for n in range(1024):
248 data = f.read(1024)
249 assert data == kblob
250 if n % 128 == 0:
251 sys.stderr.write(".")
252 sys.stderr.write(" ")
253 finally:
254 sftp.remove(f"{sftp.FOLDER}/hongry.txt")
255
256 def test_prefetch_readv(self, sftp):
257 """
258 verify that prefetch and readv don't conflict with each other.
259 """
260 kblob = bytes().join([struct.pack(">H", n) for n in range(512)])
261 try:
262 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "wb") as f:
263 f.set_pipelined(True)
264 for n in range(1024):
265 f.write(kblob)
266 if n % 128 == 0:
267 sys.stderr.write(".")
268 sys.stderr.write(" ")
269
270 assert (
271 sftp.stat(f"{sftp.FOLDER}/hongry.txt").st_size == 1024 * 1024
272 )
273
274 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "rb") as f:
275 file_size = f.stat().st_size
276 f.prefetch(file_size)
277 data = f.read(1024)
278 assert data == kblob
279
280 chunk_size = 793
281 base_offset = 512 * 1024
282 k2blob = kblob + kblob
283 chunks = [
284 (base_offset + (chunk_size * i), chunk_size)
285 for i in range(20)
286 ]
287 for data in f.readv(chunks):
288 offset = base_offset % 1024
289 assert chunk_size == len(data)
290 assert k2blob[offset : offset + chunk_size] == data
291 base_offset += chunk_size
292
293 sys.stderr.write(" ")
294 finally:
295 sftp.remove(f"{sftp.FOLDER}/hongry.txt")
296
297 def test_large_readv(self, sftp):
298 """
299 verify that a very large readv is broken up correctly and still
300 returned as a single blob.
301 """
302 kblob = bytes().join([struct.pack(">H", n) for n in range(512)])
303 try:
304 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "wb") as f:
305 f.set_pipelined(True)
306 for n in range(1024):
307 f.write(kblob)
308 if n % 128 == 0:
309 sys.stderr.write(".")
310 sys.stderr.write(" ")
311
312 assert (
313 sftp.stat(f"{sftp.FOLDER}/hongry.txt").st_size == 1024 * 1024
314 )
315
316 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "rb") as f:
317 data = list(f.readv([(23 * 1024, 128 * 1024)]))
318 assert len(data) == 1
319 data = data[0]
320 assert len(data) == 128 * 1024
321
322 sys.stderr.write(" ")
323 finally:
324 sftp.remove(f"{sftp.FOLDER}/hongry.txt")
325
326 def test_big_file_big_buffer(self, sftp):
327 """
328 write a 1MB file, with no linefeeds, and a big buffer.
329 """
330 mblob = 1024 * 1024 * "x"
331 try:
332 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "w", 128 * 1024) as f:
333 f.write(mblob)
334
335 assert (
336 sftp.stat(f"{sftp.FOLDER}/hongry.txt").st_size == 1024 * 1024
337 )
338 finally:
339 sftp.remove(f"{sftp.FOLDER}/hongry.txt")
340
341 def test_big_file_renegotiate(self, sftp):
342 """
343 write a 1MB file, forcing key renegotiation in the middle.
344 """
345 t = sftp.sock.get_transport()
346 t.packetizer.REKEY_BYTES = 512 * 1024
347 k32blob = 32 * 1024 * "x"
348 try:
349 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "w", 128 * 1024) as f:
350 for i in range(32):
351 f.write(k32blob)
352
353 assert (
354 sftp.stat(f"{sftp.FOLDER}/hongry.txt").st_size == 1024 * 1024
355 )
356 assert t.H != t.session_id
357
358 # try to read it too.
359 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "r", 128 * 1024) as f:
360 file_size = f.stat().st_size
361 f.prefetch(file_size)
362 total = 0
363 while total < 1024 * 1024:
364 total += len(f.read(32 * 1024))
365 finally:
366 sftp.remove(f"{sftp.FOLDER}/hongry.txt")
367 t.packetizer.REKEY_BYTES = pow(2, 30)
368
369 def test_prefetch_limit(self, sftp):
370 """
371 write a 1MB file and prefetch with a limit
372 """
373 kblob = 1024 * b"x"
374 start = time.time()
375
376 def expect_prefetch_extents(file, expected_extents):
377 with file._prefetch_lock:
378 assert len(file._prefetch_extents) == expected_extents
379
380 try:
381 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "w") as f:
382 for n in range(1024):
383 f.write(kblob)
384 if n % 128 == 0:
385 sys.stderr.write(".")
386 sys.stderr.write(" ")
387
388 assert (
389 sftp.stat(f"{sftp.FOLDER}/hongry.txt").st_size == 1024 * 1024
390 )
391 end = time.time()
392 sys.stderr.write(f"{round(end - start)}s")
393
394 # read with prefetch, no limit
395 # expecting 32 requests (32k * 32 == 1M)
396 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "rb") as f:
397 file_size = f.stat().st_size
398 f.prefetch(file_size)
399 wait_until(lambda: expect_prefetch_extents(f, 32))
400
401 # read with prefetch, limiting to 5 simultaneous requests
402 with sftp.open(f"{sftp.FOLDER}/hongry.txt", "rb") as f:
403 file_size = f.stat().st_size
404 f.prefetch(file_size, 5)
405 wait_until(lambda: expect_prefetch_extents(f, 5))
406 for n in range(1024):
407 with f._prefetch_lock:
408 assert len(f._prefetch_extents) <= 5
409 data = f.read(1024)
410 assert data == kblob
411
412 if n % 128 == 0:
413 sys.stderr.write(".")
414
415 finally:
416 sftp.remove(f"{sftp.FOLDER}/hongry.txt")