server_chttp2_test.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include <gtest/gtest.h>
  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. #include "src/core/lib/gprpp/host_port.h"
  25. #include "src/core/lib/security/credentials/credentials.h"
  26. #include "src/core/lib/security/credentials/fake/fake_credentials.h"
  27. #include "src/core/tsi/fake_transport_security.h"
  28. #include "test/core/util/port.h"
  29. #include "test/core/util/test_config.h"
  30. TEST(ServerChttp2, UnparseableTarget) {
  31. grpc_channel_args args = {0, nullptr};
  32. grpc_server* server = grpc_server_create(&args, nullptr);
  33. grpc_server_credentials* server_creds =
  34. grpc_insecure_server_credentials_create();
  35. int port = grpc_server_add_http2_port(server, "[", server_creds);
  36. grpc_server_credentials_release(server_creds);
  37. EXPECT_EQ(port, 0);
  38. grpc_server_destroy(server);
  39. }
  40. TEST(ServerChttp2, AddSamePortTwice) {
  41. grpc_arg a = grpc_channel_arg_integer_create(
  42. const_cast<char*>(GRPC_ARG_ALLOW_REUSEPORT), 0);
  43. grpc_channel_args args = {1, &a};
  44. int port = grpc_pick_unused_port_or_die();
  45. grpc_completion_queue* cq = grpc_completion_queue_create_for_pluck(nullptr);
  46. grpc_server* server = grpc_server_create(&args, nullptr);
  47. grpc_server_credentials* fake_creds =
  48. grpc_fake_transport_security_server_credentials_create();
  49. std::string addr = grpc_core::JoinHostPort("localhost", port);
  50. EXPECT_EQ(grpc_server_add_http2_port(server, addr.c_str(), fake_creds), port);
  51. EXPECT_EQ(grpc_server_add_http2_port(server, addr.c_str(), fake_creds), 0);
  52. grpc_server_credentials_release(fake_creds);
  53. grpc_server_shutdown_and_notify(server, cq, nullptr);
  54. grpc_completion_queue_pluck(cq, nullptr, gpr_inf_future(GPR_CLOCK_REALTIME),
  55. nullptr);
  56. grpc_server_destroy(server);
  57. grpc_completion_queue_destroy(cq);
  58. }
  59. int main(int argc, char** argv) {
  60. grpc::testing::TestEnvironment env(argc, argv);
  61. ::testing::InitGoogleTest(&argc, argv);
  62. grpc_init();
  63. int ret = RUN_ALL_TESTS();
  64. grpc_shutdown();
  65. return ret;
  66. }