Skip to content

Commit 71c795c

Browse files
committed
tests: Simplify generating payload
Signed-off-by: Jakub Jelen <[email protected]>
1 parent 0f562cd commit 71c795c

File tree

1 file changed

+16
-37
lines changed

1 file changed

+16
-37
lines changed

tests/unit/sftp_test.py

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,22 @@ def sftp_session(ssh_client_session):
2020
del sftp_sess # noqa: WPS420
2121

2222

23-
@pytest.fixture
24-
def transmit_payload():
25-
"""Generate a binary test payload."""
26-
uuid_name = uuid.uuid4()
27-
return 'Hello, {name!s}'.format(name=uuid_name).encode()
23+
@pytest.fixture(
24+
params=(32, 1024 + 1),
25+
ids=('small-payload', 'large-payload'),
26+
)
27+
def transmit_payload(request: pytest.FixtureRequest):
28+
"""Generate binary test payloads of assorted sizes.
29+
30+
The choice 32 is arbitrary small value.
31+
32+
The choice 1024 + 1 is meant to be 1B larger than the chunk size used in
33+
:file:`sftp.pyx` to make sure we excercise at least two rounds of reading/writing.
34+
"""
35+
payload_len = request.param
36+
# The use of random() here is not security relevant -- it generates just random test data
37+
random_bytes = [ord(random.choice(string.printable)) for _ in range(payload_len)] # NOSONAR
38+
return bytes(random_bytes)
2839

2940

3041
@pytest.fixture
@@ -92,35 +103,3 @@ def test_put_existing(pre_existing_dst_path, src_path, sftp_session, transmit_pa
92103
"""Check that SFTP file upload works when target file exists."""
93104
sftp_session.put(str(src_path), str(pre_existing_dst_path))
94105
assert pre_existing_dst_path.read_bytes() == transmit_payload
95-
96-
97-
@pytest.fixture
98-
def large_payload():
99-
"""Generate a large 1025 byte (1024 + 1B) test payload."""
100-
payload_len = 1024 + 1
101-
random_bytes = [ord(random.choice(string.printable)) for _ in range(payload_len)]
102-
return bytes(random_bytes)
103-
104-
105-
@pytest.fixture
106-
def src_path_large(tmp_path, large_payload):
107-
"""Return a remote path to a 1025 byte-sized file.
108-
109-
The pylibssh chunk size is 1024 so the test needs a file that would
110-
execute at least two loops.
111-
"""
112-
path = tmp_path / 'large.txt'
113-
path.write_bytes(large_payload)
114-
return path
115-
116-
117-
def test_put_large(dst_path, src_path_large, sftp_session, large_payload):
118-
"""Check that SFTP can upload large file."""
119-
sftp_session.put(str(src_path_large), str(dst_path))
120-
assert dst_path.read_bytes() == large_payload
121-
122-
123-
def test_get_large(dst_path, src_path_large, sftp_session, large_payload):
124-
"""Check that SFTP can download large file."""
125-
sftp_session.get(str(src_path_large), str(dst_path))
126-
assert dst_path.read_bytes() == large_payload

0 commit comments

Comments
 (0)