import subprocess
import os
import pytest
FIXTURES_DIR = os.path.join(os.path.dirname(__file__), "fixtures")
SIMPLE_PCAP = os.path.join(FIXTURES_DIR, "simple.pcap")
SIMPLE_PCAPNG = os.path.join(FIXTURES_DIR, "simple.pcapng")
def run_capfile(args, check=True):
result = subprocess.run(
["cargo", "run", "--"] + args,
capture_output=True,
text=True,
)
if check and result.returncode != 0:
pytest.fail(f"capfile failed: {result.stderr}")
return result
class TestCapfileInfo:
def test_info_pcap(self):
result = run_capfile(["info", SIMPLE_PCAP], check=False)
assert result.returncode == 0
assert "Format: PCAP" in result.stdout
assert "Link type: 1" in result.stdout
def test_info_pcapng(self):
result = run_capfile(["info", SIMPLE_PCAPNG], check=False)
assert result.returncode == 0
def test_info_missing_file(self):
result = run_capfile(["info", "/nonexistent.pcap"], check=False)
assert result.returncode != 0
class TestCapfileList:
def test_list_pcap(self):
result = run_capfile(["list", SIMPLE_PCAP], check=False)
assert result.returncode == 0
assert "Total packets:" in result.stdout
def test_list_pcapng(self):
result = run_capfile(["list", SIMPLE_PCAPNG], check=False)
assert result.returncode == 0
def test_list_missing_file(self):
result = run_capfile(["list", "/nonexistent.pcap"], check=False)
assert result.returncode != 0
class TestCapfileHelp:
def test_no_args_shows_help(self):
result = run_capfile([], check=False)
assert result.returncode != 0
assert "Usage:" in result.stdout or "capfile" in result.stdout.lower()
def test_unknown_command(self):
result = run_capfile(["unknown"], check=False)
assert result.returncode != 0
if __name__ == "__main__":
pytest.main([__file__, "-v"])