ActsPMSUpdatePermissionTest.cpp 12.4 KB
Newer Older
X
xia-liyun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ActsPMSTest.h"
X
xia-liyun 已提交
17
#include <sys/time.h>
X
xia-liyun 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

using namespace std;
using namespace testing::ext;

static PmsInnerApi *g_interface;
static PermissionTrans g_systemPers[] = {
    {
        "ohos.permission.CAMERA",
        "for CAMERA use",
        INUSE,
    },
    {
        "ohos.permission.RECORD_AUDIO",
        "for RECORD_AUDIO use",
        ALWAYS,
    },
    {
        "ohos.permission.READ_MEDIA_AUDIO",
        "for READ_MEDIA_AUDIO use",
        INUSE,
    },
    {
        "ohos.permission.READ_MEDIA_IMAGES",
        "for READ_MEDIA_IMAGES use",
        ALWAYS,
    },
    {
        "ohos.permission.READ_MEDIA_VIDEO",
        "for READ_MEDIA_VIDEO use",
        INUSE,
    },
    {
        "ohos.permission.WRITE_MEDIA_AUDIO",
        "for WRITE_MEDIA_AUDIO use",
        ALWAYS,
    },
    {
        "ohos.permission.WRITE_MEDIA_IMAGES",
        "for WRITE_MEDIA_IMAGES use",
        INUSE,
    },
    {
        "ohos.permission.WRITE_MEDIA_VIDEO",
        "for WRITE_MEDIA_VIDEO use",
        ALWAYS,
    },
    {
        "ohos.permission.MODIFY_AUDIO_SETTINGS",
        "for MODIFY_AUDIO_SETTINGS use",
        INUSE,
    },
};

X
xia-liyun 已提交
71 72
class ActsPMSUpdatePermissionTest : public testing::Test
{
X
xia-liyun 已提交
73 74 75 76 77
protected:
    static void SetUpTestCase(void)
    {
        CreateAppDir();
        IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(PERMISSION_SERVICE, PERM_INNER);
X
xia-liyun 已提交
78
        iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&g_interface);
X
xia-liyun 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        setuid(0);
    }
    static void TearDownTestCase(void) {}
    virtual void SetUp() {}
    virtual void TearDown()
    {
        DeletePermissions(TEST_APP_ID);
        DeletePermissions(TEST_APP_ID2);
        DeletePermissions(SUBTEST_APP_ID);
        UnLoadPermissions(TEST_TASKID);
        UnLoadPermissions(SUBTEST_TASKID);
    }
};

long GetCurrentTimeMillis()
{
    struct timeval current;
X
xia-liyun 已提交
96 97
    gettimeofday(&current, NULL);
    return current.tv_sec * MILLISECOND + current.tv_usec / MILLISECOND;
X
xia-liyun 已提交
98 99 100 101 102 103 104
}

/**
 * @tc.number     Security_AppSecurity_PermissionManager_L1_UpdatePermissionFlags_001
 * @tc.name       Update permission flags
 * @tc.desc       [C- SECURITY -1000]
 */
105
HWTEST_F(ActsPMSUpdatePermissionTest, testSecPMUpdatePMFlags_001, Function | MediumTest | Level2)
X
xia-liyun 已提交
106 107 108
{
    SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);

X
xia-liyun 已提交
109 110 111 112 113 114 115 116
    PermissionSaved *permissions = NULL;
    int permNum = 0;
    int ret = g_interface->QueryPermission(TEST_APP_ID, &permissions, &permNum);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "query ret=" << ret << endl;
    EXPECT_EQ(permNum, SYS_PERM_NUM) << "permNum =" << permNum << endl;
    if (permissions != NULL)
    {
        EXPECT_EQ(permissions[0].flags, PMS_FLAG_DEFAULT) << "flags =" << permissions[0].flags << endl;
X
xia-liyun 已提交
117 118 119
    }
    ClrPers(permissions);

X
xia-liyun 已提交
120 121
    ret = g_interface->UpdatePermissionFlags(TEST_APP_ID, g_systemPers[0].name, PMS_FLAG_DEFAULT);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "update perms flag ret =" << ret << endl;
X
xia-liyun 已提交
122

X
xia-liyun 已提交
123 124 125
    ret = g_interface->QueryPermission(TEST_APP_ID, &permissions, &permNum);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "query ret=" << ret << endl;
    EXPECT_EQ(permNum, SYS_PERM_NUM) << "permNum =" << permNum << endl;
