secure_auth_context_test.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. *
  3. * Copyright 2015 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. #include "src/cpp/common/secure_auth_context.h"
  19. #include <gtest/gtest.h>
  20. #include <grpc/grpc_security.h>
  21. #include <grpcpp/security/auth_context.h>
  22. #include "src/core/lib/security/context/security_context.h"
  23. #include "test/cpp/util/string_ref_helper.h"
  24. using grpc::testing::ToString;
  25. namespace grpc {
  26. namespace {
  27. class SecureAuthContextTest : public ::testing::Test {};
  28. // Created with nullptr
  29. TEST_F(SecureAuthContextTest, EmptyContext) {
  30. SecureAuthContext context(nullptr);
  31. EXPECT_TRUE(context.GetPeerIdentity().empty());
  32. EXPECT_TRUE(context.GetPeerIdentityPropertyName().empty());
  33. EXPECT_TRUE(context.FindPropertyValues("").empty());
  34. EXPECT_TRUE(context.FindPropertyValues("whatever").empty());
  35. EXPECT_TRUE(context.begin() == context.end());
  36. }
  37. TEST_F(SecureAuthContextTest, Properties) {
  38. grpc_core::RefCountedPtr<grpc_auth_context> ctx =
  39. grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
  40. SecureAuthContext context(ctx.get());
  41. ctx.reset();
  42. context.AddProperty("name", "chapi");
  43. context.AddProperty("name", "chapo");
  44. context.AddProperty("foo", "bar");
  45. EXPECT_TRUE(context.SetPeerIdentityPropertyName("name"));
  46. std::vector<grpc::string_ref> peer_identity = context.GetPeerIdentity();
  47. EXPECT_EQ(2u, peer_identity.size());
  48. EXPECT_EQ("chapi", ToString(peer_identity[0]));
  49. EXPECT_EQ("chapo", ToString(peer_identity[1]));
  50. EXPECT_EQ("name", context.GetPeerIdentityPropertyName());
  51. std::vector<grpc::string_ref> bar = context.FindPropertyValues("foo");
  52. EXPECT_EQ(1u, bar.size());
  53. EXPECT_EQ("bar", ToString(bar[0]));
  54. }
  55. TEST_F(SecureAuthContextTest, Iterators) {
  56. grpc_core::RefCountedPtr<grpc_auth_context> ctx =
  57. grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
  58. SecureAuthContext context(ctx.get());
  59. ctx.reset();
  60. context.AddProperty("name", "chapi");
  61. context.AddProperty("name", "chapo");
  62. context.AddProperty("foo", "bar");
  63. EXPECT_TRUE(context.SetPeerIdentityPropertyName("name"));
  64. AuthPropertyIterator iter = context.begin();
  65. EXPECT_TRUE(context.end() != iter);
  66. AuthProperty p0 = *iter;
  67. ++iter;
  68. AuthProperty p1 = *iter;
  69. iter++;
  70. AuthProperty p2 = *iter;
  71. EXPECT_EQ("name", ToString(p0.first));
  72. EXPECT_EQ("chapi", ToString(p0.second));
  73. EXPECT_EQ("name", ToString(p1.first));
  74. EXPECT_EQ("chapo", ToString(p1.second));
  75. EXPECT_EQ("foo", ToString(p2.first));
  76. EXPECT_EQ("bar", ToString(p2.second));
  77. ++iter;
  78. EXPECT_EQ(context.end(), iter);
  79. }
  80. } // namespace
  81. } // namespace grpc
  82. int main(int argc, char** argv) {
  83. ::testing::InitGoogleTest(&argc, argv);
  84. return RUN_ALL_TESTS();
  85. }