1f6603c60Sopenharmony_ci/**
2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci#include "ActsPMSTest.h"
17f6603c60Sopenharmony_ci
18f6603c60Sopenharmony_ciusing namespace std;
19f6603c60Sopenharmony_ciusing namespace testing::ext;
20f6603c60Sopenharmony_ci
21f6603c60Sopenharmony_cistatic PmsInnerApi *g_interface;
22f6603c60Sopenharmony_cistatic PermissionTrans g_systemPers[] = {
23f6603c60Sopenharmony_ci    {
24f6603c60Sopenharmony_ci        "ohos.permission.CAMERA",
25f6603c60Sopenharmony_ci        "for CAMERA use",
26f6603c60Sopenharmony_ci        INUSE,
27f6603c60Sopenharmony_ci    },
28f6603c60Sopenharmony_ci    {
29f6603c60Sopenharmony_ci        "ohos.permission.RECORD_AUDIO",
30f6603c60Sopenharmony_ci        "for RECORD_AUDIO use",
31f6603c60Sopenharmony_ci        ALWAYS,
32f6603c60Sopenharmony_ci    },
33f6603c60Sopenharmony_ci    {
34f6603c60Sopenharmony_ci        "ohos.permission.READ_MEDIA_AUDIO",
35f6603c60Sopenharmony_ci        "for READ_MEDIA_AUDIO use",
36f6603c60Sopenharmony_ci        INUSE,
37f6603c60Sopenharmony_ci    },
38f6603c60Sopenharmony_ci    {
39f6603c60Sopenharmony_ci        "ohos.permission.READ_MEDIA_IMAGES",
40f6603c60Sopenharmony_ci        "for READ_MEDIA_IMAGES use",
41f6603c60Sopenharmony_ci        ALWAYS,
42f6603c60Sopenharmony_ci    },
43f6603c60Sopenharmony_ci    {
44f6603c60Sopenharmony_ci        "ohos.permission.READ_MEDIA_VIDEO",
45f6603c60Sopenharmony_ci        "for READ_MEDIA_VIDEO use",
46f6603c60Sopenharmony_ci        INUSE,
47f6603c60Sopenharmony_ci    },
48f6603c60Sopenharmony_ci    {
49f6603c60Sopenharmony_ci        "ohos.permission.WRITE_MEDIA_AUDIO",
50f6603c60Sopenharmony_ci        "for WRITE_MEDIA_AUDIO use",
51f6603c60Sopenharmony_ci        ALWAYS,
52f6603c60Sopenharmony_ci    },
53f6603c60Sopenharmony_ci    {
54f6603c60Sopenharmony_ci        "ohos.permission.WRITE_MEDIA_IMAGES",
55f6603c60Sopenharmony_ci        "for WRITE_MEDIA_IMAGES use",
56f6603c60Sopenharmony_ci        INUSE,
57f6603c60Sopenharmony_ci    },
58f6603c60Sopenharmony_ci    {
59f6603c60Sopenharmony_ci        "ohos.permission.WRITE_MEDIA_VIDEO",
60f6603c60Sopenharmony_ci        "for WRITE_MEDIA_VIDEO use",
61f6603c60Sopenharmony_ci        ALWAYS,
62f6603c60Sopenharmony_ci    },
63f6603c60Sopenharmony_ci    {
64f6603c60Sopenharmony_ci        "ohos.permission.MODIFY_AUDIO_SETTINGS",
65f6603c60Sopenharmony_ci        "for MODIFY_AUDIO_SETTINGS use",
66f6603c60Sopenharmony_ci        INUSE,
67f6603c60Sopenharmony_ci    },
68f6603c60Sopenharmony_ci};
69f6603c60Sopenharmony_ci
70f6603c60Sopenharmony_ciclass ActsPMSCheckTest : public testing::Test {
71f6603c60Sopenharmony_ciprotected:
72f6603c60Sopenharmony_ci    static void SetUpTestCase(void)
73f6603c60Sopenharmony_ci    {
74f6603c60Sopenharmony_ci        CreateAppDir();
75f6603c60Sopenharmony_ci        IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(PERMISSION_SERVICE, PERM_INNER);
76f6603c60Sopenharmony_ci        iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **) &g_interface);
77f6603c60Sopenharmony_ci        setuid(0);
78f6603c60Sopenharmony_ci    }
79f6603c60Sopenharmony_ci    static void TearDownTestCase(void) {}
80f6603c60Sopenharmony_ci    virtual void SetUp() {}
81f6603c60Sopenharmony_ci    virtual void TearDown()
82f6603c60Sopenharmony_ci    {
83f6603c60Sopenharmony_ci        DeletePermissions(TEST_APP_ID);
84f6603c60Sopenharmony_ci        DeletePermissions(TEST_APP_ID2);
85f6603c60Sopenharmony_ci        DeletePermissions(SUBTEST_APP_ID);
86f6603c60Sopenharmony_ci        UnLoadPermissions(TEST_TASKID);
87f6603c60Sopenharmony_ci        UnLoadPermissions(SUBTEST_TASKID);
88f6603c60Sopenharmony_ci    }
89f6603c60Sopenharmony_ci};
90f6603c60Sopenharmony_ci
91f6603c60Sopenharmony_ci/**
92f6603c60Sopenharmony_ci * @tc.number     SUB_SEC_AppSEC_PermissionMgmt_PMS_1390
93f6603c60Sopenharmony_ci * @tc.name       check permissions to large and small PIDs
94f6603c60Sopenharmony_ci * @tc.desc       [C- SOFTWARE -0200]
95f6603c60Sopenharmony_ci */
96f6603c60Sopenharmony_ciHWTEST_F(ActsPMSCheckTest, testSecPMPMS_1390, Function | MediumTest | Level2)
97f6603c60Sopenharmony_ci{
98f6603c60Sopenharmony_ci    SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);
99f6603c60Sopenharmony_ci    LoadPermissions(TEST_APP_ID, MAX_PID);
100f6603c60Sopenharmony_ci    g_interface->GrantRuntimePermission(MAX_PID, g_systemPers[0].name);
101f6603c60Sopenharmony_ci    int ret = g_interface->CheckPermission(MAX_PID, g_systemPers[0].name);
102f6603c60Sopenharmony_ci    EXPECT_EQ(ret, GRANTED) << "checkperm ret = " << ret << endl;
103f6603c60Sopenharmony_ci    UnLoadPermissions(MAX_PID);
104f6603c60Sopenharmony_ci    LoadPermissions(TEST_APP_ID, MIN_PID);
105f6603c60Sopenharmony_ci    g_interface->GrantRuntimePermission(MIN_PID, g_systemPers[1].name);
106f6603c60Sopenharmony_ci    ret = g_interface->CheckPermission(MIN_PID, g_systemPers[1].name);
107f6603c60Sopenharmony_ci    EXPECT_EQ(ret, GRANTED) << "checkperm ret = " << ret << endl;
108f6603c60Sopenharmony_ci    UnLoadPermissions(MIN_PID);
109f6603c60Sopenharmony_ci    DeletePermissions(TEST_APP_ID);
110f6603c60Sopenharmony_ci}
111f6603c60Sopenharmony_ci
112f6603c60Sopenharmony_ci/**
113f6603c60Sopenharmony_ci * @tc.number     SUB_SEC_AppSEC_PermissionMgmt_PMS_1400
114f6603c60Sopenharmony_ci * @tc.name       check system_grant permissions
115f6603c60Sopenharmony_ci * @tc.desc       [C- SECURITY -1100]
116f6603c60Sopenharmony_ci */
117f6603c60Sopenharmony_ciHWTEST_F(ActsPMSCheckTest, testSecPMPMS_1400, Function | MediumTest | Level0)
118f6603c60Sopenharmony_ci{
119f6603c60Sopenharmony_ci    SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);
120f6603c60Sopenharmony_ci    LoadPermissions(TEST_APP_ID, TEST_TASKID);
121f6603c60Sopenharmony_ci    g_interface->GrantRuntimePermission(TEST_TASKID, g_systemPers[SYS_PERM_NUM - 1].name);
122f6603c60Sopenharmony_ci    int ret = g_interface->CheckPermission(TEST_TASKID, g_systemPers[SYS_PERM_NUM - 1].name);
123f6603c60Sopenharmony_ci    EXPECT_EQ(ret, GRANTED) << "checkperm ret = " << ret << endl;
124f6603c60Sopenharmony_ci    UnLoadPermissions(TEST_TASKID);
125f6603c60Sopenharmony_ci    DeletePermissions(TEST_APP_ID);
126f6603c60Sopenharmony_ci}
127f6603c60Sopenharmony_ci
128f6603c60Sopenharmony_ci/**
129f6603c60Sopenharmony_ci * @tc.number     SUB_SEC_AppSEC_PermissionMgmt_PMS_1410
130f6603c60Sopenharmony_ci * @tc.name       check granted user_grant permissions
131f6603c60Sopenharmony_ci * @tc.desc       [C- SECURITY -1100]
132f6603c60Sopenharmony_ci */
133f6603c60Sopenharmony_ciHWTEST_F(ActsPMSCheckTest, testSecPMPMS_1410, Function | MediumTest | Level0)
134f6603c60Sopenharmony_ci{
135f6603c60Sopenharmony_ci    SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);
136f6603c60Sopenharmony_ci    LoadPermissions(TEST_APP_ID, TEST_TASKID);
137f6603c60Sopenharmony_ci    g_interface->GrantRuntimePermission(TEST_TASKID, g_systemPers[0].name);
138f6603c60Sopenharmony_ci    int ret = g_interface->CheckPermission(TEST_TASKID, g_systemPers[0].name);
139f6603c60Sopenharmony_ci    EXPECT_EQ(ret, GRANTED) << "checkperm ret = " << ret << endl;
140f6603c60Sopenharmony_ci    UnLoadPermissions(TEST_TASKID);
141f6603c60Sopenharmony_ci    DeletePermissions(TEST_APP_ID);
142f6603c60Sopenharmony_ci}
143f6603c60Sopenharmony_ci
144f6603c60Sopenharmony_ci/**
145f6603c60Sopenharmony_ci * @tc.number     SUB_SEC_AppSEC_PermissionMgmt_PMS_1420
146f6603c60Sopenharmony_ci * @tc.name       check user_grant permissions
147f6603c60Sopenharmony_ci * @tc.desc       [C- SECURITY -1100]
148f6603c60Sopenharmony_ci */
149f6603c60Sopenharmony_ciHWTEST_F(ActsPMSCheckTest, testSecPMPMS_1420, Function | MediumTest | Level0)
150f6603c60Sopenharmony_ci{
151f6603c60Sopenharmony_ci    SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);
152f6603c60Sopenharmony_ci    LoadPermissions(TEST_APP_ID, TEST_TASKID);
153f6603c60Sopenharmony_ci    int ret = g_interface->CheckPermission(TEST_TASKID, g_systemPers[0].name);
154f6603c60Sopenharmony_ci    EXPECT_EQ(ret, NOT_GRANTED) << "checkperm ret = " << ret << endl;
155f6603c60Sopenharmony_ci    UnLoadPermissions(TEST_TASKID);
156f6603c60Sopenharmony_ci    DeletePermissions(TEST_APP_ID);
157f6603c60Sopenharmony_ci}