...
1
18
19 package tlscreds
20
21 import (
22 "context"
23 "crypto/tls"
24 "errors"
25 "fmt"
26 "strings"
27 "testing"
28
29 "google.golang.org/grpc"
30 "google.golang.org/grpc/credentials/tls/certprovider"
31 "google.golang.org/grpc/internal/grpctest"
32 "google.golang.org/grpc/internal/stubserver"
33 "google.golang.org/grpc/internal/testutils/xds/e2e"
34 testgrpc "google.golang.org/grpc/interop/grpc_testing"
35 testpb "google.golang.org/grpc/interop/grpc_testing"
36 "google.golang.org/grpc/testdata"
37 )
38
39 type s struct {
40 grpctest.Tester
41 }
42
43 func Test(t *testing.T) {
44 grpctest.RunSubTests(t, s{})
45 }
46
47 type failingProvider struct{}
48
49 func (f failingProvider) KeyMaterial(context.Context) (*certprovider.KeyMaterial, error) {
50 return nil, errors.New("test error")
51 }
52
53 func (f failingProvider) Close() {}
54
55 func (s) TestFailingProvider(t *testing.T) {
56 s := stubserver.StartTestService(t, nil, grpc.Creds(e2e.CreateServerTLSCredentials(t, tls.RequireAndVerifyClientCert)))
57 defer s.Stop()
58
59 cfg := fmt.Sprintf(`{
60 "ca_certificate_file": "%s",
61 "certificate_file": "%s",
62 "private_key_file": "%s"
63 }`,
64 testdata.Path("x509/server_ca_cert.pem"),
65 testdata.Path("x509/client1_cert.pem"),
66 testdata.Path("x509/client1_key.pem"))
67 tlsBundle, stop, err := NewBundle([]byte(cfg))
68 if err != nil {
69 t.Fatalf("Failed to create TLS bundle: %v", err)
70 }
71 stop()
72
73
74
75 creds, ok := tlsBundle.TransportCredentials().(*reloadingCreds)
76 if !ok {
77 t.Fatalf("Got %T, expected reloadingCreds", tlsBundle.TransportCredentials())
78 }
79 creds.provider = &failingProvider{}
80
81 conn, err := grpc.NewClient(s.Address, grpc.WithCredentialsBundle(tlsBundle), grpc.WithAuthority("x.test.example.com"))
82 if err != nil {
83 t.Fatalf("Error dialing: %v", err)
84 }
85 defer conn.Close()
86
87 client := testgrpc.NewTestServiceClient(conn)
88 _, err = client.EmptyCall(context.Background(), &testpb.Empty{})
89 if wantErr := "test error"; err == nil || !strings.Contains(err.Error(), wantErr) {
90 t.Errorf("EmptyCall() got err: %s, want err to contain: %s", err, wantErr)
91 }
92 }
93
View as plain text