Skip to content

Commit

Permalink
Allow selection of TLS library in Interop tests (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
jen20 authored and LucioFranco committed Nov 10, 2019
1 parent 23e7695 commit 79996f7
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 28 deletions.
40 changes: 34 additions & 6 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Check fmt
run: cargo fmt -- --check
- name: Check features
run: cargo hack check --all --each-feature --no-dev-deps
run: cargo hack check --all --ignore-private --each-feature --no-dev-deps
- name: Check all targets
run: cargo check --all --all-targets --all-features

Expand All @@ -53,11 +53,12 @@ jobs:
- name: Run tests
run: cargo test --all --all-features

interop:
interop-unix:
name: Interop Tests (Rustls & OpenSSL)
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
os: [ubuntu-latest, macOS-latest]
rust: [stable]

env:
Expand All @@ -67,12 +68,39 @@ jobs:
- uses: hecrj/setup-rust-action@master
with:
rust-version: ${{ matrix.rust }}
- uses: actions/checkout@master
- name: Install rustfmt
run: rustup component add rustfmt
- uses: actions/checkout@master
- name: Run interop tests
run: ./tonic-interop/test.sh
shell: bash
- name: Run interop tests with tls
run: ./tonic-interop/test.sh --use_tls
- name: Run interop tests with Rustls
run: ./tonic-interop/test.sh --use_tls tls_rustls
shell: bash
- name: Run interop tests with OpenSSL
run: ./tonic-interop/test.sh --use_tls tls_openssl
shell: bash

interop-windows:
name: Interop Tests (Rustls)
runs-on: windows-latest
strategy:
matrix:
rust: [stable]

env:
RUSTFLAGS: "-D warnings"

steps:
- uses: hecrj/setup-rust-action@master
with:
rust-version: ${{ matrix.rust }}
- name: Install rustfmt
run: rustup component add rustfmt
- uses: actions/checkout@master
- name: Run interop tests
run: ./tonic-interop/test.sh
shell: bash
- name: Run interop tests with Rustls
run: ./tonic-interop/test.sh --use_tls tls_rustls
shell: bash
1 change: 1 addition & 0 deletions tonic-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "tonic-examples"
version = "0.1.0"
authors = ["Lucio Franco <luciofranco14@gmail.com>"]
edition = "2018"
publish = false

[[bin]]
name = "helloworld-server"
Expand Down
8 changes: 7 additions & 1 deletion tonic-interop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ name = "tonic-interop"
version = "0.1.0"
authors = ["Lucio Franco <luciofranco14@gmail.com>"]
edition = "2018"
publish = false

[features]
default = ["tonic"]
tls_openssl = ["tonic", "tonic/tls", "tonic/openssl"]
tls_rustls = ["tonic", "tonic/tls", "tonic/rustls"]

[[bin]]
name = "client"
Expand All @@ -14,7 +20,7 @@ path = "src/bin/server.rs"

[dependencies]
tokio = "=0.2.0-alpha.6"
tonic = { path = "../tonic", features = ["rustls"] }
tonic = { path = "../tonic", optional = true }
prost = "0.5"
prost-derive = "0.5"
bytes = "0.4"
Expand Down
39 changes: 30 additions & 9 deletions tonic-interop/src/bin/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::time::Duration;
use structopt::{clap::arg_enum, StructOpt};
use tonic::transport::{Certificate, ClientTlsConfig, Endpoint};
use tonic::transport::Endpoint;
#[cfg(any(feature = "tls_rustls", feature = "tls_openssl"))]
use tonic::transport::{Certificate, ClientTlsConfig};
use tonic_interop::client;

#[derive(StructOpt)]
Expand All @@ -25,20 +27,39 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let test_cases = matches.test_case;

#[allow(unused_mut)]
let mut endpoint = Endpoint::from_static("http://localhost:10000")
.timeout(Duration::from_secs(5))
.concurrency_limit(30)
.clone();

if matches.use_tls {
let pem = tokio::fs::read("tonic-interop/data/ca.pem").await?;
let ca = Certificate::from_pem(pem);

endpoint.tls_config(
ClientTlsConfig::with_rustls()
.ca_certificate(ca)
.domain_name("foo.test.google.fr"),
);
#[cfg(not(any(feature = "tls_rustls", feature = "tls_openssl")))]
{
panic!("No TLS libary feature selected");
}

#[cfg(feature = "tls_rustls")]
{
let pem = tokio::fs::read("tonic-interop/data/ca.pem").await?;
let ca = Certificate::from_pem(pem);
endpoint.tls_config(
ClientTlsConfig::with_rustls()
.ca_certificate(ca)
.domain_name("foo.test.google.fr"),
);
}

#[cfg(feature = "tls_openssl")]
{
let pem = tokio::fs::read("tonic-interop/data/ca.pem").await?;
let ca = Certificate::from_pem(pem);
endpoint.tls_config(
ClientTlsConfig::with_openssl()
.ca_certificate(ca)
.domain_name("foo.test.google.fr"),
);
}
}

let channel = endpoint.connect().await?;
Expand Down
29 changes: 24 additions & 5 deletions tonic-interop/src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use http::header::HeaderName;
use structopt::StructOpt;
use tonic::body::BoxBody;
use tonic::client::GrpcService;
use tonic::transport::{Identity, Server, ServerTlsConfig};
use tonic::transport::Server;
#[cfg(any(feature = "tls_rustls", feature = "tls_openssl"))]
use tonic::transport::{Identity, ServerTlsConfig};
use tonic_interop::{server, MergeTrailers};

#[derive(StructOpt)]
Expand All @@ -22,11 +24,28 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let mut builder = Server::builder();

if matches.use_tls {
let cert = tokio::fs::read("tonic-interop/data/server1.pem").await?;
let key = tokio::fs::read("tonic-interop/data/server1.key").await?;
#[cfg(not(any(feature = "tls_rustls", feature = "tls_openssl")))]
{
panic!("No TLS libary feature selected");
}

#[cfg(feature = "tls_rustls")]
{
let cert = tokio::fs::read("tonic-interop/data/server1.pem").await?;
let key = tokio::fs::read("tonic-interop/data/server1.key").await?;
let identity = Identity::from_pem(cert, key);

builder.tls_config(ServerTlsConfig::with_rustls().identity(identity));
}

#[cfg(feature = "tls_openssl")]
{
let cert = tokio::fs::read("tonic-interop/data/server1.pem").await?;
let key = tokio::fs::read("tonic-interop/data/server1.key").await?;
let identity = Identity::from_pem(cert, key);

let identity = Identity::from_pem(cert, key);
builder.tls_config(ServerTlsConfig::with_rustls().identity(identity));
builder.tls_config(ServerTlsConfig::with_openssl().identity(identity));
}
}

builder.interceptor_fn(|svc, req| {
Expand Down
24 changes: 17 additions & 7 deletions tonic-interop/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,36 @@
set -eu
set -o pipefail

set -x

echo "Running for OS: ${OSTYPE}"

case "$OSTYPE" in
darwin*) OS="darwin"; EXT="" ;;
linux*) OS="linux"; EXT="" ;;
msys*) OS="windows"; EXT=".exe" ;;
msys*) OS="windows"; EXT=".exe" ;;
*) exit 2 ;;
esac

