Skip to content

Commit

Permalink
tests: add integration test for UdsConnectInfo
Browse files Browse the repository at this point in the history
Refs: hyperium#856

Signed-off-by: Anthony Green <agreen@starry.com>
  • Loading branch information
agreen17 committed Dec 8, 2021
1 parent 0746070 commit a4552da
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/integration_tests/Cargo.toml
Expand Up @@ -12,7 +12,7 @@ version = "0.1.0"
bytes = "1.0"
futures-util = "0.3"
prost = "0.9"
tokio = {version = "1.0", features = ["macros", "rt-multi-thread", "net"]}
tokio = {version = "1.0", features = ["fs", "macros", "rt-multi-thread", "net"]}
tonic = {path = "../../tonic"}

[dev-dependencies]
Expand Down
73 changes: 73 additions & 0 deletions tests/integration_tests/tests/connect_info.rs
Expand Up @@ -48,3 +48,76 @@ async fn getting_connect_info() {

jh.await.unwrap();
}

#[cfg(unix)]
pub mod unix {
use std::path::Path;

use futures_util::FutureExt;
use tokio::{
net::{UnixListener, UnixStream},
sync::oneshot,
};
use tokio_stream::wrappers::UnixListenerStream;
use tonic::{
transport::{server::UdsConnectInfo, Endpoint, Server, Uri},
Request, Response, Status,
};
use tower::service_fn;

use integration_tests::pb::{test_client, test_server, Input, Output};

struct Svc {}

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
let conn_info = req.extensions().get::<UdsConnectInfo>().unwrap();

// Client-side unix sockets are unnamed.
assert!(req.remote_addr().is_none());
assert!(conn_info.peer_addr.as_ref().unwrap().is_unnamed());
// This should contain process credentials for the client socket.
assert!(conn_info.peer_cred.as_ref().is_some());

Ok(Response::new(Output {}))
}
}

#[tokio::test]
async fn getting_connect_info() {
let unix_socket_path = "/tmp/uds-integration-test";
let uds = UnixListener::bind(unix_socket_path).unwrap();
let uds_stream = UnixListenerStream::new(uds);

let service = test_server::TestServer::new(Svc {});
let (tx, rx) = oneshot::channel::<()>();

let jh = tokio::spawn(async move {
Server::builder()
.add_service(service)
.serve_with_incoming_shutdown(uds_stream, rx.map(drop))
.await
.unwrap();
});

let channel = Endpoint::try_from("http://[::]:50051")
.unwrap()
.connect_with_connector(service_fn(move |_: Uri| {
UnixStream::connect(unix_socket_path)
}))
.await
.unwrap();

let mut client = test_client::TestClient::new(channel);

client.unary_call(Input {}).await.unwrap();

tx.send(()).unwrap();
jh.await.unwrap();

// tokio's `UnixListener` does not cleanup the socket automatically - we need to manually
// remove the file at the end of the test.
tokio::fs::remove_file(unix_socket_path).await.unwrap();
}
}

0 comments on commit a4552da

Please sign in to comment.