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