3 * Copyright 2016 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
26 "golang.org/x/net/context"
27 "google.golang.org/grpc/testdata"
30 func TestTLSOverrideServerName(t *testing.T) {
31 expectedServerName := "server.name"
33 c.OverrideServerName(expectedServerName)
34 if c.Info().ServerName != expectedServerName {
35 t.Fatalf("c.Info().ServerName = %v, want %v", c.Info().ServerName, expectedServerName)
39 func TestTLSClone(t *testing.T) {
40 expectedServerName := "server.name"
42 c.OverrideServerName(expectedServerName)
44 if cc.Info().ServerName != expectedServerName {
45 t.Fatalf("cc.Info().ServerName = %v, want %v", cc.Info().ServerName, expectedServerName)
47 cc.OverrideServerName("")
48 if c.Info().ServerName != expectedServerName {
49 t.Fatalf("Change in clone should not affect the original, c.Info().ServerName = %v, want %v", c.Info().ServerName, expectedServerName)
54 type serverHandshake func(net.Conn) (AuthInfo, error)
56 func TestClientHandshakeReturnsAuthInfo(t *testing.T) {
57 done := make(chan AuthInfo, 1)
58 lis := launchServer(t, tlsServerHandshake, done)
60 lisAddr := lis.Addr().String()
61 clientAuthInfo := clientHandle(t, gRPCClientHandshake, lisAddr)
62 // wait until server sends serverAuthInfo or fails.
63 serverAuthInfo, ok := <-done
65 t.Fatalf("Error at server-side")
67 if !compare(clientAuthInfo, serverAuthInfo) {
68 t.Fatalf("c.ClientHandshake(_, %v, _) = %v, want %v.", lisAddr, clientAuthInfo, serverAuthInfo)
72 func TestServerHandshakeReturnsAuthInfo(t *testing.T) {
73 done := make(chan AuthInfo, 1)
74 lis := launchServer(t, gRPCServerHandshake, done)
76 clientAuthInfo := clientHandle(t, tlsClientHandshake, lis.Addr().String())
77 // wait until server sends serverAuthInfo or fails.
78 serverAuthInfo, ok := <-done
80 t.Fatalf("Error at server-side")
82 if !compare(clientAuthInfo, serverAuthInfo) {
83 t.Fatalf("ServerHandshake(_) = %v, want %v.", serverAuthInfo, clientAuthInfo)
87 func TestServerAndClientHandshake(t *testing.T) {
88 done := make(chan AuthInfo, 1)
89 lis := launchServer(t, gRPCServerHandshake, done)
91 clientAuthInfo := clientHandle(t, gRPCClientHandshake, lis.Addr().String())
92 // wait until server sends serverAuthInfo or fails.
93 serverAuthInfo, ok := <-done
95 t.Fatalf("Error at server-side")
97 if !compare(clientAuthInfo, serverAuthInfo) {
98 t.Fatalf("AuthInfo returned by server: %v and client: %v aren't same", serverAuthInfo, clientAuthInfo)
102 func compare(a1, a2 AuthInfo) bool {
103 if a1.AuthType() != a2.AuthType() {
106 switch a1.AuthType() {
108 state1 := a1.(TLSInfo).State
109 state2 := a2.(TLSInfo).State
110 if state1.Version == state2.Version &&
111 state1.HandshakeComplete == state2.HandshakeComplete &&
112 state1.CipherSuite == state2.CipherSuite &&
113 state1.NegotiatedProtocol == state2.NegotiatedProtocol {
122 func launchServer(t *testing.T, hs serverHandshake, done chan AuthInfo) net.Listener {
123 lis, err := net.Listen("tcp", "localhost:0")
125 t.Fatalf("Failed to listen: %v", err)
127 go serverHandle(t, hs, done, lis)
131 // Is run in a separate goroutine.
132 func serverHandle(t *testing.T, hs serverHandshake, done chan AuthInfo, lis net.Listener) {
133 serverRawConn, err := lis.Accept()
135 t.Errorf("Server failed to accept connection: %v", err)
139 serverAuthInfo, err := hs(serverRawConn)
141 t.Errorf("Server failed while handshake. Error: %v", err)
142 serverRawConn.Close()
146 done <- serverAuthInfo
149 func clientHandle(t *testing.T, hs func(net.Conn, string) (AuthInfo, error), lisAddr string) AuthInfo {
150 conn, err := net.Dial("tcp", lisAddr)
152 t.Fatalf("Client failed to connect to %s. Error: %v", lisAddr, err)
155 clientAuthInfo, err := hs(conn, lisAddr)
157 t.Fatalf("Error on client while handshake. Error: %v", err)
159 return clientAuthInfo
162 // Server handshake implementation in gRPC.
163 func gRPCServerHandshake(conn net.Conn) (AuthInfo, error) {
164 serverTLS, err := NewServerTLSFromFile(testdata.Path("server1.pem"), testdata.Path("server1.key"))
168 _, serverAuthInfo, err := serverTLS.ServerHandshake(conn)
172 return serverAuthInfo, nil
175 // Client handshake implementation in gRPC.
176 func gRPCClientHandshake(conn net.Conn, lisAddr string) (AuthInfo, error) {
177 clientTLS := NewTLS(&tls.Config{InsecureSkipVerify: true})
178 _, authInfo, err := clientTLS.ClientHandshake(context.Background(), lisAddr, conn)
185 func tlsServerHandshake(conn net.Conn) (AuthInfo, error) {
186 cert, err := tls.LoadX509KeyPair(testdata.Path("server1.pem"), testdata.Path("server1.key"))
190 serverTLSConfig := &tls.Config{Certificates: []tls.Certificate{cert}}
191 serverConn := tls.Server(conn, serverTLSConfig)
192 err = serverConn.Handshake()
196 return TLSInfo{State: serverConn.ConnectionState()}, nil
199 func tlsClientHandshake(conn net.Conn, _ string) (AuthInfo, error) {
200 clientTLSConfig := &tls.Config{InsecureSkipVerify: true}
201 clientConn := tls.Client(conn, clientTLSConfig)
202 if err := clientConn.Handshake(); err != nil {
205 return TLSInfo{State: clientConn.ConnectionState()}, nil