示例#1
0
static void test_sockaddr_is_v4mapped(void) {
  struct sockaddr_in input4;
  struct sockaddr_in6 input6;
  struct sockaddr_in output4;
  struct sockaddr_in expect4;

  gpr_log(GPR_INFO, "%s", "test_sockaddr_is_v4mapped");

  /* v4mapped input should succeed. */
  input6 = make_addr6(kMapped, sizeof(kMapped));
  GPR_ASSERT(grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input6, NULL));
  GPR_ASSERT(
      grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input6, &output4));
  expect4 = make_addr4(kIPv4, sizeof(kIPv4));
  GPR_ASSERT(memcmp(&expect4, &output4, sizeof(expect4)) == 0);

  /* Non-v4mapped input should fail. */
  input6 = make_addr6(kNotQuiteMapped, sizeof(kNotQuiteMapped));
  GPR_ASSERT(
      !grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input6, NULL));
  GPR_ASSERT(
      !grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input6, &output4));
  /* Output is unchanged. */
  GPR_ASSERT(memcmp(&expect4, &output4, sizeof(expect4)) == 0);

  /* Plain IPv4 input should also fail. */
  input4 = make_addr4(kIPv4, sizeof(kIPv4));
  GPR_ASSERT(
      !grpc_sockaddr_is_v4mapped((const struct sockaddr *)&input4, NULL));
}
示例#2
0
static void test_sockaddr_to_v4mapped(void) {
  struct sockaddr_in input4;
  struct sockaddr_in6 input6;
  struct sockaddr_in6 output6;
  struct sockaddr_in6 expect6;

  gpr_log(GPR_INFO, "%s", "test_sockaddr_to_v4mapped");

  /* IPv4 input should succeed. */
  input4 = make_addr4(kIPv4, sizeof(kIPv4));
  GPR_ASSERT(
      grpc_sockaddr_to_v4mapped((const struct sockaddr *)&input4, &output6));
  expect6 = make_addr6(kMapped, sizeof(kMapped));
  GPR_ASSERT(memcmp(&expect6, &output6, sizeof(output6)) == 0);

  /* IPv6 input should fail. */
  input6 = make_addr6(kIPv6, sizeof(kIPv6));
  GPR_ASSERT(
      !grpc_sockaddr_to_v4mapped((const struct sockaddr *)&input6, &output6));
  /* Output is unchanged. */
  GPR_ASSERT(memcmp(&expect6, &output6, sizeof(output6)) == 0);

  /* Already-v4mapped input should also fail. */
  input6 = make_addr6(kMapped, sizeof(kMapped));
  GPR_ASSERT(
      !grpc_sockaddr_to_v4mapped((const struct sockaddr *)&input6, &output6));
}
示例#3
0
static void test_sockaddr_to_string(void) {
  struct sockaddr_in input4;
  struct sockaddr_in6 input6;
  struct sockaddr dummy;

  gpr_log(GPR_INFO, "%s", "test_sockaddr_to_string");

  errno = 0x7EADBEEF;

  input4 = make_addr4(kIPv4, sizeof(kIPv4));
  expect_sockaddr_str("192.0.2.1:12345", &input4, 0);
  expect_sockaddr_str("192.0.2.1:12345", &input4, 1);

  input6 = make_addr6(kIPv6, sizeof(kIPv6));
  expect_sockaddr_str("[2001:db8::1]:12345", &input6, 0);
  expect_sockaddr_str("[2001:db8::1]:12345", &input6, 1);

  input6 = make_addr6(kMapped, sizeof(kMapped));
  expect_sockaddr_str("[::ffff:192.0.2.1]:12345", &input6, 0);
  expect_sockaddr_str("192.0.2.1:12345", &input6, 1);

  input6 = make_addr6(kNotQuiteMapped, sizeof(kNotQuiteMapped));
  expect_sockaddr_str("[::fffe:c000:263]:12345", &input6, 0);
  expect_sockaddr_str("[::fffe:c000:263]:12345", &input6, 1);

  memset(&dummy, 0, sizeof(dummy));
  dummy.sa_family = 123;
  expect_sockaddr_str("(sockaddr family=123)", &dummy, 0);
  expect_sockaddr_str("(sockaddr family=123)", &dummy, 1);

  GPR_ASSERT(errno == 0x7EADBEEF);
}