"""Tests for validate module.""" import json import pytest from ect.validate import ( EXT_MAX_DEPTH, EXT_MAX_SIZE, validate_ext, validate_hash_format, valid_uuid, ) def test_valid_uuid(): assert valid_uuid("550e8400-e29b-41d4-a716-446655440000") is True assert valid_uuid("00000000-0000-0000-0000-000000000000") is True assert valid_uuid("") is False assert valid_uuid("not-a-uuid") is False assert valid_uuid("550e8400e29b41d4a716446655440000") is False # no dashes def test_validate_ext_none(): validate_ext(None) validate_ext({}) def test_validate_ext_size(): # Serialized JSON must exceed EXT_MAX_SIZE (4096) bytes big = {"x": "y" * (EXT_MAX_SIZE - 2)} # "{\"x\":\"...\"}" + payload raw = json.dumps(big) assert len(raw.encode("utf-8")) > EXT_MAX_SIZE with pytest.raises(ValueError, match="max size"): validate_ext(big) def test_validate_ext_depth(): deep = {"a": 1} for _ in range(EXT_MAX_DEPTH): deep = {"n": deep} with pytest.raises(ValueError, match="depth"): validate_ext(deep) def test_validate_hash_format_empty(): validate_hash_format("") def test_validate_hash_format_ok(): # sha-256:base64url (minimal valid) validate_hash_format("sha-256:YQ") validate_hash_format("sha-384:YQ") validate_hash_format("sha-512:YQ") def test_validate_hash_format_bad(): with pytest.raises(ValueError, match="algorithm:base64url|inp_hash"): validate_hash_format("md5:abc") with pytest.raises(ValueError, match="algorithm:base64url|inp_hash"): validate_hash_format("no-colon") # Invalid base64 that triggers decode error (e.g. binary) with pytest.raises(ValueError, match="algorithm:base64url|inp_hash"): validate_hash_format("sha-256:YQ\x00") # null in payload