2021-04-10 21:52:47 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
import base64
|
2021-05-30 06:24:12 +02:00
|
|
|
import subprocess
|
2021-04-10 21:52:47 +02:00
|
|
|
|
2021-05-30 06:24:12 +02:00
|
|
|
import pytest
|
|
|
|
from pwncat.util import random_string
|
2021-04-10 21:52:47 +02:00
|
|
|
|
|
|
|
|
2021-05-30 06:24:12 +02:00
|
|
|
def test_file_read_write(session):
|
|
|
|
""" Test file read/write of printable data """
|
2021-04-10 21:52:47 +02:00
|
|
|
|
2021-05-30 06:24:12 +02:00
|
|
|
contents = os.urandom(1024)
|
|
|
|
with session.platform.tempfile(mode="wb") as filp:
|
|
|
|
filp.write(contents)
|
|
|
|
path = filp.name
|
2021-04-10 21:52:47 +02:00
|
|
|
|
2021-05-30 06:24:12 +02:00
|
|
|
assert session.platform.Path(path).exists()
|
2021-04-10 21:52:47 +02:00
|
|
|
|
2021-05-30 06:24:12 +02:00
|
|
|
with session.platform.open(path, "rb") as filp:
|
|
|
|
assert contents == filp.read()
|
2021-04-10 21:52:47 +02:00
|
|
|
|
|
|
|
|
2021-05-30 06:24:12 +02:00
|
|
|
def test_platform_mkdir(session):
|
|
|
|
""" Test creating a directory """
|
2021-04-10 21:52:47 +02:00
|
|
|
|
2021-05-30 06:24:12 +02:00
|
|
|
path = session.platform.Path(random_string())
|
2021-04-10 21:52:47 +02:00
|
|
|
|
2021-05-30 06:24:12 +02:00
|
|
|
path.mkdir()
|
|
|
|
assert session.platform.Path(str(path)).is_dir()
|
2021-04-10 21:52:47 +02:00
|
|
|
|
|
|
|
|
2021-05-30 06:24:12 +02:00
|
|
|
def test_platform_run(session):
|
2021-04-10 21:52:47 +02:00
|
|
|
|
2021-05-30 06:24:12 +02:00
|
|
|
# Ensure command output works
|
|
|
|
output_remote = session.platform.run(
|
|
|
|
["echo", "hello world"], capture_output=True, text=True, check=True
|
|
|
|
)
|
|
|
|
assert output_remote.stdout == "hello world\n"
|
2021-04-10 21:52:47 +02:00
|
|
|
|
2021-05-30 06:24:12 +02:00
|
|
|
# Ensure we capture the process return code properly
|
|
|
|
with pytest.raises(subprocess.CalledProcessError):
|
|
|
|
session.platform.run("this_command_doesnt_exist", shell=True, check=True)
|