X
xia-liyun 已提交
126 127 128 129 130 131 132 133 134 135 136
    ClrPers(permissions);

    UnLoadPermissions(TEST_TASKID);
    DeletePermissions(TEST_APP_ID);
}

/**
 * @tc.number     Security_AppSecurity_PermissionManager_L1_UpdatePermissionFlags_002
 * @tc.name       Update permission flags (No reminds after rejection)
 * @tc.desc       [C- SECURITY -1000]
 */
137
HWTEST_F(ActsPMSUpdatePermissionTest, testSecPMUpdatePMFlags_002, Function | MediumTest | Level2)
X
xia-liyun 已提交
138 139 140
{
    SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);

X
xia-liyun 已提交
141 142 143 144 145 146 147 148
    PermissionSaved *permissions = NULL;
    int permNum = 0;
    int ret = g_interface->QueryPermission(TEST_APP_ID, &permissions, &permNum);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "query ret=" << ret << endl;
    EXPECT_EQ(permNum, SYS_PERM_NUM) << "permNum =" << permNum << endl;
    if (permissions != NULL)
    {
        EXPECT_EQ(permissions[0].flags, PMS_FLAG_DEFAULT) << "flags =" << permissions[0].flags << endl;
X
xia-liyun 已提交
149 150 151
    }
    ClrPers(permissions);

X
xia-liyun 已提交
152 153
    ret = g_interface->UpdatePermissionFlags(TEST_APP_ID, g_systemPers[0].name, PMS_FLAG_USER_FIXED);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "update perms flag ret =" << ret << endl;
X
xia-liyun 已提交
154

X
xia-liyun 已提交
155 156 157
    ret = g_interface->QueryPermission(TEST_APP_ID, &permissions, &permNum);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "query ret=" << ret << endl;
    EXPECT_EQ(permNum, SYS_PERM_NUM) << "permNum =" << permNum << endl;
X
xia-liyun 已提交
158 159 160 161 162 163 164 165 166 167 168
    ClrPers(permissions);

    UnLoadPermissions(TEST_TASKID);
    DeletePermissions(TEST_APP_ID);
}

/**
 * @tc.number     Security_AppSecurity_PermissionManager_L1_UpdatePermissionFlags_003
 * @tc.name       Update permission flags (Name does not exist)
 * @tc.desc       [C- SECURITY -1000]
 */
169
HWTEST_F(ActsPMSUpdatePermissionTest, testSecPMUpdatePMFlags_003, Function | MediumTest | Level2)
X
xia-liyun 已提交
170 171 172
{
    SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);

X
xia-liyun 已提交
173 174 175 176 177 178 179 180
    PermissionSaved *permissions = NULL;
    int permNum = 0;
    int ret = g_interface->QueryPermission(TEST_APP_ID, &permissions, &permNum);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "query ret=" << ret << endl;
    EXPECT_EQ(permNum, SYS_PERM_NUM) << "permNum =" << permNum << endl;
    if (permissions != NULL)
    {
        EXPECT_EQ(permissions[0].flags, PMS_FLAG_DEFAULT) << "flags =" << permissions[0].flags << endl;
X
xia-liyun 已提交
181 182 183
    }
    ClrPers(permissions);

X
xia-liyun 已提交
184 185 186
    // pkg name not exist
    ret = g_interface->UpdatePermissionFlags(TEST_APP_NOT_EXIST, g_systemPers[0].name, PMS_FLAG_USER_FIXED);
    EXPECT_EQ(ret, PERM_ERRORCODE_FILE_NOT_EXIST) << "update perms flag ret =" << ret << endl;
X
xia-liyun 已提交
187

X
xia-liyun 已提交
188 189 190
    // pkg perm not exist
    ret = g_interface->UpdatePermissionFlags(TEST_APP_ID, PERMISSION_UNDEFINED, PMS_FLAG_USER_FIXED);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "update perms flag ret =" << ret << endl;
X
xia-liyun 已提交
191

X
xia-liyun 已提交
192 193 194
    // pkg flag not exist
    ret = g_interface->UpdatePermissionFlags(TEST_APP_ID, g_systemPers[0].name, FLAG_NOT_EXIST);
    EXPECT_EQ(ret, PERM_ERRORCODE_INVALID_PARAMS) << "update perms flag ret =" << ret << endl;
