1 #include <gtest/gtest.h> 2 #include <stdio.h> 3 #include <sys/mman.h> 4 5 using namespace testing::ext; 6 7 class MmanMsyncTest : public testing::Test { SetUp()8 void SetUp() override {} TearDown()9 void TearDown() override {} 10 }; 11 12 /** 13 * @tc.name: msync_001 14 * @tc.desc: This test verifies whether the msync function can successfully flush data from shared memory to a temporary 15 * file on disk and return the expected success result (0). 16 * @tc.type: FUNC 17 */ 18 HWTEST_F(MmanMsyncTest, msync_001, TestSize.Level1) 19 { 20 FILE* fp = tmpfile(); 21 ASSERT_NE(fp, nullptr); 22 const char testData[] = "Good Time!"; 23 size_t testDataSize = strlen(testData); 24 fwrite(testData, sizeof(char), testDataSize, fp); 25 int fd = fileno(fp); 26 ASSERT_NE(fd, -1); 27 void* map = mmap(nullptr, testDataSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 28 ASSERT_NE(map, MAP_FAILED); 29 int rev = msync(map, testDataSize, MS_ASYNC); 30 EXPECT_EQ(rev, 0); 31 munmap(map, testDataSize); 32 fclose(fp); 33 }