Add tests for INetd 1.1.

Bug: 73032258
Test: VtsHalNetNetdV1_0TargetTest and VtsHalNetNetdV1_1TargetTest passes on marlin
Change-Id: Id4d49f08563982353a9a7db79b569e933ea03c46
Merged-In: Id4d49f08563982353a9a7db79b569e933ea03c46
(cherry picked from commit 94832f94dc902b130bf3b2814e9addc2f27a2840)
diff --git a/net/netd/testutils/Android.bp b/net/netd/testutils/Android.bp
index a7c33a5..991c272 100644
--- a/net/netd/testutils/Android.bp
+++ b/net/netd/testutils/Android.bp
@@ -13,6 +13,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+// Utility code common for all HAL versions.
 cc_library {
     name: "VtsHalNetNetdTestUtils",
     srcs: [
@@ -20,12 +21,34 @@
     ],
     export_include_dirs: ["."],
     shared_libs: [
-        "libbase",
         "libandroid_net",
+        "libbase",
+    ],
+    cflags: [
+        "-Og",
+	"-Wall",
+	"-Werror",
+    ],
+}
+
+// Common build settings for all HAL versions.
+cc_defaults {
+    name: "VtsHalNetNetdTestDefaults",
+    shared_libs: [
+        "libandroid_net",
+        "libbase",
+        "libhidlbase",
+        "liblog",
+        "libutils",
+    ],
+    static_libs: [
+        "VtsHalHidlTargetTestBase",
+        "VtsHalNetNetdTestUtils",
     ],
     cflags: [
         "-Og",
         "-Wall",
         "-Werror",
     ],
+    defaults: ["VtsHalTargetTestDefaults"],
 }
diff --git a/net/netd/testutils/VtsHalNetNetdTestUtils.cpp b/net/netd/testutils/VtsHalNetNetdTestUtils.cpp
index 1f7db2b..da377c4 100644
--- a/net/netd/testutils/VtsHalNetNetdTestUtils.cpp
+++ b/net/netd/testutils/VtsHalNetNetdTestUtils.cpp
@@ -43,7 +43,7 @@
     return ret;
 }
 
-// TODO: deduplicate this with system/netd/server/binder_test.cpp.
+// TODO: deduplicate this with system/netd/tests/binder_test.cpp.
 static std::vector<std::string> runCommand(const std::string& command) {
     std::vector<std::string> lines;
     FILE* f;
@@ -70,7 +70,7 @@
     return runCommand(command);
 }
 
-static int countMatchingIpRules(const std::string& regexString) {
+int countMatchingIpRules(const std::string& regexString) {
     const std::regex regex(regexString, std::regex_constants::extended);
     int matches = 0;
 
@@ -87,6 +87,30 @@
 }
 
 int countRulesForFwmark(const uint32_t fwmark) {
-    std::string regex = StringPrintf("from all fwmark 0x%x/.* lookup ", fwmark);
+    // Skip top nibble, which differs between rules.
+    std::string regex = StringPrintf("from all fwmark 0x[0-9a-f]+%x/.* lookup ", fwmark);
     return countMatchingIpRules(regex);
 }
+
+int checkReachability(net_handle_t netHandle, const char* addrStr) {
+    addrinfo *ai, hints = {.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV};
+    int ret = getaddrinfo(addrStr, "53", &hints, &ai);
+    if (ret) {
+        return -EINVAL;
+    }
+
+    int sock = socket(ai->ai_family, SOCK_DGRAM, 0);
+    if (sock == -1 || android_setsocknetwork(netHandle, sock) == -1) {
+        ret = -errno;
+        freeaddrinfo(ai);
+        return ret;
+    }
+
+    ret = connect(sock, ai->ai_addr, ai->ai_addrlen);
+    close(sock);
+    if (ret == -1) {
+        ret = -errno;
+    }
+    freeaddrinfo(ai);
+    return ret;
+}
diff --git a/net/netd/testutils/VtsHalNetNetdTestUtils.h b/net/netd/testutils/VtsHalNetNetdTestUtils.h
index 71d9174..ef0c7ed 100644
--- a/net/netd/testutils/VtsHalNetNetdTestUtils.h
+++ b/net/netd/testutils/VtsHalNetNetdTestUtils.h
@@ -19,12 +19,25 @@
 
 #include <android/multinetwork.h>
 
-#define IP_PATH "/system/bin/ip"
+#define EXPECT_STATUS(expectedStatus, ret)  \
+    do {                                    \
+        EXPECT_TRUE((ret).isOk());          \
+        EXPECT_EQ((expectedStatus), (ret)); \
+    } while (0)
+
+constexpr const char* IP_PATH = "/system/bin/ip";
 
 // Checks that the given network exists.
 // Returns 0 if it exists or -errno if it does not.
 int checkNetworkExists(net_handle_t netHandle);
 
+// Checks that the given network provides reachability to the given address.
+// Returns 0 if it so or -errno if not.
+int checkReachability(net_handle_t netHandle, const char* addrStr);
+
+// Counts the number of IPv4 and IPv6 routing rules that match the given regexp string.
+int countMatchingIpRules(const std::string& regexString);
+
 // Counts the number of IPv4 and IPv6 routing rules that select the specified fwmark.
 int countRulesForFwmark(const uint32_t fwmark);