[network/tests] simplify ioctl_test

...by expressing it as a simple gtest case.

Change-Id: Ie38a7b5097ac0e0d7483d7df89d2819ebc625eee
diff --git a/garnet/packages/tests/BUILD.gn b/garnet/packages/tests/BUILD.gn
index 1073c42..1ea5360 100644
--- a/garnet/packages/tests/BUILD.gn
+++ b/garnet/packages/tests/BUILD.gn
@@ -501,7 +501,6 @@
     "//src/connectivity/network/netstack/util:netstack_util_test($host_toolchain)",
     "//src/connectivity/network/tests:netstack_c_api_tests",
     "//src/connectivity/network/tests/test_filter_client",
-    "//src/connectivity/network/tests/test_ioctl_client",
     "//src/connectivity/network/tests/test_no_network_client",
   ]
 }
diff --git a/src/connectivity/network/tests/BUILD.gn b/src/connectivity/network/tests/BUILD.gn
index 24ecf1c..9c58606 100644
--- a/src/connectivity/network/tests/BUILD.gn
+++ b/src/connectivity/network/tests/BUILD.gn
@@ -12,20 +12,16 @@
 
   sources = [
     "netstack_filter_test.cc",
-    "netstack_ioctl_test.cc",
     "netstack_no_network_test.cc",
   ]
 
   deps = [
     "//garnet/lib/inet",
-    "//garnet/public/lib/netstack/c",
     "//sdk/fidl/fuchsia.net.filter",
     "//sdk/fidl/fuchsia.netstack",
     "//sdk/lib/sys/cpp/testing:integration",
     "//src/connectivity/network/testing/netemul/lib/network:ethertap",
     "//third_party/googletest:gtest_main",
-    "//zircon/public/fidl/fuchsia-hardware-ethernet",
-    "//zircon/public/fidl/fuchsia-net-stack",
     "//zircon/public/lib/zx",
   ]
 }
