49 lines
1.1 KiB
Python
49 lines
1.1 KiB
Python
"""Blowfish 加解密测试 (用已知明密文对)."""
|
|
import os
|
|
import pytest
|
|
from go1_pro_sdk import Blowfish, verify_state
|
|
from go1_pro_sdk.codec.blowfish import KNOWN_PAIRS
|
|
|
|
|
|
STATE_FILE = os.path.join(
|
|
os.path.dirname(__file__), '..', 'go1_pro_sdk', '_data', 'blowfish_state.bin'
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def bf():
|
|
return Blowfish.from_state_file(STATE_FILE)
|
|
|
|
|
|
def test_state_loads(bf):
|
|
assert len(bf.P) == 18
|
|
assert len(bf.S) == 4
|
|
assert all(len(s) == 256 for s in bf.S)
|
|
|
|
|
|
def test_state_verify(bf):
|
|
"""3/3 已知明密文对应该全部匹配."""
|
|
assert verify_state(bf) is True
|
|
|
|
|
|
def test_encrypt_known_pairs(bf):
|
|
for pt, ct in KNOWN_PAIRS:
|
|
assert bf.encrypt_block(pt) == ct
|
|
|
|
|
|
def test_decrypt_known_pairs(bf):
|
|
for pt, ct in KNOWN_PAIRS:
|
|
assert bf.decrypt_block(ct) == pt
|
|
|
|
|
|
def test_encrypt_decrypt_roundtrip(bf):
|
|
data = b'\x12\x34\x56\x78' * 100 # 400B
|
|
encrypted = bf.encrypt_ecb(data)
|
|
decrypted = bf.decrypt_ecb(encrypted)
|
|
assert data == decrypted
|
|
|
|
|
|
def test_encrypt_size_must_be_8x(bf):
|
|
with pytest.raises(AssertionError):
|
|
bf.encrypt_ecb(b'\x00' * 7)
|