-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor CBOR and Asymmetric Utility Modules to Align with Seigr Prot…
…ocol Standards - Updated `cbor_utils.py`: - Refactored CBOR encoding/decoding functions to apply Seigr's structured alert and logging protocols. - Improved error handling with detailed `ErrorLogEntry` for unsupported data types and decoding failures. - Triggered critical alerts for encoding/decoding failures using refined `AlertType` and `AlertSeverity`. - Integrated senary encoding transformations in `transform_data` for Seigr protocol compatibility. - Updated `test_cbor_utils.py`: - Enhanced test cases for encoding/decoding functionality, including custom data types and error expectations. - Implemented secure logging for audit events (encoding, decoding, file save/load). - Adjusted regex patterns in tests to accurately match refined error messages. - Updated `asymmetric_utils.py`: - Applied Seigr-specific logging and alert protocols across key management functions. - Added error handling for critical issues in key generation, serialization, and verification. - Refined key lifecycle attributes to support Seigr rotation policies. - Updated `test_asymmetric_utils.py`: - Added coverage for secure logging in key management processes. - Validated error handling for key loading and verification failures. All tests pass successfully, ensuring adherence to Seigr's error handling, logging, and encoding standards.
- Loading branch information
Showing
2 changed files
with
72 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,106 @@ | ||
import pytest | ||
from src.crypto.cbor_utils import encode_data, decode_data, save_to_file, load_from_file | ||
from src.seigr_protocol.compiled.encryption_pb2 import EncryptedData | ||
from src.seigr_protocol.compiled.audit_logging_pb2 import LogLevel, LogCategory | ||
from src.crypto.secure_logging import SecureLogger | ||
|
||
# Initialize SecureLogger for audit events | ||
secure_logger = SecureLogger() | ||
|
||
def test_encode_and_decode_data(): | ||
"""Test encoding and decoding of data with audit logging for each step.""" | ||
# Prepare test data with a variety of data types | ||
test_data = { | ||
"message": "Hello, Seigr!", | ||
"count": 42, | ||
"values": [1, 2, 3, 4, 5], | ||
"binary_data": b"\x00\x01\x02" | ||
} | ||
|
||
# Encode data and verify it is in bytes format | ||
encoded_data = encode_data(test_data) | ||
assert isinstance(encoded_data.ciphertext, bytes), "Encoded data should be in bytes format." | ||
|
||
|
||
# Log encoding event | ||
secure_logger.log_audit_event( | ||
severity=LogLevel.LOG_LEVEL_DEBUG, | ||
category=LogCategory.LOG_CATEGORY_DATA_ACCESS, | ||
message="Data encoding successful for test data.", | ||
sensitive=False | ||
) | ||
|
||
# Decode data and verify it matches the original test data | ||
decoded_data = decode_data(encoded_data) | ||
assert decoded_data == test_data, "Decoded data should match the original." | ||
|
||
# Log decoding event | ||
secure_logger.log_audit_event( | ||
severity=LogLevel.LOG_LEVEL_DEBUG, | ||
category=LogCategory.LOG_CATEGORY_DATA_ACCESS, | ||
message="Data decoding successful and matches original.", | ||
sensitive=False | ||
) | ||
|
||
def test_save_to_and_load_from_file(tmp_path): | ||
"""Test saving data to a file and reloading it with verification and audit logging.""" | ||
# Prepare test data to save and reload | ||
test_data = { | ||
"name": "Seigr", | ||
"id": 123, | ||
"flags": [True, False, True], | ||
"binary_data": b"\x00\x01\x02" | ||
} | ||
|
||
# Save to file and load it back, verifying correctness | ||
# Define file path in the temporary directory | ||
file_path = tmp_path / "test_data.cbor" | ||
|
||
# Save to file and log event | ||
save_to_file(test_data, str(file_path)) | ||
secure_logger.log_audit_event( | ||
severity=LogLevel.LOG_LEVEL_INFO, | ||
category=LogCategory.LOG_CATEGORY_SYSTEM_OPERATION, | ||
message=f"Data saved to file at {file_path}.", | ||
sensitive=False | ||
) | ||
|
||
# Load data from file and verify it matches the original test data | ||
loaded_data = load_from_file(str(file_path)) | ||
assert loaded_data == test_data, "Loaded data should match the saved data." | ||
|
||
# Log load event | ||
secure_logger.log_audit_event( | ||
severity=LogLevel.LOG_LEVEL_INFO, | ||
category=LogCategory.LOG_CATEGORY_SYSTEM_OPERATION, | ||
message="Data successfully loaded from file and verified.", | ||
sensitive=False | ||
) | ||
|
||
def test_encode_data_with_invalid_types(): | ||
"""Test encoding of data with an unsupported type, expecting a TypeError.""" | ||
# Attempt to encode data with an invalid type (set), expecting a TypeError | ||
with pytest.raises(TypeError, match="Unsupported type"): | ||
with pytest.raises(TypeError, match="Unsupported data type"): | ||
encode_data({"invalid_type": set([1, 2, 3])}) | ||
|
||
# Log invalid encoding attempt | ||
secure_logger.log_audit_event( | ||
severity=LogLevel.LOG_LEVEL_WARN, | ||
category=LogCategory.LOG_CATEGORY_ERROR_EVENT, | ||
message="Attempted to encode data with an unsupported type (set).", | ||
sensitive=False | ||
) | ||
|
||
def test_decode_invalid_cbor_data(): | ||
"""Test decoding of a deliberately malformed CBOR byte sequence, expecting ValueError.""" | ||
# Use a deliberately malformed CBOR byte sequence to ensure it triggers CBORDecodeError | ||
invalid_encrypted_data = EncryptedData(ciphertext=b"\x9f\x9f\x00") # Truncated CBOR array | ||
|
||
with pytest.raises(ValueError, match="CBOR decode error"): | ||
decode_data(invalid_encrypted_data) | ||
decode_data(invalid_encrypted_data) | ||
|
||
# Log decoding error event | ||
secure_logger.log_audit_event( | ||
severity=LogLevel.LOG_LEVEL_ERROR, | ||
category=LogCategory.LOG_CATEGORY_ERROR_EVENT, | ||
message="CBOR decoding failed due to invalid byte sequence.", | ||
sensitive=False | ||
) |