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 <stdlib.h>
17#include <sys/mman.h>
18#include "functionalext.h"
19
20const int SUCCESS = 0;
21const int FAILED = -1;
22
23/**
24 * @tc.name      : mprotect_0100
25 * @tc.desc      : The port parameter is PROT_READ,which can set the protection attribute of memory area.
26 * @tc.level     : Level 0
27 */
28void mprotect_0100(void)
29{
30    size_t align = getpagesize();
31    void *buffer = memalign(align, 8 * align);
32    int ret = mprotect(buffer, getpagesize(), PROT_READ);
33    EXPECT_EQ("mprotect_0100", ret, SUCCESS);
34}
35
36/**
37 * @tc.name      : mprotect_0200
38 * @tc.desc      : The port parameter is PROT_WRITE,which can set the protection attribute of memory area.
39 * @tc.level     : Level 0
40 */
41void mprotect_0200(void)
42{
43    size_t align = getpagesize();
44    void *buffer = memalign(align, 8 * align);
45    int ret = mprotect(buffer, getpagesize(), PROT_WRITE);
46    EXPECT_EQ("mprotect_0200", ret, SUCCESS);
47}
48
49/**
50 * @tc.name      : mprotect_0300
51 * @tc.desc      : The port parameter is PROT_EXEC,which can set the protection attribute of memory area.
52 * @tc.level     : Level 0
53 */
54void mprotect_0300(void)
55{
56    size_t align = getpagesize();
57    void *buffer = memalign(align, 8 * align);
58    int ret = mprotect(buffer, getpagesize(), PROT_EXEC);
59    EXPECT_EQ("mprotect_0300", ret, SUCCESS);
60}
61
62/**
63 * @tc.name      : mprotect_0400
64 * @tc.desc      : The port parameter is PROT_NONE,which can set the protection attribute of memory area.
65 * @tc.level     : Level 0
66 */
67void mprotect_0400(void)
68{
69    size_t align = getpagesize();
70    void *buffer = memalign(align, 8 * align);
71    int ret = mprotect(buffer, getpagesize(), PROT_NONE);
72    EXPECT_EQ("mprotect_0400", ret, SUCCESS);
73}
74
75/**
76 * @tc.name      : mprotect_0500
77 * @tc.desc      : The port parameter is PROT_READ|PROT_WRITE,which can set the protection attribute of memory area.
78 * @tc.level     : Level 1
79 */
80void mprotect_0500(void)
81{
82    size_t align = getpagesize();
83    void *buffer = memalign(align, 8 * align);
84    int ret = mprotect(buffer, getpagesize(), PROT_READ | PROT_WRITE);
85    EXPECT_EQ("mprotect_0500", ret, SUCCESS);
86}
87
88/**
89 * @tc.name      : mprotect_0600
90 * @tc.desc      : The start parameter is not an integer multiple of the page,
91 *                 and the protection attribute of the memory area cannot be set.
92 * @tc.level     : Level 2
93 */
94void mprotect_0600(void)
95{
96    int ret = mprotect((void *)0x0001, getpagesize(), PROT_READ);
97    EXPECT_EQ("mprotect_0600", ret, FAILED);
98}
99
100int main(int argc, char *argv[])
101{
102    mprotect_0100();
103    mprotect_0200();
104    mprotect_0300();
105    mprotect_0400();
106    mprotect_0500();
107    mprotect_0600();
108
109    return t_status;
110}