Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 562775e4 authored by Ken Chen's avatar Ken Chen Committed by Gerrit Code Review
Browse files

Merge "Move SetMdnsRoute() and RemoveMdnsRoute() to test utilities"

parents a67518ee daa67f1b
Loading
Loading
Loading
Loading
+0 −60
Original line number Diff line number Diff line
@@ -383,66 +383,6 @@ class ResolverTest : public ::testing::Test {
        return fmt::format("127.0.100.{}", (++counter & 0xff));
    }

    int WaitChild(pid_t pid) {
        int status;
        const pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));

        if (got_pid != pid) {
            PLOG(WARNING) << __func__ << ": waitpid failed: wanted " << pid << ", got " << got_pid;
            return 1;
        }

        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
            return 0;
        } else {
            return status;
        }
    }

    int ForkAndRun(const std::vector<std::string>& args) {
        std::vector<const char*> argv;
        argv.resize(args.size() + 1, nullptr);
        std::transform(args.begin(), args.end(), argv.begin(),
                       [](const std::string& in) { return in.c_str(); });

        pid_t pid = fork();
        if (pid == -1) {
            // Fork failed.
            PLOG(ERROR) << __func__ << ": Unable to fork";
            return -1;
        }

        if (pid == 0) {
            execv(argv[0], const_cast<char**>(argv.data()));
            PLOG(ERROR) << __func__ << ": execv failed";
            _exit(1);
        }

        int rc = WaitChild(pid);
        if (rc != 0) {
            PLOG(ERROR) << __func__ << ": Failed run: status=" << rc;
        }
        return rc;
    }

    // Add routing rules for MDNS packets, or MDNS packets won't know the destination is MDNS
    // muticast address "224.0.0.251".
    void SetMdnsRoute() {
        const std::vector<std::string> args = {
                "system/bin/ip", "route",  "add",   "local", "224.0.0.251", "dev",       "lo",
                "proto",         "static", "scope", "host",  "src",         "127.0.0.1",
        };
        EXPECT_EQ(0, ForkAndRun(args));
    }

    void RemoveMdnsRoute() {
        const std::vector<std::string> args = {
                "system/bin/ip", "route",  "del",   "local", "224.0.0.251", "dev",       "lo",
                "proto",         "static", "scope", "host",  "src",         "127.0.0.1",
        };
        EXPECT_EQ(0, ForkAndRun(args));
    }

    DnsResponderClient mDnsClient;

    bool mIsResolverOptionIPCSupported = false;
+61 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <arpa/inet.h>

#include <android-base/chrono_utils.h>
#include <android-base/logging.h>

using android::netdutils::ScopedAddrinfo;

@@ -154,3 +155,63 @@ ScopedAddrinfo safe_getaddrinfo(const char* node, const char* service,
    }
    return ScopedAddrinfo(result);
}

int WaitChild(pid_t pid) {
    int status;
    const pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));

    if (got_pid != pid) {
        PLOG(WARNING) << __func__ << ": waitpid failed: wanted " << pid << ", got " << got_pid;
        return 1;
    }

    if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
        return 0;
    } else {
        return status;
    }
}

int ForkAndRun(const std::vector<std::string>& args) {
    std::vector<const char*> argv;
    argv.resize(args.size() + 1, nullptr);
    std::transform(args.begin(), args.end(), argv.begin(),
                   [](const std::string& in) { return in.c_str(); });

    pid_t pid = fork();
    if (pid == -1) {
        // Fork failed.
        PLOG(ERROR) << __func__ << ": Unable to fork";
        return -1;
    }

    if (pid == 0) {
        execv(argv[0], const_cast<char**>(argv.data()));
        PLOG(ERROR) << __func__ << ": execv failed";
        _exit(1);
    }

    int rc = WaitChild(pid);
    if (rc != 0) {
        PLOG(ERROR) << __func__ << ": Failed run: status=" << rc;
    }
    return rc;
}

// Add routing rules for MDNS packets, or MDNS packets won't know the destination is MDNS
// muticast address "224.0.0.251".
void SetMdnsRoute() {
    const std::vector<std::string> args = {
            "system/bin/ip", "route",  "add",   "local", "224.0.0.251", "dev",       "lo",
            "proto",         "static", "scope", "host",  "src",         "127.0.0.1",
    };
    EXPECT_EQ(0, ForkAndRun(args));
}

void RemoveMdnsRoute() {
    const std::vector<std::string> args = {
            "system/bin/ip", "route",  "del",   "local", "224.0.0.251", "dev",       "lo",
            "proto",         "static", "scope", "host",  "src",         "127.0.0.1",
    };
    EXPECT_EQ(0, ForkAndRun(args));
}
 No newline at end of file
+3 −0
Original line number Diff line number Diff line
@@ -213,3 +213,6 @@ bool PollForCondition(const std::function<bool()>& condition,

android::netdutils::ScopedAddrinfo safe_getaddrinfo(const char* node, const char* service,
                                                    const struct addrinfo* hints);

void SetMdnsRoute();
void RemoveMdnsRoute();
 No newline at end of file
+0 −60
Original line number Diff line number Diff line
@@ -131,66 +131,6 @@ class TestBase : public ::testing::Test {
        return resolv_set_nameservers(TEST_NETID, servers, domains, params, std::nullopt);
    }

    int WaitChild(pid_t pid) {
        int status;
        const pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));

        if (got_pid != pid) {
            PLOG(WARNING) << __func__ << ": waitpid failed: wanted " << pid << ", got " << got_pid;
            return 1;
        }

        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
            return 0;
        } else {
            return status;
        }
    }

    int ForkAndRun(const std::vector<std::string>& args) {
        std::vector<const char*> argv;
        argv.resize(args.size() + 1, nullptr);
        std::transform(args.begin(), args.end(), argv.begin(),
                       [](const std::string& in) { return in.c_str(); });

        pid_t pid = fork();
        if (pid == -1) {
            // Fork failed.
            PLOG(ERROR) << __func__ << ": Unable to fork";
            return -1;
        }

        if (pid == 0) {
            execv(argv[0], const_cast<char**>(argv.data()));
            PLOG(ERROR) << __func__ << ": execv failed";
            _exit(1);
        }

        int rc = WaitChild(pid);
        if (rc != 0) {
            PLOG(ERROR) << __func__ << ": Failed run: status=" << rc;
        }
        return rc;
    }

    // Add routing rules for MDNS packets, or MDNS packets won't know the destination is MDNS
    // muticast address "224.0.0.251".
    void SetMdnsRoute() {
        const std::vector<std::string> args = {
                "system/bin/ip", "route",  "add",   "local", "224.0.0.251", "dev",       "lo",
                "proto",         "static", "scope", "host",  "src",         "127.0.0.1",
        };
        EXPECT_EQ(0, ForkAndRun(args));
    }

    void RemoveMdnsRoute() {
        const std::vector<std::string> args = {
                "system/bin/ip", "route",  "del",   "local", "224.0.0.251", "dev",       "lo",
                "proto",         "static", "scope", "host",  "src",         "127.0.0.1",
        };
        EXPECT_EQ(0, ForkAndRun(args));
    }

    const android_net_context mNetcontext = {
            .app_netid = TEST_NETID,
            .app_mark = MARK_UNSET,