Repository: grpc/grpc-go
File: authz/grpc_authz_end2end_test.go (Go)

Code before:
									"key": "key-abc",
									"values":
									[
										"*"
									]
								}
							]
						}
					}
				]
			}`,
		md:         metadata.Pairs("key-abc", ""),
		wantStatus: status.New(codes.PermissionDenied, "unauthorized RPC request rejected"),
	},
}

func (s) TestStaticPolicyEnd2End(t *testing.T) {
	for name, test := range authzTests {
		t.Run(name, func(t *testing.T) {
			// Start a gRPC server with gRPC authz unary and stream server interceptors.
			i, _ := authz.NewStatic(test.authzPolicy)

			lis, err := net.Listen("tcp", "localhost:0")
			if err != nil {
				t.Fatalf("error listening: %v", err)
			}
			stub := &stubserver.StubServer{
				Listener: lis,
				UnaryCallF: func(ctx context.Context, req *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
					return &testpb.SimpleResponse{}, nil
				},
				StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error {
					for {
						_, err := stream.Recv()
						if err == io.EOF {
							return stream.SendAndClose(&testpb.StreamingInputCallResponse{})
						}
						if err != nil {
							return err
						}
					}
				},
				S: grpc.NewServer(
					grpc.ChainUnaryInterceptor(i.UnaryInterceptor),
					grpc.ChainStreamInterceptor(i.StreamInterceptor)),
			}
			stubserver.StartTestService(t, stub)
			defer stub.S.Stop()

			// Establish a connection to the server.
			clientConn, err := grpc.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))

Review comment (suggestion):
I think we can avoid explicitly creating a listener here since if we dont set the `Listener` field of the stubserver, it would end up doing the same thing, i.e. create a listener on a local port.

Code after:
									"values":
									[
										"*"
									]
								}
							]
						}
					}
				]
			}`,
		md:         metadata.Pairs("key-abc", ""),
		wantStatus: status.New(codes.PermissionDenied, "unauthorized RPC request rejected"),
	},
}

func (s) TestStaticPolicyEnd2End(t *testing.T) {
	for name, test := range authzTests {
		t.Run(name, func(t *testing.T) {
			// Start a gRPC server with gRPC authz unary and stream server interceptors.
			i, _ := authz.NewStatic(test.authzPolicy)

			stub := &stubserver.StubServer{
				UnaryCallF: func(ctx context.Context, req *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
					return &testpb.SimpleResponse{}, nil
				},
				StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error {
					for {
						_, err := stream.Recv()
						if err == io.EOF {
							return stream.SendAndClose(&testpb.StreamingInputCallResponse{})
						}
						if err != nil {
							return err
						}
					}
				},
				S: grpc.NewServer(grpc.ChainUnaryInterceptor(i.UnaryInterceptor), grpc.ChainStreamInterceptor(i.StreamInterceptor)),
			}
			stubserver.StartTestService(t, stub)
			defer stub.Stop()

			// Establish a connection to the server.
			cc, err := grpc.NewClient(stub.Address, grpc.WithTransportCredentials(insecure.NewCredentials()))
			if err != nil {
				t.Fatalf("grpc.NewClient(%v) failed: %v", stub.Address, err)
			}
			defer cc.Close()
			client := testgrpc.NewTestServiceClient(cc)

			ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
			defer cancel()