xref: /aosp_15_r20/external/grpc-grpc/test/core/surface/server_chttp2_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <string>
20 
21 #include "gtest/gtest.h"
22 
23 #include <grpc/grpc.h>
24 #include <grpc/grpc_security.h>
25 #include <grpc/impl/channel_arg_names.h>
26 #include <grpc/support/time.h>
27 
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/gprpp/host_port.h"
30 #include "src/core/lib/security/credentials/fake/fake_credentials.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   grpc_server_credentials* server_creds =
38       grpc_insecure_server_credentials_create();
39   int port = grpc_server_add_http2_port(server, "[", server_creds);
40   grpc_server_credentials_release(server_creds);
41   EXPECT_EQ(port, 0);
42   grpc_server_destroy(server);
43 }
44 
TEST(ServerChttp2,AddSamePortTwice)45 TEST(ServerChttp2, AddSamePortTwice) {
46   grpc_arg a = grpc_channel_arg_integer_create(
47       const_cast<char*>(GRPC_ARG_ALLOW_REUSEPORT), 0);
48   grpc_channel_args args = {1, &a};
49 
50   int port = grpc_pick_unused_port_or_die();
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   std::string addr = grpc_core::JoinHostPort("localhost", port);
56   EXPECT_EQ(grpc_server_add_http2_port(server, addr.c_str(), fake_creds), port);
57   EXPECT_EQ(grpc_server_add_http2_port(server, addr.c_str(), fake_creds), 0);
58 
59   grpc_server_credentials_release(fake_creds);
60   grpc_server_shutdown_and_notify(server, cq, nullptr);
61   grpc_completion_queue_pluck(cq, nullptr, gpr_inf_future(GPR_CLOCK_REALTIME),
62                               nullptr);
63   grpc_server_destroy(server);
64   grpc_completion_queue_destroy(cq);
65 }
66 
main(int argc,char ** argv)67 int main(int argc, char** argv) {
68   grpc::testing::TestEnvironment env(&argc, argv);
69   ::testing::InitGoogleTest(&argc, argv);
70   grpc_init();
71   int ret = RUN_ALL_TESTS();
72   grpc_shutdown();
73   return ret;
74 }
75