1use cfg_if::cfg_if;
17use openssl_macros::corresponds;
18use std::ffi::CStr;
19
20cfg_if! {
21 if #[cfg(any(ossl110, libressl271))] {
22 use ffi::{
23 OPENSSL_VERSION, OPENSSL_CFLAGS, OPENSSL_BUILT_ON, OPENSSL_PLATFORM, OPENSSL_DIR,
24 OpenSSL_version_num, OpenSSL_version,
25 };
26 } else {
27 use ffi::{
28 SSLEAY_VERSION as OPENSSL_VERSION, SSLEAY_CFLAGS as OPENSSL_CFLAGS,
29 SSLEAY_BUILT_ON as OPENSSL_BUILT_ON, SSLEAY_PLATFORM as OPENSSL_PLATFORM,
30 SSLEAY_DIR as OPENSSL_DIR, SSLeay as OpenSSL_version_num,
31 SSLeay_version as OpenSSL_version,
32 };
33 }
34}
35
36#[corresponds(OpenSSL_version_num)]
48pub fn number() -> i64 {
49 unsafe { OpenSSL_version_num() as i64 }
50}
51
52#[corresponds(OpenSSL_version)]
54pub fn version() -> &'static str {
55 unsafe {
56 CStr::from_ptr(OpenSSL_version(OPENSSL_VERSION))
57 .to_str()
58 .unwrap()
59 }
60}
61
62#[corresponds(OpenSSL_version)]
65pub fn c_flags() -> &'static str {
66 unsafe {
67 CStr::from_ptr(OpenSSL_version(OPENSSL_CFLAGS))
68 .to_str()
69 .unwrap()
70 }
71}
72
73#[corresponds(OpenSSL_version)]
75pub fn built_on() -> &'static str {
76 unsafe {
77 CStr::from_ptr(OpenSSL_version(OPENSSL_BUILT_ON))
78 .to_str()
79 .unwrap()
80 }
81}
82
83#[corresponds(OpenSSL_version)]
85pub fn platform() -> &'static str {
86 unsafe {
87 CStr::from_ptr(OpenSSL_version(OPENSSL_PLATFORM))
88 .to_str()
89 .unwrap()
90 }
91}
92
93#[corresponds(OpenSSL_version)]
95pub fn dir() -> &'static str {
96 unsafe {
97 CStr::from_ptr(OpenSSL_version(OPENSSL_DIR))
98 .to_str()
99 .unwrap()
100 }
101}
102
103#[test]
106fn test_versions() {
107 println!("Number: '{}'", number());
108 println!("Version: '{}'", version());
109 println!("C flags: '{}'", c_flags());
110 println!("Built on: '{}'", built_on());
111 println!("Platform: '{}'", platform());
112 println!("Dir: '{}'", dir());
113
114 #[cfg(not(any(libressl, boringssl)))]
115 fn expected_name() -> &'static str {
116 "OpenSSL"
117 }
118 #[cfg(libressl)]
119 fn expected_name() -> &'static str {
120 "LibreSSL"
121 }
122 #[cfg(boringssl)]
123 fn expected_name() -> &'static str {
124 "BoringSSL"
125 }
126
127 assert!(number() > 0);
128 assert!(version().starts_with(expected_name()));
129 assert!(c_flags().starts_with("compiler:"));
130 if !built_on().is_empty() {
132 assert!(built_on().starts_with("built on:"));
133 }
134}