Commit 5f017cd5 authored by Jared Roesch's avatar Jared Roesch
Browse files

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.
parent c407530d
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -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
+29 −18
Original line number Diff line number Diff line
@@ -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 {
                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)
                })
        }
    }
        OpenSslErrors(errs)
}

#[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");
}
+2 −3
Original line number Diff line number Diff line
@@ -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);