Skip to content

Instantly share code, notes, and snippets.

@jamisonhyatt
Created May 11, 2018 16:37
Show Gist options
  • Save jamisonhyatt/ef343767a429861c039ff4e8de33b09c to your computer and use it in GitHub Desktop.
Save jamisonhyatt/ef343767a429861c039ff4e8de33b09c to your computer and use it in GitHub Desktop.
Test gRPC TLS Server
//The below code is untested - it was modified from working, but non-public code.
// testServer represents an actual running grpc server
type testServer struct {
address string
listener net.Listener
server *grpc.Server
}
func newTestServer(t *testing.T) *testServer {
//go command to generate a cert and key; see https://golang.org/src/crypto/tls/generate_cert.go
// creates `cert.pem and key.pem`
goroot := os.Getenv("GOROOT")
out, err := exec.Command("go", "run", fmt.Sprintf("%s/src/crypto/tls/generate_cert.go", goroot),
"--rsa-bits", "1024", "--host", "127.0.0.1,::1,localhost", "--ca", "--start-date", `Jan 1 00:00:00 1970`, "--duration=1000000h").CombinedOutput()
if err != nil {
fmt.Printf("%s: %s\n", string(out), err.Error())
t.Fatalf("error creating certs: %s", err)
}
creds, err := credentials.NewServerTLSFromFile("cert.pem", "key.pem")
if err != nil {
logger.Fatal("Failed to generate credentials", zap.Error(err))
}
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("failed to create listener: %v", err)
}
ts := testServer{}
ts.address = lis.Addr().String()
ts.listener = lis
ts.server = grpc.NewServer(grpc.Creds(creds))
return &ts
}
func (ts *testServer) startRouteGuideServer(rgs routeguide.RouteGuideServer) error {
routeguide.RegisterRouteGuideServer(ts.server, rgs)
return ts.server.Serve(ts.listener)
}
func (ts *testServer) gracefulShutdown() {
os.Remove("cert.pem")
os.Remove("key.pem")
ts.server.GracefulStop()
}
func TestFeature(t *testing.T) {
mockedRouteGuide := &RouteGuideServerMock{
GetFeatureFunc: func(in1 context.Context, in2 *routeguide.Point) (*routeguide.Feature, error) {
return &routeguide.Feature{Name: "ExpectedFeatureName"}, nil
},
}
ts := newTestServer(t)
go ts.startRouteGuideServer(mockedRouteGuide)
defer ts.gracefulShutdown()
fmt.Printf("started route guide server on %s\n", ts.address)
time.Sleep(time.Second * 1)
transportCreds, err := credentials.NewClientTLSFromFile("cert.pem", "");
if err != nil {
logger.Fatal("failed load CA Cert file to run TLS in local mode")
}
opts := []grpc.DialOption {grpc.WithTransportCredentials(transportCreds)}
conn, err := grpc.Dial(ts.address, opts...)
if err != nil {
t.Failf("couldn't connect to routeguide server at %s - %s" ts.address, err)
}
defer conn.Close()
client := NewRouteGuideClient(conn)
feature, err := client.GetFeature(context.Background(), &routeguide.Point{Latitude:0 , Longitude:0})
if err != nil {
t.Failf("unexpected error calling routeguide")
}
if feature.Name != "ExpectedFeatureName" {
t.Failf("expected %s, got %s", "ExpectedFeatureName", feature.Name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment