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

Commit 1a857b32 authored by Isaac Manjarres's avatar Isaac Manjarres Committed by Gerrit Code Review
Browse files

Merge "ashmem_test: Rewrite fork tests to use fork() explicitly" into main

parents 1a18d053 cc226cb6
Loading
Loading
Loading
Loading
+57 −51
Original line number Diff line number Diff line
@@ -69,6 +69,15 @@ void FillData(std::vector<uint8_t>& data) {
    }
}

static void waitForChildProcessExit(pid_t pid) {
    int exitStatus;
    pid_t childPid = waitpid(pid, &exitStatus, 0);

    ASSERT_GT(childPid, 0);
    ASSERT_TRUE(WIFEXITED(exitStatus));
    ASSERT_EQ(0, WEXITSTATUS(exitStatus));
}

TEST(AshmemTest, ForkTest) {
    const size_t size = getpagesize();
    std::vector<uint8_t> data(size);
@@ -84,28 +93,30 @@ TEST(AshmemTest, ForkTest) {
    ASSERT_EQ(0, memcmp(region1, data.data(), size));
    EXPECT_EQ(0, munmap(region1, size));

    // This part of the test forks a separate process via ASSERT_EXIT() and
    // clears the ashmem buffer.
    //
    // This is to ensure that updates to the contents of the buffer are visible
    // across processes with a reference to the buffer.
    ASSERT_EXIT(
        {

    pid_t pid = fork();
    if (!pid) {
        if (!ashmem_valid(fd)) {
            _exit(3);
        }

        void *region2 = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        if (region2 == MAP_FAILED) {
            _exit(1);
            }
            if (memcmp(region2, data.data(), size) != 0) {
        } else if (memcmp(region2, data.data(), size) != 0){
            _exit(2);
        }

        // Clear the ashmem buffer here to ensure that updates to the contents
        // of the buffer are visible across processes with a reference to the
        // buffer.
        memset(region2, 0, size);
        munmap(region2, size);
        _exit(0);
        },
        ::testing::ExitedWithCode(0), "");
    } else {
        ASSERT_GT(pid, 0);
        ASSERT_NO_FATAL_FAILURE(waitForChildProcessExit(pid));
    }

    memset(data.data(), 0, size);
    void *region2;
@@ -206,26 +217,22 @@ TEST(AshmemTest, ForkProtTest) {

    ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));

    // This part of the test forks a process via ASSERT_EXIT()
    // and has the child process change the mapping permissions of the
    // buffer to read-only.
    //
    // The intent of this is to ensure that updates to the buffer's
    // mapping permissions are visible across processes that reference the
    // buffer.
    //
    // In this case, the parent process attempts to map the buffer with write
    // permission, which should fail.
    ASSERT_EXIT(
        {
    pid_t pid = fork();
    if (!pid) {
        // Change buffer mapping permissions to read-only to ensure that
        // updates to the buffer's mapping permissions are visible across
        // processes that reference the buffer.
        if (!ashmem_valid(fd)) {
            _exit(3);
        } else if (ashmem_set_prot_region(fd, PROT_READ) == -1) {
            _exit(1);
        }
        _exit(0);
        },
        ::testing::ExitedWithCode(0), "");
    } else {
        ASSERT_GT(pid, 0);
        ASSERT_NO_FATAL_FAILURE(waitForChildProcessExit(pid));
    }

    ASSERT_NO_FATAL_FAILURE(TestProtDenied(fd, size, PROT_WRITE));
}

@@ -245,14 +252,10 @@ TEST(AshmemTest, ForkMultiRegionTest) {
        EXPECT_EQ(0, munmap(region, size));
    }

    // This part of the test creates a child process via ASSERT_EXIT()
    // that clears each of the ashmem buffers that were created earlier.
    //
    // The intent of this is for the parent process to check each region to make
    // sure that the updates from the child process are visible, thereby showing
    // that updates across multiple shared buffers are visible across multiple
    // processes.
    ASSERT_EXIT({
    pid_t pid = fork();
    if (!pid) {
        // Clear each ashmem buffer in the context of the child process to
        // ensure that the updates are visible to the parent process later.
        for (int i = 0; i < nRegions; i++) {
            if (!ashmem_valid(fd[i])) {
                _exit(3);
@@ -269,7 +272,10 @@ TEST(AshmemTest, ForkMultiRegionTest) {
            munmap(region, size);
        }
        _exit(0);
    }, ::testing::ExitedWithCode(0), "");
    } else {
        ASSERT_GT(pid, 0);
        ASSERT_NO_FATAL_FAILURE(waitForChildProcessExit(pid));
    }

    memset(data.data(), 0, size);
    for (int i = 0; i < nRegions; i++) {