X
xia-liyun 已提交
195 196 197 198 199 200 201 202 203 204 205
    ClrPers(permissions);

    UnLoadPermissions(TEST_TASKID);
    DeletePermissions(TEST_APP_ID);
}

/**
 * @tc.number     Security_AppSecurity_PermissionManager_L1_UpdatePermissionFlags_004
 * @tc.name       Update permission flags (Name empty)
 * @tc.desc       [C- SECURITY -1000]
 */
206
HWTEST_F(ActsPMSUpdatePermissionTest, testSecPMUpdatePMFlags_004, Function | MediumTest | Level2)
X
xia-liyun 已提交
207 208 209
{
    SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);

X
xia-liyun 已提交
210 211 212 213 214 215 216 217
    PermissionSaved *permissions = NULL;
    int permNum = 0;
    int ret = g_interface->QueryPermission(TEST_APP_ID, &permissions, &permNum);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "query ret=" << ret << endl;
    EXPECT_EQ(permNum, SYS_PERM_NUM) << "permNum =" << permNum << endl;
    if (permissions != NULL)
    {
        EXPECT_EQ(permissions[0].flags, PMS_FLAG_DEFAULT) << "flags =" << permissions[0].flags << endl;
X
xia-liyun 已提交
218 219 220
    }
    ClrPers(permissions);

X
xia-liyun 已提交
221 222 223
    // pkg name empty
    ret = g_interface->UpdatePermissionFlags(NULL, g_systemPers[0].name, PMS_FLAG_USER_FIXED);
    EXPECT_EQ(ret, PERM_ERRORCODE_INVALID_PARAMS) << "update perms flag ret =" << ret << endl;
X
xia-liyun 已提交
224

X
xia-liyun 已提交
225 226 227
    // pkg perm empty
    ret = g_interface->UpdatePermissionFlags(TEST_APP_ID, NULL, PMS_FLAG_USER_FIXED);
    EXPECT_EQ(ret, PERM_ERRORCODE_INVALID_PARAMS) << "update perms flag ret =" << ret << endl;
X
xia-liyun 已提交
228

X
xia-liyun 已提交
229 230 231
    // pkg flag empty
    ret = g_interface->UpdatePermissionFlags(TEST_APP_ID, g_systemPers[0].name, NULL);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "update perms flag ret =" << ret << endl;
X
xia-liyun 已提交
232 233 234 235 236 237 238 239 240 241 242
    ClrPers(permissions);

    UnLoadPermissions(TEST_TASKID);
    DeletePermissions(TEST_APP_ID);
}

/**
 * @tc.number     Security_AppSecurity_PermissionManager_L1_UpdatePermissionFlags_005
 * @tc.name       Update permission flags (Name invalid)
 * @tc.desc       [C- SECURITY -1000]
 */
243
HWTEST_F(ActsPMSUpdatePermissionTest, testSecPMUpdatePMFlags_005, Function | MediumTest | Level2)
X
xia-liyun 已提交
244 245 246
{
    SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);

X
xia-liyun 已提交
247 248 249 250 251 252 253 254
    PermissionSaved *permissions = NULL;
    int permNum = 0;
    int ret = g_interface->QueryPermission(TEST_APP_ID, &permissions, &permNum);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "query ret=" << ret << endl;
    EXPECT_EQ(permNum, SYS_PERM_NUM) << "permNum =" << permNum << endl;
    if (permissions != NULL)
    {
        EXPECT_EQ(permissions[0].flags, PMS_FLAG_DEFAULT) << "flags =" << permissions[0].flags << endl;
X
xia-liyun 已提交
255 256 257
    }
    ClrPers(permissions);

X
xia-liyun 已提交
258 259 260
    // pkg name invalid
    ret = g_interface->UpdatePermissionFlags(TEST_APP_INVALID, g_systemPers[0].name, PMS_FLAG_USER_FIXED);
    EXPECT_EQ(ret, PERM_ERRORCODE_FILE_NOT_EXIST) << "update perms flag ret =" << ret << endl;
X
xia-liyun 已提交
261

X
xia-liyun 已提交
262 263 264
    // pkg perm invalid
    ret = g_interface->UpdatePermissionFlags(TEST_APP_ID, PERMISSION_UNSUPPORTED, PMS_FLAG_USER_FIXED);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "update perms flag ret =" << ret << endl;
X
xia-liyun 已提交
265 266 267 268 269 270 271 272 273 274 275
    ClrPers(permissions);

    UnLoadPermissions(TEST_TASKID);
    DeletePermissions(TEST_APP_ID);
}

