• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 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 "ActsPMSTest.h"
17 
18 using namespace std;
19 using namespace testing::ext;
20 
21 static PmsInnerApi *g_interface;
22 static PermissionTrans g_systemPers[] = {
23     {
24         "ohos.permission.CAMERA",
25         "for CAMERA use",
26         INUSE,
27     },
28     {
29         "ohos.permission.RECORD_AUDIO",
30         "for RECORD_AUDIO use",
31         ALWAYS,
32     },
33     {
34         "ohos.permission.READ_MEDIA_AUDIO",
35         "for READ_MEDIA_AUDIO use",
36         INUSE,
37     },
38     {
39         "ohos.permission.READ_MEDIA_IMAGES",
40         "for READ_MEDIA_IMAGES use",
41         ALWAYS,
42     },
43     {
44         "ohos.permission.READ_MEDIA_VIDEO",
45         "for READ_MEDIA_VIDEO use",
46         INUSE,
47     },
48     {
49         "ohos.permission.WRITE_MEDIA_AUDIO",
50         "for WRITE_MEDIA_AUDIO use",
51         ALWAYS,
52     },
53     {
54         "ohos.permission.WRITE_MEDIA_IMAGES",
55         "for WRITE_MEDIA_IMAGES use",
56         INUSE,
57     },
58     {
59         "ohos.permission.WRITE_MEDIA_VIDEO",
60         "for WRITE_MEDIA_VIDEO use",
61         ALWAYS,
62     },
63     {
64         "ohos.permission.MODIFY_AUDIO_SETTINGS",
65         "for MODIFY_AUDIO_SETTINGS use",
66         INUSE,
67     },
68 };
69 
70 class ActsPMSCheckTest : public testing::Test {
71 protected:
SetUpTestCase(void)72     static void SetUpTestCase(void)
73     {
74         CreateAppDir();
75         IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(PERMISSION_SERVICE, PERM_INNER);
76         iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **) &g_interface);
77         setuid(0);
78     }
TearDownTestCase(void)79     static void TearDownTestCase(void) {}
SetUp()80     virtual void SetUp() {}
TearDown()81     virtual void TearDown()
82     {
83         DeletePermissions(TEST_APP_ID);
84         DeletePermissions(TEST_APP_ID2);
85         DeletePermissions(SUBTEST_APP_ID);
86         UnLoadPermissions(TEST_TASKID);
87         UnLoadPermissions(SUBTEST_TASKID);
88     }
89 };
90 
91 /**
92  * @tc.number     SUB_SEC_AppSEC_PermissionMgmt_PMS_1390
93  * @tc.name       check permissions to large and small PIDs
94  * @tc.desc       [C- SOFTWARE -0200]
95  */
96 HWTEST_F(ActsPMSCheckTest, testSecPMPMS_1390, Function | MediumTest | Level2)
97 {
98     SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);
99     LoadPermissions(TEST_APP_ID, MAX_PID);
100     g_interface->GrantRuntimePermission(MAX_PID, g_systemPers[0].name);
101     int ret = g_interface->CheckPermission(MAX_PID, g_systemPers[0].name);
102     EXPECT_EQ(ret, GRANTED) << "checkperm ret = " << ret << endl;
103     UnLoadPermissions(MAX_PID);
104     LoadPermissions(TEST_APP_ID, MIN_PID);
105     g_interface->GrantRuntimePermission(MIN_PID, g_systemPers[1].name);
106     ret = g_interface->CheckPermission(MIN_PID, g_systemPers[1].name);
107     EXPECT_EQ(ret, GRANTED) << "checkperm ret = " << ret << endl;
108     UnLoadPermissions(MIN_PID);
109     DeletePermissions(TEST_APP_ID);
110 }
111 
112 /**
113  * @tc.number     SUB_SEC_AppSEC_PermissionMgmt_PMS_1400
114  * @tc.name       check system_grant permissions
115  * @tc.desc       [C- SECURITY -1100]
116  */
117 HWTEST_F(ActsPMSCheckTest, testSecPMPMS_1400, Function | MediumTest | Level0)
118 {
119     SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);
120     LoadPermissions(TEST_APP_ID, TEST_TASKID);
121     g_interface->GrantRuntimePermission(TEST_TASKID, g_systemPers[SYS_PERM_NUM - 1].name);
122     int ret = g_interface->CheckPermission(TEST_TASKID, g_systemPers[SYS_PERM_NUM - 1].name);
123     EXPECT_EQ(ret, GRANTED) << "checkperm ret = " << ret << endl;
124     UnLoadPermissions(TEST_TASKID);
125     DeletePermissions(TEST_APP_ID);
126 }
127 
128 /**
129  * @tc.number     SUB_SEC_AppSEC_PermissionMgmt_PMS_1410
130  * @tc.name       check granted user_grant permissions
131  * @tc.desc       [C- SECURITY -1100]
132  */
133 HWTEST_F(ActsPMSCheckTest, testSecPMPMS_1410, Function | MediumTest | Level0)
134 {
135     SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);
136     LoadPermissions(TEST_APP_ID, TEST_TASKID);
137     g_interface->GrantRuntimePermission(TEST_TASKID, g_systemPers[0].name);
138     int ret = g_interface->CheckPermission(TEST_TASKID, g_systemPers[0].name);
139     EXPECT_EQ(ret, GRANTED) << "checkperm ret = " << ret << endl;
140     UnLoadPermissions(TEST_TASKID);
141     DeletePermissions(TEST_APP_ID);
142 }
143 
144 /**
145  * @tc.number     SUB_SEC_AppSEC_PermissionMgmt_PMS_1420
146  * @tc.name       check user_grant permissions
147  * @tc.desc       [C- SECURITY -1100]
148  */
149 HWTEST_F(ActsPMSCheckTest, testSecPMPMS_1420, Function | MediumTest | Level0)
150 {
151     SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);
152     LoadPermissions(TEST_APP_ID, TEST_TASKID);
153     int ret = g_interface->CheckPermission(TEST_TASKID, g_systemPers[0].name);
154     EXPECT_EQ(ret, NOT_GRANTED) << "checkperm ret = " << ret << endl;
155     UnLoadPermissions(TEST_TASKID);
156     DeletePermissions(TEST_APP_ID);
157 }