parse_address_test.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. *
  3. * Copyright 2017 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/address_utils/parse_address.h"
  19. #include <string.h>
  20. #ifdef GRPC_HAVE_UNIX_SOCKET
  21. #include <sys/un.h>
  22. #endif
  23. #include <grpc/grpc.h>
  24. #include <grpc/support/log.h>
  25. #include "src/core/lib/address_utils/sockaddr_utils.h"
  26. #include "src/core/lib/iomgr/exec_ctx.h"
  27. #include "src/core/lib/iomgr/sockaddr.h"
  28. #include "src/core/lib/iomgr/socket_utils.h"
  29. #include "test/core/util/test_config.h"
  30. #ifdef GRPC_HAVE_UNIX_SOCKET
  31. static void test_grpc_parse_unix(const char* uri_text, const char* pathname) {
  32. grpc_core::ExecCtx exec_ctx;
  33. absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text);
  34. if (!uri.ok()) {
  35. gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
  36. GPR_ASSERT(uri.ok());
  37. }
  38. grpc_resolved_address addr;
  39. GPR_ASSERT(1 == grpc_parse_uri(*uri, &addr));
  40. struct sockaddr_un* addr_un =
  41. reinterpret_cast<struct sockaddr_un*>(addr.addr);
  42. GPR_ASSERT(AF_UNIX == addr_un->sun_family);
  43. GPR_ASSERT(0 == strcmp(addr_un->sun_path, pathname));
  44. }
  45. static void test_grpc_parse_unix_abstract(const char* uri_text,
  46. const char* pathname) {
  47. grpc_core::ExecCtx exec_ctx;
  48. absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text);
  49. if (!uri.ok()) {
  50. gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
  51. GPR_ASSERT(uri.ok());
  52. }
  53. grpc_resolved_address addr;
  54. GPR_ASSERT(1 == grpc_parse_uri(*uri, &addr));
  55. struct sockaddr_un* addr_un =
  56. reinterpret_cast<struct sockaddr_un*>(addr.addr);
  57. GPR_ASSERT(AF_UNIX == addr_un->sun_family);
  58. GPR_ASSERT('\0' == addr_un->sun_path[0]);
  59. GPR_ASSERT(0 == strncmp(addr_un->sun_path + 1, pathname, strlen(pathname)));
  60. }
  61. #else /* GRPC_HAVE_UNIX_SOCKET */
  62. static void test_grpc_parse_unix(const char* uri_text, const char* pathname) {}
  63. static void test_grpc_parse_unix_abstract(const char* uri_text,
  64. const char* pathname) {}
  65. #endif /* GRPC_HAVE_UNIX_SOCKET */
  66. static void test_grpc_parse_ipv4(const char* uri_text, const char* host,
  67. unsigned short port) {
  68. grpc_core::ExecCtx exec_ctx;
  69. absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text);
  70. if (!uri.ok()) {
  71. gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
  72. GPR_ASSERT(uri.ok());
  73. }
  74. grpc_resolved_address addr;
  75. char ntop_buf[GRPC_INET_ADDRSTRLEN];
  76. GPR_ASSERT(1 == grpc_parse_ipv4(*uri, &addr));
  77. grpc_sockaddr_in* addr_in = reinterpret_cast<grpc_sockaddr_in*>(addr.addr);
  78. GPR_ASSERT(GRPC_AF_INET == addr_in->sin_family);
  79. GPR_ASSERT(nullptr != grpc_inet_ntop(GRPC_AF_INET, &addr_in->sin_addr,
  80. ntop_buf, sizeof(ntop_buf)));
  81. GPR_ASSERT(0 == strcmp(ntop_buf, host));
  82. GPR_ASSERT(grpc_ntohs(addr_in->sin_port) == port);
  83. }
  84. static void test_grpc_parse_ipv6(const char* uri_text, const char* host,
  85. unsigned short port, uint32_t scope_id) {
  86. grpc_core::ExecCtx exec_ctx;
  87. absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text);
  88. if (!uri.ok()) {
  89. gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
  90. GPR_ASSERT(uri.ok());
  91. }
  92. grpc_resolved_address addr;
  93. char ntop_buf[GRPC_INET6_ADDRSTRLEN];
  94. GPR_ASSERT(1 == grpc_parse_ipv6(*uri, &addr));
  95. grpc_sockaddr_in6* addr_in6 = reinterpret_cast<grpc_sockaddr_in6*>(addr.addr);
  96. GPR_ASSERT(GRPC_AF_INET6 == addr_in6->sin6_family);
  97. GPR_ASSERT(nullptr != grpc_inet_ntop(GRPC_AF_INET6, &addr_in6->sin6_addr,
  98. ntop_buf, sizeof(ntop_buf)));
  99. GPR_ASSERT(0 == strcmp(ntop_buf, host));
  100. GPR_ASSERT(grpc_ntohs(addr_in6->sin6_port) == port);
  101. GPR_ASSERT(addr_in6->sin6_scope_id == scope_id);
  102. }
  103. /* Test parsing invalid ipv6 addresses (valid uri_text but invalid ipv6 addr) */
  104. static void test_grpc_parse_ipv6_invalid(const char* uri_text) {
  105. grpc_core::ExecCtx exec_ctx;
  106. absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text);
  107. if (!uri.ok()) {
  108. gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
  109. GPR_ASSERT(uri.ok());
  110. }
  111. grpc_resolved_address addr;
  112. GPR_ASSERT(!grpc_parse_ipv6(*uri, &addr));
  113. }
  114. int main(int argc, char** argv) {
  115. grpc::testing::TestEnvironment env(argc, argv);
  116. grpc_init();
  117. test_grpc_parse_unix("unix:/path/name", "/path/name");
  118. test_grpc_parse_unix_abstract("unix-abstract:foobar", "foobar");
  119. test_grpc_parse_ipv4("ipv4:192.0.2.1:12345", "192.0.2.1", 12345);
  120. test_grpc_parse_ipv6("ipv6:[2001:db8::1]:12345", "2001:db8::1", 12345, 0);
  121. test_grpc_parse_ipv6("ipv6:[2001:db8::1%252]:12345", "2001:db8::1", 12345, 2);
  122. /* Address length greater than GRPC_INET6_ADDRSTRLEN */
  123. test_grpc_parse_ipv6_invalid(
  124. "ipv6:WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW45%"
  125. "25v6:45%25x$1*");
  126. grpc_shutdown();
  127. }