test_login_cipher.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """Tests for loginInfo RSA encryption (login_cipher.py).
  2. Verifies the output format matches the live capture: base64url-ish
  3. (+ -> -, / -> _), and decryption with the known public key structure
  4. round-trips (we can't decrypt without the private key, but we can
  5. verify format + length for a 1024-bit RSA = 128 bytes -> 172 base64 chars).
  6. """
  7. import base64
  8. import json
  9. from cq2_sdk.login.login_cipher import encrypt_login_info, _load_public_key
  10. def test_output_is_base64url_charset():
  11. out = encrypt_login_info("longwu988", "Longwu1988")
  12. assert "+" not in out
  13. assert "/" not in out
  14. # reverse substitutions to validate it's proper base64
  15. b64 = out.replace("-", "+").replace("_", "/")
  16. raw = base64.b64decode(b64)
  17. # 1024-bit RSA output = 128 bytes
  18. assert len(raw) == 128
  19. def test_output_nondeterministic_pkcs1_padding():
  20. """PKCS1v15 uses random padding -> two encryptions differ."""
  21. a = encrypt_login_info("user", "pass")
  22. b = encrypt_login_info("user", "pass")
  23. assert a != b
  24. def test_public_key_loads():
  25. key = _load_public_key()
  26. assert key.key_size == 1024
  27. def test_payload_is_json_username_password():
  28. """The plaintext being encrypted is JSON{username,password} (documented).
  29. We can't decrypt, but we verify the documented contract exists in the
  30. function signature and it doesn't crash on unicode."""
  31. out = encrypt_login_info("玩家测试", "密码123")
  32. assert len(out) > 100 # sane base64 length