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/grpc.h>
20 #include <grpc/grpc_security.h>
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/log.h>
23 #include <grpc/support/string_util.h>
24
25 #include "src/core/lib/gpr/host_port.h"
26 #include "src/core/lib/iomgr/resolve_address.h"
27 #include "src/core/lib/security/credentials/fake/fake_credentials.h"
28 #include "test/core/util/port.h"
29 #include "test/core/util/test_config.h"
30
test_register_method_fail(void)31 void test_register_method_fail(void) {
32 grpc_server* server = grpc_server_create(nullptr, nullptr);
33 void* method;
34 void* method_old;
35 method = grpc_server_register_method(server, nullptr, nullptr,
36 GRPC_SRM_PAYLOAD_NONE, 0);
37 GPR_ASSERT(method == nullptr);
38 method_old =
39 grpc_server_register_method(server, "m", "h", GRPC_SRM_PAYLOAD_NONE, 0);
40 GPR_ASSERT(method_old != nullptr);
41 method = grpc_server_register_method(
42 server, "m", "h", GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER, 0);
43 GPR_ASSERT(method == nullptr);
44 method_old =
45 grpc_server_register_method(server, "m2", "h2", GRPC_SRM_PAYLOAD_NONE,
46 GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST);
47 GPR_ASSERT(method_old != nullptr);
48 method =
49 grpc_server_register_method(server, "m2", "h2", GRPC_SRM_PAYLOAD_NONE, 0);
50 GPR_ASSERT(method == nullptr);
51 method = grpc_server_register_method(
52 server, "m2", "h2", GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER,
53 GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST);
54 GPR_ASSERT(method == nullptr);
55 grpc_server_destroy(server);
56 }
57
test_request_call_on_no_server_cq(void)58 void test_request_call_on_no_server_cq(void) {
59 grpc_completion_queue* cc = grpc_completion_queue_create_for_next(nullptr);
60 grpc_server* server = grpc_server_create(nullptr, nullptr);
61 GPR_ASSERT(GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE ==
62 grpc_server_request_call(server, nullptr, nullptr, nullptr, cc, cc,
63 nullptr));
64 GPR_ASSERT(GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE ==
65 grpc_server_request_registered_call(server, nullptr, nullptr,
66 nullptr, nullptr, nullptr, cc,
67 cc, nullptr));
68 grpc_completion_queue_destroy(cc);
69 grpc_server_destroy(server);
70 }
71
72 // GRPC_ARG_ALLOW_REUSEPORT isn't supported for custom servers
73 #ifndef GRPC_UV
test_bind_server_twice(void)74 void test_bind_server_twice(void) {
75 grpc_arg a;
76 a.type = GRPC_ARG_INTEGER;
77 a.key = const_cast<char*>(GRPC_ARG_ALLOW_REUSEPORT);
78 a.value.integer = 0;
79 grpc_channel_args args = {1, &a};
80
81 char* addr;
82 grpc_server* server1 = grpc_server_create(&args, nullptr);
83 grpc_server* server2 = grpc_server_create(&args, nullptr);
84 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
85 int port = grpc_pick_unused_port_or_die();
86 gpr_asprintf(&addr, "[::]:%d", port);
87 grpc_server_register_completion_queue(server1, cq, nullptr);
88 grpc_server_register_completion_queue(server2, cq, nullptr);
89 GPR_ASSERT(0 == grpc_server_add_secure_http2_port(server2, addr, nullptr));
90 GPR_ASSERT(port == grpc_server_add_insecure_http2_port(server1, addr));
91 GPR_ASSERT(0 == grpc_server_add_insecure_http2_port(server2, addr));
92 grpc_server_credentials* fake_creds =
93 grpc_fake_transport_security_server_credentials_create();
94 GPR_ASSERT(0 == grpc_server_add_secure_http2_port(server2, addr, fake_creds));
95 grpc_server_credentials_release(fake_creds);
96 grpc_server_shutdown_and_notify(server1, cq, nullptr);
97 grpc_server_shutdown_and_notify(server2, cq, nullptr);
98 grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_MONOTONIC), nullptr);
99 grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_MONOTONIC), nullptr);
100 grpc_server_destroy(server1);
101 grpc_server_destroy(server2);
102 grpc_completion_queue_destroy(cq);
103 gpr_free(addr);
104 }
105 #endif
106
test_bind_server_to_addr(const char * host,bool secure)107 void test_bind_server_to_addr(const char* host, bool secure) {
108 int port = grpc_pick_unused_port_or_die();
109 char* addr;
110 gpr_join_host_port(&addr, host, port);
111 gpr_log(GPR_INFO, "Test bind to %s", addr);
112
113 grpc_server* server = grpc_server_create(nullptr, nullptr);
114 if (secure) {
115 grpc_server_credentials* fake_creds =
116 grpc_fake_transport_security_server_credentials_create();
117 GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, fake_creds));
118 grpc_server_credentials_release(fake_creds);
119 } else {
120 GPR_ASSERT(grpc_server_add_insecure_http2_port(server, addr));
121 }
122 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
123 grpc_server_register_completion_queue(server, cq, nullptr);
124 grpc_server_start(server);
125 grpc_server_shutdown_and_notify(server, cq, nullptr);
126 grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_MONOTONIC), nullptr);
127 grpc_server_destroy(server);
128 grpc_completion_queue_destroy(cq);
129 gpr_free(addr);
130 }
131
external_dns_works(const char * host)132 static int external_dns_works(const char* host) {
133 grpc_resolved_addresses* res = nullptr;
134 grpc_error* error = grpc_blocking_resolve_address(host, "80", &res);
135 GRPC_ERROR_UNREF(error);
136 if (res != nullptr) {
137 grpc_resolved_addresses_destroy(res);
138 return 1;
139 }
140 return 0;
141 }
142
test_bind_server_to_addrs(const char ** addrs,size_t n)143 static void test_bind_server_to_addrs(const char** addrs, size_t n) {
144 for (size_t i = 0; i < n; i++) {
145 test_bind_server_to_addr(addrs[i], false);
146 test_bind_server_to_addr(addrs[i], true);
147 }
148 }
149
main(int argc,char ** argv)150 int main(int argc, char** argv) {
151 grpc_test_init(argc, argv);
152 grpc_init();
153 test_register_method_fail();
154 test_request_call_on_no_server_cq();
155 #ifndef GRPC_UV
156 test_bind_server_twice();
157 #endif
158
159 static const char* addrs[] = {
160 "::1", "127.0.0.1", "::ffff:127.0.0.1", "localhost", "0.0.0.0", "::",
161 };
162 test_bind_server_to_addrs(addrs, GPR_ARRAY_SIZE(addrs));
163
164 if (external_dns_works("loopback46.unittest.grpc.io")) {
165 static const char* dns_addrs[] = {
166 "loopback46.unittest.grpc.io",
167 "loopback4.unittest.grpc.io",
168 };
169 test_bind_server_to_addrs(dns_addrs, GPR_ARRAY_SIZE(dns_addrs));
170 }
171
172 grpc_shutdown();
173 return 0;
174 }
175