use oxitls_core::{CipherSuite, ConnectionInfo, TlsError, TlsVersion};
#[test]
fn from_rustls_error_maps_no_certs_to_cert_invalid() {
let e = rustls::Error::NoCertificatesPresented;
let t: TlsError = e.into();
assert!(
matches!(t, TlsError::CertInvalid(_)),
"expected CertInvalid, got {t:?}"
);
}
#[test]
fn from_rustls_error_maps_peer_incompatible_to_protocol_violation() {
let e = rustls::Error::PeerIncompatible(rustls::PeerIncompatible::Tls12NotOffered);
let t: TlsError = e.into();
assert!(
matches!(t, TlsError::ProtocolViolation(_)),
"expected ProtocolViolation, got {t:?}"
);
}
#[test]
fn from_rustls_error_maps_peer_misbehaved_to_protocol_violation() {
let e = rustls::Error::PeerMisbehaved(rustls::PeerMisbehaved::UnsolicitedCertExtension);
let t: TlsError = e.into();
assert!(
matches!(t, TlsError::ProtocolViolation(_)),
"expected ProtocolViolation, got {t:?}"
);
}
#[test]
fn from_rustls_error_maps_alert_to_handshake() {
let e = rustls::Error::AlertReceived(rustls::AlertDescription::HandshakeFailure);
let t: TlsError = e.into();
assert!(
matches!(t, TlsError::Handshake(_)),
"expected Handshake, got {t:?}"
);
}
#[test]
fn from_rustls_error_fallback_maps_to_other() {
let e = rustls::Error::DecryptError;
let t: TlsError = e.into();
assert!(matches!(t, TlsError::Other(_)), "expected Other, got {t:?}");
}
#[test]
fn from_rustls_error_general_fault_maps_to_other() {
let e = rustls::Error::General("some general fault".to_string());
let t: TlsError = e.into();
assert!(
matches!(t, TlsError::Other(_)),
"expected Other for General, got {t:?}"
);
}
#[test]
fn cert_invalid_is_cert() {
let e = TlsError::CertInvalid("reason".to_string());
assert!(e.is_cert(), "CertInvalid should be a cert error");
assert!(!e.is_handshake());
assert!(!e.is_io());
}
#[test]
fn protocol_violation_predicate() {
let e = TlsError::ProtocolViolation("something".to_string());
assert!(e.is_protocol_violation());
assert!(!e.is_cert());
}
#[test]
fn cert_invalid_display() {
let e = TlsError::CertInvalid("BadSignature".to_string());
assert!(
e.to_string().starts_with("invalid certificate:"),
"unexpected display: {e}"
);
}
#[test]
fn protocol_violation_display() {
let e = TlsError::ProtocolViolation("misbehaved".to_string());
assert!(
e.to_string().starts_with("protocol violation:"),
"unexpected display: {e}"
);
}
#[test]
fn cipher_suite_unknown_iana_value() {
assert_eq!(CipherSuite::Unknown.iana_value(), [0xFF, 0xFF]);
}
#[test]
fn cipher_suite_unknown_is_not_tls13() {
assert!(!CipherSuite::Unknown.is_tls13());
assert!(CipherSuite::Unknown.is_unknown());
}
#[test]
fn cipher_suite_unknown_display() {
assert_eq!(CipherSuite::Unknown.to_string(), "UNKNOWN");
}
#[test]
fn cipher_suite_unknown_from_str() {
let s: CipherSuite = "UNKNOWN".parse().expect("should parse UNKNOWN");
assert_eq!(s, CipherSuite::Unknown);
}
#[test]
fn connection_info_from_signature_is_exported() {
let _ = oxitls_core::connection_info_from::<rustls::Connection>;
}
#[test]
fn connection_info_with_all_fields() {
let info = ConnectionInfo::new()
.with_version(TlsVersion::Tls13)
.with_cipher_suite(CipherSuite::Tls13Aes256GcmSha384)
.with_alpn_protocol(b"h2".to_vec())
.with_sni("secure.example.com".to_string());
assert_eq!(info.version, Some(TlsVersion::Tls13));
assert_eq!(info.cipher_suite, Some(CipherSuite::Tls13Aes256GcmSha384));
assert_eq!(info.alpn_protocol_str(), Some("h2"));
assert_eq!(info.sni.as_deref(), Some("secure.example.com"));
}