• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*******************************************************************************
20  * This test verifies that various stack configurations result in the set of
21  * filters that we expect.
22  *
23  * This is akin to a golden-file test, and suffers the same disadvantages and
24  * advantages: it reflects that the code as written has not been modified - and
25  * valid code modifications WILL break this test and it will need updating.
26  *
27  * The intent therefore is to allow code reviewers to more easily catch changes
28  * that perturb the generated list of channel filters in different
29  * configurations and assess whether such a change is correct and desirable.
30  */
31 
32 #include <grpc/grpc.h>
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/string_util.h>
35 #include <string.h>
36 
37 #include "absl/strings/str_cat.h"
38 #include "absl/strings/str_format.h"
39 #include "absl/strings/str_join.h"
40 
41 #include "src/core/lib/channel/channel_stack_builder.h"
42 #include "src/core/lib/gpr/string.h"
43 #include "src/core/lib/surface/channel_init.h"
44 #include "src/core/lib/surface/channel_stack_type.h"
45 #include "src/core/lib/transport/transport_impl.h"
46 #include "test/core/util/test_config.h"
47 
48 // use CHECK_STACK instead
49 static int check_stack(const char* file, int line, const char* transport_name,
50                        grpc_channel_args* init_args,
51                        unsigned channel_stack_type, ...);
52 
53 // arguments: const char *transport_name   - the name of the transport type to
54 //                                           simulate
55 //            grpc_channel_args *init_args - channel args to pass down
56 //            grpc_channel_stack_type channel_stack_type - the archetype of
57 //                                           channel stack to create
58 //            variadic arguments - the (in-order) expected list of channel
59 //                                 filters to instantiate, terminated with NULL
60 #define CHECK_STACK(...) check_stack(__FILE__, __LINE__, __VA_ARGS__)
61 
main(int argc,char ** argv)62 int main(int argc, char** argv) {
63   grpc::testing::TestEnvironment env(argc, argv);
64   grpc_init();
65   int errors = 0;
66 
67   // tests with a minimal stack
68   grpc_arg minimal_stack_arg;
69   minimal_stack_arg.type = GRPC_ARG_INTEGER;
70   minimal_stack_arg.key = const_cast<char*>(GRPC_ARG_MINIMAL_STACK);
71   minimal_stack_arg.value.integer = 1;
72   grpc_channel_args minimal_stack_args = {1, &minimal_stack_arg};
73   errors +=
74       CHECK_STACK("unknown", &minimal_stack_args, GRPC_CLIENT_DIRECT_CHANNEL,
75                   "authority", "connected", NULL);
76   errors += CHECK_STACK("unknown", &minimal_stack_args, GRPC_CLIENT_SUBCHANNEL,
77                         "authority", "connected", NULL);
78   errors += CHECK_STACK("unknown", &minimal_stack_args, GRPC_SERVER_CHANNEL,
79                         "server", "connected", NULL);
80   errors += CHECK_STACK("chttp2", &minimal_stack_args,
81                         GRPC_CLIENT_DIRECT_CHANNEL, "authority", "http-client",
82                         "message_decompress", "connected", NULL);
83   errors += CHECK_STACK("chttp2", &minimal_stack_args, GRPC_CLIENT_SUBCHANNEL,
84                         "authority", "http-client", "message_decompress",
85                         "connected", NULL);
86   errors +=
87       CHECK_STACK("chttp2", &minimal_stack_args, GRPC_SERVER_CHANNEL, "server",
88                   "http-server", "message_decompress", "connected", NULL);
89   errors += CHECK_STACK(nullptr, &minimal_stack_args, GRPC_CLIENT_CHANNEL,
90                         "client-channel", NULL);
91 
92   // tests with a default stack
93   errors +=
94       CHECK_STACK("unknown", nullptr, GRPC_CLIENT_DIRECT_CHANNEL, "authority",
95                   "message_size", "deadline", "connected", NULL);
96   errors += CHECK_STACK("unknown", nullptr, GRPC_CLIENT_SUBCHANNEL, "authority",
97                         "message_size", "connected", NULL);
98   errors += CHECK_STACK("unknown", nullptr, GRPC_SERVER_CHANNEL, "server",
99                         "message_size", "deadline", "connected", NULL);
100   errors +=
101       CHECK_STACK("chttp2", nullptr, GRPC_CLIENT_DIRECT_CHANNEL, "authority",
102                   "message_size", "deadline", "http-client",
103                   "message_decompress", "message_compress", "connected", NULL);
104   errors += CHECK_STACK("chttp2", nullptr, GRPC_CLIENT_SUBCHANNEL, "authority",
105                         "message_size", "http-client", "message_decompress",
106                         "message_compress", "connected", NULL);
107   errors +=
108       CHECK_STACK("chttp2", nullptr, GRPC_SERVER_CHANNEL, "server",
109                   "message_size", "deadline", "http-server",
110                   "message_decompress", "message_compress", "connected", NULL);
111   errors += CHECK_STACK(nullptr, nullptr, GRPC_CLIENT_CHANNEL, "client-channel",
112                         NULL);
113 
114   GPR_ASSERT(errors == 0);
115   grpc_shutdown();
116   return 0;
117 }
118 
119 /*******************************************************************************
120  * End of tests definitions, start of test infrastructure
121  */
122 
check_stack(const char * file,int line,const char * transport_name,grpc_channel_args * init_args,unsigned channel_stack_type,...)123 static int check_stack(const char* file, int line, const char* transport_name,
124                        grpc_channel_args* init_args,
125                        unsigned channel_stack_type, ...) {
126   // create dummy channel stack
127   grpc_channel_stack_builder* builder = grpc_channel_stack_builder_create();
128   grpc_transport_vtable fake_transport_vtable;
129   memset(&fake_transport_vtable, 0, sizeof(grpc_transport_vtable));
130   fake_transport_vtable.name = transport_name;
131   grpc_transport fake_transport = {&fake_transport_vtable};
132   grpc_channel_stack_builder_set_target(builder, "foo.test.google.fr");
133   grpc_channel_args* channel_args = grpc_channel_args_copy(init_args);
134   if (transport_name != nullptr) {
135     grpc_channel_stack_builder_set_transport(builder, &fake_transport);
136   }
137   {
138     grpc_core::ExecCtx exec_ctx;
139     grpc_channel_stack_builder_set_channel_arguments(builder, channel_args);
140     GPR_ASSERT(grpc_channel_init_create_stack(
141         builder, (grpc_channel_stack_type)channel_stack_type));
142   }
143 
144   // build up our expectation list
145   std::vector<std::string> parts;
146   va_list args;
147   va_start(args, channel_stack_type);
148   for (;;) {
149     char* a = va_arg(args, char*);
150     if (a == nullptr) break;
151     parts.push_back(a);
152   }
153   va_end(args);
154   std::string expect = absl::StrJoin(parts, ", ");
155 
156   // build up our "got" list
157   parts.clear();
158   grpc_channel_stack_builder_iterator* it =
159       grpc_channel_stack_builder_create_iterator_at_first(builder);
160   while (grpc_channel_stack_builder_move_next(it)) {
161     const char* name = grpc_channel_stack_builder_iterator_filter_name(it);
162     if (name == nullptr) continue;
163     parts.push_back(name);
164   }
165   std::string got = absl::StrJoin(parts, ", ");
166   grpc_channel_stack_builder_iterator_destroy(it);
167 
168   // figure out result, log if there's an error
169   int result = 0;
170   if (got != expect) {
171     parts.clear();
172     for (size_t i = 0; i < channel_args->num_args; i++) {
173       std::string value;
174       switch (channel_args->args[i].type) {
175         case GRPC_ARG_INTEGER: {
176           value = absl::StrCat(channel_args->args[i].value.integer);
177           break;
178         }
179         case GRPC_ARG_STRING:
180           value = channel_args->args[i].value.string;
181           break;
182         case GRPC_ARG_POINTER: {
183           value = absl::StrFormat("%p", channel_args->args[i].value.pointer.p);
184           break;
185         }
186       }
187       parts.push_back(absl::StrCat(channel_args->args[i].key, "=", value));
188     }
189     std::string args_str = absl::StrCat("{", absl::StrJoin(parts, ", "), "}");
190 
191     gpr_log(file, line, GPR_LOG_SEVERITY_ERROR,
192             "**************************************************");
193     gpr_log(
194         file, line, GPR_LOG_SEVERITY_ERROR,
195         "FAILED transport=%s; stack_type=%s; channel_args=%s:", transport_name,
196         grpc_channel_stack_type_string(
197             static_cast<grpc_channel_stack_type>(channel_stack_type)),
198         args_str.c_str());
199     gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "EXPECTED: %s", expect.c_str());
200     gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "GOT:      %s", got.c_str());
201     result = 1;
202   }
203 
204   {
205     grpc_core::ExecCtx exec_ctx;
206     grpc_channel_stack_builder_destroy(builder);
207     grpc_channel_args_destroy(channel_args);
208   }
209 
210   return result;
211 }
212