ashmem: Remove hardcoded PAGE_SIZE usage in ashmem_test
Bug: 323061793 Test: atest libcutils_test Change-Id: I8defd5225859b52a1b96a638d418188b328a23bc
This commit is contained in:
parent
b077fcce13
commit
f791b99628
1 changed files with 44 additions and 38 deletions
|
@ -22,6 +22,8 @@
|
|||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <android-base/macros.h>
|
||||
#include <android-base/unique_fd.h>
|
||||
#include <cutils/ashmem.h>
|
||||
|
@ -61,16 +63,16 @@ void TestProtIs(const unique_fd& fd, int prot) {
|
|||
EXPECT_EQ(prot, ioctl(fd, ASHMEM_GET_PROT_MASK));
|
||||
}
|
||||
|
||||
void FillData(uint8_t* data, size_t dataLen) {
|
||||
for (size_t i = 0; i < dataLen; i++) {
|
||||
void FillData(std::vector<uint8_t>& data) {
|
||||
for (size_t i = 0; i < data.size(); i++) {
|
||||
data[i] = i & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(AshmemTest, BasicTest) {
|
||||
constexpr size_t size = PAGE_SIZE;
|
||||
uint8_t data[size];
|
||||
FillData(data, size);
|
||||
const size_t size = getpagesize();
|
||||
std::vector<uint8_t> data(size);
|
||||
FillData(data);
|
||||
|
||||
unique_fd fd;
|
||||
ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
|
||||
|
@ -78,21 +80,21 @@ TEST(AshmemTest, BasicTest) {
|
|||
void* region1 = nullptr;
|
||||
ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_READ | PROT_WRITE, ®ion1));
|
||||
|
||||
memcpy(region1, &data, size);
|
||||
ASSERT_EQ(0, memcmp(region1, &data, size));
|
||||
memcpy(region1, data.data(), size);
|
||||
ASSERT_EQ(0, memcmp(region1, data.data(), size));
|
||||
|
||||
EXPECT_EQ(0, munmap(region1, size));
|
||||
|
||||
void *region2;
|
||||
ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_READ, ®ion2));
|
||||
ASSERT_EQ(0, memcmp(region2, &data, size));
|
||||
ASSERT_EQ(0, memcmp(region2, data.data(), size));
|
||||
EXPECT_EQ(0, munmap(region2, size));
|
||||
}
|
||||
|
||||
TEST(AshmemTest, ForkTest) {
|
||||
constexpr size_t size = PAGE_SIZE;
|
||||
uint8_t data[size];
|
||||
FillData(data, size);
|
||||
const size_t size = getpagesize();
|
||||
std::vector<uint8_t> data(size);
|
||||
FillData(data);
|
||||
|
||||
unique_fd fd;
|
||||
ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
|
||||
|
@ -100,8 +102,8 @@ TEST(AshmemTest, ForkTest) {
|
|||
void* region1 = nullptr;
|
||||
ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_READ | PROT_WRITE, ®ion1));
|
||||
|
||||
memcpy(region1, &data, size);
|
||||
ASSERT_EQ(0, memcmp(region1, &data, size));
|
||||
memcpy(region1, data.data(), size);
|
||||
ASSERT_EQ(0, memcmp(region1, data.data(), size));
|
||||
EXPECT_EQ(0, munmap(region1, size));
|
||||
|
||||
ASSERT_EXIT(
|
||||
|
@ -113,7 +115,7 @@ TEST(AshmemTest, ForkTest) {
|
|||
if (region2 == MAP_FAILED) {
|
||||
_exit(1);
|
||||
}
|
||||
if (memcmp(region2, &data, size) != 0) {
|
||||
if (memcmp(region2, data.data(), size) != 0) {
|
||||
_exit(2);
|
||||
}
|
||||
memset(region2, 0, size);
|
||||
|
@ -122,10 +124,10 @@ TEST(AshmemTest, ForkTest) {
|
|||
},
|
||||
::testing::ExitedWithCode(0), "");
|
||||
|
||||
memset(&data, 0, size);
|
||||
memset(data.data(), 0, size);
|
||||
void *region2;
|
||||
ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_READ | PROT_WRITE, ®ion2));
|
||||
ASSERT_EQ(0, memcmp(region2, &data, size));
|
||||
ASSERT_EQ(0, memcmp(region2, data.data(), size));
|
||||
EXPECT_EQ(0, munmap(region2, size));
|
||||
}
|
||||
|
||||
|
@ -134,18 +136,19 @@ TEST(AshmemTest, FileOperationsTest) {
|
|||
void* region = nullptr;
|
||||
|
||||
// Allocate a 4-page buffer, but leave page-sized holes on either side
|
||||
constexpr size_t size = PAGE_SIZE * 4;
|
||||
constexpr size_t dataSize = PAGE_SIZE * 2;
|
||||
constexpr size_t holeSize = PAGE_SIZE;
|
||||
const size_t pageSize = getpagesize();
|
||||
const size_t size = pageSize * 4;
|
||||
const size_t dataSize = pageSize * 2;
|
||||
const size_t holeSize = pageSize;
|
||||
ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
|
||||
ASSERT_NO_FATAL_FAILURE(TestMmap(fd, dataSize, PROT_READ | PROT_WRITE, ®ion, holeSize));
|
||||
|
||||
uint8_t data[dataSize];
|
||||
FillData(data, dataSize);
|
||||
memcpy(region, data, dataSize);
|
||||
std::vector<uint8_t> data(dataSize);
|
||||
FillData(data);
|
||||
memcpy(region, data.data(), dataSize);
|
||||
|
||||
constexpr off_t dataStart = holeSize;
|
||||
constexpr off_t dataEnd = dataStart + dataSize;
|
||||
const off_t dataStart = holeSize;
|
||||
const off_t dataEnd = dataStart + dataSize;
|
||||
|
||||
// The sequence of seeks below looks something like this:
|
||||
//
|
||||
|
@ -163,9 +166,12 @@ TEST(AshmemTest, FileOperationsTest) {
|
|||
// Expected lseek() return value
|
||||
off_t ret;
|
||||
} seeks[] = {
|
||||
{99, SEEK_SET, 99}, {dataStart, SEEK_CUR, dataStart + 99},
|
||||
{0, SEEK_DATA, dataStart}, {dataStart, SEEK_HOLE, dataEnd},
|
||||
{-99, SEEK_END, size - 99}, {-dataStart, SEEK_CUR, dataEnd - 99},
|
||||
{99, SEEK_SET, 99},
|
||||
{dataStart, SEEK_CUR, dataStart + 99},
|
||||
{0, SEEK_DATA, dataStart},
|
||||
{dataStart, SEEK_HOLE, dataEnd},
|
||||
{-99, SEEK_END, static_cast<off_t>(size) - 99},
|
||||
{-dataStart, SEEK_CUR, dataEnd - 99},
|
||||
};
|
||||
for (const auto& cfg : seeks) {
|
||||
errno = 0;
|
||||
|
@ -180,7 +186,7 @@ TEST(AshmemTest, FileOperationsTest) {
|
|||
uint8_t buf[readSize];
|
||||
|
||||
ASSERT_EQ(readSize, TEMP_FAILURE_RETRY(read(fd, buf, readSize)));
|
||||
EXPECT_EQ(0, memcmp(buf, data + dataOff, readSize));
|
||||
EXPECT_EQ(0, memcmp(buf, &data[dataOff], readSize));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -189,7 +195,7 @@ TEST(AshmemTest, FileOperationsTest) {
|
|||
|
||||
TEST(AshmemTest, ProtTest) {
|
||||
unique_fd fd;
|
||||
constexpr size_t size = PAGE_SIZE;
|
||||
const size_t size = getpagesize();
|
||||
void *region;
|
||||
|
||||
ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ));
|
||||
|
@ -217,7 +223,7 @@ TEST(AshmemTest, ProtTest) {
|
|||
|
||||
TEST(AshmemTest, ForkProtTest) {
|
||||
unique_fd fd;
|
||||
constexpr size_t size = PAGE_SIZE;
|
||||
const size_t size = getpagesize();
|
||||
|
||||
int protFlags[] = { PROT_READ, PROT_WRITE };
|
||||
for (size_t i = 0; i < arraysize(protFlags); i++) {
|
||||
|
@ -238,9 +244,9 @@ TEST(AshmemTest, ForkProtTest) {
|
|||
}
|
||||
|
||||
TEST(AshmemTest, ForkMultiRegionTest) {
|
||||
constexpr size_t size = PAGE_SIZE;
|
||||
uint8_t data[size];
|
||||
FillData(data, size);
|
||||
const size_t size = getpagesize();
|
||||
std::vector<uint8_t> data(size);
|
||||
FillData(data);
|
||||
|
||||
constexpr int nRegions = 16;
|
||||
unique_fd fd[nRegions];
|
||||
|
@ -248,8 +254,8 @@ TEST(AshmemTest, ForkMultiRegionTest) {
|
|||
ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd[i], PROT_READ | PROT_WRITE));
|
||||
void* region = nullptr;
|
||||
ASSERT_NO_FATAL_FAILURE(TestMmap(fd[i], size, PROT_READ | PROT_WRITE, ®ion));
|
||||
memcpy(region, &data, size);
|
||||
ASSERT_EQ(0, memcmp(region, &data, size));
|
||||
memcpy(region, data.data(), size);
|
||||
ASSERT_EQ(0, memcmp(region, data.data(), size));
|
||||
EXPECT_EQ(0, munmap(region, size));
|
||||
}
|
||||
|
||||
|
@ -262,7 +268,7 @@ TEST(AshmemTest, ForkMultiRegionTest) {
|
|||
if (region == MAP_FAILED) {
|
||||
_exit(1);
|
||||
}
|
||||
if (memcmp(region, &data, size) != 0) {
|
||||
if (memcmp(region, data.data(), size) != 0) {
|
||||
munmap(region, size);
|
||||
_exit(2);
|
||||
}
|
||||
|
@ -272,11 +278,11 @@ TEST(AshmemTest, ForkMultiRegionTest) {
|
|||
_exit(0);
|
||||
}, ::testing::ExitedWithCode(0), "");
|
||||
|
||||
memset(&data, 0, size);
|
||||
memset(data.data(), 0, size);
|
||||
for (int i = 0; i < nRegions; i++) {
|
||||
void *region;
|
||||
ASSERT_NO_FATAL_FAILURE(TestMmap(fd[i], size, PROT_READ | PROT_WRITE, ®ion));
|
||||
ASSERT_EQ(0, memcmp(region, &data, size));
|
||||
ASSERT_EQ(0, memcmp(region, data.data(), size));
|
||||
EXPECT_EQ(0, munmap(region, size));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue