From 02637ec7d451c38792c42c5c2cb4d59505e13ced Mon Sep 17 00:00:00 2001 From: Valerii Hiora Date: Sun, 28 Sep 2014 08:15:51 +0300 Subject: single `ffi` module --- src/ssl/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ssl/error.rs') diff --git a/src/ssl/error.rs b/src/ssl/error.rs index 9af14dd9..452f8aad 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -1,7 +1,7 @@ use libc::c_ulong; use std::io::IoError; -use ssl::ffi; +use ffi; /// An SSL error #[deriving(Show, Clone, PartialEq, Eq)] -- cgit v1.2.3 From b3c80a76ddbd871c9ef64762d4e532c3a55b903f Mon Sep 17 00:00:00 2001 From: Jared Roesch Date: Tue, 30 Sep 2014 14:34:34 -0700 Subject: Make errors human readable Change error messages from numeric codes to human readable strings. This makes debugging failures much easier. --- src/ssl/error.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src/ssl/error.rs') diff --git a/src/ssl/error.rs b/src/ssl/error.rs index 452f8aad..b7688595 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -1,5 +1,6 @@ use libc::c_ulong; use std::io::IoError; +use std::c_str::CString; use ffi; @@ -20,24 +21,24 @@ pub enum OpensslError { /// An unknown error UnknownError { /// The library reporting the error - library: u8, + library: CString, /// The function reporting the error - function: u16, + function: CString, /// The reason for the error - reason: u16 + reason: CString } } -fn get_lib(err: c_ulong) -> u8 { - ((err >> 24) & 0xff) as u8 +fn get_lib(err: c_ulong) -> CString { + unsafe { CString::new(ffi::ERR_lib_error_string(err), false) } } -fn get_func(err: c_ulong) -> u16 { - ((err >> 12) & 0xfff) as u16 +fn get_func(err: c_ulong) -> CString { + unsafe { CString::new(ffi::ERR_func_error_string(err), false) } } -fn get_reason(err: c_ulong) -> u16 { - (err & 0xfff) as u16 +fn get_reason(err: c_ulong) -> CString { + unsafe { CString::new(ffi::ERR_reason_error_string(err), false) } } impl SslError { -- cgit v1.2.3 From 02c124a1fecee45f06e08314554502fce7a8190a Mon Sep 17 00:00:00 2001 From: Jared Roesch Date: Thu, 2 Oct 2014 02:05:49 -0700 Subject: Address CR comments and add a test --- src/ssl/error.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'src/ssl/error.rs') diff --git a/src/ssl/error.rs b/src/ssl/error.rs index b7688595..a1e8eadc 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -21,24 +21,36 @@ pub enum OpensslError { /// An unknown error UnknownError { /// The library reporting the error - library: CString, + library: String, /// The function reporting the error - function: CString, + function: String, /// The reason for the error - reason: CString + reason: String } } -fn get_lib(err: c_ulong) -> CString { - unsafe { CString::new(ffi::ERR_lib_error_string(err), false) } +fn get_lib(err: c_ulong) -> String { + unsafe { CString::new(ffi::ERR_lib_error_string(err), false) }.to_string() } -fn get_func(err: c_ulong) -> CString { - unsafe { CString::new(ffi::ERR_func_error_string(err), false) } +fn get_func(err: c_ulong) -> String { + unsafe { CString::new(ffi::ERR_func_error_string(err), false).to_string() } } -fn get_reason(err: c_ulong) -> CString { - unsafe { CString::new(ffi::ERR_reason_error_string(err), false) } +fn get_reason(err: c_ulong) -> String { + unsafe { CString::new(ffi::ERR_reason_error_string(err), false).to_string() } +} + +#[test] +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(),"20"); + assert_eq!(function.as_slice(), "119"); + assert_eq!(reason.as_slice(), "1040"); } impl SslError { -- cgit v1.2.3 From 5713c42df72da232b21dd5c55b91a527227174bb Mon Sep 17 00:00:00 2001 From: Jared Roesch Date: Thu, 2 Oct 2014 02:11:35 -0700 Subject: Fix error messages --- src/ssl/error.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/ssl/error.rs') diff --git a/src/ssl/error.rs b/src/ssl/error.rs index a1e8eadc..0c1af90a 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -48,9 +48,9 @@ fn test_uknown_error_should_have_correct_messages() { let function = get_func(err); let reason = get_reason(err); - assert_eq!(library.as_slice(),"20"); - assert_eq!(function.as_slice(), "119"); - assert_eq!(reason.as_slice(), "1040"); + 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 { -- cgit v1.2.3 From 6231a39a4187ac2728038d35216aed60b9d93075 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 5 Oct 2014 13:47:20 -0700 Subject: Ignore error string text cc #65 --- src/ssl/error.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'src/ssl/error.rs') diff --git a/src/ssl/error.rs b/src/ssl/error.rs index 0c1af90a..56105253 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -42,6 +42,7 @@ fn get_reason(err: c_ulong) -> String { } #[test] +#[ignore] // FIXME #65 fn test_uknown_error_should_have_correct_messages() { let err = 336032784; let library = get_lib(err); -- cgit v1.2.3 From 7e214fe8a885a1a0c7b16680be1e33eaa2852ceb Mon Sep 17 00:00:00 2001 From: Jared Roesch Date: Mon, 6 Oct 2014 01:53:56 -0700 Subject: Fix #65: failing test case --- src/ssl/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ssl/error.rs') diff --git a/src/ssl/error.rs b/src/ssl/error.rs index 56105253..56df2747 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -42,8 +42,8 @@ fn get_reason(err: c_ulong) -> String { } #[test] -#[ignore] // FIXME #65 fn test_uknown_error_should_have_correct_messages() { + unsafe { ffi::SSL_load_error_strings(); } let err = 336032784; let library = get_lib(err); let function = get_func(err); -- cgit v1.2.3 From fec1c43a4a348ba6356ab7c80961cc2652a2602e Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Tue, 7 Oct 2014 23:18:20 -0400 Subject: Revert "Fix #65: failing test case" --- src/ssl/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ssl/error.rs') diff --git a/src/ssl/error.rs b/src/ssl/error.rs index 56df2747..56105253 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -42,8 +42,8 @@ fn get_reason(err: c_ulong) -> String { } #[test] +#[ignore] // FIXME #65 fn test_uknown_error_should_have_correct_messages() { - unsafe { ffi::SSL_load_error_strings(); } let err = 336032784; let library = get_lib(err); let function = get_func(err); -- cgit v1.2.3 From 5f017cd549b4c76849bfd5c33e6f6962acd89535 Mon Sep 17 00:00:00 2001 From: Jared Roesch Date: Sat, 11 Oct 2014 01:50:34 -0700 Subject: 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. --- src/ssl/error.rs | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) (limited to 'src/ssl/error.rs') 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"); } -- cgit v1.2.3 From 1eb79df25abf3eede1f9d799992927986ce8c7a0 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Thu, 30 Oct 2014 09:58:22 +0100 Subject: fail! -> panic! --- src/ssl/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ssl/error.rs') diff --git a/src/ssl/error.rs b/src/ssl/error.rs index 5af6c866..7066299f 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -74,7 +74,7 @@ impl SslError { 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.") + _ => panic!("This should always be an `OpenSslErrors` variant.") }; let UnknownError { ref library, ref function, ref reason } = errs[0]; -- cgit v1.2.3 From 2569b398556dcec70ba2dc4369327172bedef375 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 16 Nov 2014 22:21:45 -0800 Subject: Impl Error for SslError --- src/ssl/error.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src/ssl/error.rs') diff --git a/src/ssl/error.rs b/src/ssl/error.rs index 7066299f..7e8daef1 100644 --- a/src/ssl/error.rs +++ b/src/ssl/error.rs @@ -1,4 +1,8 @@ +pub use self::SslError::*; +pub use self::OpensslError::*; + use libc::c_ulong; +use std::error; use std::io::IoError; use std::c_str::CString; @@ -7,7 +11,7 @@ use ffi; /// An SSL error #[deriving(Show, Clone, PartialEq, Eq)] pub enum SslError { - /// The underlying stream has reported an error + /// The underlying stream reported an error StreamError(IoError), /// The SSL session has been closed by the other end SslSessionClosed, @@ -15,6 +19,23 @@ pub enum SslError { OpenSslErrors(Vec) } +impl error::Error for SslError { + fn description(&self) -> &str { + match *self { + StreamError(_) => "The underlying stream reported an error", + SslSessionClosed => "The SSL session has been closed by the other end", + OpenSslErrors(_) => "An error in the OpenSSL library", + } + } + + fn cause(&self) -> Option<&error::Error> { + match *self { + StreamError(ref err) => Some(err as &error::Error), + _ => None + } + } +} + /// An error from the OpenSSL library #[deriving(Show, Clone, PartialEq, Eq)] pub enum OpensslError { -- cgit v1.2.3