/**
 * @tc.number     Security_AppSecurity_PermissionManager_L1_UpdatePermissionFlags_006
 * @tc.name       Updatepermissionflags interface stability test
 * @tc.desc       [C- SECURITY -1000]
 */
276
HWTEST_F(ActsPMSUpdatePermissionTest, testSecPMUpdatePMFlags_006, Function | MediumTest | Level2)
X
xia-liyun 已提交
277 278 279
{
    SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);

X
xia-liyun 已提交
280 281 282 283 284 285 286 287
    PermissionSaved *permissions = NULL;
    int permNum = 0;
    int ret = g_interface->QueryPermission(TEST_APP_ID, &permissions, &permNum);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "query ret=" << ret << endl;
    EXPECT_EQ(permNum, SYS_PERM_NUM) << "permNum =" << permNum << endl;
    if (permissions != NULL)
    {
        EXPECT_EQ(permissions[0].flags, PMS_FLAG_DEFAULT) << "flags =" << permissions[0].flags << endl;
X
xia-liyun 已提交
288 289 290
    }
    ClrPers(permissions);

X
xia-liyun 已提交
291
    for (int i = 0; i < PERFORMANCE_RUN_TIMES / 2; i++)
X
xia-liyun 已提交
292
    {
X
xia-liyun 已提交
293 294 295 296
        ret = g_interface->UpdatePermissionFlags(TEST_APP_ID, g_systemPers[0].name, PMS_FLAG_DEFAULT);
        EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "update perms flag ret =" << ret << endl;
        ret = g_interface->UpdatePermissionFlags(TEST_APP_ID, g_systemPers[0].name, PMS_FLAG_USER_FIXED);
        EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "update perms flag ret =" << ret << endl;
X
xia-liyun 已提交
297 298 299 300 301 302 303 304 305 306 307 308
    }
    ClrPers(permissions);

    UnLoadPermissions(TEST_TASKID);
    DeletePermissions(TEST_APP_ID);
}

/**
 * @tc.number     Security_AppSecurity_PermissionManager_L1_UpdatePermissionFlags_007
 * @tc.name       Updatepermissionflags interface performace test
 * @tc.desc       [C- SECURITY -1000]
 */
309
HWTEST_F(ActsPMSUpdatePermissionTest, testSecPMUpdatePMFlags_007, Function | MediumTest | Level2)
X
xia-liyun 已提交
310
{
X
xia-liyun 已提交
311 312 313 314 315 316 317 318 319 320
    SaveOrUpdatePermissions(TEST_APP_ID, g_systemPers, SYS_PERM_NUM, FIRST_INSTALL);

    PermissionSaved *permissions = NULL;
    int permNum = 0;
    int ret = g_interface->QueryPermission(TEST_APP_ID, &permissions, &permNum);
    EXPECT_EQ(ret, PERM_ERRORCODE_SUCCESS) << "query ret=" << ret << endl;
    EXPECT_EQ(permNum, SYS_PERM_NUM) << "permNum =" << permNum << endl;
    if (permissions != NULL)
    {
        EXPECT_EQ(permissions[0].flags, PMS_FLAG_DEFAULT) << "flags =" << permissions[0].flags << endl;
X
xia-liyun 已提交
321 322 323
    }
    ClrPers(permissions);

X
xia-liyun 已提交
324 325
    long startTime = GetCurrentTimeMillis();
    for (int i = 0; i < PERFORMANCE_RUN_TIMES / 2; i++)
X
xia-liyun 已提交
326
    {
X
xia-liyun 已提交
327 328
        ret = g_interface->UpdatePermissionFlags(TEST_APP_ID, g_systemPers[0].name, PMS_FLAG_DEFAULT);
        ret = g_interface->UpdatePermissionFlags(TEST_APP_ID, g_systemPers[0].name, PMS_FLAG_USER_FIXED);
X
xia-liyun 已提交
329
    }
X
xia-liyun 已提交
330 331 332
    long timeCost = GetCurrentTimeMillis() - startTime;
    long timeAvg = timeCost / 10000;
    EXPECT_LT(timeAvg, 11) << "avg times =" << timeAvg << endl;
X
xia-liyun 已提交
333 334 335 336 337
    ClrPers(permissions);

    UnLoadPermissions(TEST_TASKID);
    DeletePermissions(TEST_APP_ID);
}