Commit f5057790 authored by Marko Lalic's avatar Marko Lalic
Browse files

openssl: Add tests for server-side NPN

parent 8f05e045
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
@@ -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());
}