Skip to content

Commit

Permalink
🚧 remove pickle. other than --random-state option are removed. need t…
Browse files Browse the repository at this point in the history
…o think about random state input
  • Loading branch information
MeditationDuck committed Dec 14, 2024
1 parent 4a2e69f commit 13e5c29
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 174 deletions.
75 changes: 19 additions & 56 deletions wake/cli/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def run_test(
set_shrank_path,
)
from wake.testing.pytest_plugin_single import PytestWakePluginSingle
import json

def get_single_test_path(args: list[str]) -> tuple[bool, str | None]:
has_path = False
Expand All @@ -254,48 +255,13 @@ def get_single_test_path(args: list[str]) -> tuple[bool, str | None]:
path = arg
return has_path, path

def extract_test_path(crash_log_file_path: Path) -> str:
if crash_log_file_path is not None:
with open(crash_log_file_path, "r") as file:
for line in file:
if "Current test file" in line:
# Extract the part after the colon
parts = line.split(":")
if len(parts) == 2:
return parts[1].strip()
raise ValueError("Unexpected file format")

def extract_executed_flow_number(crash_log_file_path: Path) -> int:
if crash_log_file_path is not None:
with open(crash_log_file_path, "r") as file:
for line in file:
if "executed flow number" in line:
# Extract the number after the colon
parts = line.split(":")
if len(parts) == 2:
try:
executed_flow_number = int(parts[1].strip())
return executed_flow_number
except ValueError:
pass # Handle the case where the value after ":" is not an integer
raise ValueError("Unexpected file format")

def extract_internal_state(crash_log_file_path: Path) -> bytes:
if crash_log_file_path is not None:
def extract_crash_log_dict(crash_log_file_path: Path) -> dict:
try:
with open(crash_log_file_path, "r") as file:
for line in file:
if "Internal state of beginning of sequence" in line:
# Extract the part after the colon
parts = line.split(":")
if len(parts) == 2:
hex_string = parts[1].strip()
try:
# Convert the hex string to bytes
internal_state_bytes = bytes.fromhex(hex_string)
return internal_state_bytes
except ValueError:
pass # Handle the case where the value after ":" is not a valid hex string
raise ValueError("Unexpected file format")
crash_log_dict = json.load(file)
return crash_log_dict
except json.JSONDecodeError:
raise ValueError(f"Invalid JSON format in crash log file: {crash_log_file_path}")

def get_shrink_argument_path(shrink_path_str: str) -> Path:
try:
Expand All @@ -315,7 +281,7 @@ def get_shrink_argument_path(shrink_path_str: str) -> Path:
)
index = int(shrink_path_str)
crash_logs = sorted(
crash_logs_dir.glob("*.txt"), key=os.path.getmtime, reverse=True
crash_logs_dir.glob("*.json"), key=os.path.getmtime, reverse=True
)
if abs(index) > len(crash_logs):
raise click.BadParameter(f"Invalid crash log index: {index}")
Expand Down Expand Up @@ -350,15 +316,17 @@ def get_shrank_argument_path(shrank_path_str: str) -> Path:
"Both shrink and shrieked cannot be provided at the same time."
)

pytest_path_specified, test_path = get_single_test_path(pytest_args)


if shrink is not None:
set_fuzz_mode(1)
pytest_path_specified, test_path = get_single_test_path(pytest_args)
shrink_crash_path = get_shrink_argument_path(shrink)
path = extract_test_path(shrink_crash_path)
number = extract_executed_flow_number(shrink_crash_path)
set_error_flow_num(number)
beginning_random_state_bytes = extract_internal_state(shrink_crash_path)
set_sequence_initial_internal_state(beginning_random_state_bytes)
print("shrink from crash log: ", shrink_crash_path)
crash_log_dict = extract_crash_log_dict(shrink_crash_path)
path = crash_log_dict["test_file"]
set_error_flow_num(crash_log_dict["crash_flow_number"])
set_sequence_initial_internal_state(crash_log_dict["initial_random_state"])
if pytest_path_specified:
assert (
path == test_path
Expand All @@ -367,16 +335,11 @@ def get_shrank_argument_path(shrank_path_str: str) -> Path:
pytest_args.insert(0, path)

if shrank:
import pickle
from wake.testing.fuzzing.fuzz_shrink import ShrankInfoFile

set_fuzz_mode(2)
pytest_path_specified, test_path = get_single_test_path(pytest_args)
shrank_data_path = get_shrank_argument_path(shrank)

with open(shrank_data_path, "rb") as f:
store_data: ShrankInfoFile = pickle.load(f)
target_fuzz_path = store_data.target_fuzz_path
print("shrank from shrank data: ", shrank_data_path)
with open(shrank_data_path, "r") as f:
target_fuzz_path = json.load(f)["target_fuzz_path"]
if pytest_path_specified:
assert (
target_fuzz_path == test_path
Expand Down
6 changes: 3 additions & 3 deletions wake/development/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
] = None
_exception_handled = False

_initial_internal_state: bytes = b""
_initial_internal_state: dict = {}

_coverage_handler: Optional[CoverageHandler] = None

Expand Down Expand Up @@ -161,11 +161,11 @@ def set_exception_handler(
global _exception_handler
_exception_handler = handler

def set_sequence_initial_internal_state(intenral_state: bytes):
def set_sequence_initial_internal_state(intenral_state: dict):
global _initial_internal_state
_initial_internal_state = intenral_state

def get_sequence_initial_internal_state() -> bytes:
def get_sequence_initial_internal_state() -> dict:
return _initial_internal_state

def reset_exception_handled():
Expand Down
Loading

0 comments on commit 13e5c29

Please sign in to comment.