host_port_test.cc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "src/core/lib/gprpp/host_port.h"
  19. #include <string.h>
  20. #include <grpc/support/alloc.h>
  21. #include <grpc/support/log.h>
  22. #include "test/core/util/test_config.h"
  23. static void join_host_port_expect(const char* host, int port,
  24. const char* expected) {
  25. std::string actual = grpc_core::JoinHostPort(host, port);
  26. GPR_ASSERT(actual == expected);
  27. }
  28. static void test_join_host_port(void) {
  29. join_host_port_expect("foo", 101, "foo:101");
  30. join_host_port_expect("", 102, ":102");
  31. join_host_port_expect("1::2", 103, "[1::2]:103");
  32. join_host_port_expect("[::1]", 104, "[::1]:104");
  33. }
  34. /* Garbage in, garbage out. */
  35. static void test_join_host_port_garbage(void) {
  36. join_host_port_expect("[foo]", 105, "[foo]:105");
  37. join_host_port_expect("[::", 106, "[:::106");
  38. join_host_port_expect("::]", 107, "[::]]:107");
  39. }
  40. static void split_host_port_expect(const char* name, const char* host,
  41. const char* port, bool ret) {
  42. std::string actual_host;
  43. std::string actual_port;
  44. const bool actual_ret =
  45. grpc_core::SplitHostPort(name, &actual_host, &actual_port);
  46. GPR_ASSERT(actual_ret == ret);
  47. GPR_ASSERT(actual_host == (host == nullptr ? "" : host));
  48. GPR_ASSERT(actual_port == (port == nullptr ? "" : port));
  49. }
  50. static void test_split_host_port() {
  51. split_host_port_expect("", "", nullptr, true);
  52. split_host_port_expect("[a:b]", "a:b", nullptr, true);
  53. split_host_port_expect("1.2.3.4", "1.2.3.4", nullptr, true);
  54. split_host_port_expect("0.0.0.0:", "0.0.0.0", "", true);
  55. split_host_port_expect("a:b:c::", "a:b:c::", nullptr, true);
  56. split_host_port_expect("[a:b:c::]:", "a:b:c::", "", true);
  57. split_host_port_expect("[a:b]:30", "a:b", "30", true);
  58. split_host_port_expect("1.2.3.4:30", "1.2.3.4", "30", true);
  59. split_host_port_expect(":30", "", "30", true);
  60. }
  61. static void test_split_host_port_invalid() {
  62. split_host_port_expect("[a:b", nullptr, nullptr, false);
  63. split_host_port_expect("[a:b]30", nullptr, nullptr, false);
  64. }
  65. int main(int argc, char** argv) {
  66. grpc::testing::TestEnvironment env(argc, argv);
  67. test_join_host_port();
  68. test_join_host_port_garbage();
  69. test_split_host_port();
  70. test_split_host_port_invalid();
  71. return 0;
  72. }