aboutsummaryrefslogtreecommitdiff
path: root/openssl/src/ssl/tests.rs
diff options
context:
space:
mode:
authorManuel Schölling <[email protected]>2015-03-10 14:31:54 +0100
committerManuel Schölling <[email protected]>2015-04-06 12:22:50 +0200
commit664600eadff8a0388bc9ab2544b382e56e4fae9d (patch)
tree782893fbf9232424f4d67f374012da6f33f6262f /openssl/src/ssl/tests.rs
parentAdd connect() support for UDP sockets (diff)
downloadrust-openssl-664600eadff8a0388bc9ab2544b382e56e4fae9d.tar.xz
rust-openssl-664600eadff8a0388bc9ab2544b382e56e4fae9d.zip
Add DTLSv1 and DTLSv1.2 support
Diffstat (limited to 'openssl/src/ssl/tests.rs')
-rw-r--r--openssl/src/ssl/tests.rs215
1 files changed, 135 insertions, 80 deletions
diff --git a/openssl/src/ssl/tests.rs b/openssl/src/ssl/tests.rs
index 1da42082..4d78e182 100644
--- a/openssl/src/ssl/tests.rs
+++ b/openssl/src/ssl/tests.rs
@@ -1,4 +1,5 @@
-use serialize::hex::FromHex;
+#![allow(unused_imports)]
+
use std::net::TcpStream;
use std::io;
use std::io::prelude::*;
@@ -13,6 +14,8 @@ use crypto::hash::Type::{SHA256};
use ssl;
use ssl::SslMethod;
use ssl::SslMethod::Sslv23;
+#[cfg(feature="dtlsv1")]
+use ssl::SslMethod::Dtlsv1;
use ssl::{SslContext, SslStream, VerifyCallback};
use ssl::SSL_VERIFY_PEER;
use x509::X509StoreContext;
@@ -21,34 +24,89 @@ use x509::X509FileType;
use x509::X509;
use crypto::pkey::PKey;
+#[cfg(feature="dtlsv1")]
+use ssl::connected_socket::Connect;
+#[cfg(feature="dtlsv1")]
+use std::net::UdpSocket;
+
const PROTOCOL:SslMethod = Sslv23;
+mod udp {
+ static mut udp_port:u16 = 15410;
+
+ pub fn next_server<'a>() -> String {
+ unsafe {
+ udp_port += 1;
+ format!("127.0.0.1:{}", udp_port)
+ }
+ }
+}
+
#[test]
fn test_new_ctx() {
SslContext::new(PROTOCOL).unwrap();
}
-#[test]
-fn test_new_sslstream() {
- let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
- SslStream::new(&SslContext::new(PROTOCOL).unwrap(), stream).unwrap();
-}
+macro_rules! run_test(
+ ($module:ident, $blk:expr) => (
+ #[cfg(test)]
+ mod $module {
+ use ssl::tests::udp;
+ use std::io;
+ use std::io::prelude::*;
+ use std::path::Path;
+ use std::net::UdpSocket;
+ use std::net::TcpStream;
+ use ssl::SslMethod::Sslv23;
+ #[cfg(feature="dtlsv1")]
+ use ssl;
+ use ssl::SslMethod::Dtlsv1;
+ use ssl::{SslContext, SslStream, VerifyCallback};
+ use ssl::connected_socket::Connect;
+ use ssl::SslVerifyMode::SSL_VERIFY_PEER;
+ use crypto::hash::Type::SHA256;
+ use x509::X509StoreContext;
+ use serialize::hex::FromHex;
+ use std::time::duration::Duration;
+
+ #[test]
+ fn sslv23() {
+ let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
+ $blk(Sslv23, stream);
+ }
+
+ #[test]
+ #[cfg(feature="dtlsv1")]
+ fn dtlsv1() {
+ let sock = UdpSocket::bind("127.0.0.1:0").unwrap();
+ let stream = sock.connect(udp::next_server().as_slice()).unwrap();
+
+ $blk(Dtlsv1, stream);
+ }
+ }
+ );
+);
-#[test]
-fn test_verify_untrusted() {
- let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
- let mut ctx = SslContext::new(PROTOCOL).unwrap();
+run_test!(new_ctx, |method, _| {
+ SslContext::new(method).unwrap();
+});
+
+run_test!(new_sslstream, |method, stream| {
+ SslStream::new(&SslContext::new(method).unwrap(), stream).unwrap();
+});
+
+run_test!(verify_untrusted, |method, stream| {
+ let mut ctx = SslContext::new(method).unwrap();
ctx.set_verify(SSL_VERIFY_PEER, None);
+
match SslStream::new(&ctx, stream) {
Ok(_) => panic!("expected failure"),
Err(err) => println!("error {:?}", err)
}
-}
+});
-#[test]
-fn test_verify_trusted() {
- let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
- let mut ctx = SslContext::new(PROTOCOL).unwrap();
+run_test!(verify_trusted, |method, stream| {
+ let mut ctx = SslContext::new(method).unwrap();
ctx.set_verify(SSL_VERIFY_PEER, None);
match ctx.set_CA_file(&Path::new("test/cert.pem")) {
@@ -59,42 +117,39 @@ fn test_verify_trusted() {
Ok(_) => (),
Err(err) => panic!("Expected success, got {:?}", err)
}
-}
+});
-#[test]
-fn test_verify_untrusted_callback_override_ok() {
+run_test!(verify_untrusted_callback_override_ok, |method, stream| {
fn callback(_preverify_ok: bool, _x509_ctx: &X509StoreContext) -> bool {
true
}
- let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
- let mut ctx = SslContext::new(PROTOCOL).unwrap();
+
+ let mut ctx = SslContext::new(method).unwrap();
ctx.set_verify(SSL_VERIFY_PEER, Some(callback as VerifyCallback));
match SslStream::new(&ctx, stream) {
Ok(_) => (),
Err(err) => panic!("Expected success, got {:?}", err)
}
-}
+});
-#[test]
-fn test_verify_untrusted_callback_override_bad() {
+run_test!(verify_untrusted_callback_override_bad, |method, stream| {
fn callback(_preverify_ok: bool, _x509_ctx: &X509StoreContext) -> bool {
false
}
- let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
- let mut ctx = SslContext::new(PROTOCOL).unwrap();
+
+ let mut ctx = SslContext::new(method).unwrap();
ctx.set_verify(SSL_VERIFY_PEER, Some(callback as VerifyCallback));
assert!(SslStream::new(&ctx, stream).is_err());
-}
+});
-#[test]
-fn test_verify_trusted_callback_override_ok() {
+run_test!(verify_trusted_callback_override_ok, |method, stream| {
fn callback(_preverify_ok: bool, _x509_ctx: &X509StoreContext) -> bool {
true
}
- let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
- let mut ctx = SslContext::new(PROTOCOL).unwrap();
+
+ let mut ctx = SslContext::new(method).unwrap();
ctx.set_verify(SSL_VERIFY_PEER, Some(callback as VerifyCallback));
match ctx.set_CA_file(&Path::new("test/cert.pem")) {
@@ -105,15 +160,14 @@ fn test_verify_trusted_callback_override_ok() {
Ok(_) => (),
Err(err) => panic!("Expected success, got {:?}", err)
}
-}
+});
-#[test]
-fn test_verify_trusted_callback_override_bad() {
+run_test!(verify_trusted_callback_override_bad, |method, stream| {
fn callback(_preverify_ok: bool, _x509_ctx: &X509StoreContext) -> bool {
false
}
- let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
- let mut ctx = SslContext::new(PROTOCOL).unwrap();
+
+ let mut ctx = SslContext::new(method).unwrap();
ctx.set_verify(SSL_VERIFY_PEER, Some(callback as VerifyCallback));
match ctx.set_CA_file(&Path::new("test/cert.pem")) {
@@ -121,29 +175,27 @@ fn test_verify_trusted_callback_override_bad() {
Err(err) => panic!("Unexpected error {:?}", err)
}
assert!(SslStream::new(&ctx, stream).is_err());
-}
+});
-#[test]
-fn test_verify_callback_load_certs() {
+run_test!(verify_callback_load_certs, |method, stream| {
fn callback(_preverify_ok: bool, x509_ctx: &X509StoreContext) -> bool {
assert!(x509_ctx.get_current_cert().is_some());
true
}
- let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
- let mut ctx = SslContext::new(PROTOCOL).unwrap();
+
+ let mut ctx = SslContext::new(method).unwrap();
ctx.set_verify(SSL_VERIFY_PEER, Some(callback as VerifyCallback));
assert!(SslStream::new(&ctx, stream).is_ok());
-}
+});
-#[test]
-fn test_verify_trusted_get_error_ok() {
+run_test!(verify_trusted_get_error_ok, |method, stream| {
fn callback(_preverify_ok: bool, x509_ctx: &X509StoreContext) -> bool {
assert!(x509_ctx.get_error().is_none());
true
}
- let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
- let mut ctx = SslContext::new(PROTOCOL).unwrap();
+
+ let mut ctx = SslContext::new(method).unwrap();
ctx.set_verify(SSL_VERIFY_PEER, Some(callback as VerifyCallback));
match ctx.set_CA_file(&Path::new("test/cert.pem")) {
@@ -151,23 +203,21 @@ fn test_verify_trusted_get_error_ok() {
Err(err) => panic!("Unexpected error {:?}", err)
}
assert!(SslStream::new(&ctx, stream).is_ok());
-}
+});
-#[test]
-fn test_verify_trusted_get_error_err() {
+run_test!(verify_trusted_get_error_err, |method, stream| {
fn callback(_preverify_ok: bool, x509_ctx: &X509StoreContext) -> bool {
assert!(x509_ctx.get_error().is_some());
false
}
- let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
- let mut ctx = SslContext::new(PROTOCOL).unwrap();
+
+ let mut ctx = SslContext::new(method).unwrap();
ctx.set_verify(SSL_VERIFY_PEER, Some(callback as VerifyCallback));
assert!(SslStream::new(&ctx, stream).is_err());
-}
+});
-#[test]
-fn test_verify_callback_data() {
+run_test!(verify_callback_data, |method, stream| {
fn callback(_preverify_ok: bool, x509_ctx: &X509StoreContext, node_id: &Vec<u8>) -> bool {
let cert = x509_ctx.get_current_cert();
match cert {
@@ -178,8 +228,7 @@ fn test_verify_callback_data() {
}
}
}
- let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
- let mut ctx = SslContext::new(PROTOCOL).unwrap();
+ let mut ctx = SslContext::new(method).unwrap();
// Node id was generated as SHA256 hash of certificate "test/cert.pem"
// in DER format.
@@ -194,7 +243,7 @@ fn test_verify_callback_data() {
Ok(_) => (),
Err(err) => panic!("Expected success, got {:?}", err)
}
-}
+});
#[test]
fn test_set_certificate_and_private_key() {
@@ -217,51 +266,46 @@ fn test_set_certificate_and_private_key() {
assert!(ctx.check_private_key().is_ok());
}
-#[test]
-fn test_get_ctx_options() {
- let mut ctx = SslContext::new(Sslv23).unwrap();
+run_test!(get_ctx_options, |method, _| {
+ let mut ctx = SslContext::new(method).unwrap();
ctx.get_options();
-}
+});
-#[test]
-fn test_set_ctx_options() {
- let mut ctx = SslContext::new(Sslv23).unwrap();
+run_test!(set_ctx_options, |method, _| {
+ let mut ctx = SslContext::new(method).unwrap();
let opts = ctx.set_options(ssl::SSL_OP_NO_TICKET);
assert!(opts.contains(ssl::SSL_OP_NO_TICKET));
assert!(!opts.contains(ssl::SSL_OP_CISCO_ANYCONNECT));
let more_opts = ctx.set_options(ssl::SSL_OP_CISCO_ANYCONNECT);
assert!(more_opts.contains(ssl::SSL_OP_NO_TICKET));
assert!(more_opts.contains(ssl::SSL_OP_CISCO_ANYCONNECT));
-}
+});
-#[test]
-fn test_clear_ctx_options() {
- let mut ctx = SslContext::new(Sslv23).unwrap();
+run_test!(clear_ctx_options, |method, _| {
+ let mut ctx = SslContext::new(method).unwrap();
ctx.set_options(ssl::SSL_OP_ALL);
let opts = ctx.clear_options(ssl::SSL_OP_ALL);
assert!(!opts.contains(ssl::SSL_OP_ALL));
-}
+});
-#[test]
-fn test_write() {
- let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
- let mut stream = SslStream::new(&SslContext::new(PROTOCOL).unwrap(), stream).unwrap();
- stream.write_all("hello".as_bytes()).unwrap();
- stream.flush().unwrap();
- stream.write_all(" there".as_bytes()).unwrap();
- stream.flush().unwrap();
-}
+run_test!(write, |method, stream| {
+ let mut s = SslStream::new(&SslContext::new(method).unwrap(), stream).unwrap();
+ s.write_all("hello".as_bytes()).unwrap();
+ s.flush().unwrap();
+ s.write_all(" there".as_bytes()).unwrap();
+ s.flush().unwrap();
+});
#[test]
fn test_read() {
- let stream = TcpStream::connect("127.0.0.1:15418").unwrap();
- let mut stream = SslStream::new(&SslContext::new(PROTOCOL).unwrap(), stream).unwrap();
+ let tcp = TcpStream::connect("127.0.0.1:15418").unwrap();
+ let mut stream = SslStream::new(&SslContext::new(Sslv23).unwrap(), tcp).unwrap();
stream.write_all("GET /\r\n\r\n".as_bytes()).unwrap();
stream.flush().unwrap();
- println!("written");
io::copy(&mut stream, &mut io::sink()).ok().expect("read error");
}
+
/// Tests that connecting with the client using NPN, but the server not does not
/// break the existing connection behavior.
#[test]
@@ -396,3 +440,14 @@ mod dtlsv1 {
SslContext::new(PROTOCOL).unwrap();
}
}
+
+#[test]
+#[cfg(feature = "dtlsv1")]
+fn test_read_dtlsv1() {
+ let sock = UdpSocket::bind("127.0.0.1:0").unwrap();
+ let stream = sock.connect(udp::next_server().as_slice()).unwrap();
+
+ let mut stream = SslStream::new(&SslContext::new(Dtlsv1).unwrap(), stream).unwrap();
+ let mut buf = [0u8;100];
+ assert!(stream.read(&mut buf).is_ok());
+}