-
Notifications
You must be signed in to change notification settings - Fork 4.6k
ClientHandshake to return AuthInfo #956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
1165b1e
d1b12d3
0129b49
c62cf7b
5b3192c
ff332b6
915cb50
6c58b32
9d3e997
c980740
6fdee01
b792ae8
16853da
40952fe
1db9a22
848da09
49c5700
e7832cf
ecc30a5
74f10a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,11 @@ | |
| package credentials | ||
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "net" | ||
| "testing" | ||
|
|
||
| "golang.org/x/net/context" | ||
| ) | ||
|
|
||
| func TestTLSOverrideServerName(t *testing.T) { | ||
|
|
@@ -59,3 +63,95 @@ func TestTLSClone(t *testing.T) { | |
| t.Fatalf("Change in clone should not affect the original, c.Info().ServerName = %v, want %v", c.Info().ServerName, expectedServerName) | ||
| } | ||
| } | ||
|
|
||
| const tlsDir = "../test/testdata/" | ||
|
|
||
| func TestTLSClientHandshakeReturnsAuthInfo(t *testing.T) { | ||
| lis, err := net.Listen("tcp", "localhost:0") | ||
| if err != nil { | ||
| t.Fatalf("Failed to listen: %v", err) | ||
| } | ||
| defer lis.Close() | ||
| serverTLS, err := NewServerTLSFromFile(tlsDir+"server1.pem", tlsDir+"server1.key") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create server TLS. Error: %v", err) | ||
| } | ||
| var serverAuthInfo TLSInfo | ||
| done := make(chan bool) | ||
| go func() { | ||
| defer func() { | ||
| done <- true | ||
| }() | ||
| serverRawConn, err := lis.Accept() | ||
| if err != nil { | ||
| t.Fatalf("Server failed to accept connection: %v", err) | ||
|
||
| } | ||
| serverConn := tls.Server(serverRawConn, serverTLS.(*tlsCreds).config) | ||
| serverErr := serverConn.Handshake() | ||
| if serverErr != nil { | ||
| t.Fatalf("Error on server while handshake. Error: %v", serverErr) | ||
|
||
| } | ||
| serverAuthInfo = TLSInfo{serverConn.ConnectionState()} | ||
|
||
| }() | ||
| conn, err := net.Dial("tcp", lis.Addr().String()) | ||
| if err != nil { | ||
| t.Fatalf("Client failed to connect to local server. Error: %v", err) | ||
|
||
| } | ||
| defer conn.Close() | ||
| c := NewTLS(&tls.Config{InsecureSkipVerify: true}) | ||
| _, authInfo, err := c.ClientHandshake(context.Background(), lis.Addr().String(), conn) | ||
| if err != nil { | ||
| t.Fatalf("Error on client while handshake. Error: %v", err) | ||
| } | ||
| // wait until server has populated the serverAuthInfo struct. | ||
|
||
| <-done | ||
|
||
| if authInfo.(TLSInfo).State.Version != serverAuthInfo.State.Version { | ||
| t.Fatalf("c.ClientHandshake(_, %v, _) = %v, want %v.", lis.Addr().String(), authInfo, serverAuthInfo) | ||
|
||
| } | ||
| } | ||
|
|
||
| func TestTLSServerHandshakeReturnsAuthInfo(t *testing.T) { | ||
| lis, err := net.Listen("tcp", "localhost:0") | ||
|
||
| if err != nil { | ||
| t.Fatalf("Failed to listen: %v", err) | ||
| } | ||
| defer lis.Close() | ||
| serverTLS, err := NewServerTLSFromFile(tlsDir+"server1.pem", tlsDir+"server1.key") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create server TLS. Error: %v", err) | ||
| } | ||
| var serverAuthInfo AuthInfo | ||
| done := make(chan bool) | ||
| go func() { | ||
| defer func() { | ||
| done <- true | ||
| }() | ||
| serverRawConn, err := lis.Accept() | ||
| if err != nil { | ||
| t.Fatalf("Server failed to accept connection: %v", err) | ||
| } | ||
| var serverErr error | ||
| _, serverAuthInfo, serverErr = serverTLS.ServerHandshake(serverRawConn) | ||
| if serverErr != nil { | ||
| t.Fatalf("Error on server while handshake. Error: %v", serverErr) | ||
| } | ||
| }() | ||
| conn, err := net.Dial("tcp", lis.Addr().String()) | ||
| if err != nil { | ||
| t.Fatalf("Client failed to connect to local server. Error: %v", err) | ||
| } | ||
| defer conn.Close() | ||
| c := NewTLS(&tls.Config{InsecureSkipVerify: true}) | ||
| clientConn := tls.Client(conn, c.(*tlsCreds).config) | ||
| err = clientConn.Handshake() | ||
| if err != nil { | ||
| t.Fatalf("Error on client while handshake. Error: %v", err) | ||
| } | ||
| authInfo := TLSInfo{clientConn.ConnectionState()} | ||
|
||
| // wait until server has populated the serverAuthInfo struct. | ||
| <-done | ||
| if authInfo.State.Version != serverAuthInfo.(TLSInfo).State.Version { | ||
| t.Fatalf("ServerHandshake(_) = %v, want %v.", serverAuthInfo, authInfo) | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use tls.ConnectionState directly?