Some checks failed
Cross-Node Transaction Testing / transaction-test (push) Has been cancelled
Deploy to Testnet / deploy-testnet (push) Has been cancelled
Documentation Validation / validate-docs (push) Has been cancelled
Documentation Validation / validate-policies-strict (push) Has been cancelled
Integration Tests / test-service-integration (push) Has been cancelled
Multi-Node Stress Testing / stress-test (push) Has been cancelled
Python Tests / test-python (push) Has been cancelled
Security Scanning / security-scan (push) Has been cancelled
- Added logger initialization to EventRouter in events.py - Fixed datetime.timedelta references to use timedelta directly in security_hardening.py - Fixed StateTransition timestamp default_factory to use lambda in state.py - Fixed StateValidator.validate_transitions to only check source states exist - Moved cross_chain_bridge_enhanced.py to cross_chain/bridge_enhanced.py - Updated import paths in global_marketplace
156 lines
5.8 KiB
Python
156 lines
5.8 KiB
Python
"""
|
|
Property-based tests for AITBC validation functions using hypothesis.
|
|
Tests ensure that validation functions maintain expected properties across random inputs.
|
|
"""
|
|
|
|
import pytest
|
|
from hypothesis import given, strategies as st, settings
|
|
|
|
from aitbc.utils.validation import (
|
|
validate_address,
|
|
validate_hash,
|
|
validate_url,
|
|
validate_port,
|
|
validate_email,
|
|
validate_non_empty,
|
|
validate_positive_number,
|
|
validate_range,
|
|
validate_chain_id,
|
|
validate_uuid
|
|
)
|
|
from aitbc.exceptions import ValidationError
|
|
|
|
|
|
class TestValidationProperties:
|
|
"""Property-based tests for validation functions"""
|
|
|
|
@given(st.text(min_size=1, max_size=100).filter(lambda x: x and x.strip()))
|
|
@settings(max_examples=50)
|
|
def test_validate_non_empty_strings(self, text):
|
|
"""Test that non-empty strings pass validation"""
|
|
assert validate_non_empty(text)
|
|
|
|
@given(st.just(""))
|
|
@settings(max_examples=10)
|
|
def test_validate_empty_strings(self, empty_string):
|
|
"""Test that empty strings fail validation"""
|
|
with pytest.raises(ValidationError):
|
|
validate_non_empty(empty_string)
|
|
|
|
@given(st.integers(min_value=1, max_value=1000000))
|
|
@settings(max_examples=50)
|
|
def test_validate_positive_numbers(self, number):
|
|
"""Test that positive numbers pass validation"""
|
|
assert validate_positive_number(number)
|
|
|
|
@given(st.integers(max_value=0))
|
|
@settings(max_examples=50)
|
|
def test_validate_non_positive_numbers(self, number):
|
|
"""Test that non-positive numbers fail validation"""
|
|
with pytest.raises(ValidationError):
|
|
validate_positive_number(number)
|
|
|
|
@given(st.integers(min_value=0, max_value=100), st.integers(min_value=101, max_value=200))
|
|
@settings(max_examples=50)
|
|
def test_validate_range_in_bounds(self, value, max_val):
|
|
"""Test that values in range pass validation"""
|
|
assert validate_range(value, 0, max_val)
|
|
|
|
@given(st.integers(min_value=-100, max_value=-1))
|
|
@settings(max_examples=50)
|
|
def test_validate_range_out_of_bounds(self, value):
|
|
"""Test that values out of range fail validation"""
|
|
with pytest.raises(ValidationError):
|
|
validate_range(value, 0, 100)
|
|
|
|
@given(st.integers(min_value=1, max_value=65535))
|
|
@settings(max_examples=50)
|
|
def test_validate_valid_ports(self, port):
|
|
"""Test that valid ports pass validation"""
|
|
assert validate_port(port)
|
|
|
|
@given(st.integers(min_value=65536, max_value=100000))
|
|
@settings(max_examples=50)
|
|
def test_validate_invalid_ports(self, port):
|
|
"""Test that invalid ports fail validation"""
|
|
with pytest.raises(ValidationError):
|
|
validate_port(port)
|
|
|
|
@given(st.just("test@example.com"))
|
|
@settings(max_examples=10)
|
|
def test_validate_valid_emails(self, email_addr):
|
|
"""Test that valid email addresses pass validation"""
|
|
assert validate_email(email_addr)
|
|
|
|
@given(st.text(min_size=1, max_size=50).filter(lambda x: '@' not in x))
|
|
@settings(max_examples=50)
|
|
def test_validate_invalid_emails(self, text):
|
|
"""Test that invalid email addresses fail validation"""
|
|
with pytest.raises(ValidationError):
|
|
validate_email(text)
|
|
|
|
@given(st.just("ait" + "a" * 10))
|
|
@settings(max_examples=10)
|
|
def test_validate_valid_address(self, address):
|
|
"""Test that valid AITBC addresses pass validation"""
|
|
assert validate_address(address)
|
|
|
|
@given(st.text(min_size=1, max_size=50).filter(lambda x: not x.startswith('ait')))
|
|
@settings(max_examples=50)
|
|
def test_validate_invalid_address_format(self, text):
|
|
"""Test that invalid address formats fail validation"""
|
|
with pytest.raises(ValidationError):
|
|
validate_address(text)
|
|
|
|
@given(st.just("a" * 64))
|
|
@settings(max_examples=10)
|
|
def test_validate_valid_hash(self, hash_str):
|
|
"""Test that valid hashes pass validation"""
|
|
assert validate_hash(hash_str)
|
|
|
|
@given(st.text(min_size=1, max_size=50).filter(lambda x: not x.isalnum() or len(x) != 64))
|
|
@settings(max_examples=50)
|
|
def test_validate_invalid_hash_format(self, text):
|
|
"""Test that invalid hash formats fail validation"""
|
|
with pytest.raises(ValidationError):
|
|
validate_hash(text)
|
|
|
|
@given(st.just("ait-mainnet"))
|
|
@settings(max_examples=10)
|
|
def test_validate_valid_chain_id(self, chain_id):
|
|
"""Test that valid chain IDs pass validation"""
|
|
assert validate_chain_id(chain_id)
|
|
|
|
@given(st.text(min_size=1, max_size=50).filter(lambda x: not x.replace('-', '').isalnum() and x.replace('-', '') != ''))
|
|
@settings(max_examples=50)
|
|
def test_validate_invalid_chain_id(self, text):
|
|
"""Test that invalid chain IDs fail validation"""
|
|
with pytest.raises(ValidationError):
|
|
validate_chain_id(text)
|
|
|
|
@given(st.uuids())
|
|
@settings(max_examples=50)
|
|
def test_validate_valid_uuid(self, uuid_obj):
|
|
"""Test that valid UUIDs pass validation"""
|
|
assert validate_uuid(str(uuid_obj))
|
|
|
|
@given(st.text(min_size=1, max_size=50).filter(lambda x: '-' not in x))
|
|
@settings(max_examples=50)
|
|
def test_validate_invalid_uuid(self, text):
|
|
"""Test that invalid UUIDs fail validation"""
|
|
with pytest.raises(ValidationError):
|
|
validate_uuid(text)
|
|
|
|
@given(st.just("http://localhost:8000"))
|
|
@settings(max_examples=10)
|
|
def test_validate_valid_url(self, url):
|
|
"""Test that valid URLs pass validation"""
|
|
assert validate_url(url)
|
|
|
|
@given(st.text(min_size=1, max_size=50).filter(lambda x: 'http' not in x and 'https' not in x))
|
|
@settings(max_examples=50)
|
|
def test_validate_invalid_url(self, text):
|
|
"""Test that invalid URLs fail validation"""
|
|
with pytest.raises(ValidationError):
|
|
validate_url(text)
|