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 <sys/mman.h>
17#include <stdlib.h>
18#include "functionalext.h"
19
20/**
21 * @tc.name      : mlock2_0100
22 * @tc.desc      : Lock 4k memory, release memory after unlocking
23 * @tc.level     : Level 0
24 */
25void mlock2_0100(void)
26{
27    size_t memsize = 4096;
28    char *memory = (char *)malloc(memsize);
29    if (memory == NULL) {
30        EXPECT_PTRNE("mlock2_0100", memory, NULL);
31        return;
32    }
33    memset(memory, '\0', memsize);
34    int ret = mlock2(memory, memsize, CMPFLAG);
35    EXPECT_EQ("mlock2_0100", ret, CMPFLAG);
36
37    munlock(memory, memsize);
38    free(memory);
39    memory = NULL;
40}
41
42/**
43 * @tc.name      : mlock2_0200
44 * @tc.desc      : Lock 1k memory and mark all 4k memory size, release memory after unlocking
45 * @tc.level     : Level 0
46 */
47void mlock2_0200(void)
48{
49    size_t memsize = 4096;
50    size_t memchip = 1024;
51    char *memory = (char *)malloc(memsize);
52    if (memory == NULL) {
53        EXPECT_PTRNE("mlock2_0200", memory, NULL);
54        return;
55    }
56    memset(memory, '\0', memsize);
57    int ret = mlock2(memory, memchip, MLOCK_ONFAULT);
58    EXPECT_EQ("mlock2_0200", ret, CMPFLAG);
59
60    munlock(memory, memsize);
61    free(memory);
62    memory = NULL;
63}
64
65int main(void)
66{
67    mlock2_0100();
68    mlock2_0200();
69    return t_status;
70}
71