Repository: grpc/grpc-go
File: credentials/credentials_ext_test.go (Go)

Code before:

// ServerHandshake performs the server-side handshake.
func (c *testCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
	if c.WithValidator {
		return rawConn, testAuthInfoWithValidator{}, nil
	}
	return rawConn, testAuthInfoNoValidator{}, nil
}

// Clone creates a copy of testCreds.
func (c *testCreds) Clone() credentials.TransportCredentials {
	return &testCreds{WithValidator: c.WithValidator}
}

// Info provides protocol information.
func (c *testCreds) Info() credentials.ProtocolInfo {
	return credentials.ProtocolInfo{}
}

// OverrideServerName overrides the server name used for verification.
func (c *testCreds) OverrideServerName(serverName string) error {
	return nil
}

// TestCorrectAuthorityWithCustomCreds tests the `grpc.CallAuthority` call
// option using custom credentials. It verifies behavior both, when the
// credentials implement AuthorityValidator with both correct and incorrect
// authority overrides, as well as when the credentials do not implement
// AuthorityValidator. The later two cases, i.e when the credentials do not
// implement AuthorityValidator, and the authority used to override is invalid,
// are expected to fail with `UNAVAILABLE` status code.
func (s) TestCorrectAuthorityWithCustomCreds(t *testing.T) {
	tests := []struct {
		name       string
		creds      credentials.TransportCredentials
		wantAuth   string
		wantStatus codes.Code
	}{
		{
			name:       "CorrectAuthorityWithFakeCreds",
			wantAuth:   "auth.test.example.com",
			creds:      &testCreds{WithValidator: true},
			wantStatus: codes.OK,
		},
		{
			name:       "IncorrectAuthorityWithFakeCreds",
			wantAuth:   "auth.example.com",
			creds:      &testCreds{WithValidator: true},
			wantStatus: codes.Unavailable,
		},
		{

Review comment (bug):
nit: the paragraph is hard to read since there are multiple cases. Can use bullets. Something like

```
// TestCorrectAuthorityWithCustomCreds tests the `grpc.CallAuthority` call
// option using custom credentials. It verifies following behaviors that are expected
// fail with `UNAVAILABLE` status code:
// - when the credentials implement AuthorityValidator with correct and incorrect authority
// - when credentials do not implement AuthorityValidator but dial option `grpc.CallAuthority` is used
// - when the override authority is invalid
```

Code after:
// AuthType returns the authentication type.
func (testAuthInfoWithValidator) AuthType() string {
	return "test"
}

// ValidateAuthority implements credentials.AuthorityValidator.
func (v testAuthInfoWithValidator) ValidateAuthority(authority string) error {
	if authority == v.validAuthority {
		return nil
	}
	return fmt.Errorf("invalid authority %q, want %q", authority, v.validAuthority)
}

// testCreds is a test TransportCredentials that can optionally support
// authority validation.
type testCreds struct {
	authority string
}

// ClientHandshake performs the client-side handshake.
func (c *testCreds) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
	if c.authority != "" {
		return rawConn, testAuthInfoWithValidator{validAuthority: c.authority}, nil
	}
	return rawConn, testAuthInfoNoValidator{}, nil
}

// ServerHandshake performs the server-side handshake.
func (c *testCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
	if c.authority != "" {
		return rawConn, testAuthInfoWithValidator{validAuthority: c.authority}, nil
	}
	return rawConn, testAuthInfoNoValidator{}, nil
}

// Clone creates a copy of testCreds.
func (c *testCreds) Clone() credentials.TransportCredentials {
	return &testCreds{authority: c.authority}
}

// Info provides protocol information.
func (c *testCreds) Info() credentials.ProtocolInfo {
	return credentials.ProtocolInfo{}
}

// OverrideServerName overrides the server name used for verification.
func (c *testCreds) OverrideServerName(serverName string) error {
	return nil
}

// TestAuthorityValidationFailureWithCustomCreds tests the `grpc.CallAuthority`