11"""Unit tests for _HederaTrustManager certificate validation."""
2+
23import hashlib
34import pytest
45from src .hiero_sdk_python .node import _HederaTrustManager
@@ -11,7 +12,7 @@ def test_trust_manager_init_with_cert_hash():
1112 cert_hash = b"abc123def456"
1213 trust_manager = _HederaTrustManager (cert_hash , verify_certificate = True )
1314 # UTF-8 decodable strings are decoded directly, not converted to hex
14- assert trust_manager .cert_hash == cert_hash .decode (' utf-8' ).lower ()
15+ assert trust_manager .cert_hash == cert_hash .decode (" utf-8" ).lower ()
1516
1617
1718def test_trust_manager_init_with_utf8_hex_string ():
@@ -45,8 +46,10 @@ def test_trust_manager_check_server_trusted_matching_hash():
4546 pem_cert = b"-----BEGIN CERTIFICATE-----\n TEST_CERT\n -----END CERTIFICATE-----\n "
4647 cert_hash_bytes = hashlib .sha384 (pem_cert ).digest ()
4748 cert_hash_hex = cert_hash_bytes .hex ().lower ()
48-
49- trust_manager = _HederaTrustManager (cert_hash_hex .encode ('utf-8' ), verify_certificate = True )
49+
50+ trust_manager = _HederaTrustManager (
51+ cert_hash_hex .encode ("utf-8" ), verify_certificate = True
52+ )
5053 # Should not raise
5154 assert trust_manager .check_server_trusted (pem_cert ) is True
5255
@@ -55,17 +58,17 @@ def test_trust_manager_check_server_trusted_mismatched_hash():
5558 """Test certificate validation raises error on hash mismatch."""
5659 pem_cert = b"-----BEGIN CERTIFICATE-----\n TEST_CERT\n -----END CERTIFICATE-----\n "
5760 wrong_hash = b"wrong_hash_value"
58-
61+
5962 trust_manager = _HederaTrustManager (wrong_hash , verify_certificate = True )
60-
63+
6164 with pytest .raises (ValueError , match = "Failed to confirm the server's certificate" ):
6265 trust_manager .check_server_trusted (pem_cert )
6366
6467
6568def test_trust_manager_check_server_trusted_no_verification ():
6669 """Test certificate validation skipped when verification disabled."""
6770 pem_cert = b"-----BEGIN CERTIFICATE-----\n TEST_CERT\n -----END CERTIFICATE-----\n "
68-
71+
6972 trust_manager = _HederaTrustManager (None , verify_certificate = False )
7073 # Should not raise even without cert hash
7174 assert trust_manager .check_server_trusted (pem_cert ) is True
@@ -88,8 +91,7 @@ def test_trust_manager_normalize_hash_lowercase():
8891def test_trust_manager_normalize_hash_unicode_decode_error ():
8992 """Test hash normalization handles Unicode decode errors."""
9093 # Create bytes that can't be decoded as UTF-8
91- cert_hash = bytes ([0xff , 0xfe , 0xfd ])
94+ cert_hash = bytes ([0xFF , 0xFE , 0xFD ])
9295 trust_manager = _HederaTrustManager (cert_hash , verify_certificate = True )
9396 # Should fall back to hex encoding
9497 assert trust_manager .cert_hash == cert_hash .hex ().lower ()
95-
0 commit comments