auth_property_iterator_test.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <gtest/gtest.h>
  19. #include <grpc/grpc_security.h>
  20. #include <grpcpp/security/auth_context.h>
  21. #include "src/core/lib/security/context/security_context.h"
  22. #include "src/cpp/common/secure_auth_context.h"
  23. #include "test/cpp/util/string_ref_helper.h"
  24. using ::grpc::testing::ToString;
  25. namespace grpc {
  26. namespace {
  27. class TestAuthPropertyIterator : public AuthPropertyIterator {
  28. public:
  29. TestAuthPropertyIterator() {}
  30. TestAuthPropertyIterator(const grpc_auth_property* property,
  31. const grpc_auth_property_iterator* iter)
  32. : AuthPropertyIterator(property, iter) {}
  33. };
  34. class AuthPropertyIteratorTest : public ::testing::Test {
  35. protected:
  36. void SetUp() override {
  37. ctx_ = grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
  38. grpc_auth_context_add_cstring_property(ctx_.get(), "name", "chapi");
  39. grpc_auth_context_add_cstring_property(ctx_.get(), "name", "chapo");
  40. grpc_auth_context_add_cstring_property(ctx_.get(), "foo", "bar");
  41. EXPECT_EQ(1, grpc_auth_context_set_peer_identity_property_name(ctx_.get(),
  42. "name"));
  43. }
  44. grpc_core::RefCountedPtr<grpc_auth_context> ctx_;
  45. };
  46. TEST_F(AuthPropertyIteratorTest, DefaultCtor) {
  47. TestAuthPropertyIterator iter1;
  48. TestAuthPropertyIterator iter2;
  49. EXPECT_EQ(iter1, iter2);
  50. }
  51. TEST_F(AuthPropertyIteratorTest, GeneralTest) {
  52. grpc_auth_property_iterator c_iter =
  53. grpc_auth_context_property_iterator(ctx_.get());
  54. const grpc_auth_property* property =
  55. grpc_auth_property_iterator_next(&c_iter);
  56. TestAuthPropertyIterator iter(property, &c_iter);
  57. TestAuthPropertyIterator empty_iter;
  58. EXPECT_FALSE(iter == empty_iter);
  59. AuthProperty p0 = *iter;
  60. ++iter;
  61. AuthProperty p1 = *iter;
  62. iter++;
  63. AuthProperty p2 = *iter;
  64. EXPECT_EQ("name", ToString(p0.first));
  65. EXPECT_EQ("chapi", ToString(p0.second));
  66. EXPECT_EQ("name", ToString(p1.first));
  67. EXPECT_EQ("chapo", ToString(p1.second));
  68. EXPECT_EQ("foo", ToString(p2.first));
  69. EXPECT_EQ("bar", ToString(p2.second));
  70. ++iter;
  71. EXPECT_EQ(empty_iter, iter);
  72. }
  73. } // namespace
  74. } // namespace grpc
  75. int main(int argc, char** argv) {
  76. ::testing::InitGoogleTest(&argc, argv);
  77. return RUN_ALL_TESTS();
  78. }