• 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/grpc.h>
20 #include <grpc/grpc_security.h>
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/log.h>
23 #include <grpc/support/time.h>
24 
25 #include "src/core/lib/gpr/host_port.h"
26 #include "src/core/lib/security/credentials/credentials.h"
27 #include "src/core/lib/security/credentials/fake/fake_credentials.h"
28 #include "src/core/tsi/fake_transport_security.h"
29 #include "test/core/util/port.h"
30 #include "test/core/util/test_config.h"
31 
test_unparsable_target(void)32 void test_unparsable_target(void) {
33   grpc_channel_args args = {0, nullptr};
34   grpc_server* server = grpc_server_create(&args, nullptr);
35   int port = grpc_server_add_insecure_http2_port(server, "[");
36   GPR_ASSERT(port == 0);
37   grpc_server_destroy(server);
38 }
39 
40 // GRPC_ARG_ALLOW_REUSEPORT isn't supported for custom servers
41 #ifndef GRPC_UV
test_add_same_port_twice()42 void test_add_same_port_twice() {
43   grpc_arg a;
44   a.type = GRPC_ARG_INTEGER;
45   a.key = const_cast<char*>(GRPC_ARG_ALLOW_REUSEPORT);
46   a.value.integer = 0;
47   grpc_channel_args args = {1, &a};
48 
49   int port = grpc_pick_unused_port_or_die();
50   char* addr = nullptr;
51   grpc_completion_queue* cq = grpc_completion_queue_create_for_pluck(nullptr);
52   grpc_server* server = grpc_server_create(&args, nullptr);
53   grpc_server_credentials* fake_creds =
54       grpc_fake_transport_security_server_credentials_create();
55   gpr_join_host_port(&addr, "localhost", port);
56   GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, fake_creds));
57   GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, fake_creds) == 0);
58 
59   grpc_server_credentials_release(fake_creds);
60   gpr_free(addr);
61   grpc_server_shutdown_and_notify(server, cq, nullptr);
62   grpc_completion_queue_pluck(cq, nullptr, gpr_inf_future(GPR_CLOCK_REALTIME),
63                               nullptr);
64   grpc_server_destroy(server);
65   grpc_completion_queue_destroy(cq);
66 }
67 #endif
68 
main(int argc,char ** argv)69 int main(int argc, char** argv) {
70   grpc_test_init(argc, argv);
71   grpc_init();
72   test_unparsable_target();
73 #ifndef GRPC_UV
74   test_add_same_port_twice();
75 #endif
76   grpc_shutdown();
77   return 0;
78 }
79