From f50577909ebea982313657594d6a58578a305e11 Mon Sep 17 00:00:00 2001 From: Marko Lalic Date: Wed, 18 Mar 2015 20:55:07 +0100 Subject: [PATCH] openssl: Add tests for server-side NPN --- openssl/src/ssl/tests.rs | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/openssl/src/ssl/tests.rs b/openssl/src/ssl/tests.rs index 33ae619f..468b1053 100644 --- a/openssl/src/ssl/tests.rs +++ b/openssl/src/ssl/tests.rs @@ -292,3 +292,44 @@ fn test_connect_with_npn_successful_single_match() { // is used. assert_eq!(b"spdy/3.1", stream.get_selected_npn_protocol().unwrap()); } + +/// Tests that when the `SslStream` is created as a server stream, the protocols +/// are correctly advertised to the client. +#[test] +#[cfg(feature = "npn")] +fn test_npn_server_advertise_multiple() { + let localhost = "127.0.0.1:15420"; + let listener = TcpListener::bind(localhost).unwrap(); + // We create a different context instance for the server... + let listener_ctx = { + let mut ctx = SslContext::new(Sslv23).unwrap(); + ctx.set_verify(SslVerifyPeer, None); + ctx.set_npn_protocols(&[b"http/1.1", b"spdy/3.1"]); + ctx.set_certificate_file( + &Path::new("test/cert.pem"), X509FileType::PEM); + ctx.set_private_key_file( + &Path::new("test/key.pem"), X509FileType::PEM); + ctx + }; + // Have the listener wait on the connection in a different thread. + thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + let _ = SslStream::new_server(&listener_ctx, stream).unwrap(); + }); + + let mut ctx = SslContext::new(Sslv23).unwrap(); + ctx.set_verify(SslVerifyPeer, None); + ctx.set_npn_protocols(&[b"spdy/3.1"]); + match ctx.set_CA_file(&Path::new("test/cert.pem")) { + None => {} + Some(err) => panic!("Unexpected error {:?}", err) + } + // Now connect to the socket and make sure the protocol negotiation works... + let stream = TcpStream::connect(localhost).unwrap(); + let stream = match SslStream::new(&ctx, stream) { + Ok(stream) => stream, + Err(err) => panic!("Expected success, got {:?}", err) + }; + // SPDY is selected since that's the only thing the client supports. + assert_eq!(b"spdy/3.1", stream.get_selected_npn_protocol().unwrap()); +}