test_encoding.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import pytest
  2. from cq2_sdk.utils.encoding import encode6bit, decode6bit
  3. def test_encode_empty():
  4. assert encode6bit(b"") == b""
  5. def test_decode_empty():
  6. assert decode6bit(b"") == b""
  7. def test_encode_ascii_produces_printable():
  8. # 3 input bytes -> 4 output bytes
  9. result = encode6bit(b"abc")
  10. assert isinstance(result, bytes)
  11. assert len(result) == 4
  12. # All output bytes should be printable ASCII (>= 0x3B offset means >= 0x3B)
  13. for b in result:
  14. assert 0x3B <= b <= 0x3B + 0x3F # 0x3B to 0x7A
  15. def test_roundtrip_3_bytes():
  16. original = b"abc"
  17. assert decode6bit(encode6bit(original)) == original
  18. def test_roundtrip_typical_password():
  19. original = b"hello world 123 !@#"
  20. encoded = encode6bit(original)
  21. decoded = decode6bit(encoded)
  22. assert decoded == original
  23. @pytest.mark.parametrize("inp", [
  24. b"a",
  25. b"ab",
  26. b"abcd",
  27. b"abcdef",
  28. b"a" * 100,
  29. b"\x00\xff\x7f\x80",
  30. bytes(range(256)),
  31. ])
  32. def test_roundtrip_various_lengths(inp):
  33. encoded = encode6bit(inp)
  34. decoded = decode6bit(encoded)
  35. assert decoded == inp, f"failed for input of length {len(inp)}"
  36. def test_encode_known_vector_abc():
  37. # Literal derived from the extracted substitution table and ARM64 codec.
  38. assert encode6bit(b"abc") == b"xK_H"