|
2 | 2 | import random
|
3 | 3 |
|
4 | 4 | import oqs
|
5 |
| -from oqs.oqs import Signature |
| 5 | +from oqs.oqs import Signature, native |
6 | 6 |
|
7 | 7 | # Sigs for which unit testing is disabled
|
8 | 8 | disabled_sig_patterns = []
|
@@ -44,6 +44,31 @@ def check_correctness_with_ctx_str(alg_name: str) -> None:
|
44 | 44 | assert sig.verify_with_ctx_str(message, signature, context, public_key) # noqa: S101
|
45 | 45 |
|
46 | 46 |
|
| 47 | +def test_sig_with_ctx_support_detection() -> None: |
| 48 | + """ |
| 49 | + Test that sig_with_ctx_support matches the C API and that sign_with_ctx_str |
| 50 | + raises on unsupported algorithms. |
| 51 | + """ |
| 52 | + for alg_name in oqs.get_enabled_sig_mechanisms(): |
| 53 | + with Signature(alg_name) as sig: |
| 54 | + # Check Python attribute matches C API |
| 55 | + c_api_result = native().OQS_SIG_supports_ctx_str(sig.method_name) |
| 56 | + assert bool(sig.sig_with_ctx_support) == bool(c_api_result), ( # noqa: S101 |
| 57 | + f"sig_with_ctx_support mismatch for {alg_name}" |
| 58 | + ) |
| 59 | + # If not supported, sign_with_ctx_str should raise |
| 60 | + if not sig.sig_with_ctx_support: |
| 61 | + try: |
| 62 | + sig.sign_with_ctx_str(b"msg", b"context") |
| 63 | + except RuntimeError as e: |
| 64 | + if "not supported" not in str(e): |
| 65 | + msg = f"Unexpected exception message: {e}" |
| 66 | + raise AssertionError(msg) from e |
| 67 | + else: |
| 68 | + msg = f"sign_with_ctx_str did not raise for {alg_name} without context support" |
| 69 | + raise AssertionError(msg) |
| 70 | + |
| 71 | + |
47 | 72 | def test_wrong_message() -> tuple[None, str]:
|
48 | 73 | for alg_name in oqs.get_enabled_sig_mechanisms():
|
49 | 74 | if any(item in alg_name for item in disabled_sig_patterns):
|
|
0 commit comments