diff --git a/src/ffi.rs b/src/ffi.rs index 0bdae5b6e0a7911972e1a50ffe40688f3e3638bb..46466d39afac8e479dff249735b6cfeb361eb639 100755 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -2,6 +2,7 @@ #![allow(dead_code)] use libc::{c_void, c_int, c_char, c_ulong, c_long, c_uint, c_uchar, size_t}; use std::ptr; +use sync::one::{Once, ONCE_INIT}; pub use bn::BIGNUM; @@ -181,6 +182,17 @@ extern {} #[link(name="wsock32")] extern { } +pub fn init() { + static mut INIT: Once = ONCE_INIT; + + unsafe { + INIT.doit(|| { + SSL_library_init(); + SSL_load_error_strings() + }) + } +} + // Functions converted from macros pub unsafe fn BIO_eof(b: *mut BIO) -> bool { BIO_ctrl(b, BIO_CTRL_EOF, 0, ptr::null_mut()) == 1 diff --git a/src/ssl/error.rs b/src/ssl/error.rs index 56105253d2fbb51c5f2c80c338336a311b71f3b5..5af6c866b5f73f314ecac70295ccab92e3fe4bd3 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -41,19 +41,6 @@ fn get_reason(err: c_ulong) -> String { unsafe { CString::new(ffi::ERR_reason_error_string(err), false).to_string() } } -#[test] -#[ignore] // FIXME #65 -fn test_uknown_error_should_have_correct_messages() { - let err = 336032784; - let library = get_lib(err); - let function = get_func(err); - let reason = get_reason(err); - - assert_eq!(library.as_slice(),"SSL routines"); - assert_eq!(function.as_slice(), "SSL23_GET_SERVER_HELLO"); - assert_eq!(reason.as_slice(), "sslv3 alert handshake failure"); -} - impl SslError { /// Creates a new `OpenSslErrors` with the current contents of the error /// stack. @@ -62,13 +49,37 @@ impl SslError { loop { match unsafe { ffi::ERR_get_error() } { 0 => break, - err => errs.push(UnknownError { - library: get_lib(err), - function: get_func(err), - reason: get_reason(err) - }) + err => errs.push(SslError::from_error_code(err)) } } OpenSslErrors(errs) } + + /// Creates an `SslError` from the raw numeric error code. + pub fn from_error(err: c_ulong) -> SslError { + OpenSslErrors(vec![SslError::from_error_code(err)]) + } + + fn from_error_code(err: c_ulong) -> OpensslError { + ffi::init(); + UnknownError { + library: get_lib(err), + function: get_func(err), + reason: get_reason(err) + } + } +} + +#[test] +fn test_uknown_error_should_have_correct_messages() { + let errs = match SslError::from_error(336032784) { + OpenSslErrors(errs) => errs, + _ => fail!("This should always be an `OpenSslErrors` variant.") + }; + + let UnknownError { ref library, ref function, ref reason } = errs[0]; + + assert_eq!(library.as_slice(),"SSL routines"); + assert_eq!(function.as_slice(), "SSL23_GET_SERVER_HELLO"); + assert_eq!(reason.as_slice(), "sslv3 alert handshake failure"); } diff --git a/src/ssl/mod.rs b/src/ssl/mod.rs index 31307a032a77bb40e2c624259bf91e701e52cb91..33112f7af6a0dfe7672faaaa6dd98222142038d5 100644 --- a/src/ssl/mod.rs +++ b/src/ssl/mod.rs @@ -23,9 +23,8 @@ fn init() { unsafe { INIT.doit(|| { - ffi::SSL_library_init(); - ffi::SSL_load_error_strings(); - ffi::ERR_load_crypto_strings(); + ffi::init(); + let verify_idx = ffi::SSL_CTX_get_ex_new_index(0, ptr::null(), None, None, None); assert!(verify_idx >= 0);