auth_context_test.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <string.h>
  34. #include "src/core/security/security_context.h"
  35. #include "src/core/support/string.h"
  36. #include "test/core/util/test_config.h"
  37. #include <grpc/support/log.h>
  38. static void
  39. test_empty_context (void)
  40. {
  41. grpc_auth_context *ctx = grpc_auth_context_create (NULL);
  42. grpc_auth_property_iterator it;
  43. gpr_log (GPR_INFO, "test_empty_context");
  44. GPR_ASSERT (ctx != NULL);
  45. GPR_ASSERT (grpc_auth_context_peer_identity_property_name (ctx) == NULL);
  46. it = grpc_auth_context_peer_identity (ctx);
  47. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == NULL);
  48. it = grpc_auth_context_property_iterator (ctx);
  49. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == NULL);
  50. it = grpc_auth_context_find_properties_by_name (ctx, "foo");
  51. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == NULL);
  52. GPR_ASSERT (grpc_auth_context_set_peer_identity_property_name (ctx, "bar") == 0);
  53. GPR_ASSERT (grpc_auth_context_peer_identity_property_name (ctx) == NULL);
  54. GRPC_AUTH_CONTEXT_UNREF (ctx, "test");
  55. }
  56. static void
  57. test_simple_context (void)
  58. {
  59. grpc_auth_context *ctx = grpc_auth_context_create (NULL);
  60. grpc_auth_property_iterator it;
  61. size_t i;
  62. gpr_log (GPR_INFO, "test_simple_context");
  63. GPR_ASSERT (ctx != NULL);
  64. grpc_auth_context_add_cstring_property (ctx, "name", "chapi");
  65. grpc_auth_context_add_cstring_property (ctx, "name", "chapo");
  66. grpc_auth_context_add_cstring_property (ctx, "foo", "bar");
  67. GPR_ASSERT (ctx->properties.count == 3);
  68. GPR_ASSERT (grpc_auth_context_set_peer_identity_property_name (ctx, "name") == 1);
  69. GPR_ASSERT (strcmp (grpc_auth_context_peer_identity_property_name (ctx), "name") == 0);
  70. it = grpc_auth_context_property_iterator (ctx);
  71. for (i = 0; i < ctx->properties.count; i++)
  72. {
  73. const grpc_auth_property *p = grpc_auth_property_iterator_next (&it);
  74. GPR_ASSERT (p == &ctx->properties.array[i]);
  75. }
  76. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == NULL);
  77. it = grpc_auth_context_find_properties_by_name (ctx, "foo");
  78. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == &ctx->properties.array[2]);
  79. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == NULL);
  80. it = grpc_auth_context_peer_identity (ctx);
  81. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == &ctx->properties.array[0]);
  82. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == &ctx->properties.array[1]);
  83. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == NULL);
  84. GRPC_AUTH_CONTEXT_UNREF (ctx, "test");
  85. }
  86. static void
  87. test_chained_context (void)
  88. {
  89. grpc_auth_context *chained = grpc_auth_context_create (NULL);
  90. grpc_auth_context *ctx = grpc_auth_context_create (chained);
  91. grpc_auth_property_iterator it;
  92. size_t i;
  93. gpr_log (GPR_INFO, "test_chained_context");
  94. GRPC_AUTH_CONTEXT_UNREF (chained, "chained");
  95. grpc_auth_context_add_cstring_property (chained, "name", "padapo");
  96. grpc_auth_context_add_cstring_property (chained, "foo", "baz");
  97. grpc_auth_context_add_cstring_property (ctx, "name", "chapi");
  98. grpc_auth_context_add_cstring_property (ctx, "name", "chap0");
  99. grpc_auth_context_add_cstring_property (ctx, "foo", "bar");
  100. GPR_ASSERT (grpc_auth_context_set_peer_identity_property_name (ctx, "name") == 1);
  101. GPR_ASSERT (strcmp (grpc_auth_context_peer_identity_property_name (ctx), "name") == 0);
  102. it = grpc_auth_context_property_iterator (ctx);
  103. for (i = 0; i < ctx->properties.count; i++)
  104. {
  105. const grpc_auth_property *p = grpc_auth_property_iterator_next (&it);
  106. GPR_ASSERT (p == &ctx->properties.array[i]);
  107. }
  108. for (i = 0; i < chained->properties.count; i++)
  109. {
  110. const grpc_auth_property *p = grpc_auth_property_iterator_next (&it);
  111. GPR_ASSERT (p == &chained->properties.array[i]);
  112. }
  113. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == NULL);
  114. it = grpc_auth_context_find_properties_by_name (ctx, "foo");
  115. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == &ctx->properties.array[2]);
  116. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == &chained->properties.array[1]);
  117. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == NULL);
  118. it = grpc_auth_context_peer_identity (ctx);
  119. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == &ctx->properties.array[0]);
  120. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == &ctx->properties.array[1]);
  121. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == &chained->properties.array[0]);
  122. GPR_ASSERT (grpc_auth_property_iterator_next (&it) == NULL);
  123. GRPC_AUTH_CONTEXT_UNREF (ctx, "test");
  124. }
  125. int
  126. main (int argc, char **argv)
  127. {
  128. grpc_test_init (argc, argv);
  129. test_empty_context ();
  130. test_simple_context ();
  131. test_chained_context ();
  132. return 0;
  133. }