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_M_SIZE 1024
22#define TEST_M_NEW_SIZE 2048
23
24/**
25 * @tc.name      : mremap_0100
26 * @tc.desc      : Modify the size of the memory map
27 * @tc.level     : Level 0
28 */
29void mremap_0100(void)
30{
31    FILE *fp = tmpfile();
32    if (!fp) {
33        EXPECT_PTRNE("mremap_0100", fp, NULL);
34        return;
35    }
36
37    int fd = fileno(fp);
38    if (fd == -1) {
39        fclose(fp);
40        EXPECT_TRUE("mremap_0100", fd != -1);
41        return;
42    }
43    void *map = mmap(NULL, TEST_M_SIZE, PROT_READ, MAP_SHARED, fd, 0);
44    if (!map) {
45        EXPECT_PTRNE("mremap_0100", map, NULL);
46        return;
47    }
48    void *map_new = mremap(map, TEST_M_SIZE, TEST_M_NEW_SIZE, MREMAP_MAYMOVE);
49    EXPECT_PTREQ("mremap_0100", map_new, map);
50    int rev = munmap(map_new, TEST_M_NEW_SIZE);
51    EXPECT_EQ("mremap_0100", rev, 0);
52    rev = fclose(fp);
53    EXPECT_EQ("mremap_0100", rev, 0);
54}
55
56/**
57 * @tc.name      : mremap_0200
58 * @tc.desc      : Modify the size of the memory map with the maximum value, failed
59 * @tc.level     : Level 2
60 */
61void mremap_0200(void)
62{
63    FILE *fp = tmpfile();
64    if (!fp) {
65        EXPECT_PTRNE("mremap_0200", fp, NULL);
66        return;
67    }
68
69    int fd = fileno(fp);
70    if (fd == -1) {
71        fclose(fp);
72        EXPECT_TRUE("mremap_0200", fd != -1);
73        return;
74    }
75
76    void *map = mmap(NULL, TEST_M_SIZE, PROT_READ, MAP_SHARED, fd, 0);
77    if (!map) {
78        EXPECT_PTRNE("mremap_0200", map, NULL);
79        return;
80    }
81    void *map_new = mremap(map, TEST_M_SIZE, PTRDIFF_MAX, MREMAP_MAYMOVE);
82    EXPECT_PTRNE("mremap_0200", map_new, map);
83    int rev = munmap(map, TEST_M_SIZE);
84    EXPECT_EQ("mremap_0200", rev, 0);
85    rev = fclose(fp);
86    EXPECT_EQ("mremap_0200", rev, 0);
87}
88
89int main(void)
90{
91    mremap_0100();
92    mremap_0200();
93    return t_status;
94}