• 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 <gtest/gtest.h>
20 
21 #include <grpc/grpc.h>
22 #include <grpc/grpc_security.h>
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/time.h>
26 
27 #include "src/core/lib/gprpp/host_port.h"
28 #include "src/core/lib/security/credentials/credentials.h"
29 #include "src/core/lib/security/credentials/fake/fake_credentials.h"
30 #include "src/core/tsi/fake_transport_security.h"
31 #include "test/core/util/port.h"
32 #include "test/core/util/test_config.h"
33 
TEST(ServerChttp2,UnparseableTarget)34 TEST(ServerChttp2, UnparseableTarget) {
35   grpc_channel_args args = {0, nullptr};
36   grpc_server* server = grpc_server_create(&args, nullptr);
37   int port = grpc_server_add_insecure_http2_port(server, "[");
38   EXPECT_EQ(port, 0);
39   grpc_server_destroy(server);
40 }
41 
42 // GRPC_ARG_ALLOW_REUSEPORT isn't supported for custom servers
43 #ifndef GRPC_UV
TEST(ServerChttp2,AddSamePortTwice)44 TEST(ServerChttp2, AddSamePortTwice) {
45   grpc_arg a = grpc_channel_arg_integer_create(
46       const_cast<char*>(GRPC_ARG_ALLOW_REUSEPORT), 0);
47   grpc_channel_args args = {1, &a};
48 
49   int port = grpc_pick_unused_port_or_die();
50   grpc_completion_queue* cq = grpc_completion_queue_create_for_pluck(nullptr);
51   grpc_server* server = grpc_server_create(&args, nullptr);
52   grpc_server_credentials* fake_creds =
53       grpc_fake_transport_security_server_credentials_create();
54   std::string addr = grpc_core::JoinHostPort("localhost", port);
55   EXPECT_EQ(grpc_server_add_secure_http2_port(server, addr.c_str(), fake_creds),
56             port);
57   EXPECT_EQ(grpc_server_add_secure_http2_port(server, addr.c_str(), fake_creds),
58             0);
59 
60   grpc_server_credentials_release(fake_creds);
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::testing::TestEnvironment env(argc, argv);
71   ::testing::InitGoogleTest(&argc, argv);
72   grpc_init();
73   int ret = RUN_ALL_TESTS();
74   grpc_shutdown();
75   return ret;
76 }
77