(t *testing.T)
| 479 | } |
| 480 | |
| 481 | func (s) TestFileWatcherEnd2End(t *testing.T) { |
| 482 | for name, test := range authzTests { |
| 483 | t.Run(name, func(t *testing.T) { |
| 484 | file := createTmpPolicyFile(t, name, []byte(test.authzPolicy)) |
| 485 | i, _ := authz.NewFileWatcher(file, 1*time.Second) |
| 486 | defer i.Close() |
| 487 | |
| 488 | stub := &stubserver.StubServer{ |
| 489 | UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) { |
| 490 | return &testpb.SimpleResponse{}, nil |
| 491 | }, |
| 492 | StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error { |
| 493 | for { |
| 494 | _, err := stream.Recv() |
| 495 | if err == io.EOF { |
| 496 | return stream.SendAndClose(&testpb.StreamingInputCallResponse{}) |
| 497 | } |
| 498 | if err != nil { |
| 499 | return err |
| 500 | } |
| 501 | } |
| 502 | }, |
| 503 | // Start a gRPC server with gRPC authz unary and stream server interceptors. |
| 504 | S: grpc.NewServer(grpc.ChainUnaryInterceptor(i.UnaryInterceptor), grpc.ChainStreamInterceptor(i.StreamInterceptor)), |
| 505 | } |
| 506 | stubserver.StartTestService(t, stub) |
| 507 | defer stub.Stop() |
| 508 | |
| 509 | // Establish a connection to the server. |
| 510 | cc, err := grpc.NewClient(stub.Address, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 511 | if err != nil { |
| 512 | t.Fatalf("grpc.NewClient(%v) failed: %v", stub.Address, err) |
| 513 | } |
| 514 | defer cc.Close() |
| 515 | client := testgrpc.NewTestServiceClient(cc) |
| 516 | |
| 517 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 518 | defer cancel() |
| 519 | ctx = metadata.NewOutgoingContext(ctx, test.md) |
| 520 | |
| 521 | // Verifying authorization decision for Unary RPC. |
| 522 | _, err = client.UnaryCall(ctx, &testpb.SimpleRequest{}) |
| 523 | if got := status.Convert(err); got.Code() != test.wantStatus.Code() || got.Message() != test.wantStatus.Message() { |
| 524 | t.Fatalf("[UnaryCall] error want:{%v} got:{%v}", test.wantStatus.Err(), got.Err()) |
| 525 | } |
| 526 | |
| 527 | // Verifying authorization decision for Streaming RPC. |
| 528 | stream, err := client.StreamingInputCall(ctx) |
| 529 | if err != nil { |
| 530 | t.Fatalf("failed StreamingInputCall : %v", err) |
| 531 | } |
| 532 | req := &testpb.StreamingInputCallRequest{ |
| 533 | Payload: &testpb.Payload{ |
| 534 | Body: []byte("hi"), |
| 535 | }, |
| 536 | } |
| 537 | if err := stream.Send(req); err != nil && err != io.EOF { |
| 538 | t.Fatalf("failed stream.Send : %v", err) |
nothing calls this directly
no test coverage detected