Debian Codegraph @

test/core/security/auth_context_test.cc

raw
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 <gtest/gtest.h>
22
23 #include <grpc/support/log.h>
24
25 #include "src/core/lib/gpr/string.h"
26 #include "src/core/lib/gprpp/ref_counted_ptr.h"
27 #include "src/core/lib/security/context/security_context.h"
28 #include "test/core/util/test_config.h"
29
30 TEST(AuthContextTest, EmptyContext) {
31 grpc_core::RefCountedPtr<grpc_auth_context> ctx =
32 grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
33 grpc_auth_property_iterator it;
34
35 gpr_log(GPR_INFO, "test_empty_context");
36 ASSERT_NE(ctx, nullptr);
37 ASSERT_EQ(grpc_auth_context_peer_identity_property_name(ctx.get()), nullptr);
38 it = grpc_auth_context_peer_identity(ctx.get());
39 ASSERT_EQ(grpc_auth_property_iterator_next(&it), nullptr);
40 it = grpc_auth_context_property_iterator(ctx.get());
41 ASSERT_EQ(grpc_auth_property_iterator_next(&it), nullptr);
42 it = grpc_auth_context_find_properties_by_name(ctx.get(), "foo");
43 ASSERT_EQ(grpc_auth_property_iterator_next(&it), nullptr);
44 ASSERT_EQ(grpc_auth_context_set_peer_identity_property_name(ctx.get(), "bar"),
45 0);
46 ASSERT_EQ(grpc_auth_context_peer_identity_property_name(ctx.get()), nullptr);
47 ctx.reset(DEBUG_LOCATION, "test");
48 }
49
50 TEST(AuthContextTest, SimpleContext) {
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 ASSERT_NE(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 ASSERT_EQ(ctx->properties().count, 3);
62 ASSERT_EQ(
63 grpc_auth_context_set_peer_identity_property_name(ctx.get(), "name"), 1);
64
65 ASSERT_STREQ(grpc_auth_context_peer_identity_property_name(ctx.get()),
66 "name");
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 ASSERT_EQ(p, &ctx->properties().array[i]);
71 }
72 ASSERT_EQ(grpc_auth_property_iterator_next(&it), nullptr);
73
74 it = grpc_auth_context_find_properties_by_name(ctx.get(), "foo");
75 ASSERT_EQ(grpc_auth_property_iterator_next(&it), &ctx->properties().array[2]);
76 ASSERT_EQ(grpc_auth_property_iterator_next(&it), nullptr);
77
78 it = grpc_auth_context_peer_identity(ctx.get());
79 ASSERT_EQ(grpc_auth_property_iterator_next(&it), &ctx->properties().array[0]);
80 ASSERT_EQ(grpc_auth_property_iterator_next(&it), &ctx->properties().array[1]);
81 ASSERT_EQ(grpc_auth_property_iterator_next(&it), nullptr);
82
83 ctx.reset(DEBUG_LOCATION, "test");
84 }
85
86 TEST(AuthContextTest, ChainedContext) {
87 grpc_core::RefCountedPtr<grpc_auth_context> chained =
88 grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
89 grpc_auth_context* chained_ptr = chained.get();
90 grpc_core::RefCountedPtr<grpc_auth_context> ctx =
91 grpc_core::MakeRefCounted<grpc_auth_context>(std::move(chained));
92
93 grpc_auth_property_iterator it;
94 size_t i;
95
96 gpr_log(GPR_INFO, "test_chained_context");
97 grpc_auth_context_add_cstring_property(chained_ptr, "name", "padapo");
98 grpc_auth_context_add_cstring_property(chained_ptr, "foo", "baz");
99 grpc_auth_context_add_cstring_property(ctx.get(), "name", "chapi");
100 grpc_auth_context_add_cstring_property(ctx.get(), "name", "chap0");
101 grpc_auth_context_add_cstring_property(ctx.get(), "foo", "bar");
102 ASSERT_EQ(
103 grpc_auth_context_set_peer_identity_property_name(ctx.get(), "name"), 1);
104
105 ASSERT_STREQ(grpc_auth_context_peer_identity_property_name(ctx.get()),
106 "name");
107 it = grpc_auth_context_property_iterator(ctx.get());
108 for (i = 0; i < ctx->properties().count; i++) {
109 const grpc_auth_property* p = grpc_auth_property_iterator_next(&it);
110 ASSERT_EQ(p, &ctx->properties().array[i]);
111 }
112 for (i = 0; i < chained_ptr->properties().count; i++) {
113 const grpc_auth_property* p = grpc_auth_property_iterator_next(&it);
114 ASSERT_EQ(p, &chained_ptr->properties().array[i]);
115 }
116 ASSERT_EQ(grpc_auth_property_iterator_next(&it), nullptr);
117
118 it = grpc_auth_context_find_properties_by_name(ctx.get(), "foo");
119 ASSERT_EQ(grpc_auth_property_iterator_next(&it), &ctx->properties().array[2]);
120 ASSERT_EQ(grpc_auth_property_iterator_next(&it),
121 &chained_ptr->properties().array[1]);
122 ASSERT_EQ(grpc_auth_property_iterator_next(&it), nullptr);
123
124 it = grpc_auth_context_peer_identity(ctx.get());
125 ASSERT_EQ(grpc_auth_property_iterator_next(&it), &ctx->properties().array[0]);
126 ASSERT_EQ(grpc_auth_property_iterator_next(&it), &ctx->properties().array[1]);
127 ASSERT_EQ(grpc_auth_property_iterator_next(&it),
128 &chained_ptr->properties().array[0]);
129 ASSERT_EQ(grpc_auth_property_iterator_next(&it), nullptr);
130
131 ctx.reset(DEBUG_LOCATION, "test");
132 }
133
134 TEST(AuthContextTest, ContextWithExtension) {
135 class SampleExtension : public grpc_auth_context::Extension {};
136 grpc_core::RefCountedPtr<grpc_auth_context> ctx =
137 grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
138 // Just set the extension, the goal of this test is to catch any memory
139 // leaks when context goes out of scope.
140 ctx->set_extension(std::make_unique<SampleExtension>());
141 }
142
143 int main(int argc, char** argv) {
144 grpc::testing::TestEnvironment env(&argc, argv);
145 ::testing::InitGoogleTest(&argc, argv);
146 return RUN_ALL_TESTS();
147 }