cargo build -p tonic-interop --bins

ARG="${1:-""}"
TLS_PROVIDER="${2:-""}"

if [[ -n "${TLS_PROVIDER}" ]] ; then
FEATURES="--features ${TLS_PROVIDER}"
else
FEATURES=
fi

(cd tonic-interop && cargo build --bins ${FEATURES})

SERVER="tonic-interop/bin/server_${OS}_amd64${EXT}"

# TLS_CA="tonic-interop/data/ca.pem"
TLS_CRT="tonic-interop/data/server1.pem"
TLS_KEY="tonic-interop/data/server1.key"

# run the test server
./"${SERVER}" $ARG --tls_cert_file $TLS_CRT --tls_key_file $TLS_KEY &
./"${SERVER}" ${ARG} --tls_cert_file $TLS_CRT --tls_key_file $TLS_KEY &
SERVER_PID=$!
echo ":; started grpc-go test server."

Expand All @@ -35,12 +45,12 @@ sleep 1
./target/debug/client \
--test_case=empty_unary,large_unary,client_streaming,server_streaming,ping_pong,\
empty_stream,status_code_and_message,special_status_message,unimplemented_method,\
unimplemented_service,custom_metadata $ARG
unimplemented_service,custom_metadata ${ARG}

echo ":; killing test server"; kill ${SERVER_PID};

# run the test server
./target/debug/server $ARG &
./target/debug/server ${ARG} &
SERVER_PID=$!
echo ":; started tonic test server."

Expand All @@ -53,4 +63,4 @@ sleep 1
./target/debug/client \
--test_case=empty_unary,large_unary,client_streaming,server_streaming,ping_pong,\
empty_stream,status_code_and_message,special_status_message,unimplemented_method,\
unimplemented_service,custom_metadata $ARG
unimplemented_service,custom_metadata ${ARG}

0 comments on commit 79996f7

Please sign in to comment.