mirror of
https://github.com/calebstewart/pwncat.git
synced 2024-12-02 13:24:15 +01:00
Added better file io tests which pass
This commit is contained in:
parent
8e40b1759d
commit
5544954852
49
tests/test_fileio.py
Normal file
49
tests/test_fileio.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from pwncat.util import random_string
|
||||||
|
|
||||||
|
|
||||||
|
def do_file_test(session, content):
|
||||||
|
"""Do a generic file test"""
|
||||||
|
|
||||||
|
name = random_string() + ".txt"
|
||||||
|
mode = "b" if isinstance(content, bytes) else ""
|
||||||
|
|
||||||
|
with session.platform.open(name, mode + "w") as filp:
|
||||||
|
assert filp.write(content) == len(content)
|
||||||
|
|
||||||
|
with session.platform.open(name, mode + "r") as filp:
|
||||||
|
assert filp.read() == content
|
||||||
|
|
||||||
|
# In some cases, the act of reading/writing causes a shell to hang
|
||||||
|
# so double check that.
|
||||||
|
result = session.platform.run(
|
||||||
|
["echo", "hello world"], capture_output=True, text=True
|
||||||
|
)
|
||||||
|
assert result.stdout == "hello world\n"
|
||||||
|
|
||||||
|
|
||||||
|
def test_small_text(session):
|
||||||
|
"""Test writing a small text-only file"""
|
||||||
|
|
||||||
|
do_file_test(session, "hello world")
|
||||||
|
|
||||||
|
|
||||||
|
def test_large_text(session):
|
||||||
|
"""Test writing and reading a large text file"""
|
||||||
|
|
||||||
|
contents = ("A" * 1000 + "\n") * 10
|
||||||
|
do_file_test(session, contents)
|
||||||
|
|
||||||
|
|
||||||
|
def test_small_binary(session):
|
||||||
|
"""Test writing a small amount of binary data"""
|
||||||
|
|
||||||
|
contents = bytes(list(range(32)))
|
||||||
|
do_file_test(session, contents)
|
||||||
|
|
||||||
|
|
||||||
|
def test_large_binary(session):
|
||||||
|
|
||||||
|
contents = bytes(list(range(32))) * 400
|
||||||
|
do_file_test(session, contents)
|
@ -10,27 +10,8 @@ from pwncat.util import random_string
|
|||||||
from pwncat.platform.windows import PowershellError
|
from pwncat.platform.windows import PowershellError
|
||||||
|
|
||||||
|
|
||||||
def test_platform_file_io(session):
|
|
||||||
""" Test file read/write of printable data """
|
|
||||||
|
|
||||||
# Generate random binary data
|
|
||||||
contents = os.urandom(1024)
|
|
||||||
|
|
||||||
# Create a new temporary file
|
|
||||||
with session.platform.tempfile(mode="wb") as filp:
|
|
||||||
filp.write(contents)
|
|
||||||
path = filp.name
|
|
||||||
|
|
||||||
# Ensure it exists
|
|
||||||
assert session.platform.Path(path).exists()
|
|
||||||
|
|
||||||
# Read the data back and ensure it matches
|
|
||||||
with session.platform.open(path, "rb") as filp:
|
|
||||||
assert contents == filp.read()
|
|
||||||
|
|
||||||
|
|
||||||
def test_platform_dir_io(session):
|
def test_platform_dir_io(session):
|
||||||
""" Test creating a directory and interacting with the contents """
|
"""Test creating a directory and interacting with the contents"""
|
||||||
|
|
||||||
# Create a path object representing the new remote directory
|
# Create a path object representing the new remote directory
|
||||||
path = session.platform.Path(random_string())
|
path = session.platform.Path(random_string())
|
||||||
@ -61,7 +42,7 @@ def test_platform_run(session):
|
|||||||
|
|
||||||
|
|
||||||
def test_platform_su(session):
|
def test_platform_su(session):
|
||||||
""" Test running `su` """
|
"""Test running `su`"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
session.platform.su("john", "P@ssw0rd")
|
session.platform.su("john", "P@ssw0rd")
|
||||||
@ -77,7 +58,7 @@ def test_platform_su(session):
|
|||||||
|
|
||||||
|
|
||||||
def test_platform_sudo(session):
|
def test_platform_sudo(session):
|
||||||
""" Testing running `sudo` """
|
"""Testing running `sudo`"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
@ -103,7 +84,7 @@ def test_platform_sudo(session):
|
|||||||
|
|
||||||
|
|
||||||
def test_windows_powershell(windows):
|
def test_windows_powershell(windows):
|
||||||
""" Test powershell execution """
|
"""Test powershell execution"""
|
||||||
|
|
||||||
# Run a real powershell snippet
|
# Run a real powershell snippet
|
||||||
r = windows.platform.powershell("$PSVersionTable.PSVersion")
|
r = windows.platform.powershell("$PSVersionTable.PSVersion")
|
||||||
|
Loading…
Reference in New Issue
Block a user