Make the VTS tests for the INetd HAL 1.0 more realistic.

The VTS test for the netd HAL creates an OEM network, but it
doesn't check whether the network exists. Fix that by using
the native multinetwork API calls to select the network.
Also, check that when an OEM network is created, there are no IP
rules that select for its fwmark. There should be none because
a network that has just been created has no interfaces.

Put utility code into a new VtsHalNetNetdTestUtils static library
to avoid code duplication when we add new tests for future HALversions.

Bug: 73032258
Test: VtsHalNetNetdV1_0TargetTest passes on marlin
Change-Id: I345b9b27d7c2c8bdd6ce6d60430490dac8c3a989
Merged-In: Ie574990f7640fdc7d83ccd58baf6dfe1069a3654
(cherry picked from commit 79f693702c35dca81f7390d1619736a204e61a70)
diff --git a/net/netd/testutils/VtsHalNetNetdTestUtils.cpp b/net/netd/testutils/VtsHalNetNetdTestUtils.cpp
new file mode 100644
index 0000000..1f7db2b
--- /dev/null
+++ b/net/netd/testutils/VtsHalNetNetdTestUtils.cpp
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <regex>
+#include <string>
+#include <vector>
+
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <android-base/stringprintf.h>
+#include <android/multinetwork.h>
+
+#include "VtsHalNetNetdTestUtils.h"
+
+using android::base::StringPrintf;
+
+int checkNetworkExists(net_handle_t netHandle) {
+    int sock = socket(AF_INET6, SOCK_STREAM, 0);
+    if (sock == -1) {
+        return -ENOTSOCK;
+    }
+    int ret = android_setsocknetwork(netHandle, sock);
+    if (ret) {
+        ret = -errno;
+    }
+    close(sock);
+    return ret;
+}
+
+// TODO: deduplicate this with system/netd/server/binder_test.cpp.
+static std::vector<std::string> runCommand(const std::string& command) {
+    std::vector<std::string> lines;
+    FILE* f;
+
+    if ((f = popen(command.c_str(), "r")) == nullptr) {
+        perror("popen");
+        return lines;
+    }
+
+    char* line = nullptr;
+    size_t bufsize = 0;
+    ssize_t linelen = 0;
+    while ((linelen = getline(&line, &bufsize, f)) >= 0) {
+        lines.push_back(std::string(line, linelen));
+    }
+
+    free(line);
+    pclose(f);
+    return lines;
+}
+
+static std::vector<std::string> listIpRules(const char* ipVersion) {
+    std::string command = StringPrintf("%s %s rule list", IP_PATH, ipVersion);
+    return runCommand(command);
+}
+
+static int countMatchingIpRules(const std::string& regexString) {
+    const std::regex regex(regexString, std::regex_constants::extended);
+    int matches = 0;
+
+    for (const char* version : {"-4", "-6"}) {
+        std::vector<std::string> rules = listIpRules(version);
+        for (const auto& rule : rules) {
+            if (std::regex_search(rule, regex)) {
+                matches++;
+            }
+        }
+    }
+
+    return matches;
+}
+
+int countRulesForFwmark(const uint32_t fwmark) {
+    std::string regex = StringPrintf("from all fwmark 0x%x/.* lookup ", fwmark);
+    return countMatchingIpRules(regex);
+}