credentials_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  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
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  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.
  16. *
  17. */
  18. package credentials
  19. import (
  20. "context"
  21. "crypto/tls"
  22. "net"
  23. "testing"
  24. "google.golang.org/grpc/testdata"
  25. )
  26. func TestTLSOverrideServerName(t *testing.T) {
  27. expectedServerName := "server.name"
  28. c := NewTLS(nil)
  29. c.OverrideServerName(expectedServerName)
  30. if c.Info().ServerName != expectedServerName {
  31. t.Fatalf("c.Info().ServerName = %v, want %v", c.Info().ServerName, expectedServerName)
  32. }
  33. }
  34. func TestTLSClone(t *testing.T) {
  35. expectedServerName := "server.name"
  36. c := NewTLS(nil)
  37. c.OverrideServerName(expectedServerName)
  38. cc := c.Clone()
  39. if cc.Info().ServerName != expectedServerName {
  40. t.Fatalf("cc.Info().ServerName = %v, want %v", cc.Info().ServerName, expectedServerName)
  41. }
  42. cc.OverrideServerName("")
  43. if c.Info().ServerName != expectedServerName {
  44. t.Fatalf("Change in clone should not affect the original, c.Info().ServerName = %v, want %v", c.Info().ServerName, expectedServerName)
  45. }
  46. }
  47. type serverHandshake func(net.Conn) (AuthInfo, error)
  48. func TestClientHandshakeReturnsAuthInfo(t *testing.T) {
  49. done := make(chan AuthInfo, 1)
  50. lis := launchServer(t, tlsServerHandshake, done)
  51. defer lis.Close()
  52. lisAddr := lis.Addr().String()
  53. clientAuthInfo := clientHandle(t, gRPCClientHandshake, lisAddr)
  54. // wait until server sends serverAuthInfo or fails.
  55. serverAuthInfo, ok := <-done
  56. if !ok {
  57. t.Fatalf("Error at server-side")
  58. }
  59. if !compare(clientAuthInfo, serverAuthInfo) {
  60. t.Fatalf("c.ClientHandshake(_, %v, _) = %v, want %v.", lisAddr, clientAuthInfo, serverAuthInfo)
  61. }
  62. }
  63. func TestServerHandshakeReturnsAuthInfo(t *testing.T) {
  64. done := make(chan AuthInfo, 1)
  65. lis := launchServer(t, gRPCServerHandshake, done)
  66. defer lis.Close()
  67. clientAuthInfo := clientHandle(t, tlsClientHandshake, lis.Addr().String())
  68. // wait until server sends serverAuthInfo or fails.
  69. serverAuthInfo, ok := <-done
  70. if !ok {
  71. t.Fatalf("Error at server-side")
  72. }
  73. if !compare(clientAuthInfo, serverAuthInfo) {
  74. t.Fatalf("ServerHandshake(_) = %v, want %v.", serverAuthInfo, clientAuthInfo)
  75. }
  76. }
  77. func TestServerAndClientHandshake(t *testing.T) {
  78. done := make(chan AuthInfo, 1)
  79. lis := launchServer(t, gRPCServerHandshake, done)
  80. defer lis.Close()
  81. clientAuthInfo := clientHandle(t, gRPCClientHandshake, lis.Addr().String())
  82. // wait until server sends serverAuthInfo or fails.
  83. serverAuthInfo, ok := <-done
  84. if !ok {
  85. t.Fatalf("Error at server-side")
  86. }
  87. if !compare(clientAuthInfo, serverAuthInfo) {
  88. t.Fatalf("AuthInfo returned by server: %v and client: %v aren't same", serverAuthInfo, clientAuthInfo)
  89. }
  90. }
  91. func compare(a1, a2 AuthInfo) bool {
  92. if a1.AuthType() != a2.AuthType() {
  93. return false
  94. }
  95. switch a1.AuthType() {
  96. case "tls":
  97. state1 := a1.(TLSInfo).State
  98. state2 := a2.(TLSInfo).State
  99. if state1.Version == state2.Version &&
  100. state1.HandshakeComplete == state2.HandshakeComplete &&
  101. state1.CipherSuite == state2.CipherSuite &&
  102. state1.NegotiatedProtocol == state2.NegotiatedProtocol {
  103. return true
  104. }
  105. return false
  106. default:
  107. return false
  108. }
  109. }
  110. func launchServer(t *testing.T, hs serverHandshake, done chan AuthInfo) net.Listener {
  111. lis, err := net.Listen("tcp", "localhost:0")
  112. if err != nil {
  113. t.Fatalf("Failed to listen: %v", err)
  114. }
  115. go serverHandle(t, hs, done, lis)
  116. return lis
  117. }
  118. // Is run in a separate goroutine.
  119. func serverHandle(t *testing.T, hs serverHandshake, done chan AuthInfo, lis net.Listener) {
  120. serverRawConn, err := lis.Accept()
  121. if err != nil {
  122. t.Errorf("Server failed to accept connection: %v", err)
  123. close(done)
  124. return
  125. }
  126. serverAuthInfo, err := hs(serverRawConn)
  127. if err != nil {
  128. t.Errorf("Server failed while handshake. Error: %v", err)
  129. serverRawConn.Close()
  130. close(done)
  131. return
  132. }
  133. done <- serverAuthInfo
  134. }
  135. func clientHandle(t *testing.T, hs func(net.Conn, string) (AuthInfo, error), lisAddr string) AuthInfo {
  136. conn, err := net.Dial("tcp", lisAddr)
  137. if err != nil {
  138. t.Fatalf("Client failed to connect to %s. Error: %v", lisAddr, err)
  139. }
  140. defer conn.Close()
  141. clientAuthInfo, err := hs(conn, lisAddr)
  142. if err != nil {
  143. t.Fatalf("Error on client while handshake. Error: %v", err)
  144. }
  145. return clientAuthInfo
  146. }
  147. // Server handshake implementation in gRPC.
  148. func gRPCServerHandshake(conn net.Conn) (AuthInfo, error) {
  149. serverTLS, err := NewServerTLSFromFile(testdata.Path("server1.pem"), testdata.Path("server1.key"))
  150. if err != nil {
  151. return nil, err
  152. }
  153. _, serverAuthInfo, err := serverTLS.ServerHandshake(conn)
  154. if err != nil {
  155. return nil, err
  156. }
  157. return serverAuthInfo, nil
  158. }
  159. // Client handshake implementation in gRPC.
  160. func gRPCClientHandshake(conn net.Conn, lisAddr string) (AuthInfo, error) {
  161. clientTLS := NewTLS(&tls.Config{InsecureSkipVerify: true})
  162. _, authInfo, err := clientTLS.ClientHandshake(context.Background(), lisAddr, conn)
  163. if err != nil {
  164. return nil, err
  165. }
  166. return authInfo, nil
  167. }
  168. func tlsServerHandshake(conn net.Conn) (AuthInfo, error) {
  169. cert, err := tls.LoadX509KeyPair(testdata.Path("server1.pem"), testdata.Path("server1.key"))
  170. if err != nil {
  171. return nil, err
  172. }
  173. serverTLSConfig := &tls.Config{Certificates: []tls.Certificate{cert}}
  174. serverConn := tls.Server(conn, serverTLSConfig)
  175. err = serverConn.Handshake()
  176. if err != nil {
  177. return nil, err
  178. }
  179. return TLSInfo{State: serverConn.ConnectionState()}, nil
  180. }
  181. func tlsClientHandshake(conn net.Conn, _ string) (AuthInfo, error) {
  182. clientTLSConfig := &tls.Config{InsecureSkipVerify: true}
  183. clientConn := tls.Client(conn, clientTLSConfig)
  184. if err := clientConn.Handshake(); err != nil {
  185. return nil, err
  186. }
  187. return TLSInfo{State: clientConn.ConnectionState()}, nil
  188. }