Skip to content

Commit 0efc201

Browse files
committed
adding test cases
1 parent b28690a commit 0efc201

3 files changed

Lines changed: 39 additions & 12 deletions

File tree

awscrt/aws_iot_metrics.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def _certificate_source_metrics_value(source):
6868
6969
Mapping: CERTIFICATE_FILES->A, PKCS11->B, WINDOWS_CERT_STORE->C, PKCS12_FILE->E.
7070
"D" is reserved for Java KeyStore.
71+
Returns None if source is None or unrecognized.
7172
"""
7273
from awscrt.io import _CertificateSource
7374
mapping = {
@@ -237,7 +238,7 @@ def _get_encoded_feature_list(client_options):
237238
- D (outbound_topic_alias_behavior): from topic_aliasing_options.outbound_behavior
238239
- E (inbound_topic_alias_behavior): from topic_aliasing_options.inbound_behavior
239240
- H (http_proxy_type): HTTP or HTTPS based on proxy TLS settings
240-
- I (certificate_source): Detected from TlsContextOptions
241+
- I (certificate_source): detected from TlsContextOptions
241242
- J (tls_cipher_preference): mapped from TlsCipherPref on the TLS context
242243
- K (minimum_tls_version): mapped from TlsVersion on the TLS context
243244
@@ -292,7 +293,7 @@ def _get_encoded_feature_list(client_options):
292293
val = _http_proxy_type_metrics_value(client_options.http_proxy_options)
293294
features.append(f"{_MetricsFeatureId.HTTP_PROXY_TYPE.value}/{val}")
294295

295-
# I: certificate_source - Auto-detected from TlsContextOptions factory method
296+
# I: certificate_source - detected from TlsContextOptions factory method
296297
if client_options.tls_ctx is not None:
297298
val = _certificate_source_metrics_value(getattr(client_options.tls_ctx, '_certificate_source', None))
298299
if val:
@@ -325,7 +326,7 @@ def _get_encoded_feature_list_mqtt3(proxy_options, tls_ctx=None):
325326
326327
Conditionally includes:
327328
- H (http_proxy_type): HTTP or HTTPS based on proxy TLS settings
328-
- I (certificate_source): auto-detected from TlsContextOptions factory method
329+
- I (certificate_source): detected from TlsContextOptions
329330
- J (tls_cipher_preference): mapped from TlsCipherPref on the TLS context
330331
- K (minimum_tls_version): mapped from TlsVersion on the TLS context
331332
@@ -344,7 +345,7 @@ def _get_encoded_feature_list_mqtt3(proxy_options, tls_ctx=None):
344345
val = _http_proxy_type_metrics_value(proxy_options)
345346
features.append(f"{_MetricsFeatureId.HTTP_PROXY_TYPE.value}/{val}")
346347

347-
# I: certificate_source - Auto-detected from TlsContextOptions factory method
348+
# I: certificate_source - detected from TlsContextOptions factory method
348349
if tls_ctx is not None:
349350
val = _certificate_source_metrics_value(getattr(tls_ctx, '_certificate_source', None))
350351
if val:
@@ -461,7 +462,7 @@ def _create_metrics_mqtt5(client_options):
461462
"""Create the final AWSIoTMetrics object for an MQTT5 client.
462463
463464
Generates the CRT feature list from the full set of MQTT5 ClientOptions,
464-
including auto-detected certificate source from the TLS context.
465+
including detected certificate source from the TLS context.
465466
466467
Args:
467468
client_options: MQTT5 ClientOptions dataclass containing all
@@ -477,7 +478,7 @@ def _create_metrics_mqtt3(user_metrics=None, proxy_options=None, tls_ctx=None):
477478
"""Create the final AWSIoTMetrics object for an MQTT3 connection.
478479
479480
Generates the CRT feature list from the MQTT3 connection parameters,
480-
including auto-detected certificate source from the TLS context.
481+
including detected certificate source from the TLS context.
481482
482483
Args:
483484
user_metrics: Optional AWSIoTMetrics configuration provided by the IoT SDK.

awscrt/io.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ class _CertificateSource(IntEnum):
1919
"""Certificate source types for mTLS authentication.
2020
2121
Used by TlsContextOptions to track which factory method created the
22-
TLS configuration. The CRT detects this and encodes it as
22+
TLS configuration. The CRT reads this and encodes it as
2323
feature ID "I" in the metrics string.
2424
"""
25-
CERTIFICATE_FILES = 0
26-
PKCS11 = 1
27-
WINDOWS_CERT_STORE = 2
28-
PKCS12_FILE = 3
25+
CERTIFICATE_FILES = 0 # PEM cert + key files
26+
PKCS11 = 1 # Hardware security module via PKCS11
27+
WINDOWS_CERT_STORE = 2 # windows certificate source
28+
PKCS12_FILE = 4 # PKCS#12 (.p12/.pfx) files
2929

3030

3131
class LogLevel(IntEnum):

test/test_aws_iot_metrics.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
_protocol_version_metrics_value,
1212
_socket_implementation_metrics_value,
1313
_http_proxy_type_metrics_value,
14+
_certificate_source_metrics_value,
1415
IOT_SDK_METRICS_FEATURE_VERSION,
1516
_get_encoded_feature_list,
1617
_get_encoded_feature_list_mqtt3,
@@ -26,7 +27,7 @@
2627
InboundTopicAliasBehaviorType,
2728
TopicAliasingOptions,
2829
)
29-
from awscrt.io import ClientTlsContext, TlsContextOptions, TlsConnectionOptions, TlsVersion, TlsCipherPref
30+
from awscrt.io import ClientTlsContext, TlsContextOptions, TlsConnectionOptions, TlsVersion, TlsCipherPref, _CertificateSource
3031
from awscrt.http import HttpProxyOptions
3132

3233

@@ -419,5 +420,30 @@ def test_custom_library_name(self):
419420
self.assertEqual("MyCustomSDK/1.0", result.library_name)
420421

421422

423+
class TestCertificateSourceMetrics(NativeResourceTest):
424+
"""Test certificate source metrics encoding."""
425+
426+
def test_certificate_source_mapping_values(self):
427+
"""Verify each _CertificateSource maps to the correct metrics value."""
428+
self.assertEqual("A", _certificate_source_metrics_value(_CertificateSource.CERTIFICATE_FILES))
429+
self.assertEqual("B", _certificate_source_metrics_value(_CertificateSource.PKCS11))
430+
self.assertEqual("C", _certificate_source_metrics_value(_CertificateSource.WINDOWS_CERT_STORE))
431+
self.assertEqual("E", _certificate_source_metrics_value(_CertificateSource.PKCS12_FILE))
432+
self.assertIsNone(_certificate_source_metrics_value(None))
433+
434+
def test_certificate_source_in_feature_list(self):
435+
"""TLS context with certificate source should include feature I in metrics."""
436+
tls_options = TlsContextOptions()
437+
tls_options._certificate_source = _CertificateSource.CERTIFICATE_FILES
438+
tls_ctx = ClientTlsContext(tls_options)
439+
440+
options = ClientOptions(host_name="localhost", port=8883, tls_ctx=tls_ctx)
441+
result = _get_encoded_feature_list(options)
442+
self.assertIn(f"{_MetricsFeatureId.CERTIFICATE_SOURCE.value}/A", result)
443+
444+
result_mqtt3 = _get_encoded_feature_list_mqtt3(proxy_options=None, tls_ctx=tls_ctx)
445+
self.assertIn(f"{_MetricsFeatureId.CERTIFICATE_SOURCE.value}/A", result_mqtt3)
446+
447+
422448
if __name__ == '__main__':
423449
unittest.main()

0 commit comments

Comments
 (0)