creds_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 related
  20. // tests to this file.
  21. import (
  22. "context"
  23. "testing"
  24. "google.golang.org/grpc"
  25. "google.golang.org/grpc/credentials"
  26. testpb "google.golang.org/grpc/test/grpc_testing"
  27. "google.golang.org/grpc/testdata"
  28. )
  29. const (
  30. bundlePerRPCOnly = "perRPCOnly"
  31. bundleTLSOnly = "tlsOnly"
  32. )
  33. type testCredsBundle struct {
  34. t *testing.T
  35. mode string
  36. }
  37. func (c *testCredsBundle) TransportCredentials() credentials.TransportCredentials {
  38. if c.mode == bundlePerRPCOnly {
  39. return nil
  40. }
  41. creds, err := credentials.NewClientTLSFromFile(testdata.Path("ca.pem"), "x.test.youtube.com")
  42. if err != nil {
  43. c.t.Logf("Failed to load credentials: %v", err)
  44. return nil
  45. }
  46. return creds
  47. }
  48. func (c *testCredsBundle) PerRPCCredentials() credentials.PerRPCCredentials {
  49. if c.mode == bundleTLSOnly {
  50. return nil
  51. }
  52. return testPerRPCCredentials{}
  53. }
  54. func (c *testCredsBundle) NewWithMode(mode string) (credentials.Bundle, error) {
  55. return &testCredsBundle{mode: mode}, nil
  56. }
  57. func (s) TestCredsBundleBoth(t *testing.T) {
  58. te := newTest(t, env{name: "creds-bundle", network: "tcp", balancer: "v1", security: "empty"})
  59. te.tapHandle = authHandle
  60. te.customDialOptions = []grpc.DialOption{
  61. grpc.WithCredentialsBundle(&testCredsBundle{t: t}),
  62. }
  63. creds, err := credentials.NewServerTLSFromFile(testdata.Path("server1.pem"), testdata.Path("server1.key"))
  64. if err != nil {
  65. t.Fatalf("Failed to generate credentials %v", err)
  66. }
  67. te.customServerOptions = []grpc.ServerOption{
  68. grpc.Creds(creds),
  69. }
  70. te.startServer(&testServer{})
  71. defer te.tearDown()
  72. cc := te.clientConn()
  73. tc := testpb.NewTestServiceClient(cc)
  74. if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
  75. t.Fatalf("Test failed. Reason: %v", err)
  76. }
  77. }
  78. func (s) TestCredsBundleTransportCredentials(t *testing.T) {
  79. te := newTest(t, env{name: "creds-bundle", network: "tcp", balancer: "v1", security: "empty"})
  80. te.customDialOptions = []grpc.DialOption{
  81. grpc.WithCredentialsBundle(&testCredsBundle{t: t, mode: bundleTLSOnly}),
  82. }
  83. creds, err := credentials.NewServerTLSFromFile(testdata.Path("server1.pem"), testdata.Path("server1.key"))
  84. if err != nil {
  85. t.Fatalf("Failed to generate credentials %v", err)
  86. }
  87. te.customServerOptions = []grpc.ServerOption{
  88. grpc.Creds(creds),
  89. }
  90. te.startServer(&testServer{})
  91. defer te.tearDown()
  92. cc := te.clientConn()
  93. tc := testpb.NewTestServiceClient(cc)
  94. if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
  95. t.Fatalf("Test failed. Reason: %v", err)
  96. }
  97. }
  98. func (s) TestCredsBundlePerRPCCredentials(t *testing.T) {
  99. te := newTest(t, env{name: "creds-bundle", network: "tcp", balancer: "v1", security: "empty"})
  100. te.tapHandle = authHandle
  101. te.customDialOptions = []grpc.DialOption{
  102. grpc.WithCredentialsBundle(&testCredsBundle{t: t, mode: bundlePerRPCOnly}),
  103. }
  104. te.startServer(&testServer{})
  105. defer te.tearDown()
  106. cc := te.clientConn()
  107. tc := testpb.NewTestServiceClient(cc)
  108. if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
  109. t.Fatalf("Test failed. Reason: %v", err)
  110. }
  111. }