aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/client_channel/parse_address_test.cc
diff options
context:
space:
mode:
authorGravatar Yash Tibrewal <yashkt@google.com>2017-11-09 17:46:29 -0800
committerGravatar Yash Tibrewal <yashkt@google.com>2017-11-09 17:46:29 -0800
commit4e9265c828f0b559b5fdba04913fed46bf771399 (patch)
tree4a379fc2bdc037753cf8d81f8b86327e4bc50a42 /test/core/client_channel/parse_address_test.cc
parent0ee7574732a06e8cace4e099a678f4bd5dbff679 (diff)
parentd9da7387b8057f3bd99a417a5ee905377bce9296 (diff)
Merge with master
Diffstat (limited to 'test/core/client_channel/parse_address_test.cc')
-rw-r--r--test/core/client_channel/parse_address_test.cc101
1 files changed, 101 insertions, 0 deletions
diff --git a/test/core/client_channel/parse_address_test.cc b/test/core/client_channel/parse_address_test.cc
new file mode 100644
index 0000000000..17725ba5ff
--- /dev/null
+++ b/test/core/client_channel/parse_address_test.cc
@@ -0,0 +1,101 @@
+/*
+ *
+ * Copyright 2017 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "src/core/ext/filters/client_channel/parse_address.h"
+#include "src/core/lib/iomgr/sockaddr.h"
+
+#include <string.h>
+#ifdef GRPC_HAVE_UNIX_SOCKET
+#include <sys/un.h>
+#endif
+
+#include <grpc/support/log.h>
+
+#include "src/core/lib/iomgr/exec_ctx.h"
+#include "src/core/lib/iomgr/socket_utils.h"
+#include "test/core/util/test_config.h"
+
+#ifdef GRPC_HAVE_UNIX_SOCKET
+
+static void test_grpc_parse_unix(const char* uri_text, const char* pathname) {
+ ExecCtx _local_exec_ctx;
+ grpc_uri* uri = grpc_uri_parse(uri_text, 0);
+ grpc_resolved_address addr;
+
+ GPR_ASSERT(1 == grpc_parse_unix(uri, &addr));
+ struct sockaddr_un* addr_un = (struct sockaddr_un*)addr.addr;
+ GPR_ASSERT(AF_UNIX == addr_un->sun_family);
+ GPR_ASSERT(0 == strcmp(addr_un->sun_path, pathname));
+
+ grpc_uri_destroy(uri);
+ grpc_exec_ctx_finish();
+}
+
+#else /* GRPC_HAVE_UNIX_SOCKET */
+
+static void test_grpc_parse_unix(const char* uri_text, const char* pathname) {}
+
+#endif /* GRPC_HAVE_UNIX_SOCKET */
+
+static void test_grpc_parse_ipv4(const char* uri_text, const char* host,
+ unsigned short port) {
+ ExecCtx _local_exec_ctx;
+ grpc_uri* uri = grpc_uri_parse(uri_text, 0);
+ grpc_resolved_address addr;
+ char ntop_buf[INET_ADDRSTRLEN];
+
+ GPR_ASSERT(1 == grpc_parse_ipv4(uri, &addr));
+ struct sockaddr_in* addr_in = (struct sockaddr_in*)addr.addr;
+ GPR_ASSERT(AF_INET == addr_in->sin_family);
+ GPR_ASSERT(NULL != grpc_inet_ntop(AF_INET, &addr_in->sin_addr, ntop_buf,
+ sizeof(ntop_buf)));
+ GPR_ASSERT(0 == strcmp(ntop_buf, host));
+ GPR_ASSERT(ntohs(addr_in->sin_port) == port);
+
+ grpc_uri_destroy(uri);
+ grpc_exec_ctx_finish();
+}
+
+static void test_grpc_parse_ipv6(const char* uri_text, const char* host,
+ unsigned short port, uint32_t scope_id) {
+ ExecCtx _local_exec_ctx;
+ grpc_uri* uri = grpc_uri_parse(uri_text, 0);
+ grpc_resolved_address addr;
+ char ntop_buf[INET6_ADDRSTRLEN];
+
+ GPR_ASSERT(1 == grpc_parse_ipv6(uri, &addr));
+ struct sockaddr_in6* addr_in6 = (struct sockaddr_in6*)addr.addr;
+ GPR_ASSERT(AF_INET6 == addr_in6->sin6_family);
+ GPR_ASSERT(NULL != grpc_inet_ntop(AF_INET6, &addr_in6->sin6_addr, ntop_buf,
+ sizeof(ntop_buf)));
+ GPR_ASSERT(0 == strcmp(ntop_buf, host));
+ GPR_ASSERT(ntohs(addr_in6->sin6_port) == port);
+ GPR_ASSERT(addr_in6->sin6_scope_id == scope_id);
+
+ grpc_uri_destroy(uri);
+ grpc_exec_ctx_finish();
+}
+
+int main(int argc, char** argv) {
+ grpc_test_init(argc, argv);
+
+ test_grpc_parse_unix("unix:/path/name", "/path/name");
+ test_grpc_parse_ipv4("ipv4:192.0.2.1:12345", "192.0.2.1", 12345);
+ test_grpc_parse_ipv6("ipv6:[2001:db8::1]:12345", "2001:db8::1", 12345, 0);
+ test_grpc_parse_ipv6("ipv6:[2001:db8::1%252]:12345", "2001:db8::1", 12345, 2);
+}