aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJared Roesch <[email protected]>2014-10-11 01:50:34 -0700
committerJared Roesch <[email protected]>2014-10-11 01:57:33 -0700
commit5f017cd549b4c76849bfd5c33e6f6962acd89535 (patch)
tree4ea8770a90ca801e48e65032f756dccef069b5c5 /src
parentMerge pull request #62 from vhbit/feature-matrix (diff)
downloadrust-openssl-5f017cd549b4c76849bfd5c33e6f6962acd89535.tar.xz
rust-openssl-5f017cd549b4c76849bfd5c33e6f6962acd89535.zip
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.
Diffstat (limited to 'src')
-rwxr-xr-xsrc/ffi.rs12
-rw-r--r--src/ssl/error.rs47
-rw-r--r--src/ssl/mod.rs5
3 files changed, 43 insertions, 21 deletions
diff --git a/src/ffi.rs b/src/ffi.rs
index 0bdae5b6..46466d39 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 56105253..5af6c866 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 31307a03..33112f7a 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);