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

Commit 5ea6b0c1 authored by Ken Chen's avatar Ken Chen Committed by Automerger Merge Worker
Browse files

Merge "Move SetMdnsRoute() and RemoveMdnsRoute() to test utilities" am: 562775e4 am: 5c11be8f

Original change: https://android-review.googlesource.com/c/platform/packages/modules/DnsResolver/+/2009283

Change-Id: If359bbd8f8b934374529576f947dfc5653d501e1
parents eedcbf07 5c11be8f
Loading
Loading
Loading
Loading
+0 −60
Original line number Original line Diff line number Diff line
@@ -383,66 +383,6 @@ class ResolverTest : public ::testing::Test {
        return fmt::format("127.0.100.{}", (++counter & 0xff));
        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;
    DnsResponderClient mDnsClient;


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


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


using android::netdutils::ScopedAddrinfo;
using android::netdutils::ScopedAddrinfo;


@@ -154,3 +155,63 @@ ScopedAddrinfo safe_getaddrinfo(const char* node, const char* service,
    }
    }
    return ScopedAddrinfo(result);
    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 Original line 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,
android::netdutils::ScopedAddrinfo safe_getaddrinfo(const char* node, const char* service,
                                                    const struct addrinfo* hints);
                                                    const struct addrinfo* hints);

void SetMdnsRoute();
void RemoveMdnsRoute();
 No newline at end of file
+0 −60
Original line number Original line 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);
        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 = {
    const android_net_context mNetcontext = {
            .app_netid = TEST_NETID,
            .app_netid = TEST_NETID,
            .app_mark = MARK_UNSET,
            .app_mark = MARK_UNSET,