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