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 <errno.h>
17 #include <stdlib.h>
18 #include <sys/mman.h>
19 #include "functionalext.h"
20
21 #define TEST_MS_SIZE 1024
22
23 /**
24 * @tc.name : msync_0100
25 * @tc.desc : Synchronize a file with a memory map
26 * @tc.level : Level 0
27 */
msync_0100(void)28 void msync_0100(void)
29 {
30 FILE *fp = tmpfile();
31 if (!fp) {
32 EXPECT_PTRNE("msync_0100", fp, NULL);
33 return;
34 }
35
36 int fd = fileno(fp);
37 if (fd == -1) {
38 fclose(fp);
39 EXPECT_TRUE("msync_0100", fd != -1);
40 return;
41 }
42 void *map = mmap(NULL, TEST_MS_SIZE, PROT_READ, MAP_SHARED, fd, 0);
43 if (!map) {
44 EXPECT_PTRNE("msync_0100", map, NULL);
45 return;
46 }
47 int rev = msync(map, TEST_MS_SIZE, MS_ASYNC);
48 EXPECT_EQ("msync_0100", rev, 0);
49 rev = munmap(map, TEST_MS_SIZE);
50 EXPECT_EQ("msync_0100", rev, 0);
51 rev = fclose(fp);
52 EXPECT_EQ("msync_0100", rev, 0);
53 }
54
main(void)55 int main(void)
56 {
57 msync_0100();
58 return t_status;
59 }