1570af302Sopenharmony_ci/** 2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4570af302Sopenharmony_ci * you may not use this file except in compliance with the License. 5570af302Sopenharmony_ci * You may obtain a copy of the License at 6570af302Sopenharmony_ci * 7570af302Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8570af302Sopenharmony_ci * 9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12570af302Sopenharmony_ci * See the License for the specific language governing permissions and 13570af302Sopenharmony_ci * limitations under the License. 14570af302Sopenharmony_ci */ 15570af302Sopenharmony_ci 16570af302Sopenharmony_ci#include <fcntl.h> 17570af302Sopenharmony_ci#include <stdlib.h> 18570af302Sopenharmony_ci#include <sys/mman.h> 19570af302Sopenharmony_ci#include <sys/stat.h> 20570af302Sopenharmony_ci#include <unistd.h> 21570af302Sopenharmony_ci#include "functionalext.h" 22570af302Sopenharmony_ci 23570af302Sopenharmony_ciconst int EOK = 0; 24570af302Sopenharmony_ciconst int SUCCESS = 0; 25570af302Sopenharmony_ciconst int FAILED = -1; 26570af302Sopenharmony_ci#define OFF_MASK ((-0x2000ULL << (8 * sizeof(long) - 1)) | (4096ULL - 1)) 27570af302Sopenharmony_ci 28570af302Sopenharmony_ci/** 29570af302Sopenharmony_ci * @tc.name : mmap_0100 30570af302Sopenharmony_ci * @tc.desc : The parameters are valid, prot is PROT_READ, 31570af302Sopenharmony_ci * and flags is MAP_PRIVATE, which can map the file content into memory 32570af302Sopenharmony_ci * @tc.level : Level 0 33570af302Sopenharmony_ci */ 34570af302Sopenharmony_civoid mmap_0100(void) 35570af302Sopenharmony_ci{ 36570af302Sopenharmony_ci int fd, mm; 37570af302Sopenharmony_ci void *start; 38570af302Sopenharmony_ci const char *ptr = "/data/test.txt"; 39570af302Sopenharmony_ci static char str[] = "this is a sample!"; 40570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w+"); 41570af302Sopenharmony_ci EXPECT_PTRNE("mmap_0100", fptr, NULL); 42570af302Sopenharmony_ci 43570af302Sopenharmony_ci struct stat statbuff; 44570af302Sopenharmony_ci fwrite(str, sizeof(char), strlen(str), fptr); 45570af302Sopenharmony_ci fseek(fptr, 0L, SEEK_SET); 46570af302Sopenharmony_ci EXPECT_TRUE("mmap_0100", fgetc(fptr) != EOF); 47570af302Sopenharmony_ci int back = stat(ptr, &statbuff); 48570af302Sopenharmony_ci fclose(fptr); 49570af302Sopenharmony_ci 50570af302Sopenharmony_ci fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777); 51570af302Sopenharmony_ci EXPECT_NE("mmap_0100", fd, -1); 52570af302Sopenharmony_ci start = mmap(NULL, statbuff.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 53570af302Sopenharmony_ci EXPECT_EQ("mmap_0100", back, SUCCESS); 54570af302Sopenharmony_ci EXPECT_TRUE("mmap_0100", start != MAP_FAILED); 55570af302Sopenharmony_ci 56570af302Sopenharmony_ci mm = munmap(start, statbuff.st_size); 57570af302Sopenharmony_ci EXPECT_EQ("mmap_0100", mm, SUCCESS); 58570af302Sopenharmony_ci close(fd); 59570af302Sopenharmony_ci remove(ptr); 60570af302Sopenharmony_ci remove("/data/test.txt"); 61570af302Sopenharmony_ci fptr = NULL; 62570af302Sopenharmony_ci ptr = NULL; 63570af302Sopenharmony_ci} 64570af302Sopenharmony_ci 65570af302Sopenharmony_ci/** 66570af302Sopenharmony_ci * @tc.name : mmap_0200 67570af302Sopenharmony_ci * @tc.desc : The parameters are valid, prot is PROT_READ|PROT_WRITE, 68570af302Sopenharmony_ci * and flags is MAP_SHARED, which can map the file content into memory 69570af302Sopenharmony_ci * @tc.level : Level 0 70570af302Sopenharmony_ci */ 71570af302Sopenharmony_civoid mmap_0200(void) 72570af302Sopenharmony_ci{ 73570af302Sopenharmony_ci int fd, mm; 74570af302Sopenharmony_ci void *start; 75570af302Sopenharmony_ci const char *ptr = "/data/test.txt"; 76570af302Sopenharmony_ci static char str[] = "this is a sample!"; 77570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w+"); 78570af302Sopenharmony_ci EXPECT_PTRNE("mmap_0200", fptr, NULL); 79570af302Sopenharmony_ci 80570af302Sopenharmony_ci struct stat statbuff; 81570af302Sopenharmony_ci struct stat sb; 82570af302Sopenharmony_ci fwrite(str, sizeof(char), strlen(str), fptr); 83570af302Sopenharmony_ci fseek(fptr, 0L, SEEK_SET); 84570af302Sopenharmony_ci EXPECT_TRUE("mmap_0200", fgetc(fptr) != EOF); 85570af302Sopenharmony_ci 86570af302Sopenharmony_ci int back = stat(ptr, &statbuff); 87570af302Sopenharmony_ci fclose(fptr); 88570af302Sopenharmony_ci fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777); 89570af302Sopenharmony_ci EXPECT_NE("mmap_0200", fd, -1); 90570af302Sopenharmony_ci start = mmap(NULL, statbuff.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 91570af302Sopenharmony_ci EXPECT_EQ("mmap_0200", back, SUCCESS); 92570af302Sopenharmony_ci EXPECT_TRUE("mmap_0200", start != MAP_FAILED); 93570af302Sopenharmony_ci 94570af302Sopenharmony_ci mm = munmap(start, statbuff.st_size); 95570af302Sopenharmony_ci EXPECT_EQ("mmap_0200", mm, SUCCESS); 96570af302Sopenharmony_ci close(fd); 97570af302Sopenharmony_ci remove(ptr); 98570af302Sopenharmony_ci remove("/data/test.txt"); 99570af302Sopenharmony_ci fptr = NULL; 100570af302Sopenharmony_ci ptr = NULL; 101570af302Sopenharmony_ci} 102570af302Sopenharmony_ci 103570af302Sopenharmony_ci/** 104570af302Sopenharmony_ci * @tc.name : mmap_0300 105570af302Sopenharmony_ci * @tc.desc : The parameters are valid, prot is PROT_READ|PROT_WRITE, and flags is MAP_SHARED|MAP_ANON, 106570af302Sopenharmony_ci * which can map the file content into memory 107570af302Sopenharmony_ci * @tc.level : Level 1 108570af302Sopenharmony_ci */ 109570af302Sopenharmony_civoid mmap_0300(void) 110570af302Sopenharmony_ci{ 111570af302Sopenharmony_ci void *start; 112570af302Sopenharmony_ci int mm; 113570af302Sopenharmony_ci start = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 114570af302Sopenharmony_ci EXPECT_TRUE("mmap_0300", start != MAP_FAILED); 115570af302Sopenharmony_ci mm = munmap(start, getpagesize()); 116570af302Sopenharmony_ci EXPECT_EQ("mmap_0300", mm, SUCCESS); 117570af302Sopenharmony_ci} 118570af302Sopenharmony_ci 119570af302Sopenharmony_ci/** 120570af302Sopenharmony_ci * @tc.name : mmap_0400 121570af302Sopenharmony_ci * @tc.desc : The parameters are valid, prot is PROT_EXEC, and flags is MAP_LOCKED, 122570af302Sopenharmony_ci * which can map the file content into memory 123570af302Sopenharmony_ci * @tc.level : Level 1 124570af302Sopenharmony_ci */ 125570af302Sopenharmony_civoid mmap_0400(void) 126570af302Sopenharmony_ci{ 127570af302Sopenharmony_ci int fd, mm; 128570af302Sopenharmony_ci void *start; 129570af302Sopenharmony_ci const char *ptr = "/data/test.txt"; 130570af302Sopenharmony_ci static char str[] = "this is a sample!"; 131570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w+"); 132570af302Sopenharmony_ci EXPECT_PTRNE("mmap_0400", fptr, NULL); 133570af302Sopenharmony_ci 134570af302Sopenharmony_ci struct stat statbuff; 135570af302Sopenharmony_ci struct stat sb; 136570af302Sopenharmony_ci fwrite(str, sizeof(char), strlen(str), fptr); 137570af302Sopenharmony_ci fseek(fptr, 0L, SEEK_SET); 138570af302Sopenharmony_ci EXPECT_TRUE("mmap_0400", fgetc(fptr) != EOF); 139570af302Sopenharmony_ci 140570af302Sopenharmony_ci int back = stat(ptr, &statbuff); 141570af302Sopenharmony_ci fclose(fptr); 142570af302Sopenharmony_ci fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777); 143570af302Sopenharmony_ci EXPECT_NE("mmap_0400", fd, -1); 144570af302Sopenharmony_ci start = mmap(NULL, statbuff.st_size, PROT_EXEC, MAP_PRIVATE | MAP_LOCKED, fd, 0); 145570af302Sopenharmony_ci EXPECT_EQ("mmap_0400", back, SUCCESS); 146570af302Sopenharmony_ci EXPECT_TRUE("mmap_0400", start != MAP_FAILED); 147570af302Sopenharmony_ci mm = munmap(start, statbuff.st_size); 148570af302Sopenharmony_ci EXPECT_EQ("mmap_0400", mm, SUCCESS); 149570af302Sopenharmony_ci remove(ptr); 150570af302Sopenharmony_ci remove("/data/test.txt"); 151570af302Sopenharmony_ci fptr = NULL; 152570af302Sopenharmony_ci ptr = NULL; 153570af302Sopenharmony_ci close(fd); 154570af302Sopenharmony_ci} 155570af302Sopenharmony_ci 156570af302Sopenharmony_ci/** 157570af302Sopenharmony_ci * @tc.name : mmap_0500 158570af302Sopenharmony_ci * @tc.desc : The parameters are valid, prot is PROT_READ|PROT_WRITE, and flags is MAP_PRIVATE|MAP_DENYWRITE, 159570af302Sopenharmony_ci * which can map the file content into memory 160570af302Sopenharmony_ci * @tc.level : Level 1 161570af302Sopenharmony_ci */ 162570af302Sopenharmony_civoid mmap_0500(void) 163570af302Sopenharmony_ci{ 164570af302Sopenharmony_ci int fd, mm; 165570af302Sopenharmony_ci void *start; 166570af302Sopenharmony_ci const char *ptr = "/data/test.txt"; 167570af302Sopenharmony_ci static char str[] = "this is a sample!"; 168570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w+"); 169570af302Sopenharmony_ci EXPECT_PTRNE("mmap_0500", fptr, NULL); 170570af302Sopenharmony_ci 171570af302Sopenharmony_ci struct stat statbuff; 172570af302Sopenharmony_ci struct stat sb; 173570af302Sopenharmony_ci fwrite(str, sizeof(char), strlen(str), fptr); 174570af302Sopenharmony_ci fseek(fptr, 0L, SEEK_SET); 175570af302Sopenharmony_ci EXPECT_TRUE("mmap_0500", fgetc(fptr) != EOF); 176570af302Sopenharmony_ci 177570af302Sopenharmony_ci int back = stat(ptr, &statbuff); 178570af302Sopenharmony_ci fclose(fptr); 179570af302Sopenharmony_ci fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777); 180570af302Sopenharmony_ci EXPECT_NE("mmap_0500", fd, -1); 181570af302Sopenharmony_ci 182570af302Sopenharmony_ci start = mmap(NULL, statbuff.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_DENYWRITE, fd, 0); 183570af302Sopenharmony_ci EXPECT_EQ("mmap_0500", back, SUCCESS); 184570af302Sopenharmony_ci EXPECT_TRUE("mmap_0500", start != MAP_FAILED); 185570af302Sopenharmony_ci mm = munmap(start, statbuff.st_size); 186570af302Sopenharmony_ci EXPECT_EQ("mmap_0500", mm, SUCCESS); 187570af302Sopenharmony_ci remove(ptr); 188570af302Sopenharmony_ci remove("/data/test.txt"); 189570af302Sopenharmony_ci fptr = NULL; 190570af302Sopenharmony_ci ptr = NULL; 191570af302Sopenharmony_ci close(fd); 192570af302Sopenharmony_ci} 193570af302Sopenharmony_ci 194570af302Sopenharmony_ci/** 195570af302Sopenharmony_ci * @tc.name : mmap_0600 196570af302Sopenharmony_ci * @tc.desc : The parameters are valid, prot is PROT_NONE, and flags is MAP_PRIVATE|MAP_FIXED, 197570af302Sopenharmony_ci * which can map the file content into memory 198570af302Sopenharmony_ci * @tc.level : Level 2 199570af302Sopenharmony_ci */ 200570af302Sopenharmony_civoid mmap_0600(void) 201570af302Sopenharmony_ci{ 202570af302Sopenharmony_ci int fd, mm; 203570af302Sopenharmony_ci void *start; 204570af302Sopenharmony_ci const char *ptr = "/data/test.txt"; 205570af302Sopenharmony_ci static char str[] = "this is a sample!"; 206570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w+"); 207570af302Sopenharmony_ci EXPECT_PTRNE("mmap_0600", fptr, NULL); 208570af302Sopenharmony_ci 209570af302Sopenharmony_ci struct stat statbuff; 210570af302Sopenharmony_ci struct stat sb; 211570af302Sopenharmony_ci fwrite(str, sizeof(char), strlen(str), fptr); 212570af302Sopenharmony_ci fseek(fptr, 0L, SEEK_SET); 213570af302Sopenharmony_ci EXPECT_TRUE("mmap_0600", fgetc(fptr) != EOF); 214570af302Sopenharmony_ci 215570af302Sopenharmony_ci int back = stat(ptr, &statbuff); 216570af302Sopenharmony_ci fclose(fptr); 217570af302Sopenharmony_ci fd = open("/data/test.txt", O_RDONLY); 218570af302Sopenharmony_ci EXPECT_NE("mmap_0600", fd, -1); 219570af302Sopenharmony_ci start = mmap(NULL, statbuff.st_size, PROT_NONE, MAP_PRIVATE | MAP_FIXED, -1, 0); 220570af302Sopenharmony_ci EXPECT_EQ("mmap_0600", back, SUCCESS); 221570af302Sopenharmony_ci EXPECT_EQ("mmap_0600", start, MAP_FAILED); 222570af302Sopenharmony_ci remove(ptr); 223570af302Sopenharmony_ci remove("/data/test.txt"); 224570af302Sopenharmony_ci fptr = NULL; 225570af302Sopenharmony_ci ptr = NULL; 226570af302Sopenharmony_ci close(fd); 227570af302Sopenharmony_ci} 228570af302Sopenharmony_ci 229570af302Sopenharmony_ci/** 230570af302Sopenharmony_ci * @tc.name : mmap_0700 231570af302Sopenharmony_ci * @tc.desc : The parameter is invalid, the off parameter is OFF_MASK, 232570af302Sopenharmony_ci * the file content cannot be mapped into memory 233570af302Sopenharmony_ci * @tc.level : Level 2 234570af302Sopenharmony_ci */ 235570af302Sopenharmony_civoid mmap_0700(void) 236570af302Sopenharmony_ci{ 237570af302Sopenharmony_ci int fd; 238570af302Sopenharmony_ci void *start; 239570af302Sopenharmony_ci const char *ptr = "/data/test.txt"; 240570af302Sopenharmony_ci static char str[] = "this is a sample!"; 241570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w+"); 242570af302Sopenharmony_ci EXPECT_PTRNE("mmap_0700", fptr, NULL); 243570af302Sopenharmony_ci 244570af302Sopenharmony_ci struct stat statbuff; 245570af302Sopenharmony_ci struct stat sb; 246570af302Sopenharmony_ci fwrite(str, sizeof(char), strlen(str), fptr); 247570af302Sopenharmony_ci fseek(fptr, 0L, SEEK_SET); 248570af302Sopenharmony_ci EXPECT_TRUE("mmap_0700", fgetc(fptr) != EOF); 249570af302Sopenharmony_ci 250570af302Sopenharmony_ci int back = stat(ptr, &statbuff); 251570af302Sopenharmony_ci fclose(fptr); 252570af302Sopenharmony_ci fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777); 253570af302Sopenharmony_ci EXPECT_NE("mmap_0700", fd, -1); 254570af302Sopenharmony_ci 255570af302Sopenharmony_ci start = mmap(NULL, statbuff.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, OFF_MASK); 256570af302Sopenharmony_ci EXPECT_EQ("mmap_0700", back, SUCCESS); 257570af302Sopenharmony_ci EXPECT_EQ("mmap_0700", start, MAP_FAILED); 258570af302Sopenharmony_ci remove(ptr); 259570af302Sopenharmony_ci remove("/data/test.txt"); 260570af302Sopenharmony_ci fptr = NULL; 261570af302Sopenharmony_ci ptr = NULL; 262570af302Sopenharmony_ci close(fd); 263570af302Sopenharmony_ci} 264570af302Sopenharmony_ci 265570af302Sopenharmony_ci/** 266570af302Sopenharmony_ci * @tc.name : mmap_0800 267570af302Sopenharmony_ci * @tc.desc : The parameter is invalid, the len parameter exceeds the maximum value, 268570af302Sopenharmony_ci * and the file content cannot be mapped into memory 269570af302Sopenharmony_ci * @tc.level : Level 2 270570af302Sopenharmony_ci */ 271570af302Sopenharmony_civoid mmap_0800(void) 272570af302Sopenharmony_ci{ 273570af302Sopenharmony_ci int fd; 274570af302Sopenharmony_ci void *start; 275570af302Sopenharmony_ci const char *ptr = "/data/test.txt"; 276570af302Sopenharmony_ci static char str[] = "this is a sample!"; 277570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w+"); 278570af302Sopenharmony_ci EXPECT_PTRNE("mmap_0800", fptr, NULL); 279570af302Sopenharmony_ci 280570af302Sopenharmony_ci fwrite(str, sizeof(char), strlen(str), fptr); 281570af302Sopenharmony_ci fseek(fptr, 0L, SEEK_SET); 282570af302Sopenharmony_ci EXPECT_TRUE("mmap_0800", fgetc(fptr) != EOF); 283570af302Sopenharmony_ci 284570af302Sopenharmony_ci fclose(fptr); 285570af302Sopenharmony_ci fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777); 286570af302Sopenharmony_ci EXPECT_NE("mmap_0800", fd, -1); 287570af302Sopenharmony_ci 288570af302Sopenharmony_ci start = mmap(NULL, PTRDIFF_MAX + 1, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 289570af302Sopenharmony_ci EXPECT_EQ("mmap_0800", start, MAP_FAILED); 290570af302Sopenharmony_ci remove(ptr); 291570af302Sopenharmony_ci remove("/data/test.txt"); 292570af302Sopenharmony_ci fptr = NULL; 293570af302Sopenharmony_ci ptr = NULL; 294570af302Sopenharmony_ci close(fd); 295570af302Sopenharmony_ci} 296570af302Sopenharmony_ci 297570af302Sopenharmony_ci/** 298570af302Sopenharmony_ci * @tc.name : mmap_0900 299570af302Sopenharmony_ci * @tc.desc : The parameter is invalid, the len parameter is 0, the file content cannot be mapped into memory 300570af302Sopenharmony_ci * @tc.level : Level 2 301570af302Sopenharmony_ci */ 302570af302Sopenharmony_civoid mmap_0900(void) 303570af302Sopenharmony_ci{ 304570af302Sopenharmony_ci int fd; 305570af302Sopenharmony_ci void *start; 306570af302Sopenharmony_ci const char *ptr = "/data/test.txt"; 307570af302Sopenharmony_ci static char str[] = "this is a sample!"; 308570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w+"); 309570af302Sopenharmony_ci EXPECT_PTRNE("mmap_0900", fptr, NULL); 310570af302Sopenharmony_ci 311570af302Sopenharmony_ci fwrite(str, sizeof(char), strlen(str), fptr); 312570af302Sopenharmony_ci fseek(fptr, 0L, SEEK_SET); 313570af302Sopenharmony_ci EXPECT_TRUE("mmap_0900", fgetc(fptr) != EOF); 314570af302Sopenharmony_ci 315570af302Sopenharmony_ci fclose(fptr); 316570af302Sopenharmony_ci fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777); 317570af302Sopenharmony_ci EXPECT_NE("mmap_0900", fd, -1); 318570af302Sopenharmony_ci 319570af302Sopenharmony_ci start = mmap(NULL, 0, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 320570af302Sopenharmony_ci EXPECT_EQ("mmap_0900", start, MAP_FAILED); 321570af302Sopenharmony_ci remove(ptr); 322570af302Sopenharmony_ci remove("/data/test.txt"); 323570af302Sopenharmony_ci fptr = NULL; 324570af302Sopenharmony_ci ptr = NULL; 325570af302Sopenharmony_ci close(fd); 326570af302Sopenharmony_ci} 327570af302Sopenharmony_ci 328570af302Sopenharmony_ci/** 329570af302Sopenharmony_ci * @tc.name : mmap_1000 330570af302Sopenharmony_ci * @tc.desc : The parameter is invalid, the flags parameter is MAP_SHARED|MAP_ANON, and the off parameter is 331570af302Sopenharmony_ci * not an integer multiple of the page, the file content cannot be mapped into the memory 332570af302Sopenharmony_ci * @tc.level : Level 2 333570af302Sopenharmony_ci */ 334570af302Sopenharmony_civoid mmap_1000(void) 335570af302Sopenharmony_ci{ 336570af302Sopenharmony_ci int fd; 337570af302Sopenharmony_ci void *start; 338570af302Sopenharmony_ci const char *ptr = "/data/test.txt"; 339570af302Sopenharmony_ci static char str[] = "this is a sample!"; 340570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w+"); 341570af302Sopenharmony_ci EXPECT_PTRNE("mmap_1000", fptr, NULL); 342570af302Sopenharmony_ci 343570af302Sopenharmony_ci struct stat statbuff; 344570af302Sopenharmony_ci struct stat sb; 345570af302Sopenharmony_ci fwrite(str, sizeof(char), strlen(str), fptr); 346570af302Sopenharmony_ci fseek(fptr, 0L, SEEK_SET); 347570af302Sopenharmony_ci EXPECT_TRUE("mmap_1000", fgetc(fptr) != EOF); 348570af302Sopenharmony_ci 349570af302Sopenharmony_ci int back = stat(ptr, &statbuff); 350570af302Sopenharmony_ci fclose(fptr); 351570af302Sopenharmony_ci fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777); 352570af302Sopenharmony_ci EXPECT_NE("mmap_1000", fd, -1); 353570af302Sopenharmony_ci 354570af302Sopenharmony_ci start = mmap(NULL, statbuff.st_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, fd, getpagesize() - 1); 355570af302Sopenharmony_ci EXPECT_EQ("mmap_1000", back, SUCCESS); 356570af302Sopenharmony_ci EXPECT_EQ("mmap_1000", start, MAP_FAILED); 357570af302Sopenharmony_ci remove(ptr); 358570af302Sopenharmony_ci remove("/data/test.txt"); 359570af302Sopenharmony_ci fptr = NULL; 360570af302Sopenharmony_ci ptr = NULL; 361570af302Sopenharmony_ci close(fd); 362570af302Sopenharmony_ci} 363570af302Sopenharmony_ci 364570af302Sopenharmony_ci/** 365570af302Sopenharmony_ci * @tc.name : mmap_1100 366570af302Sopenharmony_ci * @tc.desc : The parameter is invalid, start is not an integer multiple of the page, 367570af302Sopenharmony_ci * and the file content cannot be mapped into memory 368570af302Sopenharmony_ci * @tc.level : Level 1 369570af302Sopenharmony_ci */ 370570af302Sopenharmony_civoid mmap_1100(void) 371570af302Sopenharmony_ci{ 372570af302Sopenharmony_ci int fd, mm; 373570af302Sopenharmony_ci void *start; 374570af302Sopenharmony_ci const char *ptr = "/data/test.txt"; 375570af302Sopenharmony_ci static char str[] = "this is a sample!"; 376570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w+"); 377570af302Sopenharmony_ci EXPECT_PTRNE("mmap_1100", fptr, NULL); 378570af302Sopenharmony_ci 379570af302Sopenharmony_ci struct stat statbuff; 380570af302Sopenharmony_ci struct stat sb; 381570af302Sopenharmony_ci fwrite(str, sizeof(char), strlen(str), fptr); 382570af302Sopenharmony_ci fseek(fptr, 0L, SEEK_SET); 383570af302Sopenharmony_ci EXPECT_TRUE("mmap_1100", fgetc(fptr) != EOF); 384570af302Sopenharmony_ci 385570af302Sopenharmony_ci int back = stat(ptr, &statbuff); 386570af302Sopenharmony_ci fclose(fptr); 387570af302Sopenharmony_ci fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777); 388570af302Sopenharmony_ci EXPECT_NE("mmap_1100", fd, -1); 389570af302Sopenharmony_ci 390570af302Sopenharmony_ci int get = getpagesize() - 1; 391570af302Sopenharmony_ci start = mmap((void *)(&get), statbuff.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_DENYWRITE, fd, 0); 392570af302Sopenharmony_ci EXPECT_EQ("mmap_1100", back, SUCCESS); 393570af302Sopenharmony_ci EXPECT_TRUE("mmap_1100", start != MAP_FAILED); 394570af302Sopenharmony_ci mm = munmap(start, statbuff.st_size); 395570af302Sopenharmony_ci EXPECT_EQ("mmap_1100", mm, SUCCESS); 396570af302Sopenharmony_ci remove(ptr); 397570af302Sopenharmony_ci remove("/data/test.txt"); 398570af302Sopenharmony_ci fptr = NULL; 399570af302Sopenharmony_ci ptr = NULL; 400570af302Sopenharmony_ci close(fd); 401570af302Sopenharmony_ci} 402570af302Sopenharmony_ci 403570af302Sopenharmony_ciint main(int argc, char *argv[]) 404570af302Sopenharmony_ci{ 405570af302Sopenharmony_ci mmap_0100(); 406570af302Sopenharmony_ci mmap_0200(); 407570af302Sopenharmony_ci mmap_0300(); 408570af302Sopenharmony_ci mmap_0400(); 409570af302Sopenharmony_ci mmap_0500(); 410570af302Sopenharmony_ci mmap_0600(); 411570af302Sopenharmony_ci mmap_0700(); 412570af302Sopenharmony_ci mmap_0800(); 413570af302Sopenharmony_ci mmap_0900(); 414570af302Sopenharmony_ci mmap_1000(); 415570af302Sopenharmony_ci mmap_1100(); 416570af302Sopenharmony_ci 417570af302Sopenharmony_ci return t_status; 418570af302Sopenharmony_ci} 419