Debian Codegraph @

crt/s2n/bindings/rust/standard/s2n-tls-metrics-schema/tests/static_lists.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 use s2n_tls_metrics_schema::static_lists::*;
5 use s2n_tls_sys_internal::{
6 s2n_cipher_suite, s2n_ecc_named_curve, s2n_kem_group, s2n_signature_scheme,
7 };
8 use std::{
9 collections::HashSet,
10 ffi::{CStr, c_char},
11 };
12
13 unsafe fn static_memory_to_str(value: *const c_char) -> &'static str {
14 unsafe { CStr::from_ptr(value).to_str().unwrap() }
15 }
16
17 fn cipher_info_from_s2n(s2n_cipher: &s2n_cipher_suite) -> CipherInformation {
18 unsafe {
19 let openssl_name = static_memory_to_str(s2n_cipher.name);
20 let iana_description = static_memory_to_str(s2n_cipher.iana_name);
21 let iana_value = s2n_cipher.iana_value;
22 CipherInformation::new(iana_description, iana_value, openssl_name)
23 }
24 }
25
26 fn group_info_from_s2n_kem(kem_group: &s2n_kem_group) -> GroupInformation {
27 unsafe {
28 let name = static_memory_to_str(kem_group.name);
29 let iana_id = kem_group.iana_id;
30 GroupInformation::new(name, iana_id)
31 }
32 }
33
34 fn group_info_from_s2n_ecc(curve: &s2n_ecc_named_curve) -> GroupInformation {
35 unsafe {
36 let name = static_memory_to_str(curve.name);
37 let iana_id = curve.iana_id;
38 GroupInformation::new(name, iana_id)
39 }
40 }
41
42 fn sig_info_from_s2n(scheme: &s2n_signature_scheme) -> SignatureSchemeInformation {
43 unsafe {
44 let name = static_memory_to_str(scheme.name);
45 let iana_value = scheme.iana_value;
46 SignatureSchemeInformation::new(name, iana_value)
47 }
48 }
49
50 fn all_available_ciphers() -> Vec<CipherInformation> {
51 let ciphers: HashSet<CipherInformation> = s2n_tls_sys_internal::security_policy_table()
52 .iter()
53 .flat_map(|sp| {
54 let sp = unsafe { &*sp.security_policy };
55 sp.ciphers()
56 .iter()
57 .cloned()
58 .map(cipher_info_from_s2n)
59 .collect::<Vec<_>>()
60 })
61 .collect();
62 let mut ciphers: Vec<CipherInformation> = ciphers.into_iter().collect();
63 ciphers.sort_by_key(|cipher| cipher.iana_description);
64 ciphers
65 }
66
67 fn all_available_groups() -> Vec<GroupInformation> {
68 let groups: HashSet<GroupInformation> = s2n_tls_sys_internal::security_policy_table()
69 .iter()
70 .flat_map(|sp| {
71 let sp = unsafe { &*sp.security_policy };
72 let curves = sp
73 .curves()
74 .iter()
75 .map(|curve| group_info_from_s2n_ecc(curve));
76 let kem_groups = sp.kems().iter().map(|kem| group_info_from_s2n_kem(kem));
77 curves.chain(kem_groups).collect::<Vec<GroupInformation>>()
78 })
79 .collect();
80 let mut groups: Vec<GroupInformation> = groups.into_iter().collect();
81 groups.sort_by_key(|group| group.iana_description);
82 groups
83 }
84
85 fn all_available_signatures() -> Vec<SignatureSchemeInformation> {
86 let sigs: HashSet<SignatureSchemeInformation> = s2n_tls_sys_internal::security_policy_table()
87 .iter()
88 .flat_map(|sp| {
89 let sp = unsafe { &*sp.security_policy };
90 sp.signatures().iter().map(|sig| sig_info_from_s2n(sig))
91 })
92 .collect();
93 let mut sigs: Vec<SignatureSchemeInformation> = sigs.into_iter().collect();
94 sigs.sort_by_key(|sig| sig.description);
95 sigs
96 }
97
98 #[test]
99 fn all_ciphers_in_static_list() {
100 let ciphers = all_available_ciphers();
101 assert_eq!(ciphers.as_slice(), &CIPHERS_AVAILABLE_IN_S2N[..]);
102 }
103
104 #[test]
105 fn all_groups_in_static_list() {
106 let groups = all_available_groups();
107 assert_eq!(groups.as_slice(), &GROUPS_AVAILABLE_IN_S2N[..]);
108 }
109
110 #[test]
111 fn all_signature_schemes_in_static_list() {
112 let schemes = all_available_signatures();
113 assert_eq!(schemes.as_slice(), &SIGNATURE_SCHEMES_AVAILABLE_IN_S2N[..]);
114 }