Refactor init and error handling code
Move common ffi initialization code to 'ffi::init()' and the initialization of error handling to a a shared location.
This commit is contained in:
parent
c407530dae
commit
5f017cd549
12
src/ffi.rs
12
src/ffi.rs
|
|
@ -2,6 +2,7 @@
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
use libc::{c_void, c_int, c_char, c_ulong, c_long, c_uint, c_uchar, size_t};
|
use libc::{c_void, c_int, c_char, c_ulong, c_long, c_uint, c_uchar, size_t};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
use sync::one::{Once, ONCE_INIT};
|
||||||
|
|
||||||
pub use bn::BIGNUM;
|
pub use bn::BIGNUM;
|
||||||
|
|
||||||
|
|
@ -181,6 +182,17 @@ extern {}
|
||||||
#[link(name="wsock32")]
|
#[link(name="wsock32")]
|
||||||
extern { }
|
extern { }
|
||||||
|
|
||||||
|
pub fn init() {
|
||||||
|
static mut INIT: Once = ONCE_INIT;
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
INIT.doit(|| {
|
||||||
|
SSL_library_init();
|
||||||
|
SSL_load_error_strings()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Functions converted from macros
|
// Functions converted from macros
|
||||||
pub unsafe fn BIO_eof(b: *mut BIO) -> bool {
|
pub unsafe fn BIO_eof(b: *mut BIO) -> bool {
|
||||||
BIO_ctrl(b, BIO_CTRL_EOF, 0, ptr::null_mut()) == 1
|
BIO_ctrl(b, BIO_CTRL_EOF, 0, ptr::null_mut()) == 1
|
||||||
|
|
|
||||||
|
|
@ -41,19 +41,6 @@ fn get_reason(err: c_ulong) -> String {
|
||||||
unsafe { CString::new(ffi::ERR_reason_error_string(err), false).to_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 {
|
impl SslError {
|
||||||
/// Creates a new `OpenSslErrors` with the current contents of the error
|
/// Creates a new `OpenSslErrors` with the current contents of the error
|
||||||
/// stack.
|
/// stack.
|
||||||
|
|
@ -62,13 +49,37 @@ impl SslError {
|
||||||
loop {
|
loop {
|
||||||
match unsafe { ffi::ERR_get_error() } {
|
match unsafe { ffi::ERR_get_error() } {
|
||||||
0 => break,
|
0 => break,
|
||||||
err => errs.push(UnknownError {
|
err => errs.push(SslError::from_error_code(err))
|
||||||
library: get_lib(err),
|
|
||||||
function: get_func(err),
|
|
||||||
reason: get_reason(err)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
OpenSslErrors(errs)
|
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");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,9 +23,8 @@ fn init() {
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
INIT.doit(|| {
|
INIT.doit(|| {
|
||||||
ffi::SSL_library_init();
|
ffi::init();
|
||||||
ffi::SSL_load_error_strings();
|
|
||||||
ffi::ERR_load_crypto_strings();
|
|
||||||
let verify_idx = ffi::SSL_CTX_get_ex_new_index(0, ptr::null(), None,
|
let verify_idx = ffi::SSL_CTX_get_ex_new_index(0, ptr::null(), None,
|
||||||
None, None);
|
None, None);
|
||||||
assert!(verify_idx >= 0);
|
assert!(verify_idx >= 0);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue