channel_args_test.cc
Go to the documentation of this file.
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 
20 
21 #include <string.h>
22 
23 #include <gtest/gtest.h>
24 
25 #include <grpc/grpc_security.h>
27 #include <grpc/impl/codegen/log.h>
28 #include <grpc/support/log.h>
29 
37 
38 namespace grpc_core {
39 
40 TEST(ChannelArgsTest, Noop) { ChannelArgs(); }
41 
42 TEST(ChannelArgsTest, SetGetRemove) {
43  const grpc_arg_pointer_vtable malloc_vtable = {
44  // copy
45  [](void* p) { return p; },
46  // destroy
47  [](void*) {},
48  // equal
49  [](void* p1, void* p2) { return QsortCompare(p1, p2); },
50  };
51  void* ptr = gpr_malloc(42);
52 
53  ChannelArgs a;
54  ChannelArgs b = a.Set("answer", 42);
55  ChannelArgs c = b.Set("foo", "bar");
56  ChannelArgs d = c.Set("ptr", ChannelArgs::Pointer(ptr, &malloc_vtable));
57  ChannelArgs e = d.Set("alpha", "beta");
58  ChannelArgs f = e.Remove("answer");
59  EXPECT_EQ(a.Get("answer"), nullptr);
60  EXPECT_EQ(*b.Get("answer"), ChannelArgs::Value(42));
61  EXPECT_EQ(*c.Get("answer"), ChannelArgs::Value(42));
62  EXPECT_EQ(c.GetInt("answer"), 42);
63  EXPECT_EQ(c.GetString("answer"), absl::nullopt);
64  EXPECT_EQ(f.Get("answer"), nullptr);
65  EXPECT_EQ(*c.Get("foo"), ChannelArgs::Value("bar"));
66  EXPECT_EQ(c.GetString("foo"), "bar");
67  EXPECT_EQ(c.GetString("answer"), absl::nullopt);
68  EXPECT_EQ(*d.Get("ptr"),
69  ChannelArgs::Value(ChannelArgs::Pointer(ptr, &malloc_vtable)));
70  EXPECT_EQ(*e.Get("alpha"), ChannelArgs::Value("beta"));
71  gpr_free(ptr);
72 }
73 
74 TEST(ChannelArgsTest, StoreRefCountedPtr) {
75  struct Test : public RefCounted<Test> {
76  explicit Test(int n) : n(n) {}
77  int n;
78  static int ChannelArgsCompare(const Test* a, const Test* b) {
79  return a->n - b->n;
80  }
81  };
82  auto p = MakeRefCounted<Test>(123);
83 
84  ChannelArgs a;
85  a = a.Set("test", p);
86  EXPECT_EQ(a.GetPointer<Test>("test")->n, 123);
87 }
88 
89 TEST(ChannelArgsTest, ObjectApi) {
90  struct MyFancyObject : public RefCounted<MyFancyObject> {
91  explicit MyFancyObject(int n) : n(n) {}
92  static absl::string_view ChannelArgName() {
93  return "grpc.internal.my-fancy-object";
94  }
95  int n;
96  static int ChannelArgsCompare(const MyFancyObject* a,
97  const MyFancyObject* b) {
98  return a->n - b->n;
99  }
100  };
101  auto p = MakeRefCounted<MyFancyObject>(42);
102  ChannelArgs a;
103  a = a.SetObject(std::move(p));
104  EXPECT_EQ(a.GetObject<MyFancyObject>()->n, 42);
105 }
106 
107 TEST(ChannelArgsTest, ToAndFromC) {
108  const grpc_arg_pointer_vtable malloc_vtable = {
109  // copy
110  [](void* p) { return p; },
111  // destroy
112  [](void*) {},
113  // equal
114  [](void* p1, void* p2) { return QsortCompare(p1, p2); },
115  };
116  void* ptr = gpr_malloc(42);
118  .Set("answer", 42)
119  .Set("foo", "bar")
120  .Set("ptr", ChannelArgs::Pointer(ptr, &malloc_vtable))
121  .Set("alpha", "beta");
122  const grpc_channel_args* c = a.ToC();
125  EXPECT_EQ(a, b);
126  gpr_free(ptr);
127 }
128 
129 } // namespace grpc_core
130 
131 TEST(GrpcChannelArgsTest, Create) {
133  grpc_arg to_add[2];
134  grpc_channel_args* ch_args;
135 
136  to_add[0] =
137  grpc_channel_arg_integer_create(const_cast<char*>("int_arg"), 123);
138  to_add[1] = grpc_channel_arg_string_create(const_cast<char*>("str key"),
139  const_cast<char*>("str value"));
140  ch_args = grpc_channel_args_copy_and_add(nullptr, to_add, 2);
141 
142  GPR_ASSERT(ch_args->num_args == 2);
143  GPR_ASSERT(strcmp(ch_args->args[0].key, to_add[0].key) == 0);
144  GPR_ASSERT(ch_args->args[0].type == to_add[0].type);
145  GPR_ASSERT(ch_args->args[0].value.integer == to_add[0].value.integer);
146 
147  GPR_ASSERT(strcmp(ch_args->args[1].key, to_add[1].key) == 0);
148  GPR_ASSERT(ch_args->args[1].type == to_add[1].type);
149  GPR_ASSERT(strcmp(ch_args->args[1].value.string, to_add[1].value.string) ==
150  0);
151 
152  grpc_channel_args_destroy(ch_args);
153 }
154 
155 struct fake_class {
156  int foo;
157 };
158 
159 static void* fake_pointer_arg_copy(void* arg) {
160  gpr_log(GPR_DEBUG, "fake_pointer_arg_copy");
161  fake_class* fc = static_cast<fake_class*>(arg);
162  fake_class* new_fc = static_cast<fake_class*>(gpr_malloc(sizeof(fake_class)));
163  new_fc->foo = fc->foo;
164  return new_fc;
165 }
166 
167 static void fake_pointer_arg_destroy(void* arg) {
168  gpr_log(GPR_DEBUG, "fake_pointer_arg_destroy");
169  fake_class* fc = static_cast<fake_class*>(arg);
170  gpr_free(fc);
171 }
172 
173 static int fake_pointer_cmp(void* a, void* b) {
174  return grpc_core::QsortCompare(a, b);
175 }
176 
179 
180 TEST(GrpcChannelArgsTest, ChannelCreateWithArgs) {
181  grpc_arg client_a[3];
182 
183  client_a[0] =
184  grpc_channel_arg_integer_create(const_cast<char*>("arg_int"), 0);
185  client_a[1] = grpc_channel_arg_string_create(
186  const_cast<char*>("arg_str"), const_cast<char*>("arg_str_val"));
187  // allocated and adds custom pointer arg
188  fake_class* fc = static_cast<fake_class*>(gpr_malloc(sizeof(fake_class)));
189  fc->foo = 42;
190  client_a[2] = grpc_channel_arg_pointer_create(
191  const_cast<char*>("arg_pointer"), fc, &fake_pointer_arg_vtable);
192 
193  // creates channel
194  grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a};
196  grpc_channel* c = grpc_channel_create("fake_target", creds, &client_args);
198  // user is can free the memory they allocated here
199  gpr_free(fc);
201 }
202 
204  grpc_channel_args* old_args,
205  grpc_channel_stack_type /*type*/) {
206  GPR_ASSERT(old_args != nullptr);
207  GPR_ASSERT(grpc_channel_args_find(old_args, "arg_int")->value.integer == 0);
208  GPR_ASSERT(strcmp(grpc_channel_args_find(old_args, "arg_str")->value.string,
209  "arg_str_val") == 0);
210  GPR_ASSERT(
211  grpc_channel_args_find(old_args, "arg_pointer")->value.pointer.vtable ==
213 
214  if (strcmp(target, "no_op_mutator") == 0) {
215  return old_args;
216  }
217 
218  GPR_ASSERT(strcmp(target, "minimal_stack_mutator") == 0);
219  const char* args_to_remove[] = {"arg_int", "arg_str", "arg_pointer"};
220 
221  grpc_arg no_deadline_filter_arg = grpc_channel_arg_integer_create(
222  const_cast<char*>(GRPC_ARG_MINIMAL_STACK), 1);
223  grpc_channel_args* new_args = nullptr;
225  old_args, args_to_remove, GPR_ARRAY_SIZE(args_to_remove),
226  &no_deadline_filter_arg, 1);
227  grpc_channel_args_destroy(old_args);
228  return new_args;
229 }
230 
231 TEST(GrpcChannelArgsTest, TestServerCreateWithArgs) {
232  grpc_arg server_a[3];
233 
234  // adds integer arg
235  server_a[0].type = GRPC_ARG_INTEGER;
236  server_a[0].key = const_cast<char*>("arg_int");
237  server_a[0].value.integer = 0;
238 
239  // adds const str arg
240  server_a[1].type = GRPC_ARG_STRING;
241  server_a[1].key = const_cast<char*>("arg_str");
242  server_a[1].value.string = const_cast<char*>("arg_str_val");
243 
244  // allocated and adds custom pointer arg
245  fake_class* fc = static_cast<fake_class*>(gpr_malloc(sizeof(fake_class)));
246  fc->foo = 42;
247  server_a[2].type = GRPC_ARG_POINTER;
248  server_a[2].key = const_cast<char*>("arg_pointer");
250  server_a[2].value.pointer.p = fc;
251 
252  // creates server
253  grpc_channel_args server_args = {GPR_ARRAY_SIZE(server_a), server_a};
254  grpc_server* s = grpc_server_create(&server_args, nullptr);
255  // user is can free the memory they allocated here
256  gpr_free(fc);
258 }
259 
260 int main(int argc, char** argv) {
261  ::testing::InitGoogleTest(&argc, argv);
262  grpc::testing::TestEnvironment env(&argc, argv);
263  grpc_init();
264  auto r = RUN_ALL_TESTS();
265  grpc_shutdown();
266  return r;
267 }
grpc_arg
Definition: grpc_types.h:103
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
fake_pointer_cmp
static int fake_pointer_cmp(void *a, void *b)
Definition: channel_args_test.cc:173
log.h
Test
void Test(StringPiece pattern, const RE2::Options &options, StringPiece text)
Definition: bloaty/third_party/re2/re2/fuzzing/re2_fuzzer.cc:20
generate.env
env
Definition: generate.py:37
GRPC_ARG_INTEGER
@ GRPC_ARG_INTEGER
Definition: grpc_types.h:81
grpc_arg::value
union grpc_arg::grpc_arg_value value
mutate_channel_args
grpc_channel_args * mutate_channel_args(const char *target, grpc_channel_args *old_args, grpc_channel_stack_type)
Definition: channel_args_test.cc:203
grpc_arg::grpc_arg_value::pointer
struct grpc_arg::grpc_arg_value::grpc_arg_pointer pointer
GRPC_ARG_STRING
@ GRPC_ARG_STRING
Definition: grpc_types.h:80
grpc_core
Definition: call_metric_recorder.h:31
string.h
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
useful.h
grpc_core::ChannelArgs::FromC
static ChannelArgs FromC(const grpc_channel_args *args)
Definition: channel_args.cc:84
grpc_server_create
GRPCAPI grpc_server * grpc_server_create(const grpc_channel_args *args, void *reserved)
Definition: src/core/lib/surface/server.cc:1456
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
grpc_channel_args_copy_and_add_and_remove
grpc_channel_args * grpc_channel_args_copy_and_add_and_remove(const grpc_channel_args *src, const char **to_remove, size_t num_to_remove, const grpc_arg *to_add, size_t num_to_add)
Definition: channel_args.cc:246
grpc_core::TEST
TEST(AvlTest, NoOp)
Definition: avl_test.cc:21
log.h
main
int main(int argc, char **argv)
Definition: channel_args_test.cc:260
grpc_channel_arg_string_create
grpc_arg grpc_channel_arg_string_create(char *name, char *value)
Definition: channel_args.cc:476
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
grpc_security.h
grpc_arg_pointer_vtable
Definition: grpc_types.h:85
grpc_channel_args
Definition: grpc_types.h:132
fake_pointer_arg_vtable
static const grpc_arg_pointer_vtable fake_pointer_arg_vtable
Definition: channel_args_test.cc:177
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
grpc_types.h
fake_class::foo
int foo
Definition: channel_args_test.cc:156
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
fake_pointer_arg_destroy
static void fake_pointer_arg_destroy(void *arg)
Definition: channel_args_test.cc:167
grpc_arg::grpc_arg_value::string
char * string
Definition: grpc_types.h:107
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
fake_class
Definition: channel_args_test.cc:155
grpc_arg::grpc_arg_value::grpc_arg_pointer::vtable
const grpc_arg_pointer_vtable * vtable
Definition: grpc_types.h:111
channel_stack.h
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_insecure_credentials_create
GRPCAPI grpc_channel_credentials * grpc_insecure_credentials_create()
Definition: core/lib/security/credentials/insecure/insecure_credentials.cc:64
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
grpc_channel_args::num_args
size_t num_args
Definition: grpc_types.h:133
arg
Definition: cmdline.cc:40
grpc_arg::grpc_arg_value::grpc_arg_pointer::p
void * p
Definition: grpc_types.h:110
grpc_server
struct grpc_server grpc_server
Definition: grpc_types.h:65
grpc_server_destroy
GRPCAPI void grpc_server_destroy(grpc_server *server)
Definition: src/core/lib/surface/server.cc:1519
TEST
TEST(GrpcChannelArgsTest, Create)
Definition: channel_args_test.cc:131
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::RefCounted
Definition: ref_counted.h:280
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
grpc_core::ExecCtx
Definition: exec_ctx.h:97
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
test_config.h
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_core::ChannelArgs::Set
GRPC_MUST_USE_RESULT ChannelArgs Set(absl::string_view name, Value value) const
Definition: channel_args.cc:113
grpc_channel_credentials_release
GRPCAPI void grpc_channel_credentials_release(grpc_channel_credentials *creds)
Definition: credentials.cc:36
GPR_ARRAY_SIZE
#define GPR_ARRAY_SIZE(array)
Definition: useful.h:129
grpc_channel_create
GRPCAPI grpc_channel * grpc_channel_create(const char *target, grpc_channel_credentials *creds, const grpc_channel_args *args)
Definition: chttp2_connector.cc:366
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_channel_arg_integer_create
grpc_arg grpc_channel_arg_integer_create(char *name, int value)
Definition: channel_args.cc:484
grpc_core::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
grpc_core::ChannelArgs::Pointer
Definition: channel_args.h:113
ref_counted.h
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
fix_build_deps.r
r
Definition: fix_build_deps.py:491
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_arg::key
char * key
Definition: grpc_types.h:105
GRPC_ARG_MINIMAL_STACK
#define GRPC_ARG_MINIMAL_STACK
Definition: grpc_types.h:147
grpc_channel_stack_type
grpc_channel_stack_type
Definition: channel_stack_type.h:24
arg
struct arg arg
grpc_channel_destroy
GRPCAPI void grpc_channel_destroy(grpc_channel *channel)
Definition: channel.cc:437
exec_ctx.h
fc
StreamFlowControl fc
Definition: flow_control_fuzzer.cc:90
grpc_core::ChannelArgs
Definition: channel_args.h:111
ref_counted_ptr.h
grpc_channel
struct grpc_channel grpc_channel
Definition: grpc_types.h:62
channel_args.h
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
GRPC_ARG_POINTER
@ GRPC_ARG_POINTER
Definition: grpc_types.h:82
grpc_arg::grpc_arg_value::integer
int integer
Definition: grpc_types.h:108
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_arg::type
grpc_arg_type type
Definition: grpc_types.h:104
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
grpc_channel_args::args
grpc_arg * args
Definition: grpc_types.h:134
grpc_channel_arg_pointer_create
grpc_arg grpc_channel_arg_pointer_create(char *name, void *value, const grpc_arg_pointer_vtable *vtable)
Definition: channel_args.cc:492
grpc_channel_args_find
const grpc_arg * grpc_channel_args_find(const grpc_channel_args *args, const char *name)
Definition: channel_args.cc:393
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
grpc_channel_credentials
Definition: src/core/lib/security/credentials/credentials.h:96
fake_pointer_arg_copy
static void * fake_pointer_arg_copy(void *arg)
Definition: channel_args_test.cc:159
Test
Definition: hpack_parser_test.cc:43
grpc_channel_args_copy_and_add
grpc_channel_args * grpc_channel_args_copy_and_add(const grpc_channel_args *src, const grpc_arg *to_add, size_t num_to_add)
Definition: channel_args.cc:224
channel.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:52