creds_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package test
  19. // TODO(https://github.com/grpc/grpc-go/issues/2330): move all creds releated
  20. // tests to this file.
  21. import (
  22. "context"
  23. "testing"
  24. "google.golang.org/grpc"
  25. "google.golang.org/grpc/credentials"
  26. "google.golang.org/grpc/internal/leakcheck"
  27. testpb "google.golang.org/grpc/test/grpc_testing"
  28. "google.golang.org/grpc/testdata"
  29. )
  30. const (
  31. bundlePerRPCOnly = "perRPCOnly"
  32. bundleTLSOnly = "tlsOnly"
  33. )
  34. type testCredsBundle struct {
  35. t *testing.T
  36. mode string
  37. }
  38. func (c *testCredsBundle) TransportCredentials() credentials.TransportCredentials {
  39. if c.mode == bundlePerRPCOnly {
  40. return nil
  41. }
  42. creds, err := credentials.NewClientTLSFromFile(testdata.Path("ca.pem"), "x.test.youtube.com")
  43. if err != nil {
  44. c.t.Logf("Failed to load credentials: %v", err)
  45. return nil
  46. }
  47. return creds
  48. }
  49. func (c *testCredsBundle) PerRPCCredentials() credentials.PerRPCCredentials {
  50. if c.mode == bundleTLSOnly {
  51. return nil
  52. }
  53. return testPerRPCCredentials{}
  54. }
  55. func (c *testCredsBundle) NewWithMode(mode string) (credentials.Bundle, error) {
  56. return &testCredsBundle{mode: mode}, nil
  57. }
  58. func TestCredsBundleBoth(t *testing.T) {
  59. defer leakcheck.Check(t)
  60. te := newTest(t, env{name: "creds-bundle", network: "tcp", balancer: "v1", security: "empty"})
  61. te.tapHandle = authHandle
  62. te.customDialOptions = []grpc.DialOption{
  63. grpc.WithCredentialsBundle(&testCredsBundle{t: t}),
  64. }
  65. creds, err := credentials.NewServerTLSFromFile(testdata.Path("server1.pem"), testdata.Path("server1.key"))
  66. if err != nil {
  67. t.Fatalf("Failed to generate credentials %v", err)
  68. }
  69. te.customServerOptions = []grpc.ServerOption{
  70. grpc.Creds(creds),
  71. }
  72. te.startServer(&testServer{})
  73. defer te.tearDown()
  74. cc := te.clientConn()
  75. tc := testpb.NewTestServiceClient(cc)
  76. if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
  77. t.Fatalf("Test failed. Reason: %v", err)
  78. }
  79. }
  80. func TestCredsBundleTransportCredentials(t *testing.T) {
  81. defer leakcheck.Check(t)
  82. te := newTest(t, env{name: "creds-bundle", network: "tcp", balancer: "v1", security: "empty"})
  83. te.customDialOptions = []grpc.DialOption{
  84. grpc.WithCredentialsBundle(&testCredsBundle{t: t, mode: bundleTLSOnly}),
  85. }
  86. creds, err := credentials.NewServerTLSFromFile(testdata.Path("server1.pem"), testdata.Path("server1.key"))
  87. if err != nil {
  88. t.Fatalf("Failed to generate credentials %v", err)
  89. }
  90. te.customServerOptions = []grpc.ServerOption{
  91. grpc.Creds(creds),
  92. }
  93. te.startServer(&testServer{})
  94. defer te.tearDown()
  95. cc := te.clientConn()
  96. tc := testpb.NewTestServiceClient(cc)
  97. if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
  98. t.Fatalf("Test failed. Reason: %v", err)
  99. }
  100. }
  101. func TestCredsBundlePerRPCCredentials(t *testing.T) {
  102. defer leakcheck.Check(t)
  103. te := newTest(t, env{name: "creds-bundle", network: "tcp", balancer: "v1", security: "empty"})
  104. te.tapHandle = authHandle
  105. te.customDialOptions = []grpc.DialOption{
  106. grpc.WithCredentialsBundle(&testCredsBundle{t: t, mode: bundlePerRPCOnly}),
  107. }
  108. te.startServer(&testServer{})
  109. defer te.tearDown()
  110. cc := te.clientConn()
  111. tc := testpb.NewTestServiceClient(cc)
  112. if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
  113. t.Fatalf("Test failed. Reason: %v", err)
  114. }
  115. }