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