• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <errno.h>
2 #include <gtest/gtest.h>
3 #include <sys/mman.h>
4 
5 using namespace testing::ext;
6 
7 class MmanMprotectTest : public testing::Test {
SetUp()8     void SetUp() override {}
TearDown()9     void TearDown() override {}
10 };
11 
12 constexpr int SIZE = 8;
13 
14 /**
15  * @tc.name: mprotect_001
16  * @tc.desc: This test verifies that the MProtect function can successfully write data after setting the memory page to
17  *           writable permission, and can still successfully read data after setting the memory page to read-only
18  *           permission.
19  * @tc.type: FUNC
20  */
21 HWTEST_F(MmanMprotectTest, mprotect_001, TestSize.Level1)
22 {
23     size_t pageSize = getpagesize();
24     void* memoryBuffer = memalign(pageSize, SIZE * pageSize);
25     int protectResult = mprotect(memoryBuffer, pageSize, PROT_WRITE);
26     EXPECT_EQ(0, protectResult);
27     char* bufferPtr = static_cast<char*>(memoryBuffer);
28     strcpy(bufferPtr, "test");
29     protectResult = mprotect(memoryBuffer, pageSize, PROT_READ | PROT_WRITE | PROT_EXEC);
30     EXPECT_EQ(0, protectResult);
31     free(memoryBuffer);
32 }