• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *   http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <sys/mman.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include "functionalext.h"
22 
23 const int EOK = 0;
24 const int SUCCESS = 0;
25 const int FAILED = -1;
26 #define OFF_MASK ((-0x2000ULL << (8 * sizeof(long) - 1)) | (4096ULL - 1))
27 
28 /**
29  * @tc.name      : mmap_0100
30  * @tc.desc      : The parameters are valid, prot is PROT_READ,
31  *                 and flags is MAP_PRIVATE, which can map the file content into memory
32  * @tc.level     : Level 0
33  */
mmap_0100(void)34 void mmap_0100(void)
35 {
36     int fd, mm;
37     void *start;
38     const char *ptr = "/data/test.txt";
39     static char str[] = "this is a sample!";
40     FILE *fptr = fopen(ptr, "w+");
41     EXPECT_PTRNE("mmap_0100", fptr, NULL);
42 
43     struct stat statbuff;
44     fwrite(str, sizeof(char), strlen(str), fptr);
45     fseek(fptr, 0L, SEEK_SET);
46     EXPECT_TRUE("mmap_0100", fgetc(fptr) != EOF);
47     int back = stat(ptr, &statbuff);
48     fclose(fptr);
49 
50     fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777);
51     EXPECT_NE("mmap_0100", fd, -1);
52     start = mmap(NULL, statbuff.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
53     EXPECT_EQ("mmap_0100", back, SUCCESS);
54     EXPECT_TRUE("mmap_0100", start != MAP_FAILED);
55 
56     mm = munmap(start, statbuff.st_size);
57     EXPECT_EQ("mmap_0100", mm, SUCCESS);
58     close(fd);
59     remove(ptr);
60     remove("/data/test.txt");
61     fptr = NULL;
62     ptr = NULL;
63 }
64 
65 /**
66  * @tc.name      : mmap_0200
67  * @tc.desc      : The parameters are valid, prot is PROT_READ|PROT_WRITE,
68  *                 and flags is MAP_SHARED, which can map the file content into memory
69  * @tc.level     : Level 0
70  */
mmap_0200(void)71 void mmap_0200(void)
72 {
73     int fd, mm;
74     void *start;
75     const char *ptr = "/data/test.txt";
76     static char str[] = "this is a sample!";
77     FILE *fptr = fopen(ptr, "w+");
78     EXPECT_PTRNE("mmap_0200", fptr, NULL);
79 
80     struct stat statbuff;
81     struct stat sb;
82     fwrite(str, sizeof(char), strlen(str), fptr);
83     fseek(fptr, 0L, SEEK_SET);
84     EXPECT_TRUE("mmap_0200", fgetc(fptr) != EOF);
85 
86     int back = stat(ptr, &statbuff);
87     fclose(fptr);
88     fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777);
89     EXPECT_NE("mmap_0200", fd, -1);
90     start = mmap(NULL, statbuff.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
91     EXPECT_EQ("mmap_0200", back, SUCCESS);
92     EXPECT_TRUE("mmap_0200", start != MAP_FAILED);
93 
94     mm = munmap(start, statbuff.st_size);
95     EXPECT_EQ("mmap_0200", mm, SUCCESS);
96     close(fd);
97     remove(ptr);
98     remove("/data/test.txt");
99     fptr = NULL;
100     ptr = NULL;
101 }
102 
103 /**
104  * @tc.name      : mmap_0300
105  * @tc.desc      : The parameters are valid, prot is PROT_READ|PROT_WRITE, and flags is MAP_SHARED|MAP_ANON,
106  *                 which can map the file content into memory
107  * @tc.level     : Level 1
108  */
mmap_0300(void)109 void mmap_0300(void)
110 {
111     void *start;
112     int mm;
113     start = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
114     EXPECT_TRUE("mmap_0300", start != MAP_FAILED);
115     mm = munmap(start, getpagesize());
116     EXPECT_EQ("mmap_0300", mm, SUCCESS);
117 }
118 
119 /**
120  * @tc.name      : mmap_0400
121  * @tc.desc      : The parameters are valid, prot is PROT_EXEC, and flags is MAP_LOCKED,
122  *                 which can map the file content into memory
123  * @tc.level     : Level 1
124  */
mmap_0400(void)125 void mmap_0400(void)
126 {
127     int fd, mm;
128     void *start;
129     const char *ptr = "/data/test.txt";
130     static char str[] = "this is a sample!";
131     FILE *fptr = fopen(ptr, "w+");
132     EXPECT_PTRNE("mmap_0400", fptr, NULL);
133 
134     struct stat statbuff;
135     struct stat sb;
136     fwrite(str, sizeof(char), strlen(str), fptr);
137     fseek(fptr, 0L, SEEK_SET);
138     EXPECT_TRUE("mmap_0400", fgetc(fptr) != EOF);
139 
140     int back = stat(ptr, &statbuff);
141     fclose(fptr);
142     fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777);
143     EXPECT_NE("mmap_0400", fd, -1);
144     start = mmap(NULL, statbuff.st_size, PROT_EXEC, MAP_PRIVATE | MAP_LOCKED, fd, 0);
145     EXPECT_EQ("mmap_0400", back, SUCCESS);
146     EXPECT_TRUE("mmap_0400", start != MAP_FAILED);
147     mm = munmap(start, statbuff.st_size);
148     EXPECT_EQ("mmap_0400", mm, SUCCESS);
149     remove(ptr);
150     remove("/data/test.txt");
151     fptr = NULL;
152     ptr = NULL;
153     close(fd);
154 }
155 
156 /**
157  * @tc.name      : mmap_0500
158  * @tc.desc      : The parameters are valid, prot is PROT_READ|PROT_WRITE, and flags is MAP_PRIVATE|MAP_DENYWRITE,
159  *                 which can map the file content into memory
160  * @tc.level     : Level 1
161  */
mmap_0500(void)162 void mmap_0500(void)
163 {
164     int fd, mm;
165     void *start;
166     const char *ptr = "/data/test.txt";
167     static char str[] = "this is a sample!";
168     FILE *fptr = fopen(ptr, "w+");
169     EXPECT_PTRNE("mmap_0500", fptr, NULL);
170 
171     struct stat statbuff;
172     struct stat sb;
173     fwrite(str, sizeof(char), strlen(str), fptr);
174     fseek(fptr, 0L, SEEK_SET);
175     EXPECT_TRUE("mmap_0500", fgetc(fptr) != EOF);
176 
177     int back = stat(ptr, &statbuff);
178     fclose(fptr);
179     fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777);
180     EXPECT_NE("mmap_0500", fd, -1);
181 
182     start = mmap(NULL, statbuff.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_DENYWRITE, fd, 0);
183     EXPECT_EQ("mmap_0500", back, SUCCESS);
184     EXPECT_TRUE("mmap_0500", start != MAP_FAILED);
185     mm = munmap(start, statbuff.st_size);
186     EXPECT_EQ("mmap_0500", mm, SUCCESS);
187     remove(ptr);
188     remove("/data/test.txt");
189     fptr = NULL;
190     ptr = NULL;
191     close(fd);
192 }
193 
194 /**
195  * @tc.name      : mmap_0600
196  * @tc.desc      : The parameters are valid, prot is PROT_NONE, and flags is MAP_PRIVATE|MAP_FIXED,
197  *                 which can map the file content into memory
198  * @tc.level     : Level 2
199  */
mmap_0600(void)200 void mmap_0600(void)
201 {
202     int fd, mm;
203     void *start;
204     const char *ptr = "/data/test.txt";
205     static char str[] = "this is a sample!";
206     FILE *fptr = fopen(ptr, "w+");
207     EXPECT_PTRNE("mmap_0600", fptr, NULL);
208 
209     struct stat statbuff;
210     struct stat sb;
211     fwrite(str, sizeof(char), strlen(str), fptr);
212     fseek(fptr, 0L, SEEK_SET);
213     EXPECT_TRUE("mmap_0600", fgetc(fptr) != EOF);
214 
215     int back = stat(ptr, &statbuff);
216     fclose(fptr);
217     fd = open("/data/test.txt", O_RDONLY);
218     EXPECT_NE("mmap_0600", fd, -1);
219     start = mmap(NULL, statbuff.st_size, PROT_NONE, MAP_PRIVATE | MAP_FIXED, -1, 0);
220     EXPECT_EQ("mmap_0600", back, SUCCESS);
221     EXPECT_EQ("mmap_0600", start, MAP_FAILED);
222     remove(ptr);
223     remove("/data/test.txt");
224     fptr = NULL;
225     ptr = NULL;
226     close(fd);
227 }
228 
229 /**
230  * @tc.name      : mmap_0700
231  * @tc.desc      : The parameter is invalid, the off parameter is OFF_MASK,
232  *                 the file content cannot be mapped into memory
233  * @tc.level     : Level 2
234  */
mmap_0700(void)235 void mmap_0700(void)
236 {
237     int fd;
238     void *start;
239     const char *ptr = "/data/test.txt";
240     static char str[] = "this is a sample!";
241     FILE *fptr = fopen(ptr, "w+");
242     EXPECT_PTRNE("mmap_0700", fptr, NULL);
243 
244     struct stat statbuff;
245     struct stat sb;
246     fwrite(str, sizeof(char), strlen(str), fptr);
247     fseek(fptr, 0L, SEEK_SET);
248     EXPECT_TRUE("mmap_0700", fgetc(fptr) != EOF);
249 
250     int back = stat(ptr, &statbuff);
251     fclose(fptr);
252     fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777);
253     EXPECT_NE("mmap_0700", fd, -1);
254 
255     start = mmap(NULL, statbuff.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, OFF_MASK);
256     EXPECT_EQ("mmap_0700", back, SUCCESS);
257     EXPECT_EQ("mmap_0700", start, MAP_FAILED);
258     remove(ptr);
259     remove("/data/test.txt");
260     fptr = NULL;
261     ptr = NULL;
262     close(fd);
263 }
264 
265 /**
266  * @tc.name      : mmap_0800
267  * @tc.desc      : The parameter is invalid, the len parameter exceeds the maximum value,
268  *                 and the file content cannot be mapped into memory
269  * @tc.level     : Level 2
270  */
mmap_0800(void)271 void mmap_0800(void)
272 {
273     int fd;
274     void *start;
275     const char *ptr = "/data/test.txt";
276     static char str[] = "this is a sample!";
277     FILE *fptr = fopen(ptr, "w+");
278     EXPECT_PTRNE("mmap_0800", fptr, NULL);
279 
280     fwrite(str, sizeof(char), strlen(str), fptr);
281     fseek(fptr, 0L, SEEK_SET);
282     EXPECT_TRUE("mmap_0800", fgetc(fptr) != EOF);
283 
284     fclose(fptr);
285     fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777);
286     EXPECT_NE("mmap_0800", fd, -1);
287 
288     start = mmap(NULL, PTRDIFF_MAX + 1, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
289     EXPECT_EQ("mmap_0800", start, MAP_FAILED);
290     remove(ptr);
291     remove("/data/test.txt");
292     fptr = NULL;
293     ptr = NULL;
294     close(fd);
295 }
296 
297 /**
298  * @tc.name      : mmap_0900
299  * @tc.desc      : The parameter is invalid, the len parameter is 0, the file content cannot be mapped into memory
300  * @tc.level     : Level 2
301  */
mmap_0900(void)302 void mmap_0900(void)
303 {
304     int fd;
305     void *start;
306     const char *ptr = "/data/test.txt";
307     static char str[] = "this is a sample!";
308     FILE *fptr = fopen(ptr, "w+");
309     EXPECT_PTRNE("mmap_0900", fptr, NULL);
310 
311     fwrite(str, sizeof(char), strlen(str), fptr);
312     fseek(fptr, 0L, SEEK_SET);
313     EXPECT_TRUE("mmap_0900", fgetc(fptr) != EOF);
314 
315     fclose(fptr);
316     fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777);
317     EXPECT_NE("mmap_0900", fd, -1);
318 
319     start = mmap(NULL, 0, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
320     EXPECT_EQ("mmap_0900", start, MAP_FAILED);
321     remove(ptr);
322     remove("/data/test.txt");
323     fptr = NULL;
324     ptr = NULL;
325     close(fd);
326 }
327 
328 /**
329  * @tc.name      : mmap_1000
330  * @tc.desc      : The parameter is invalid, the flags parameter is MAP_SHARED|MAP_ANON, and the off parameter is
331  *                 not an integer multiple of the page, the file content cannot be mapped into the memory
332  * @tc.level     : Level 2
333  */
mmap_1000(void)334 void mmap_1000(void)
335 {
336     int fd;
337     void *start;
338     const char *ptr = "/data/test.txt";
339     static char str[] = "this is a sample!";
340     FILE *fptr = fopen(ptr, "w+");
341     EXPECT_PTRNE("mmap_1000", fptr, NULL);
342 
343     struct stat statbuff;
344     struct stat sb;
345     fwrite(str, sizeof(char), strlen(str), fptr);
346     fseek(fptr, 0L, SEEK_SET);
347     EXPECT_TRUE("mmap_1000", fgetc(fptr) != EOF);
348 
349     int back = stat(ptr, &statbuff);
350     fclose(fptr);
351     fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777);
352     EXPECT_NE("mmap_1000", fd, -1);
353 
354     start = mmap(NULL, statbuff.st_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, fd, getpagesize() - 1);
355     EXPECT_EQ("mmap_1000", back, SUCCESS);
356     EXPECT_EQ("mmap_1000", start, MAP_FAILED);
357     remove(ptr);
358     remove("/data/test.txt");
359     fptr = NULL;
360     ptr = NULL;
361     close(fd);
362 }
363 
364 /**
365  * @tc.name      : mmap_1100
366  * @tc.desc      : The parameter is invalid, start is not an integer multiple of the page,
367  *                 and the file content cannot be mapped into memory
368  * @tc.level     : Level 1
369  */
mmap_1100(void)370 void mmap_1100(void)
371 {
372     int fd, mm;
373     void *start;
374     const char *ptr = "/data/test.txt";
375     static char str[] = "this is a sample!";
376     FILE *fptr = fopen(ptr, "w+");
377     EXPECT_PTRNE("mmap_1100", fptr, NULL);
378 
379     struct stat statbuff;
380     struct stat sb;
381     fwrite(str, sizeof(char), strlen(str), fptr);
382     fseek(fptr, 0L, SEEK_SET);
383     EXPECT_TRUE("mmap_1100", fgetc(fptr) != EOF);
384 
385     int back = stat(ptr, &statbuff);
386     fclose(fptr);
387     fd = open("/data/test.txt", O_RDWR | O_CREAT, 0777);
388     EXPECT_NE("mmap_1100", fd, -1);
389 
390     int get = getpagesize() - 1;
391     start = mmap((void *)(&get), statbuff.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_DENYWRITE, fd, 0);
392     EXPECT_EQ("mmap_1100", back, SUCCESS);
393     EXPECT_TRUE("mmap_1100", start != MAP_FAILED);
394     mm = munmap(start, statbuff.st_size);
395     EXPECT_EQ("mmap_1100", mm, SUCCESS);
396     remove(ptr);
397     remove("/data/test.txt");
398     fptr = NULL;
399     ptr = NULL;
400     close(fd);
401 }
402 
main(int argc,char * argv[])403 int main(int argc, char *argv[])
404 {
405     mmap_0100();
406     mmap_0200();
407     mmap_0300();
408     mmap_0400();
409     mmap_0500();
410     mmap_0600();
411     mmap_0700();
412     mmap_0800();
413     mmap_0900();
414     mmap_1000();
415     mmap_1100();
416 
417     return t_status;
418 }
419