[network/tests] simplify no_network_test

...by expressing it as a simple gtest case and using netemul.

Change-Id: I1094cee515d5f149c4eb5b1d968ac1a68c16d2da
diff --git a/garnet/packages/tests/BUILD.gn b/garnet/packages/tests/BUILD.gn
index 3c0bc30..03416f4 100644
--- a/garnet/packages/tests/BUILD.gn
+++ b/garnet/packages/tests/BUILD.gn
@@ -500,7 +500,6 @@
     "//src/connectivity/network/netstack/tests:netstack_manual_tests",
     "//src/connectivity/network/netstack/util:netstack_util_test($host_toolchain)",
     "//src/connectivity/network/tests:netstack_c_api_tests",
-    "//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 649e732..1cbad95 100644
--- a/src/connectivity/network/tests/BUILD.gn
+++ b/src/connectivity/network/tests/BUILD.gn
@@ -7,21 +7,20 @@
 import("//build/test/test_package.gni")
 import("//build/testing/environments.gni")
 
-executable("netstack_integration_test") {
+group("netemul_empty_binaries") {
+  write_file("$root_out_dir/netstack_no_network_test", "")
+}
+
+executable("netstack_no_network_test_client") {
   testonly = true
 
   sources = [
-    "netstack_no_network_test.cc",
+    "netstack_no_network_test_client.cc",
   ]
 
   deps = [
     "//garnet/lib/inet",
-    "//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/lib/zx",
   ]
 }
 
@@ -82,15 +81,10 @@
 test_package("netstack_integration_tests") {
   deps = [
     ":netstack_fidl_integration",
-    ":netstack_integration_test",
   ]
 
   tests = [
     {
-      name = "netstack_integration_test"
-      environments = [ qemu_env ]
-    },
-    {
       name = "netstack_fidl_integration_lib_test"
       environments = [ qemu_env ]
     },
@@ -99,8 +93,23 @@
 
 test_package("netstack_c_api_tests") {
   deps = [
+    ":netemul_empty_binaries",
     ":netstack_bsdsocket_c_api_test",
     ":netstack_fuchsia_c_api_test",
+    ":netstack_no_network_test_client",
+  ]
+
+  binaries = [
+    {
+      name = "netstack_no_network_test_client"
+    },
+  ]
+
+  meta = [
+    {
+      path = rebase_path("meta/netstack_no_network_test_client.cmx")
+      dest = "netstack_no_network_test_client.cmx"
+    },
   ]
 
   tests = [
@@ -112,5 +121,9 @@
       name = "netstack_fuchsia_c_api_test"
       environments = [ qemu_env ]
     },
+    {
+      name = "netstack_no_network_test"
+      environments = [ qemu_env ]
+    },
   ]
 }
diff --git a/src/connectivity/network/tests/meta/netstack_integration_test.cmx b/src/connectivity/network/tests/meta/netstack_integration_test.cmx
deleted file mode 100644
index 53c9db3..0000000
--- a/src/connectivity/network/tests/meta/netstack_integration_test.cmx
+++ /dev/null
@@ -1,15 +0,0 @@
-{
-    "program": {
-        "binary": "test/netstack_integration_test"
-    },
-    "sandbox": {
-        "dev": [
-            "misc/tapctl",
-            "class/ethernet"
-        ],
-        "services": [
-            "fuchsia.sys.Environment",
-            "fuchsia.sys.Loader"
-        ]
-    }
-}
diff --git a/src/connectivity/network/tests/meta/netstack_no_network_test.cmx b/src/connectivity/network/tests/meta/netstack_no_network_test.cmx
new file mode 100644
index 0000000..23aeadc
--- /dev/null
+++ b/src/connectivity/network/tests/meta/netstack_no_network_test.cmx
@@ -0,0 +1,20 @@
+{
+    "facets": {
+        "fuchsia.netemul": {
+            "default_url": "fuchsia-pkg://fuchsia.com/netstack_c_api_tests#meta/netstack_no_network_test_client.cmx",
+            "environment": {
+                "name": "no_network",
+                "services": {
+                    "fuchsia.net.SocketProvider": "fuchsia-pkg://fuchsia.com/netstack#meta/netstack.cmx"
+                },
+                "test": [
+                    {}
+                ]
+            }
+        }
+    },
+    "program": {
+        "binary": "bin/app"
+    },
+    "runner": "fuchsia-pkg://fuchsia.com/netemul_runner#meta/netemul_runner.cmx"
+}
diff --git a/src/connectivity/network/tests/test_no_network_client/meta/test_no_network_client.cmx b/src/connectivity/network/tests/meta/netstack_no_network_test_client.cmx
similarity index 68%
rename from src/connectivity/network/tests/test_no_network_client/meta/test_no_network_client.cmx
rename to src/connectivity/network/tests/meta/netstack_no_network_test_client.cmx
index 557f4b5..daf59d0 100644
--- a/src/connectivity/network/tests/test_no_network_client/meta/test_no_network_client.cmx
+++ b/src/connectivity/network/tests/meta/netstack_no_network_test_client.cmx
@@ -1,6 +1,6 @@
 {
     "program": {
-        "binary": "bin/no_network_client"
+        "binary": "bin/netstack_no_network_test_client"
     },
     "sandbox": {
         "services": [
diff --git a/src/connectivity/network/tests/netstack_no_network_test.cc b/src/connectivity/network/tests/netstack_no_network_test.cc
deleted file mode 100644
index c070321..0000000
--- a/src/connectivity/network/tests/netstack_no_network_test.cc
+++ /dev/null
@@ -1,112 +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.
-
-#include <fuchsia/net/cpp/fidl.h>
-#include <fuchsia/netstack/cpp/fidl.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 "garnet/lib/inet/ip_address.h"
-#include "gtest/gtest.h"
-
-namespace {
-class NetstackNoNetworkTest : public sys::testing::TestWithEnvironment {};
-
-const char kNetstackUrl[] =
-    "fuchsia-pkg://fuchsia.com/netstack#meta/netstack.cmx";
-const char kTopoPath[] = "/fake/topo/path";
-const char kInterfaceName[] = "en0";
-const char kTestNoNetworkClientUrl[] =
-    "fuchsia-pkg://fuchsia.com/test_no_network_client#meta/"
-    "test_no_network_client.cmx";
-
-TEST_F(NetstackNoNetworkTest, DisableEthernetInterface) {
-  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_);
-
-  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(
-      "NetstackNoNetworkTest_DisableEthernetInterface", std::move(services));
-  ASSERT_TRUE(WaitForEnclosingEnvToStart(env.get()));
-
-  auto eth_config = netemul::EthertapConfig("DisableEthernetInterface");
-  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(false);
-
-  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; }));
-
-  inet::IpAddress ip = inet::IpAddress(192, 168, 0, 2);
-  fuchsia::net::IpAddress addr;
-  fuchsia::net::Ipv4Address ipv4;
-  memcpy(ipv4.addr.data(), ip.as_bytes(), 4);
-  addr.set_ipv4(ipv4);
-
-  fuchsia::netstack::Status net_status =
-      fuchsia::netstack::Status::UNKNOWN_ERROR;
-  netstack->SetInterfaceAddress(
-      eth_id, std::move(addr), 32,
-      [&net_status](fuchsia::netstack::NetErr result) {
-        net_status = result.status;
-      });
-  ASSERT_TRUE(RunLoopWithTimeoutOrUntil(
-      [&net_status] { return net_status == fuchsia::netstack::Status::OK; },
-      zx::sec(10)));
-
-  // Run the client without enabling the interface.
-
-  fuchsia::sys::LaunchInfo client_launch_info;
-  client_launch_info.url = kTestNoNetworkClientUrl;
-  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) << "Exit code was non-zero, got: " << exit_code;
-  EXPECT_EQ(term_reason, fuchsia::sys::TerminationReason::EXITED)
-      << "TerminationReason was not 'EXITED' as expected, got: "
-      << sys::TerminationReasonToString(term_reason);
-}
-
-}  // namespace
diff --git a/src/connectivity/network/tests/test_no_network_client/no_network_test.cc b/src/connectivity/network/tests/netstack_no_network_test_client.cc
similarity index 100%
rename from src/connectivity/network/tests/test_no_network_client/no_network_test.cc
rename to src/connectivity/network/tests/netstack_no_network_test_client.cc
diff --git a/src/connectivity/network/tests/test_no_network_client/BUILD.gn b/src/connectivity/network/tests/test_no_network_client/BUILD.gn
deleted file mode 100644
index deb2218..0000000
--- a/src/connectivity/network/tests/test_no_network_client/BUILD.gn
+++ /dev/null
@@ -1,43 +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")
-import("//build/test.gni")
-
-test("no_network_client") {
-  sources = [
-    "no_network_test.cc",
-  ]
-
-  deps = [
-    "//third_party/googletest:gtest_main",
-    "//zircon/public/lib/fdio",
-  ]
-}
-
-# We don't use 'test_package' as it requires 'tests' to be
-# defined.
-package("test_no_network_client") {
-  testonly = true
-
-  deps = [
-    ":no_network_client",
-  ]
-
-  # Note that we use 'binaries' instead of 'tests' so that CQ
-  # won't run this package directly.
-  binaries = [
-    {
-      name = "no_network_client"
-      environments = [ qemu_env ]
-    },
-  ]
-
-  meta = [
-    {
-      path = rebase_path("meta/test_no_network_client.cmx")
-      dest = "test_no_network_client.cmx"
-    },
-  ]
-}