auth_context_test.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <string.h>
  19. #include <grpc/support/log.h>
  20. #include "src/core/lib/gpr/string.h"
  21. #include "src/core/lib/gprpp/ref_counted_ptr.h"
  22. #include "src/core/lib/security/context/security_context.h"
  23. #include "test/core/util/test_config.h"
  24. static void test_empty_context(void) {
  25. grpc_core::RefCountedPtr<grpc_auth_context> ctx =
  26. grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
  27. grpc_auth_property_iterator it;
  28. gpr_log(GPR_INFO, "test_empty_context");
  29. GPR_ASSERT(ctx != nullptr);
  30. GPR_ASSERT(grpc_auth_context_peer_identity_property_name(ctx.get()) ==
  31. nullptr);
  32. it = grpc_auth_context_peer_identity(ctx.get());
  33. GPR_ASSERT(grpc_auth_property_iterator_next(&it) == nullptr);
  34. it = grpc_auth_context_property_iterator(ctx.get());
  35. GPR_ASSERT(grpc_auth_property_iterator_next(&it) == nullptr);
  36. it = grpc_auth_context_find_properties_by_name(ctx.get(), "foo");
  37. GPR_ASSERT(grpc_auth_property_iterator_next(&it) == nullptr);
  38. GPR_ASSERT(
  39. grpc_auth_context_set_peer_identity_property_name(ctx.get(), "bar") == 0);
  40. GPR_ASSERT(grpc_auth_context_peer_identity_property_name(ctx.get()) ==
  41. nullptr);
  42. ctx.reset(DEBUG_LOCATION, "test");
  43. }
  44. static void test_simple_context(void) {
  45. grpc_core::RefCountedPtr<grpc_auth_context> ctx =
  46. grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
  47. grpc_auth_property_iterator it;
  48. size_t i;
  49. gpr_log(GPR_INFO, "test_simple_context");
  50. GPR_ASSERT(ctx != nullptr);
  51. grpc_auth_context_add_cstring_property(ctx.get(), "name", "chapi");
  52. grpc_auth_context_add_cstring_property(ctx.get(), "name", "chapo");
  53. grpc_auth_context_add_cstring_property(ctx.get(), "foo", "bar");
  54. GPR_ASSERT(ctx->properties().count == 3);
  55. GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(ctx.get(),
  56. "name") == 1);
  57. GPR_ASSERT(strcmp(grpc_auth_context_peer_identity_property_name(ctx.get()),
  58. "name") == 0);
  59. it = grpc_auth_context_property_iterator(ctx.get());
  60. for (i = 0; i < ctx->properties().count; i++) {
  61. const grpc_auth_property* p = grpc_auth_property_iterator_next(&it);
  62. GPR_ASSERT(p == &ctx->properties().array[i]);
  63. }
  64. GPR_ASSERT(grpc_auth_property_iterator_next(&it) == nullptr);
  65. it = grpc_auth_context_find_properties_by_name(ctx.get(), "foo");
  66. GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
  67. &ctx->properties().array[2]);
  68. GPR_ASSERT(grpc_auth_property_iterator_next(&it) == nullptr);
  69. it = grpc_auth_context_peer_identity(ctx.get());
  70. GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
  71. &ctx->properties().array[0]);
  72. GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
  73. &ctx->properties().array[1]);
  74. GPR_ASSERT(grpc_auth_property_iterator_next(&it) == nullptr);
  75. ctx.reset(DEBUG_LOCATION, "test");
  76. }
  77. static void test_chained_context(void) {
  78. grpc_core::RefCountedPtr<grpc_auth_context> chained =
  79. grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
  80. grpc_auth_context* chained_ptr = chained.get();
  81. grpc_core::RefCountedPtr<grpc_auth_context> ctx =
  82. grpc_core::MakeRefCounted<grpc_auth_context>(std::move(chained));
  83. grpc_auth_property_iterator it;
  84. size_t i;
  85. gpr_log(GPR_INFO, "test_chained_context");
  86. grpc_auth_context_add_cstring_property(chained_ptr, "name", "padapo");
  87. grpc_auth_context_add_cstring_property(chained_ptr, "foo", "baz");
  88. grpc_auth_context_add_cstring_property(ctx.get(), "name", "chapi");
  89. grpc_auth_context_add_cstring_property(ctx.get(), "name", "chap0");
  90. grpc_auth_context_add_cstring_property(ctx.get(), "foo", "bar");
  91. GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(ctx.get(),
  92. "name") == 1);
  93. GPR_ASSERT(strcmp(grpc_auth_context_peer_identity_property_name(ctx.get()),
  94. "name") == 0);
  95. it = grpc_auth_context_property_iterator(ctx.get());
  96. for (i = 0; i < ctx->properties().count; i++) {
  97. const grpc_auth_property* p = grpc_auth_property_iterator_next(&it);
  98. GPR_ASSERT(p == &ctx->properties().array[i]);
  99. }
  100. for (i = 0; i < chained_ptr->properties().count; i++) {
  101. const grpc_auth_property* p = grpc_auth_property_iterator_next(&it);
  102. GPR_ASSERT(p == &chained_ptr->properties().array[i]);
  103. }
  104. GPR_ASSERT(grpc_auth_property_iterator_next(&it) == nullptr);
  105. it = grpc_auth_context_find_properties_by_name(ctx.get(), "foo");
  106. GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
  107. &ctx->properties().array[2]);
  108. GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
  109. &chained_ptr->properties().array[1]);
  110. GPR_ASSERT(grpc_auth_property_iterator_next(&it) == nullptr);
  111. it = grpc_auth_context_peer_identity(ctx.get());
  112. GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
  113. &ctx->properties().array[0]);
  114. GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
  115. &ctx->properties().array[1]);
  116. GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
  117. &chained_ptr->properties().array[0]);
  118. GPR_ASSERT(grpc_auth_property_iterator_next(&it) == nullptr);
  119. ctx.reset(DEBUG_LOCATION, "test");
  120. }
  121. int main(int argc, char** argv) {
  122. grpc::testing::TestEnvironment env(argc, argv);
  123. test_empty_context();
  124. test_simple_context();
  125. test_chained_context();
  126. return 0;
  127. }