Repository: grpc/grpc-go
File: test/creds_test.go (Go)

Code before:
	cc.Connect()

	rawConn := cred.getRawConn()
	// Give grpc a chance to see the error and potentially close the connection.
	// And check that connection is not closed after that.
	time.Sleep(100 * time.Millisecond)
	// Check rawConn is not closed.
	if n, err := rawConn.Write([]byte{0}); n <= 0 || err != nil {
		t.Errorf("Read() = %v, %v; want n>0, <nil>", n, err)
	}
}

type audienceTestCreds struct{}

func (a *audienceTestCreds) GetRequestMetadata(_ context.Context, uri ...string) (map[string]string, error) {
	endpoint := ""
	if len(uri) > 0 {
		endpoint = uri[0]
	}
	return nil, status.Error(codes.Unknown, endpoint)
}

func (a *audienceTestCreds) RequireTransportSecurity() bool { return false }

func (s) TestGRPCMethodInAudienceWhenEnvironmentSet(t *testing.T) {
	oldAudienceIsFullPath := envconfig.AudienceIsFullPath
	defer func() {
		envconfig.AudienceIsFullPath = oldAudienceIsFullPath
	}()
	envconfig.AudienceIsFullPath = true

	te := newTest(t, env{name: "context-request-info", network: "tcp"})
	te.userAgent = testAppUA
	te.startServer(&testServer{security: te.e.security})
	defer te.tearDown()

	wantMethod := fmt.Sprintf("https://%s/grpc.testing.TestService/EmptyCall", te.srvAddr)

	cc := te.clientConn(grpc.WithPerRPCCredentials(&audienceTestCreds{}))
	tc := testgrpc.NewTestServiceClient(cc)

	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); status.Convert(err).Message() != wantMethod {
		t.Fatalf("ss.client.EmptyCall(_, _) = _, %v; want _, _.Message()=%q", err, wantMethod)
	}

	if _, err := tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); status.Convert(err).Message() != wantMethod {
		t.Fatalf("ss.client.EmptyCall(_, _) = _, %v; want _, _.Message()=%q", err, wantMethod)
	}


Review comment (suggestion):
This can be changed to use the testutil function to set and unset the environment config.
`testutils.SetEnvConfig(t, &envconfig.AudienceIsFullPath, true)`
This will set to true and reset to original value in the end.

Code after:
	cc.Connect()

	rawConn := cred.getRawConn()
	// Give grpc a chance to see the error and potentially close the connection.
	// And check that connection is not closed after that.
	time.Sleep(100 * time.Millisecond)
	// Check rawConn is not closed.
	if n, err := rawConn.Write([]byte{0}); n <= 0 || err != nil {
		t.Errorf("Read() = %v, %v; want n>0, <nil>", n, err)
	}
}

type audienceTestCreds struct{}

func (a *audienceTestCreds) GetRequestMetadata(_ context.Context, uri ...string) (map[string]string, error) {
	var endpoint string
	if len(uri) > 0 {
		endpoint = uri[0]
	}
	return nil, status.Error(codes.Unknown, endpoint)
}

func (a *audienceTestCreds) RequireTransportSecurity() bool { return false }

func (s) TestGRPCMethodInAudienceWhenEnvironmentSet(t *testing.T) {
	te := newTest(t, env{name: "method-in-audience", network: "tcp"})
	te.userAgent = testAppUA
	te.startServer(&testServer{security: te.e.security})
	defer te.tearDown()

	cc := te.clientConn(grpc.WithPerRPCCredentials(&audienceTestCreds{}))
	tc := testgrpc.NewTestServiceClient(cc)

	tests := []struct {
		name               string
		endpoint           string
		audienceIsFullPath bool
	}{
		{
			name:               "full-path-sent",
			endpoint:           fmt.Sprintf("https://%s/grpc.testing.TestService/EmptyCall", te.srvAddr),
			audienceIsFullPath: true,
		},
		{
			name:               "method-omitted",
			endpoint:           fmt.Sprintf("https://%s/grpc.testing.TestService", te.srvAddr),
			audienceIsFullPath: false,
		},
	}

	for _, test := range tests {