• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/impl/propagation_bits.h>
20 #include <grpc/slice.h>
21 #include <grpc/status.h>
22 #include <grpc/support/time.h>
23 
24 #include <algorithm>
25 #include <memory>
26 #include <string>
27 
28 #include "absl/log/check.h"
29 #include "absl/log/log.h"
30 #include "absl/status/statusor.h"
31 #include "src/core/lib/iomgr/port.h"
32 #include "src/core/lib/iomgr/resolved_address.h"
33 
34 // This test won't work except with posix sockets enabled
35 #ifdef GRPC_POSIX_SOCKET_EV
36 
37 #include <grpc/credentials.h>
38 #include <grpc/grpc.h>
39 #include <grpc/grpc_security.h>
40 #include <grpc/support/alloc.h>
41 #include <string.h>
42 
43 #include <vector>
44 
45 #include "absl/strings/str_format.h"
46 #include "absl/strings/str_join.h"
47 #include "absl/strings/str_split.h"
48 #include "absl/strings/string_view.h"
49 #include "src/core/lib/address_utils/sockaddr_utils.h"
50 #include "src/core/lib/iomgr/error.h"
51 #include "src/core/lib/iomgr/resolve_address.h"
52 #include "src/core/lib/iomgr/socket_utils_posix.h"
53 #include "src/core/lib/transport/error_utils.h"
54 #include "src/core/util/host_port.h"
55 #include "test/core/end2end/cq_verifier.h"
56 #include "test/core/test_util/port.h"
57 #include "test/core/test_util/test_config.h"
58 
59 // This test exercises IPv4, IPv6, and dualstack sockets in various ways.
60 
drain_cq(grpc_completion_queue * cq)61 static void drain_cq(grpc_completion_queue* cq) {
62   grpc_event ev;
63   do {
64     ev = grpc_completion_queue_next(
65         cq, grpc_timeout_milliseconds_to_deadline(5000), nullptr);
66   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
67 }
68 
log_resolved_addrs(const char * label,const char * hostname)69 static void log_resolved_addrs(const char* label, const char* hostname) {
70   absl::StatusOr<std::vector<grpc_resolved_address>> addresses_or =
71       grpc_core::GetDNSResolver()->LookupHostnameBlocking(hostname, "80");
72   if (!addresses_or.ok()) {
73     GRPC_LOG_IF_ERROR(hostname,
74                       absl_status_to_grpc_error(addresses_or.status()));
75     return;
76   }
77   for (const auto& addr : *addresses_or) {
78     LOG(INFO) << label << ": " << grpc_sockaddr_to_uri(&addr)->c_str();
79   }
80 }
81 
test_connect(const char * server_host,const char * client_host,int port,int expect_ok)82 void test_connect(const char* server_host, const char* client_host, int port,
83                   int expect_ok) {
84   grpc_channel* client;
85   grpc_server* server;
86   grpc_completion_queue* cq;
87   grpc_call* c;
88   grpc_call* s;
89   gpr_timespec deadline;
90   int got_port;
91   grpc_op ops[6];
92   grpc_op* op;
93   grpc_metadata_array initial_metadata_recv;
94   grpc_metadata_array trailing_metadata_recv;
95   grpc_metadata_array request_metadata_recv;
96   grpc_status_code status;
97   grpc_call_error error;
98   grpc_slice details;
99   int was_cancelled = 2;
100   grpc_call_details call_details;
101   char* peer;
102   int picked_port = 0;
103 
104   if (port == 0) {
105     port = grpc_pick_unused_port_or_die();
106     picked_port = 1;
107   }
108 
109   std::string server_hostport = grpc_core::JoinHostPort(server_host, port);
110 
111   grpc_metadata_array_init(&initial_metadata_recv);
112   grpc_metadata_array_init(&trailing_metadata_recv);
113   grpc_metadata_array_init(&request_metadata_recv);
114   grpc_call_details_init(&call_details);
115 
116   // Create server.
117   cq = grpc_completion_queue_create_for_next(nullptr);
118   server = grpc_server_create(nullptr, nullptr);
119   grpc_server_register_completion_queue(server, cq, nullptr);
120   grpc_server_credentials* server_creds =
121       grpc_insecure_server_credentials_create();
122   CHECK((got_port = grpc_server_add_http2_port(server, server_hostport.c_str(),
123                                                server_creds)) > 0);
124   grpc_server_credentials_release(server_creds);
125   if (port == 0) {
126     port = got_port;
127   } else {
128     CHECK_EQ(port, got_port);
129   }
130   grpc_server_start(server);
131   grpc_core::CqVerifier cqv(cq);
132 
133   // Create client.
134   std::string client_hostport;
135   if (client_host[0] == 'i') {
136     // for ipv4:/ipv6: addresses, concatenate the port to each of the parts
137     std::vector<absl::string_view> uri_parts =
138         absl::StrSplit(client_host, ',', absl::SkipEmpty());
139     std::vector<std::string> hosts_with_port;
140     hosts_with_port.reserve(uri_parts.size());
141     for (const absl::string_view& uri_part : uri_parts) {
142       hosts_with_port.push_back(absl::StrFormat("%s:%d", uri_part, port));
143     }
144     client_hostport = absl::StrJoin(hosts_with_port, ",");
145   } else {
146     client_hostport = grpc_core::JoinHostPort(client_host, port);
147   }
148   grpc_channel_credentials* creds = grpc_insecure_credentials_create();
149   client = grpc_channel_create(client_hostport.c_str(), creds, nullptr);
150   grpc_channel_credentials_release(creds);
151 
152   LOG(INFO) << "Testing with server=" << server_hostport.c_str()
153             << " client=" << client_hostport.c_str() << " (expecting "
154             << (expect_ok ? "success" : "failure") << ")";
155   log_resolved_addrs("server resolved addr", server_host);
156   log_resolved_addrs("client resolved addr", client_host);
157 
158   if (expect_ok) {
159     // Normal deadline, shouldn't be reached.
160     deadline = grpc_timeout_milliseconds_to_deadline(60000);
161   } else {
162     // Give up faster when failure is expected.
163     // BUG: Setting this to 1000 reveals a memory leak (b/18608927).
164     deadline = grpc_timeout_milliseconds_to_deadline(8000);
165   }
166 
167   // Send a trivial request.
168   grpc_slice host = grpc_slice_from_static_string("foo.test.google.fr");
169   c = grpc_channel_create_call(client, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
170                                grpc_slice_from_static_string("/foo"), &host,
171                                deadline, nullptr);
172   CHECK(c);
173 
174   memset(ops, 0, sizeof(ops));
175   op = ops;
176   op->op = GRPC_OP_SEND_INITIAL_METADATA;
177   op->data.send_initial_metadata.count = 0;
178   op->flags = expect_ok ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0;
179   op->reserved = nullptr;
180   op++;
181   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
182   op->flags = 0;
183   op->reserved = nullptr;
184   op++;
185   op->op = GRPC_OP_RECV_INITIAL_METADATA;
186   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
187   op->flags = 0;
188   op->reserved = nullptr;
189   op++;
190   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
191   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
192   op->data.recv_status_on_client.status = &status;
193   op->data.recv_status_on_client.status_details = &details;
194   op->flags = 0;
195   op->reserved = nullptr;
196   op++;
197   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops),
198                                 grpc_core::CqVerifier::tag(1), nullptr);
199   CHECK_EQ(error, GRPC_CALL_OK);
200 
201   if (expect_ok) {
202     // Check for a successful request.
203     error = grpc_server_request_call(server, &s, &call_details,
204                                      &request_metadata_recv, cq, cq,
205                                      grpc_core::CqVerifier::tag(101));
206     CHECK_EQ(error, GRPC_CALL_OK);
207     cqv.Expect(grpc_core::CqVerifier::tag(101), true);
208     cqv.Verify();
209 
210     memset(ops, 0, sizeof(ops));
211     op = ops;
212     op->op = GRPC_OP_SEND_INITIAL_METADATA;
213     op->data.send_initial_metadata.count = 0;
214     op->flags = 0;
215     op++;
216     op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
217     op->data.send_status_from_server.trailing_metadata_count = 0;
218     op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
219     grpc_slice status_details = grpc_slice_from_static_string("xyz");
220     op->data.send_status_from_server.status_details = &status_details;
221     op->flags = 0;
222     op++;
223     op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
224     op->data.recv_close_on_server.cancelled = &was_cancelled;
225     op->flags = 0;
226     op++;
227     error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops),
228                                   grpc_core::CqVerifier::tag(102), nullptr);
229     CHECK_EQ(error, GRPC_CALL_OK);
230 
231     cqv.Expect(grpc_core::CqVerifier::tag(102), true);
232     cqv.Expect(grpc_core::CqVerifier::tag(1), true);
233     cqv.Verify();
234 
235     peer = grpc_call_get_peer(c);
236     VLOG(2) << "got peer: '" << peer << "'";
237     gpr_free(peer);
238 
239     CHECK_EQ(status, GRPC_STATUS_UNIMPLEMENTED);
240     CHECK_EQ(grpc_slice_str_cmp(details, "xyz"), 0);
241     CHECK_EQ(grpc_slice_str_cmp(call_details.method, "/foo"), 0);
242     CHECK_EQ(grpc_slice_str_cmp(call_details.host, "foo.test.google.fr"), 0);
243     CHECK_EQ(was_cancelled, 0);
244 
245     grpc_call_unref(s);
246   } else {
247     // Check for a failed connection.
248     cqv.Expect(grpc_core::CqVerifier::tag(1), true);
249     cqv.Verify();
250 
251     LOG(INFO) << "status: " << status
252               << " (expected: " << GRPC_STATUS_UNAVAILABLE << ")";
253     CHECK_EQ(status, GRPC_STATUS_UNAVAILABLE);
254   }
255 
256   grpc_call_unref(c);
257 
258   // Destroy client.
259   grpc_channel_destroy(client);
260 
261   // Destroy server.
262   grpc_server_shutdown_and_notify(server, cq, grpc_core::CqVerifier::tag(1000));
263   grpc_event ev;
264   do {
265     ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5),
266                                     nullptr);
267   } while (ev.type != GRPC_OP_COMPLETE ||
268            ev.tag != grpc_core::CqVerifier::tag(1000));
269 
270   grpc_server_destroy(server);
271   grpc_completion_queue_shutdown(cq);
272   drain_cq(cq);
273   grpc_completion_queue_destroy(cq);
274 
275   grpc_metadata_array_destroy(&initial_metadata_recv);
276   grpc_metadata_array_destroy(&trailing_metadata_recv);
277   grpc_metadata_array_destroy(&request_metadata_recv);
278 
279   grpc_call_details_destroy(&call_details);
280   grpc_slice_unref(details);
281   if (picked_port) {
282     grpc_recycle_unused_port(port);
283   }
284 }
285 
external_dns_works(const char * host)286 int external_dns_works(const char* host) {
287   auto addresses_or =
288       grpc_core::GetDNSResolver()->LookupHostnameBlocking(host, "80");
289   if (!addresses_or.ok()) {
290     return 0;
291   }
292   int result = 1;
293   for (const auto& addr : *addresses_or) {
294     // Kokoro on Macservice uses Google DNS64 servers by default
295     // (https://en.wikipedia.org/wiki/Google_Public_DNS) and that breaks
296     // "dualstack_socket_test" due to loopback4.unittest.grpc.io resolving to
297     // [64:ff9b::7f00:1]. (Working as expected for DNS64, but it prevents the
298     // dualstack_socket_test from functioning correctly). See b/201064791.
299     if (grpc_sockaddr_to_uri(&addr).value() ==
300         "ipv6:%5B64:ff9b::7f00:1%5D:80") {
301       LOG(INFO) << "Detected DNS64 server response. Tests that depend on "
302                    "*.unittest.grpc.io. will be skipped as they won't work "
303                    "with DNS64.";
304       result = 0;
305       break;
306     }
307   }
308   return result;
309 }
310 
main(int argc,char ** argv)311 int main(int argc, char** argv) {
312   int do_ipv6 = 1;
313 
314   grpc::testing::TestEnvironment env(&argc, argv);
315   grpc_init();
316 
317   if (!grpc_ipv6_loopback_available()) {
318     LOG(INFO) << "Can't bind to ::1.  Skipping IPv6 tests.";
319     do_ipv6 = 0;
320   }
321 
322   // For coverage, test with and without dualstack sockets.
323   for (grpc_forbid_dualstack_sockets_for_testing = 0;
324        grpc_forbid_dualstack_sockets_for_testing <= 1;
325        grpc_forbid_dualstack_sockets_for_testing++) {
326     // :: and 0.0.0.0 are handled identically.
327     test_connect("::", "127.0.0.1", 0, 1);
328     test_connect("::", "::ffff:127.0.0.1", 0, 1);
329     test_connect("::", "ipv4:127.0.0.1", 0, 1);
330     test_connect("::", "ipv6:[::ffff:127.0.0.1]", 0, 1);
331     test_connect("::", "localhost", 0, 1);
332     test_connect("0.0.0.0", "127.0.0.1", 0, 1);
333     test_connect("0.0.0.0", "::ffff:127.0.0.1", 0, 1);
334     test_connect("0.0.0.0", "ipv4:127.0.0.1", 0, 1);
335     test_connect("0.0.0.0", "ipv4:127.0.0.1,127.0.0.2,127.0.0.3", 0, 1);
336     test_connect("0.0.0.0", "ipv6:[::ffff:127.0.0.1],[::ffff:127.0.0.2]", 0, 1);
337     test_connect("0.0.0.0", "localhost", 0, 1);
338     if (do_ipv6) {
339       test_connect("::", "::1", 0, 1);
340       test_connect("0.0.0.0", "::1", 0, 1);
341       test_connect("::", "ipv6:[::1]", 0, 1);
342       test_connect("0.0.0.0", "ipv6:[::1]", 0, 1);
343     }
344 
345     // These only work when the families agree.
346     test_connect("127.0.0.1", "127.0.0.1", 0, 1);
347     test_connect("127.0.0.1", "ipv4:127.0.0.1", 0, 1);
348     if (do_ipv6) {
349       test_connect("::1", "::1", 0, 1);
350       test_connect("::1", "127.0.0.1", 0, 0);
351       test_connect("127.0.0.1", "::1", 0, 0);
352       test_connect("::1", "ipv6:[::1]", 0, 1);
353       test_connect("::1", "ipv4:127.0.0.1", 0, 0);
354       test_connect("127.0.0.1", "ipv6:[::1]", 0, 0);
355     }
356 
357     if (!external_dns_works("loopback4.unittest.grpc.io") ||
358         !external_dns_works("loopback46.unittest.grpc.io")) {
359       LOG(INFO) << "Skipping tests that depend on *.unittest.grpc.io.";
360     } else {
361       test_connect("loopback46.unittest.grpc.io", "loopback4.unittest.grpc.io",
362                    0, 1);
363       test_connect("loopback4.unittest.grpc.io", "loopback46.unittest.grpc.io",
364                    0, 1);
365       if (do_ipv6) {
366         test_connect("loopback46.unittest.grpc.io",
367                      "loopback6.unittest.grpc.io", 0, 1);
368         test_connect("loopback6.unittest.grpc.io",
369                      "loopback46.unittest.grpc.io", 0, 1);
370         test_connect("loopback4.unittest.grpc.io", "loopback6.unittest.grpc.io",
371                      0, 0);
372         test_connect("loopback6.unittest.grpc.io", "loopback4.unittest.grpc.io",
373                      0, 0);
374       }
375     }
376   }
377 
378   grpc_shutdown();
379 
380   return 0;
381 }
382 
383 #else  // GRPC_POSIX_SOCKET_EV
384 
main(int argc,char ** argv)385 int main(int argc, char** argv) { return 1; }
386 
387 #endif  // GRPC_POSIX_SOCKET_EV
388