@@ -70,12 +66,13 @@
 test("netstack_fuchsia_c_api_test") {
   sources = [
     "fdio_test.cc",
-    "nodename_test.cc",
+    "ioctl_test.cc",
     "util.cc",
     "util.h",
   ]
 
   deps = [
+    "//garnet/public/lib/netstack/c",
     "//third_party/googletest:gtest_main",
     "//zircon/public/fidl/fuchsia-net:fuchsia-net_c",
     "//zircon/public/lib/fdio",
diff --git a/src/connectivity/network/tests/ioctl_test.cc b/src/connectivity/network/tests/ioctl_test.cc
new file mode 100644
index 0000000..9e60263
--- /dev/null
+++ b/src/connectivity/network/tests/ioctl_test.cc
@@ -0,0 +1,36 @@
+// Copyright 2019 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <lib/netstack/c/netconfig.h>
+#include <sys/utsname.h>
+#include <unistd.h>
+
+#include "gtest/gtest.h"
+
+namespace {
+
+TEST(NetstackTest, IoctlNetcGetNodename) {
+  // gethostname calls uname, which bottoms out in a call to
+  // ioctl_netc_get_nodename that isn't otherwise exposed in the SDK.
+  char hostname[65];
+  EXPECT_EQ(gethostname(hostname, sizeof(hostname)), 0) << strerror(errno);
+  struct utsname uts;
+  EXPECT_EQ(uname(&uts), 0) << strerror(errno);
+}
+
+TEST(NetstackTest, IoctlNetcGetNumIfs) {
+  int fd = socket(AF_INET, SOCK_STREAM, 0);
+  ASSERT_GE(fd, 0) << strerror(errno);
+
+  netc_get_if_info_t get_if_info;
+  ASSERT_GE(ioctl_netc_get_num_ifs(fd, &get_if_info.n_info), 0)
+      << strerror(errno);
+
+  for (uint32_t i = 0; i < get_if_info.n_info; i++) {
+    ASSERT_GE(ioctl_netc_get_if_info_at(fd, &i, &get_if_info.info[i]), 0)
+        << strerror(errno);
+  }
+}
+
+}  // namespace
diff --git a/src/connectivity/network/tests/netstack_ioctl_test.cc b/src/connectivity/network/tests/netstack_ioctl_test.cc
deleted file mode 100644
index 697fc42..0000000
--- a/src/connectivity/network/tests/netstack_ioctl_test.cc
+++ /dev/null
@@ -1,95 +0,0 @@
-// Copyright 2018 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include <fuchsia/netstack/cpp/fidl.h>
-#include <lib/netstack/c/netconfig.h>
-#include <lib/sys/cpp/file_descriptor.h>
-#include <lib/sys/cpp/termination_reason.h>
-#include <lib/sys/cpp/testing/test_with_environment.h>
-#include <src/connectivity/network/testing/netemul/lib/network/ethernet_client.h>
-#include <src/connectivity/network/testing/netemul/lib/network/ethertap_client.h>
-
-#include "gtest/gtest.h"
-
-namespace {
-class NetstackIoctlTest : public sys::testing::TestWithEnvironment {};
-
-const char kNetstackUrl[] =
-    "fuchsia-pkg://fuchsia.com/netstack#meta/netstack.cmx";
-const char kTestIoctlClientUrl[] =
-    "fuchsia-pkg://fuchsia.com/test_ioctl_client#meta/test_ioctl_client.cmx";
-const char kTopoPath[] = "/fake/topo/path";
-const char kInterfaceName[] = "en0";
-
-TEST_F(NetstackIoctlTest, RunIoctlClient) {
-  auto services = CreateServices();
-
-  fuchsia::sys::LaunchInfo netstack_launch_info;
-  netstack_launch_info.url = kNetstackUrl;
-  netstack_launch_info.out = sys::CloneFileDescriptor(STDOUT_FILENO);
-  netstack_launch_info.err = sys::CloneFileDescriptor(STDERR_FILENO);
-  services->AddServiceWithLaunchInfo(std::move(netstack_launch_info),
-                                     fuchsia::netstack::Netstack::Name_);
-
-  // There's not currently a way to register one component for multiple
-  // services. Duplicate component URLs will only be launched once, so there
-  // will only be one netstack process running in this hermetic environment.
-  fuchsia::sys::LaunchInfo socket_provider_launch_info;
-  socket_provider_launch_info.url = kNetstackUrl;
-  socket_provider_launch_info.out = sys::CloneFileDescriptor(STDOUT_FILENO);
-  socket_provider_launch_info.err = sys::CloneFileDescriptor(STDERR_FILENO);
-  services->AddServiceWithLaunchInfo(std::move(socket_provider_launch_info),
-                                     fuchsia::net::SocketProvider::Name_);
-
-  auto env = CreateNewEnclosingEnvironment("NetstackLaunchTest_IoctlTest",
-                                           std::move(services));
-  ASSERT_TRUE(WaitForEnclosingEnvToStart(env.get()));
-
-  // Create a tap device so both the loopback and non-loopback code paths in
-  // ioctl_netc_get_ifinfo are exercised.
-  auto eth_config = netemul::EthertapConfig("IoctlTest");
-  auto tap = netemul::EthertapClient::Create(eth_config);
-  ASSERT_TRUE(tap) << "failed to create ethertap device";
-  tap->SetLinkUp(true);
-
-  netemul::EthernetClientFactory eth_factory;
-  auto eth = eth_factory.RetrieveWithMAC(eth_config.tap_cfg.mac);
-  ASSERT_TRUE(eth) << "failed to retrieve ethernet client";
-
-  fuchsia::netstack::NetstackPtr netstack;
-  env->ConnectToService(netstack.NewRequest());
-
-  fuchsia::netstack::InterfaceConfig config;
-  config.name = kInterfaceName;
-  config.ip_address_config.set_dhcp(true);
-
-  uint32_t eth_id = 0;
-  netstack->AddEthernetDevice(std::move(kTopoPath), std::move(config),
-                              std::move(eth->device()),
-                              [&eth_id](uint32_t id) { eth_id = id; });
-  ASSERT_TRUE(RunLoopUntil([&eth_id] { return eth_id != 0; }));
-
-  fuchsia::sys::LaunchInfo client_launch_info;
-  client_launch_info.url = kTestIoctlClientUrl;
-  client_launch_info.out = sys::CloneFileDescriptor(STDOUT_FILENO);
-  client_launch_info.err = sys::CloneFileDescriptor(STDERR_FILENO);
-  auto controller = env.get()->CreateComponent(std::move(client_launch_info));
-
-  bool wait = false;
-  int64_t exit_code;
-  fuchsia::sys::TerminationReason term_reason;
-  controller.events().OnTerminated =
-      [&wait, &exit_code, &term_reason](
-          int64_t retcode, fuchsia::sys::TerminationReason reason) {
-        wait = true;
-        exit_code = retcode;
-        term_reason = reason;
-      };
-  EXPECT_TRUE(RunLoopUntil([&wait] { return wait; }));
-  EXPECT_EQ(exit_code, 0);
-  EXPECT_EQ(term_reason, fuchsia::sys::TerminationReason::EXITED)
-      << "got: " << sys::TerminationReasonToString(term_reason);
-}
-
-}  // namespace
diff --git a/src/connectivity/network/tests/nodename_test.cc b/src/connectivity/network/tests/nodename_test.cc
deleted file mode 100644
index c57d060..0000000
--- a/src/connectivity/network/tests/nodename_test.cc
+++ /dev/null
@@ -1,15 +0,0 @@
-#include <sys/utsname.h>
-#include <unistd.h>
-
-#include "gtest/gtest.h"
-
-namespace {
-TEST(NetstackTest, IoctlNetcGetNodename) {
-  // gethostname calls uname, which bottoms out in a call to
-  // ioctl_netc_get_nodename that isn't otherwise exposed in the SDK.
-  char hostname[65];
-  EXPECT_EQ(gethostname(hostname, sizeof(hostname)), 0) << strerror(errno);
-  struct utsname uts;
-  EXPECT_EQ(uname(&uts), 0) << strerror(errno);
-}
-}  // namespace
diff --git a/src/connectivity/network/tests/test_ioctl_client/BUILD.gn b/src/connectivity/network/tests/test_ioctl_client/BUILD.gn
deleted file mode 100644
index 27d7200..0000000
--- a/src/connectivity/network/tests/test_ioctl_client/BUILD.gn
+++ /dev/null
@@ -1,29 +0,0 @@
-# Copyright 2019 The Fuchsia Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style license that can be
-# found in the LICENSE file.
-import("//build/package.gni")
-
-executable("ioctl_client") {
-  sources = [
-    "test_ioctl_client.cc",
-  ]
-
-  deps = [
-    "//garnet/lib/inet",
-    "//garnet/public/lib/netstack/c",
-  ]
-}
-
-package("test_ioctl_client") {
-  deps = [
-    ":ioctl_client",
-  ]
-  binary = "ioctl_client"
-
-  meta = [
-    {
-      path = rebase_path("meta/test_ioctl_client.cmx")
-      dest = "test_ioctl_client.cmx"
-    },
-  ]
-}
diff --git a/src/connectivity/network/tests/test_ioctl_client/meta/test_ioctl_client.cmx b/src/connectivity/network/tests/test_ioctl_client/meta/test_ioctl_client.cmx
deleted file mode 100644
index 4426fe1..0000000
--- a/src/connectivity/network/tests/test_ioctl_client/meta/test_ioctl_client.cmx
+++ /dev/null
@@ -1,10 +0,0 @@
-{
-    "program": {
-        "binary": "bin/app"
-    },
-    "sandbox": {
-        "services": [
-            "fuchsia.net.SocketProvider"
-        ]
-    }
-}
diff --git a/src/connectivity/network/tests/test_ioctl_client/test_ioctl_client.cc b/src/connectivity/network/tests/test_ioctl_client/test_ioctl_client.cc
deleted file mode 100644
index cd33769..0000000
--- a/src/connectivity/network/tests/test_ioctl_client/test_ioctl_client.cc
+++ /dev/null
@@ -1,41 +0,0 @@
-// Copyright 2018 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include <lib/netstack/c/netconfig.h>
-
-#include <cerrno>
-#include <string>
-
-#include <arpa/inet.h>
-#include <fcntl.h>
-#include <inttypes.h>
-#include <stdio.h>
-#include <string.h>
-
-int main() {
-  const int fd = socket(AF_INET6, SOCK_STREAM, 0);
-  if (fd < 0) {
-    fprintf(stderr, "socket(AF_INET, SOCK_DGRAM, 0) failed: errno: %d\n",
-            errno);
-    return 1;
-  }
-
-  netc_get_if_info_t get_if_info;
-  const ssize_t size = ioctl_netc_get_num_ifs(fd, &get_if_info.n_info);
-  if (size < 0) {
-    fprintf(stderr, "ioctl_netc_get_num_ifs() failed: errno: %d\n", errno);
-    return 1;
-  }
-
-  for (uint32_t i = 0; i < get_if_info.n_info; i++) {
-    const ssize_t size =
-        ioctl_netc_get_if_info_at(fd, &i, &get_if_info.info[i]);
-    if (size < 0) {
-      fprintf(stderr, "ioctl_netc_get_if_info_at() failed\n: errno: %d", errno);
-      return 1;
-    }
-  }
-
-  return 0;
-}