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

Commit dd218e6c authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "libcutils: test: increase use of ashmem_valid(fd)"

parents 0e239bbd 276758da
Loading
Loading
Loading
Loading
+48 −22
Original line number Diff line number Diff line
@@ -14,11 +14,18 @@
 * limitations under the License.
 */

#include <errno.h>
#include <linux/fs.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>

#include <android-base/macros.h>
#include <android-base/unique_fd.h>
#include <cutils/ashmem.h>
#include <gtest/gtest.h>
#include <linux/fs.h>
#include <sys/mman.h>

using android::base::unique_fd;

@@ -31,15 +38,21 @@ void TestCreateRegion(size_t size, unique_fd &fd, int prot) {
}

void TestMmap(const unique_fd& fd, size_t size, int prot, void** region, off_t off = 0) {
    ASSERT_TRUE(fd >= 0);
    ASSERT_TRUE(ashmem_valid(fd));
    *region = mmap(nullptr, size, prot, MAP_SHARED, fd, off);
    ASSERT_NE(MAP_FAILED, *region);
}

void TestProtDenied(const unique_fd &fd, size_t size, int prot) {
    ASSERT_TRUE(fd >= 0);
    ASSERT_TRUE(ashmem_valid(fd));
    EXPECT_EQ(MAP_FAILED, mmap(nullptr, size, prot, MAP_SHARED, fd, 0));
}

void TestProtIs(const unique_fd& fd, int prot) {
    ASSERT_TRUE(fd >= 0);
    ASSERT_TRUE(ashmem_valid(fd));
    EXPECT_EQ(prot, ioctl(fd, ASHMEM_GET_PROT_MASK));
}

@@ -86,7 +99,11 @@ TEST(AshmemTest, ForkTest) {
    ASSERT_EQ(0, memcmp(region1, &data, size));
    EXPECT_EQ(0, munmap(region1, size));

    ASSERT_EXIT({
    ASSERT_EXIT(
        {
            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);
@@ -97,7 +114,8 @@ TEST(AshmemTest, ForkTest) {
            memset(region2, 0, size);
            munmap(region2, size);
            _exit(0);
    }, ::testing::ExitedWithCode(0),"");
        },
        ::testing::ExitedWithCode(0), "");

    memset(&data, 0, size);
    void *region2;
@@ -146,6 +164,7 @@ TEST(AshmemTest, FileOperationsTest) {
    };
    for (const auto& cfg : seeks) {
        errno = 0;
        ASSERT_TRUE(ashmem_valid(fd));
        auto off = lseek(fd, cfg.offset, cfg.whence);
        ASSERT_EQ(cfg.ret, off) << "lseek(" << cfg.offset << ", " << cfg.whence << ") failed"
                                << (errno ? ": " : "") << (errno ? strerror(errno) : "");
@@ -196,15 +215,19 @@ TEST(AshmemTest, ForkProtTest) {
    constexpr size_t size = PAGE_SIZE;

    int protFlags[] = { PROT_READ, PROT_WRITE };
    for (int i = 0; i < 2; i++) {
    for (size_t i = 0; i < arraysize(protFlags); i++) {
        ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
        ASSERT_EXIT({
            if (ashmem_set_prot_region(fd, protFlags[i]) >= 0) {
        ASSERT_EXIT(
            {
                if (!ashmem_valid(fd)) {
                    _exit(3);
                } else if (ashmem_set_prot_region(fd, protFlags[i]) >= 0) {
                    _exit(0);
                } else {
                    _exit(1);
                }
        }, ::testing::ExitedWithCode(0), "");
            },
            ::testing::ExitedWithCode(0), "");
        ASSERT_NO_FATAL_FAILURE(TestProtDenied(fd, size, protFlags[1-i]));
    }
}
@@ -227,6 +250,9 @@ TEST(AshmemTest, ForkMultiRegionTest) {

    ASSERT_EXIT({
        for (int i = 0; i < nRegions; i++) {
            if (!ashmem_valid(fd[i])) {
                _exit(3);
            }
            void *region = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd[i], 0);
            if (region == MAP_FAILED) {
                _exit(1);