Debian Codegraph @

crt/s2n/bindings/rust/standard/integration/tests/metrics_stability.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 //! This test verifies that the most recent released version of
5 //! s2n-tls-metrics-subscriber (currently 0.0.3) builds and runs against the
6 //! mainline s2n-tls bindings.
7 //!
8 //! If this file fails to compile, it means mainline has broken backward
9 //! compatibility with the released subscriber. The fix is to restore the
10 //! missing APIs (with #[deprecated] annotations) in the bindings.
11
12 use old_metrics_subscriber::{
13 AggregatedMetricsSubscriber, Attribution, MetricRecord, TelemetrySink,
14 };
15 use s2n_tls::{
16 security::DEFAULT_TLS13,
17 testing::{build_config, config_builder, TestPair},
18 };
19 use std::sync::{Arc, Mutex};
20
21 #[derive(Debug, Clone)]
22 struct VecSink {
23 records: Arc<Mutex<Vec<MetricRecord>>>,
24 }
25
26 impl VecSink {
27 fn new() -> Self {
28 Self {
29 records: Arc::new(Mutex::new(Vec::new())),
30 }
31 }
32 }
33
34 impl TelemetrySink for VecSink {
35 fn export_record(&self, record: &MetricRecord) {
36 self.records.lock().unwrap().push(record.clone());
37 }
38 }
39
40 /// The released s2n-tls-metrics-subscriber 0.0.3 must build and run
41 /// a handshake against mainline s2n-tls bindings.
42 #[test]
43 fn old_event_subscriber_builds() {
44 let sink = VecSink::new();
45 let attribution = Attribution {
46 service: "test-service".to_owned(),
47 resource: "test-resource".to_owned(),
48 component: "test-component".to_owned(),
49 };
50 let subscriber = AggregatedMetricsSubscriber::new(sink.clone(), attribution);
51
52 let server_config = {
53 let mut cfg = config_builder(&DEFAULT_TLS13).unwrap();
54 cfg.set_event_subscriber(subscriber.clone()).unwrap();
55 cfg.build().unwrap()
56 };
57 let client_config = build_config(&DEFAULT_TLS13).unwrap();
58
59 let mut pair = TestPair::from_configs(&client_config, &server_config);
60 pair.handshake().unwrap();
61
62 subscriber.finish_record();
63
64 let records = sink.records.lock().unwrap();
65 assert_eq!(records.len(), 1);
66
67 // Serialize the record and assert on the content to verify the event APIs
68 // populated the subscriber correctly (not just that it compiled).
69 let json: serde_json::Value = serde_json::to_value(&records[0]).unwrap();
70 let handshake = &json["handshake"];
71
72 // One successful handshake was recorded
73 assert_eq!(handshake["handshake_count"], 1);
74 assert_eq!(handshake["synthetic_traffic_count"], 0);
75
76 // Negotiated parameters: exactly one of each was counted
77 assert_eq!(total_count(&handshake["negotiated_protocols"]), 1);
78 assert_eq!(total_count(&handshake["negotiated_ciphers"]), 1);
79 assert_eq!(total_count(&handshake["negotiated_groups"]), 1);
80 assert_eq!(total_count(&handshake["negotiated_signatures"]), 1);
81
82 // Supported parameters from the client hello should be non-empty
83 assert!(total_count(&handshake["supported_protocols"]) >= 1);
84 assert!(total_count(&handshake["supported_ciphers"]) >= 1);
85 assert!(total_count(&handshake["supported_groups"]) >= 1);
86 assert!(total_count(&handshake["supported_signatures"]) >= 1);
87
88 // Timers were populated (non-zero)
89 assert!(handshake["handshake_duration_us"].as_u64().unwrap() > 0);
90 assert!(handshake["handshake_compute_us"].as_u64().unwrap() > 0);
91
92 // Attribution was passed through
93 assert_eq!(json["attribution"]["service"], "test-service");
94 assert_eq!(json["attribution"]["resource"], "test-resource");
95 assert_eq!(json["attribution"]["component"], "test-component");
96 }
97
98 /// Sum all counts in a frozen counter. The serialized form is an array of
99 /// `[key, count]` pairs (where key may itself be a nested array).
100 fn total_count(counter: &serde_json::Value) -> u64 {
101 counter
102 .as_array()
103 .expect("counter should be an array")
104 .iter()
105 .filter_map(|entry| {
106 let arr = entry.as_array()?;
107 arr.last()?.as_u64()
108 })
109 .sum()
110 }