Debian Codegraph @

crt/s2n/bindings/rust/standard/s2n-tls-metrics-subscriber/tests/public_api.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 //! Integration test verifying that the subscriber's opaque `MetricRecord` can be
5 //! serialized and then deserialized into the schema type by a downstream consumer.
6
7 /// The opaque `MetricRecord` from the subscriber can be serialized, then
8 /// deserialized into the schema type by a downstream consumer.
9 #[test]
10 fn opaque_serialize_then_schema_deserialize() {
11 use s2n_tls_metrics_subscriber::{
12 AggregatedMetricsSubscriber, Attribution, MetricRecord, TelemetrySink,
13 };
14 use std::sync::{Arc, Mutex};
15
16 #[derive(Clone)]
17 struct CaptureSink(Arc<Mutex<Vec<Vec<u8>>>>);
18 impl TelemetrySink for CaptureSink {
19 fn export_record(&self, record: &MetricRecord) {
20 let bytes = serde_json::to_vec(record).unwrap();
21 self.0.lock().unwrap().push(bytes);
22 }
23 }
24
25 let sink = CaptureSink(Arc::new(Mutex::new(Vec::new())));
26 let subscriber = AggregatedMetricsSubscriber::new(
27 sink.clone(),
28 Attribution {
29 service: "test".to_owned(),
30 resource: "r".to_owned(),
31 component: "frontend".to_owned(),
32 },
33 );
34
35 // Wire up and do a handshake
36 use s2n_tls::{
37 security::DEFAULT_TLS13,
38 testing::{build_config, config_builder},
39 };
40 let server_config = {
41 let mut c = config_builder(&DEFAULT_TLS13).unwrap();
42 c.set_event_subscriber(subscriber.clone()).unwrap();
43 c.build().unwrap()
44 };
45 let client_config = build_config(&DEFAULT_TLS13).unwrap();
46 let mut pair = s2n_tls::testing::TestPair::from_configs(&client_config, &server_config);
47 pair.handshake().unwrap();
48 subscriber.finish_record();
49
50 // Consumer deserializes with schema crate
51 let captured = sink.0.lock().unwrap();
52 assert_eq!(captured.len(), 1);
53 let schema_record: s2n_tls_metrics_schema::record::MetricRecord =
54 serde_json::from_slice(&captured[0]).unwrap();
55
56 assert_eq!(schema_record.attribution.service, "test");
57 assert_eq!(schema_record.attribution.component, "frontend");
58 assert_eq!(schema_record.handshake.handshake_success_count, 1);
59 assert!(
60 schema_record
61 .handshake
62 .negotiated_ciphers
63 .iter_non_zero()
64 .count()
65 > 0
66 );
67 }