50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
import os
|
|
import tempfile
|
|
import pytest
|
|
from ayn_antivirus.utils.helpers import (
|
|
format_size, format_duration, is_root, validate_ip,
|
|
validate_domain, generate_id, hash_file, safe_path
|
|
)
|
|
|
|
def test_format_size():
|
|
assert format_size(0) == "0.0 B"
|
|
assert format_size(1024) == "1.0 KB"
|
|
assert format_size(1048576) == "1.0 MB"
|
|
assert format_size(1073741824) == "1.0 GB"
|
|
|
|
def test_format_duration():
|
|
assert "0s" in format_duration(0) or "0" in format_duration(0)
|
|
result = format_duration(3661)
|
|
assert "1h" in result
|
|
assert "1m" in result
|
|
|
|
def test_validate_ip():
|
|
assert validate_ip("192.168.1.1") == True
|
|
assert validate_ip("10.0.0.1") == True
|
|
assert validate_ip("999.999.999.999") == False
|
|
assert validate_ip("not-an-ip") == False
|
|
assert validate_ip("") == False
|
|
|
|
def test_validate_domain():
|
|
assert validate_domain("example.com") == True
|
|
assert validate_domain("sub.example.com") == True
|
|
assert validate_domain("") == False
|
|
|
|
def test_generate_id():
|
|
id1 = generate_id()
|
|
id2 = generate_id()
|
|
assert isinstance(id1, str)
|
|
assert len(id1) == 32
|
|
assert id1 != id2
|
|
|
|
def test_hash_file(tmp_path):
|
|
f = tmp_path / "test.txt"
|
|
f.write_text("hello world")
|
|
h = hash_file(str(f))
|
|
assert isinstance(h, str)
|
|
assert len(h) == 64 # sha256 hex
|
|
|
|
def test_safe_path(tmp_path):
|
|
result = safe_path(str(tmp_path))
|
|
assert result is not None
|