1 /*
2 *
3 * Copyright 2017 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 "src/core/lib/channel/channel_stack_builder.h"
20
21 #include <limits.h>
22 #include <string.h>
23
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/string_util.h>
27
28 #include "src/core/lib/slice/slice_internal.h"
29 #include "src/core/lib/surface/channel_init.h"
30 #include "test/core/util/test_config.h"
31
channel_init_func(grpc_channel_element * elem,grpc_channel_element_args * args)32 static grpc_error* channel_init_func(grpc_channel_element* elem,
33 grpc_channel_element_args* args) {
34 return GRPC_ERROR_NONE;
35 }
36
call_init_func(grpc_call_element * elem,const grpc_call_element_args * args)37 static grpc_error* call_init_func(grpc_call_element* elem,
38 const grpc_call_element_args* args) {
39 return GRPC_ERROR_NONE;
40 }
41
channel_destroy_func(grpc_channel_element * elem)42 static void channel_destroy_func(grpc_channel_element* elem) {}
43
call_destroy_func(grpc_call_element * elem,const grpc_call_final_info * final_info,grpc_closure * ignored)44 static void call_destroy_func(grpc_call_element* elem,
45 const grpc_call_final_info* final_info,
46 grpc_closure* ignored) {}
47
call_func(grpc_call_element * elem,grpc_transport_stream_op_batch * op)48 static void call_func(grpc_call_element* elem,
49 grpc_transport_stream_op_batch* op) {}
50
channel_func(grpc_channel_element * elem,grpc_transport_op * op)51 static void channel_func(grpc_channel_element* elem, grpc_transport_op* op) {
52 if (op->disconnect_with_error != GRPC_ERROR_NONE) {
53 GRPC_ERROR_UNREF(op->disconnect_with_error);
54 }
55 GRPC_CLOSURE_SCHED(op->on_consumed, GRPC_ERROR_NONE);
56 }
57
58 bool g_replacement_fn_called = false;
59 bool g_original_fn_called = false;
set_arg_once_fn(grpc_channel_stack * channel_stack,grpc_channel_element * elem,void * arg)60 void set_arg_once_fn(grpc_channel_stack* channel_stack,
61 grpc_channel_element* elem, void* arg) {
62 bool* called = static_cast<bool*>(arg);
63 // Make sure this function is only called once per arg.
64 GPR_ASSERT(*called == false);
65 *called = true;
66 }
67
test_channel_stack_builder_filter_replace(void)68 static void test_channel_stack_builder_filter_replace(void) {
69 grpc_channel* channel =
70 grpc_insecure_channel_create("target name isn't used", nullptr, nullptr);
71 GPR_ASSERT(channel != nullptr);
72 // Make sure the high priority filter has been created.
73 GPR_ASSERT(g_replacement_fn_called);
74 // ... and that the low priority one hasn't.
75 GPR_ASSERT(!g_original_fn_called);
76 grpc_channel_destroy(channel);
77 }
78
79 const grpc_channel_filter replacement_filter = {
80 call_func,
81 channel_func,
82 0,
83 call_init_func,
84 grpc_call_stack_ignore_set_pollset_or_pollset_set,
85 call_destroy_func,
86 0,
87 channel_init_func,
88 channel_destroy_func,
89 grpc_channel_next_get_info,
90 "filter_name"};
91
92 const grpc_channel_filter original_filter = {
93 call_func,
94 channel_func,
95 0,
96 call_init_func,
97 grpc_call_stack_ignore_set_pollset_or_pollset_set,
98 call_destroy_func,
99 0,
100 channel_init_func,
101 channel_destroy_func,
102 grpc_channel_next_get_info,
103 "filter_name"};
104
add_replacement_filter(grpc_channel_stack_builder * builder,void * arg)105 static bool add_replacement_filter(grpc_channel_stack_builder* builder,
106 void* arg) {
107 const grpc_channel_filter* filter =
108 static_cast<const grpc_channel_filter*>(arg);
109 // Get rid of any other version of the filter, as determined by having the
110 // same name.
111 GPR_ASSERT(grpc_channel_stack_builder_remove_filter(builder, filter->name));
112 return grpc_channel_stack_builder_prepend_filter(
113 builder, filter, set_arg_once_fn, &g_replacement_fn_called);
114 }
115
add_original_filter(grpc_channel_stack_builder * builder,void * arg)116 static bool add_original_filter(grpc_channel_stack_builder* builder,
117 void* arg) {
118 return grpc_channel_stack_builder_prepend_filter(
119 builder, static_cast<const grpc_channel_filter*>(arg), set_arg_once_fn,
120 &g_original_fn_called);
121 }
122
init_plugin(void)123 static void init_plugin(void) {
124 grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX,
125 add_original_filter,
126 (void*)&original_filter);
127 grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX,
128 add_replacement_filter,
129 (void*)&replacement_filter);
130 }
131
destroy_plugin(void)132 static void destroy_plugin(void) {}
133
main(int argc,char ** argv)134 int main(int argc, char** argv) {
135 grpc_test_init(argc, argv);
136 grpc_register_plugin(init_plugin, destroy_plugin);
137 grpc_init();
138 test_channel_stack_builder_filter_replace();
139 grpc_shutdown();
140 return 0;
141 }
142