parse_address_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. *
  3. * Copyright 2017, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "src/core/ext/client_channel/parse_address.h"
  34. #include "src/core/lib/iomgr/sockaddr.h"
  35. #include <string.h>
  36. #ifdef GRPC_HAVE_UNIX_SOCKET
  37. #include <sys/un.h>
  38. #endif
  39. #include <grpc/support/log.h>
  40. #include "src/core/lib/iomgr/socket_utils.h"
  41. #include "test/core/util/test_config.h"
  42. #ifdef GRPC_HAVE_UNIX_SOCKET
  43. static void test_parse_unix(const char *uri_text, const char *pathname) {
  44. grpc_uri *uri = grpc_uri_parse(uri_text, 0);
  45. grpc_resolved_address addr;
  46. GPR_ASSERT(1 == parse_unix(uri, &addr));
  47. struct sockaddr_un *addr_un = (struct sockaddr_un *)addr.addr;
  48. GPR_ASSERT(AF_UNIX == addr_un->sun_family);
  49. GPR_ASSERT(0 == strcmp(addr_un->sun_path, pathname));
  50. grpc_uri_destroy(uri);
  51. }
  52. #else /* GRPC_HAVE_UNIX_SOCKET */
  53. static void test_parse_unix(const char *uri_text, const char *pathname) {}
  54. #endif /* GRPC_HAVE_UNIX_SOCKET */
  55. static void test_parse_ipv4(const char *uri_text, const char *host,
  56. unsigned short port) {
  57. grpc_uri *uri = grpc_uri_parse(uri_text, 0);
  58. grpc_resolved_address addr;
  59. char ntop_buf[INET_ADDRSTRLEN];
  60. GPR_ASSERT(1 == parse_ipv4(uri, &addr));
  61. struct sockaddr_in *addr_in = (struct sockaddr_in *)addr.addr;
  62. GPR_ASSERT(AF_INET == addr_in->sin_family);
  63. GPR_ASSERT(NULL != grpc_inet_ntop(AF_INET, &addr_in->sin_addr, ntop_buf,
  64. sizeof(ntop_buf)));
  65. GPR_ASSERT(0 == strcmp(ntop_buf, host));
  66. GPR_ASSERT(ntohs(addr_in->sin_port) == port);
  67. grpc_uri_destroy(uri);
  68. }
  69. static void test_parse_ipv6(const char *uri_text, const char *host,
  70. unsigned short port, u_int32_t scope_id) {
  71. grpc_uri *uri = grpc_uri_parse(uri_text, 0);
  72. grpc_resolved_address addr;
  73. char ntop_buf[INET6_ADDRSTRLEN];
  74. GPR_ASSERT(1 == parse_ipv6(uri, &addr));
  75. struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr.addr;
  76. GPR_ASSERT(AF_INET6 == addr_in6->sin6_family);
  77. GPR_ASSERT(NULL != grpc_inet_ntop(AF_INET6, &addr_in6->sin6_addr, ntop_buf,
  78. sizeof(ntop_buf)));
  79. GPR_ASSERT(0 == strcmp(ntop_buf, host));
  80. GPR_ASSERT(ntohs(addr_in6->sin6_port) == port);
  81. GPR_ASSERT(addr_in6->sin6_scope_id == scope_id);
  82. grpc_uri_destroy(uri);
  83. }
  84. int main(int argc, char **argv) {
  85. grpc_test_init(argc, argv);
  86. test_parse_unix("unix:/path/name", "/path/name");
  87. test_parse_ipv4("ipv4:192.0.2.1:12345", "192.0.2.1", 12345);
  88. test_parse_ipv6("ipv6:[2001:db8::1]:12345", "2001:db8::1", 12345, 0);
  89. test_parse_ipv6("ipv6:[2001:db8::1%252]:12345", "2001:db8::1", 12345, 2);
  90. }