utils_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 alts
  19. import (
  20. "context"
  21. "io"
  22. "strings"
  23. "testing"
  24. altspb "google.golang.org/grpc/credentials/alts/internal/proto/grpc_gcp"
  25. "google.golang.org/grpc/peer"
  26. )
  27. func TestIsRunningOnGCP(t *testing.T) {
  28. for _, tc := range []struct {
  29. description string
  30. testOS string
  31. testReader io.Reader
  32. out bool
  33. }{
  34. // Linux tests.
  35. {"linux: not a GCP platform", "linux", strings.NewReader("not GCP"), false},
  36. {"Linux: GCP platform (Google)", "linux", strings.NewReader("Google"), true},
  37. {"Linux: GCP platform (Google Compute Engine)", "linux", strings.NewReader("Google Compute Engine"), true},
  38. {"Linux: GCP platform (Google Compute Engine) with extra spaces", "linux", strings.NewReader(" Google Compute Engine "), true},
  39. // Windows tests.
  40. {"windows: not a GCP platform", "windows", strings.NewReader("not GCP"), false},
  41. {"windows: GCP platform (Google)", "windows", strings.NewReader("Google"), true},
  42. {"windows: GCP platform (Google) with extra spaces", "windows", strings.NewReader(" Google "), true},
  43. } {
  44. reverseFunc := setup(tc.testOS, tc.testReader)
  45. if got, want := isRunningOnGCP(), tc.out; got != want {
  46. t.Errorf("%v: isRunningOnGCP()=%v, want %v", tc.description, got, want)
  47. }
  48. reverseFunc()
  49. }
  50. }
  51. func setup(testOS string, testReader io.Reader) func() {
  52. tmpOS := runningOS
  53. tmpReader := manufacturerReader
  54. // Set test OS and reader function.
  55. runningOS = testOS
  56. manufacturerReader = func() (io.Reader, error) {
  57. return testReader, nil
  58. }
  59. return func() {
  60. runningOS = tmpOS
  61. manufacturerReader = tmpReader
  62. }
  63. }
  64. func TestAuthInfoFromContext(t *testing.T) {
  65. ctx := context.Background()
  66. altsAuthInfo := &fakeALTSAuthInfo{}
  67. p := &peer.Peer{
  68. AuthInfo: altsAuthInfo,
  69. }
  70. for _, tc := range []struct {
  71. desc string
  72. ctx context.Context
  73. success bool
  74. out AuthInfo
  75. }{
  76. {
  77. "working case",
  78. peer.NewContext(ctx, p),
  79. true,
  80. altsAuthInfo,
  81. },
  82. } {
  83. authInfo, err := AuthInfoFromContext(tc.ctx)
  84. if got, want := (err == nil), tc.success; got != want {
  85. t.Errorf("%v: AuthInfoFromContext(_)=(err=nil)=%v, want %v", tc.desc, got, want)
  86. }
  87. if got, want := authInfo, tc.out; got != want {
  88. t.Errorf("%v:, AuthInfoFromContext(_)=(%v, _), want (%v, _)", tc.desc, got, want)
  89. }
  90. }
  91. }
  92. func TestAuthInfoFromPeer(t *testing.T) {
  93. altsAuthInfo := &fakeALTSAuthInfo{}
  94. p := &peer.Peer{
  95. AuthInfo: altsAuthInfo,
  96. }
  97. for _, tc := range []struct {
  98. desc string
  99. p *peer.Peer
  100. success bool
  101. out AuthInfo
  102. }{
  103. {
  104. "working case",
  105. p,
  106. true,
  107. altsAuthInfo,
  108. },
  109. } {
  110. authInfo, err := AuthInfoFromPeer(tc.p)
  111. if got, want := (err == nil), tc.success; got != want {
  112. t.Errorf("%v: AuthInfoFromPeer(_)=(err=nil)=%v, want %v", tc.desc, got, want)
  113. }
  114. if got, want := authInfo, tc.out; got != want {
  115. t.Errorf("%v:, AuthInfoFromPeer(_)=(%v, _), want (%v, _)", tc.desc, got, want)
  116. }
  117. }
  118. }
  119. type fakeALTSAuthInfo struct{}
  120. func (*fakeALTSAuthInfo) AuthType() string { return "" }
  121. func (*fakeALTSAuthInfo) ApplicationProtocol() string { return "" }
  122. func (*fakeALTSAuthInfo) RecordProtocol() string { return "" }
  123. func (*fakeALTSAuthInfo) SecurityLevel() altspb.SecurityLevel {
  124. return altspb.SecurityLevel_SECURITY_NONE
  125. }
  126. func (*fakeALTSAuthInfo) PeerServiceAccount() string { return "" }
  127. func (*fakeALTSAuthInfo) LocalServiceAccount() string { return "" }
  128. func (*fakeALTSAuthInfo) PeerRPCVersions() *altspb.RpcProtocolVersions { return nil }