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