(t *testing.T, network, address string)
| 42 | ) |
| 43 | |
| 44 | func testLocalCredsE2ESucceed(t *testing.T, network, address string) error { |
| 45 | lis, err := net.Listen(network, address) |
| 46 | if err != nil { |
| 47 | return fmt.Errorf("Failed to create listener: %v", err) |
| 48 | } |
| 49 | ss := &stubserver.StubServer{ |
| 50 | Listener: lis, |
| 51 | EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) { |
| 52 | pr, ok := peer.FromContext(ctx) |
| 53 | if !ok { |
| 54 | return nil, status.Error(codes.DataLoss, "Failed to get peer from ctx") |
| 55 | } |
| 56 | type internalInfo interface { |
| 57 | GetCommonAuthInfo() credentials.CommonAuthInfo |
| 58 | } |
| 59 | var secLevel credentials.SecurityLevel |
| 60 | if info, ok := (pr.AuthInfo).(internalInfo); ok { |
| 61 | secLevel = info.GetCommonAuthInfo().SecurityLevel |
| 62 | } else { |
| 63 | return nil, status.Errorf(codes.Unauthenticated, "peer.AuthInfo does not implement GetCommonAuthInfo()") |
| 64 | } |
| 65 | // Check security level |
| 66 | switch network { |
| 67 | case "unix": |
| 68 | if secLevel != credentials.PrivacyAndIntegrity { |
| 69 | return nil, status.Errorf(codes.Unauthenticated, "Wrong security level: got %q, want %q", secLevel, credentials.PrivacyAndIntegrity) |
| 70 | } |
| 71 | case "tcp": |
| 72 | if secLevel != credentials.NoSecurity { |
| 73 | return nil, status.Errorf(codes.Unauthenticated, "Wrong security level: got %q, want %q", secLevel, credentials.NoSecurity) |
| 74 | } |
| 75 | } |
| 76 | return &testpb.Empty{}, nil |
| 77 | }, |
| 78 | S: grpc.NewServer(grpc.Creds(local.NewCredentials())), |
| 79 | } |
| 80 | stubserver.StartTestService(t, ss) |
| 81 | defer ss.S.Stop() |
| 82 | |
| 83 | var cc *grpc.ClientConn |
| 84 | lisAddr := lis.Addr().String() |
| 85 | |
| 86 | switch network { |
| 87 | case "unix": |
| 88 | cc, err = grpc.NewClient("passthrough:///"+lisAddr, grpc.WithTransportCredentials(local.NewCredentials()), grpc.WithContextDialer( |
| 89 | func(_ context.Context, addr string) (net.Conn, error) { |
| 90 | return net.Dial("unix", addr) |
| 91 | })) |
| 92 | case "tcp": |
| 93 | cc, err = grpc.NewClient(lisAddr, grpc.WithTransportCredentials(local.NewCredentials())) |
| 94 | default: |
| 95 | return fmt.Errorf("unsupported network %q", network) |
| 96 | } |
| 97 | if err != nil { |
| 98 | return fmt.Errorf("Failed to create a client for server: %v, %v", err, lisAddr) |
| 99 | } |
| 100 | defer cc.Close() |
| 101 |
no test coverage detected