[fdio] Move getaddrinfo tests from network

Discovered this target isn't connected to the build graph; connect it
and rehabilitate the test for modern requirements:
- Update to use fuchsia_*_package
- Include required cmx fragments
- List Rust sources
- Deny unused results
- Use net-declare
- Remove spawn-detach

Change-Id: I53d9a7959a5def5126b7314ef5ecd6e660e95d93
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/514021
Fuchsia-Auto-Submit: Tamir Duberstein <tamird@google.com>
Reviewed-by: Bruno Dal Bo <brunodalbo@google.com>
Commit-Queue: Tamir Duberstein <tamird@google.com>
diff --git a/sdk/lib/fdio/tests/BUILD.gn b/sdk/lib/fdio/tests/BUILD.gn
index 5210665..ebec6e46 100644
--- a/sdk/lib/fdio/tests/BUILD.gn
+++ b/sdk/lib/fdio/tests/BUILD.gn
@@ -27,6 +27,7 @@
     ":chroot-child($host_toolchain)",
     ":fdio-unittest-package",
     "constructors:tests",
+    "getaddrinfo:tests",
   ]
   if (host_os == "linux") {
     deps += [ ":fdio-test($host_toolchain)" ]
diff --git a/sdk/lib/fdio/tests/getaddrinfo/BUILD.gn b/sdk/lib/fdio/tests/getaddrinfo/BUILD.gn
new file mode 100644
index 0000000..55fbd53
--- /dev/null
+++ b/sdk/lib/fdio/tests/getaddrinfo/BUILD.gn
@@ -0,0 +1,51 @@
+# 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/rust/rustc_test.gni")
+import("//src/sys/build/components.gni")
+
+executable("getaddrinfo_test_client") {
+  testonly = true
+
+  sources = [ "getaddrinfo_test_client.cc" ]
+
+  deps = [ "//src/lib/fxl/test:gtest_main" ]
+}
+
+rustc_test("getaddrinfo_test") {
+  edition = "2018"
+
+  sources = [ "src/lib.rs" ]
+
+  deps = [
+    "//sdk/fidl/fuchsia.net:fuchsia.net-rustc",
+    "//src/connectivity/lib/net-declare",
+    "//src/lib/fuchsia-async",
+    "//src/lib/fuchsia-component",
+    "//third_party/rust_crates:futures",
+  ]
+
+  configs -= [ "//build/config/rust:allow_unused_results" ]
+}
+
+fuchsia_test_component("getaddrinfo_test_client_component") {
+  component_name = "getaddrinfo_test_client"
+  deps = [ ":getaddrinfo_test_client" ]
+  manifest = "meta/getaddrinfo_test_client.cmx"
+}
+
+fuchsia_test_component("getaddrinfo_test_component") {
+  deps = [ ":getaddrinfo_test" ]
+  manifest = "meta/getaddrinfo_test.cmx"
+}
+
+fuchsia_test_package("getaddrinfo_tests") {
+  deps = [ ":getaddrinfo_test_client_component" ]
+  test_components = [ ":getaddrinfo_test_component" ]
+}
+
+group("tests") {
+  testonly = true
+  deps = [ ":getaddrinfo_tests" ]
+}
diff --git a/src/connectivity/network/tests/getaddrinfo/getaddrinfo_test_client.cc b/sdk/lib/fdio/tests/getaddrinfo/getaddrinfo_test_client.cc
similarity index 100%
rename from src/connectivity/network/tests/getaddrinfo/getaddrinfo_test_client.cc
rename to sdk/lib/fdio/tests/getaddrinfo/getaddrinfo_test_client.cc
diff --git a/sdk/lib/fdio/tests/getaddrinfo/meta/getaddrinfo_test.cmx b/sdk/lib/fdio/tests/getaddrinfo/meta/getaddrinfo_test.cmx
new file mode 100644
index 0000000..3771732e
--- /dev/null
+++ b/sdk/lib/fdio/tests/getaddrinfo/meta/getaddrinfo_test.cmx
@@ -0,0 +1,15 @@
+{
+    "include": [
+        "sdk/lib/diagnostics/syslog/client.shard.cmx"
+    ],
+    "program": {
+        "binary": "bin/getaddrinfo_test"
+    },
+    "sandbox": {
+        "services": [
+            "fuchsia.sys.Environment",
+            "fuchsia.sys.Launcher",
+            "fuchsia.sys.Loader"
+        ]
+    }
+}
diff --git a/src/connectivity/network/tests/getaddrinfo/meta/getaddrinfo_test_client.cmx b/sdk/lib/fdio/tests/getaddrinfo/meta/getaddrinfo_test_client.cmx
similarity index 68%
rename from src/connectivity/network/tests/getaddrinfo/meta/getaddrinfo_test_client.cmx
rename to sdk/lib/fdio/tests/getaddrinfo/meta/getaddrinfo_test_client.cmx
index 2a61402..96d6ce9 100644
--- a/src/connectivity/network/tests/getaddrinfo/meta/getaddrinfo_test_client.cmx
+++ b/sdk/lib/fdio/tests/getaddrinfo/meta/getaddrinfo_test_client.cmx
@@ -1,4 +1,7 @@
 {
+    "include": [
+        "sdk/lib/diagnostics/syslog/client.shard.cmx"
+    ],
     "program": {
         "binary": "bin/getaddrinfo_test_client"
     },
diff --git a/sdk/lib/fdio/tests/getaddrinfo/src/lib.rs b/sdk/lib/fdio/tests/getaddrinfo/src/lib.rs
new file mode 100644
index 0000000..ef1b47a
--- /dev/null
+++ b/sdk/lib/fdio/tests/getaddrinfo/src/lib.rs
@@ -0,0 +1,58 @@
+// 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.
+
+use {
+    fidl_fuchsia_net as fnet, fuchsia_async as fasync,
+    fuchsia_component::client,
+    fuchsia_component::server::ServiceFs,
+    futures::{FutureExt as _, StreamExt as _, TryStreamExt as _},
+};
+
+#[fasync::run_singlethreaded(test)]
+async fn test_getaddrinfo() {
+    let mut fs = ServiceFs::new();
+    let _: &mut ServiceFs<_> = fs.add_fidl_service(|s: fnet::NameLookupRequestStream| s);
+
+    let env = fs
+        .create_salted_nested_environment("test_getaddrinfo")
+        .expect("failed to create environment");
+    let app = client::AppBuilder::new(
+        "fuchsia-pkg://fuchsia.com/getaddrinfo_tests#meta/getaddrinfo_test_client.cmx",
+    )
+    .output(env.launcher())
+    .expect("failed to launch test client");
+
+    let mut fs = fs.map(Ok).try_for_each_concurrent(None, |stream| {
+        stream.try_for_each_concurrent(None, |request| match request {
+            fnet::NameLookupRequest::LookupIp { hostname, options, responder } => {
+                futures::future::ready(responder.send(&mut if hostname == "example.com" {
+                    Ok(fnet::IpAddressInfo {
+                        ipv4_addrs: options
+                            .contains(fnet::LookupIpOptions::V4Addrs)
+                            .then(|| net_declare::fidl_ip_v4!("192.0.2.1"))
+                            .into_iter()
+                            .collect(),
+                        ipv6_addrs: options
+                            .contains(fnet::LookupIpOptions::V6Addrs)
+                            .then(|| net_declare::fidl_ip_v6!("2001:db8::1"))
+                            .into_iter()
+                            .collect(),
+                        canonical_name: None,
+                    })
+                } else {
+                    Err(fnet::LookupError::NotFound)
+                }))
+            }
+            request => panic!("unexpected request: {:?}", request),
+        })
+    });
+
+    futures::select! {
+        res = app.fuse() => {
+            let output = res.expect("failed to wait for test client exit");
+            output.ok().expect("test client exited with nonzero status code")
+        },
+        res = fs => panic!("request stream terminated: {:?}", res),
+    }
+}
diff --git a/src/connectivity/network/tests/getaddrinfo/BUILD.gn b/src/connectivity/network/tests/getaddrinfo/BUILD.gn
deleted file mode 100644
index d5921b0..0000000
--- a/src/connectivity/network/tests/getaddrinfo/BUILD.gn
+++ /dev/null
@@ -1,58 +0,0 @@
-# Copyright 2016 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/rust/rustc_test.gni")
-import("//build/test/test_package.gni")
-
-executable("getaddrinfo_test_client") {
-  testonly = true
-
-  sources = [ "getaddrinfo_test_client.cc" ]
-
-  deps = [ "//src/lib/fxl/test:gtest_main" ]
-}
-
-rustc_test("getaddrinfo_test") {
-  edition = "2018"
-
-  deps = [
-    "//sdk/fidl/fuchsia.net:fuchsia.net-rustc",
-    "//sdk/fidl/fuchsia.sys:fuchsia.sys-rustc",
-    "//src/lib/fdio/rust:fdio",
-    "//src/lib/fidl/rust/fidl",
-    "//src/lib/fuchsia-async",
-    "//src/lib/fuchsia-component",
-    "//src/lib/zircon/rust:fuchsia-zircon",
-    "//third_party/rust_crates:anyhow",
-    "//third_party/rust_crates:futures",
-    "//third_party/rust_crates:thiserror",
-  ]
-}
-
-# TODO move these tests into fdio, once rust is supported in the zircon build
-test_package("getaddrinfo_tests") {
-  deps = [
-    ":getaddrinfo_test",
-    ":getaddrinfo_test_client",
-  ]
-
-  binaries = [
-    {
-      name = "getaddrinfo_test_client"
-    },
-  ]
-
-  meta = [
-    {
-      path = rebase_path("meta/getaddrinfo_test_client.cmx")
-      dest = "getaddrinfo_test_client.cmx"
-    },
-  ]
-
-  tests = [
-    {
-      name = "getaddrinfo_test"
-    },
-  ]
-}
diff --git a/src/connectivity/network/tests/getaddrinfo/meta/getaddrinfo_test.cmx b/src/connectivity/network/tests/getaddrinfo/meta/getaddrinfo_test.cmx
deleted file mode 100644
index b48fa94..0000000
--- a/src/connectivity/network/tests/getaddrinfo/meta/getaddrinfo_test.cmx
+++ /dev/null
@@ -1,10 +0,0 @@
-{
-    "program": {
-        "binary": "test/getaddrinfo_test"
-    },
-    "sandbox": {
-        "services": [
-            "fuchsia.sys.Launcher"
-        ]
-    }
-}
diff --git a/src/connectivity/network/tests/getaddrinfo/src/lib.rs b/src/connectivity/network/tests/getaddrinfo/src/lib.rs
deleted file mode 100644
index dd49db0..0000000
--- a/src/connectivity/network/tests/getaddrinfo/src/lib.rs
+++ /dev/null
@@ -1,89 +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.
-
-#[cfg(test)]
-mod test {
-    use {
-        fidl::endpoints::{DiscoverableService, ServiceMarker},
-        fidl_fuchsia_net as fnet, fidl_fuchsia_sys as fsys, fuchsia_async as fasync,
-        fuchsia_component::client::connect_to_service,
-        fuchsia_component::server::ServiceFs,
-        fuchsia_zircon as zx,
-        futures::StreamExt,
-    };
-
-    #[fasync::run_singlethreaded(test)]
-    async fn test_getaddrinfo() {
-        let (client_chan, server_chan) = zx::Channel::create().unwrap();
-        let (controller, server_end) =
-            fidl::endpoints::create_proxy::<fsys::ComponentControllerMarker>().unwrap();
-
-        let mut launch_info = fsys::LaunchInfo {
-            url: "fuchsia-pkg://fuchsia.com/getaddrinfo_tests#meta/getaddrinfo_test_client.cmx"
-                .to_string(),
-            arguments: None,
-            out: None,
-            err: None,
-            directory_request: None,
-            flat_namespace: None,
-            additional_services: Some(Box::new(fsys::ServiceList {
-                names: vec![fnet::NameLookupMarker::SERVICE_NAME.to_string()],
-                provider: None,
-                host_directory: Some(client_chan),
-            })),
-        };
-        let launcher_svc = connect_to_service::<fsys::LauncherMarker>().unwrap();
-        launcher_svc.create_component(&mut launch_info, Some(server_end)).unwrap();
-
-        let mut fs = ServiceFs::new();
-        fs.add_fidl_service_at(
-            fnet::NameLookupMarker::NAME,
-            |mut stream: fnet::NameLookupRequestStream| {
-                fasync::Task::spawn(async move {
-                    while let Some(Ok(fnet::NameLookupRequest::LookupIp {
-                        hostname,
-                        options,
-                        responder,
-                    })) = stream.next().await
-                    {
-                        let mut result = fnet::IpAddressInfo {
-                            ipv4_addrs: vec![],
-                            ipv6_addrs: vec![],
-                            canonical_name: None,
-                        };
-                        if hostname == "example.com" {
-                            if options.contains(fnet::LookupIpOptions::V4Addrs) {
-                                result.ipv4_addrs =
-                                    vec![fnet::Ipv4Address { addr: [192, 0, 2, 1] }];
-                            }
-                            if options.contains(fnet::LookupIpOptions::V6Addrs) {
-                                result.ipv6_addrs = vec![fnet::Ipv6Address {
-                                    addr: [
-                                        0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
-                                    ],
-                                }];
-                            }
-                            responder.send(&mut Ok(result)).unwrap();
-                        } else {
-                            responder.send(&mut Err(fnet::LookupError::NotFound)).unwrap();
-                        }
-                    }
-                })
-                .detach();
-            },
-        );
-        fs.serve_connection(server_chan).unwrap();
-        fs.collect::<()>().await;
-        let mut controller_stream = controller.take_event_stream();
-        match controller_stream.next().await.unwrap().unwrap() {
-            fsys::ComponentControllerEvent::OnTerminated {
-                termination_reason: fsys::TerminationReason::Exited,
-                return_code: 0,
-            } => (),
-            event => {
-                panic!("Unexpected component controller event: {:?}", event);
-            }
-        }
-    }
-}