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 <gtest/gtest.h>
17 
18 #include "cm_test_common.h"
19 
20 #include "cert_manager_api.h"
21 
22 using namespace testing::ext;
23 using namespace CertmanagerTest;
24 namespace {
25 static constexpr uint32_t DEFAULT_AUTH_URI_LEN = 256;
26 static constexpr uint32_t DEFAULT_BASE_APP_ID = 1000;
27 static constexpr uint32_t APP_UID_COUNT_ONE = 1;
28 static constexpr uint32_t APP_UID_COUNT_MULTI = 10;
29 static constexpr uint32_t APP_UID_REMOVE_COUNT = 4;
30 static constexpr uint32_t DEFAULT_APP_UID_COUNT = 256;
31 
32 class CmGetAuthListTest : public testing::Test {
33 public:
34     static void SetUpTestCase(void);
35 
36     static void TearDownTestCase(void);
37 
38     void SetUp();
39 
40     void TearDown();
41 };
42 
SetUpTestCase(void)43 void CmGetAuthListTest::SetUpTestCase(void)
44 {
45     SetATPermission();
46 }
47 
TearDownTestCase(void)48 void CmGetAuthListTest::TearDownTestCase(void)
49 {
50 }
51 
52 static const uint8_t g_uriData[] = "oh:t=ak;o=GetAuthList;u=0;a=0";
53 static const CmBlob g_keyUri = { sizeof(g_uriData), (uint8_t *)g_uriData };
54 
SetUp()55 void CmGetAuthListTest::SetUp()
56 {
57     uint8_t aliasData[] = "GetAuthList";
58     struct CmBlob alias = { sizeof(aliasData), aliasData };
59 
60     int32_t ret = TestGenerateAppCert(&alias, CERT_KEY_ALG_RSA, CM_CREDENTIAL_STORE);
61     EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert failed, retcode:" << ret;
62 
63     uint32_t appId = DEFAULT_BASE_APP_ID;
64     uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
65     struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
66 
67     ret = CmGrantAppCertificate(&g_keyUri, appId, &authUri);
68     EXPECT_EQ(ret, CM_SUCCESS) << "CmGrantAppCertificate failed, retcode:" << ret;
69 }
70 
TearDown()71 void CmGetAuthListTest::TearDown()
72 {
73     int32_t ret = CmUninstallAppCert(&g_keyUri, CM_CREDENTIAL_STORE);
74     EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
75 
76     uint32_t appUid[DEFAULT_APP_UID_COUNT] = {0};
77     struct CmAppUidList appUidList = { DEFAULT_APP_UID_COUNT, appUid };
78     ret = CmGetAuthorizedAppList(&g_keyUri, &appUidList);
79     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAuthorizedAppList failed, retcode:" << ret;
80 
81     uint32_t expectCount = 0;
82     EXPECT_EQ(appUidList.appUidCount, expectCount);
83 }
84 
TestRemoveGrant(uint32_t count,uint32_t baseAppId)85 static void TestRemoveGrant(uint32_t count, uint32_t baseAppId)
86 {
87     int32_t ret;
88     uint32_t appId;
89     for (uint32_t i = 0; i < count; ++i) {
90         appId = baseAppId + i;
91         ret = CmRemoveGrantedApp(&g_keyUri, appId);
92         EXPECT_EQ(ret, CM_SUCCESS) << "CmRemoveGrantedApp failed, retcode:" << ret;
93     }
94 }
95 
TestGrant(uint32_t count,uint32_t baseAppId)96 static void TestGrant(uint32_t count, uint32_t baseAppId)
97 {
98     uint32_t appId;
99     uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
100     struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
101 
102     int32_t ret;
103     for (uint32_t i = 0; i < count; ++i) {
104         appId = baseAppId + i;
105         authUri.size = DEFAULT_AUTH_URI_LEN;
106         ret = CmGrantAppCertificate(&g_keyUri, appId, &authUri);
107         EXPECT_EQ(ret, CM_SUCCESS) << "CmGrantAppCertificate failed, retcode:" << ret;
108         ret = CmIsAuthorizedApp(&authUri);
109         EXPECT_EQ(ret, CM_SUCCESS) << "CmIsAuthorizedApp failed, retcode:" << ret;
110     }
111 }
112 
CheckGetAuthedList(uint32_t count,uint32_t baseAppId)113 static void CheckGetAuthedList(uint32_t count, uint32_t baseAppId)
114 {
115     uint32_t appUid[DEFAULT_APP_UID_COUNT] = {0};
116     struct CmAppUidList appUidList = { DEFAULT_APP_UID_COUNT, appUid };
117     int32_t ret = CmGetAuthorizedAppList(&g_keyUri, &appUidList);
118     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetAuthorizedAppList failed, retcode:" << ret;
119 
120     EXPECT_EQ(appUidList.appUidCount, count);
121 
122     uint32_t uidValidCount = 0;
123     for (uint32_t i = 0; i < count; ++i) {
124         for (uint32_t j = 0; j < appUidList.appUidCount; ++j) {
125             if ((baseAppId + i) == appUidList.appUid[j]) {
126                 uidValidCount++;
127             }
128         }
129     }
130     EXPECT_EQ(uidValidCount, count);
131 }
132 
133 /* caller make sure grantCount is no smaller than removeCount */
TestGetAuthList(uint32_t grantCount,uint32_t removeCount)134 static void TestGetAuthList(uint32_t grantCount, uint32_t removeCount)
135 {
136     TestGrant(grantCount, DEFAULT_BASE_APP_ID);
137     CheckGetAuthedList(grantCount, DEFAULT_BASE_APP_ID);
138 
139     uint32_t remainCount = grantCount - removeCount;
140     uint32_t remainBaseAppId = DEFAULT_BASE_APP_ID + removeCount;
141 
142     if (removeCount != 0) {
143         TestRemoveGrant(removeCount, DEFAULT_BASE_APP_ID);
144         CheckGetAuthedList(remainCount, remainBaseAppId);
145     }
146 
147     /* clear environment */
148     TestRemoveGrant(remainCount, remainBaseAppId);
149     CheckGetAuthedList(0, 0);
150 }
151 
152 /**
153  * @tc.name: CmGetAuthListTest001
154  * @tc.desc: Test CmGetAuthListTest keyUri is NULL
155  * @tc.type: FUNC
156  * @tc.require: AR000H0MIA /SR000H09NA
157  */
158 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest001, TestSize.Level0)
159 {
160     struct CmBlob *keyUri = nullptr; /* keyUri is NULL */
161     struct CmAppUidList appUidList = { 0, nullptr };
162     int32_t ret = CmGetAuthorizedAppList(keyUri, &appUidList);
163     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
164 }
165 
166 /**
167  * @tc.name: CmGetAuthListTest002
168  * @tc.desc: Test CmGetAuthListTest keyUri size is 0
169  * @tc.type: FUNC
170  * @tc.require: AR000H0MIA /SR000H09NA
171  */
172 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest002, TestSize.Level0)
173 {
174     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
175     struct CmBlob keyUri = { 0, uriData }; /* keyUri size is 0 */
176     struct CmAppUidList appUidList = { 0, nullptr };
177     int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
178     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
179 }
180 
181 /**
182  * @tc.name: CmGetAuthListTest003
183  * @tc.desc: Test CmGetAuthListTest keyUri data is null
184  * @tc.type: FUNC
185  * @tc.require: AR000H0MIA /SR000H09NA
186  */
187 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest003, TestSize.Level0)
188 {
189     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
190     struct CmBlob keyUri = { sizeof(uriData), nullptr }; /* keyUri data is null */
191     struct CmAppUidList appUidList = { 0, nullptr };
192     int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
193     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
194 }
195 
196 /**
197  * @tc.name: CmGetAuthListTest004
198  * @tc.desc: Test CmGetAuthListTest keyUri data not end of '\0'
199  * @tc.type: FUNC
200  * @tc.require: AR000H0MIA /SR000H09NA
201  */
202 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest004, TestSize.Level0)
203 {
204     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
205     struct CmBlob keyUri = { strlen((char *)uriData), uriData }; /* keyUri data not end of '\0' */
206     struct CmAppUidList appUidList = { 0, nullptr };
207     int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
208     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
209 }
210 
211 /**
212  * @tc.name: CmGetAuthListTest005
213  * @tc.desc: Test CmGetAuthListTest keyUri data has no app
214  * @tc.type: FUNC
215  * @tc.require: AR000H0MIA /SR000H09NA
216  */
217 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest005, TestSize.Level0)
218 {
219     /* keyUri data has no app */
220     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0";
221     struct CmBlob keyUri = { sizeof(uriData), uriData };
222     struct CmAppUidList appUidList = { 0, nullptr };
223     int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
224     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
225 }
226 
227 /**
228  * @tc.name: CmGetAuthListTest006
229  * @tc.desc: Test CmGetAuthListTest keyUri data has no user
230  * @tc.type: FUNC
231  * @tc.require: AR000H0MIA /SR000H09NA
232  */
233 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest006, TestSize.Level0)
234 {
235     /* keyUri data has no user */
236     uint8_t uriData[] = "oh:t=ak;o=keyA;a=0";
237     struct CmBlob keyUri = { sizeof(uriData), uriData };
238     struct CmAppUidList appUidList = { 0, nullptr };
239     int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
240     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
241 }
242 
243 /**
244  * @tc.name: CmGetAuthListTest007
245  * @tc.desc: Test CmGetAuthListTest keyUri data has no object
246  * @tc.type: FUNC
247  * @tc.require: AR000H0MIA /SR000H09NA
248  */
249 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest007, TestSize.Level0)
250 {
251     /* keyUri data has no object */
252     uint8_t uriData[] = "oh:t=ak;u=0;a=0";
253     struct CmBlob keyUri = { sizeof(uriData), uriData };
254     struct CmAppUidList appUidList = { 0, nullptr };
255     int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
256     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
257 }
258 
259 /**
260  * @tc.name: CmGetAuthListTest008
261  * @tc.desc: Test CmGetAuthListTest keyUri data type not ak
262  * @tc.type: FUNC
263  * @tc.require: AR000H0MIA /SR000H09NA
264  */
265 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest008, TestSize.Level0)
266 {
267     /* keyUri data type not ak */
268     uint8_t uriData[] = "oh:t=m;o=keyA;u=0;a=0";
269     struct CmBlob keyUri = { sizeof(uriData), uriData };
270     struct CmAppUidList appUidList = { 0, nullptr };
271     int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList);
272     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
273 }
274 
275 /**
276  * @tc.name: CmGetAuthListTest009
277  * @tc.desc: Test CmGetAuthListTest authUriList is NULL
278  * @tc.type: FUNC
279  * @tc.require: AR000H0MIA /SR000H09NA
280  */
281 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest009, TestSize.Level0)
282 {
283     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
284     struct CmBlob keyUri = { sizeof(uriData), uriData };
285     struct CmAppUidList *appUidList = nullptr; /* authUriList is NULL */
286     int32_t ret = CmGetAuthorizedAppList(&keyUri, appUidList);
287     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
288 }
289 
290 /**
291  * @tc.name: CmGetAuthListTest010
292  * @tc.desc: Test CmGetAuthListTest authlist count too small
293  * @tc.type: FUNC
294  * @tc.require: AR000H0MIA /SR000H09NA
295  */
296 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest010, TestSize.Level0)
297 {
298     struct CmAppUidList appUidList = { 0, nullptr };
299     int32_t ret = CmGetAuthorizedAppList(&g_keyUri, &appUidList);
300     EXPECT_EQ(ret, CMR_ERROR_BUFFER_TOO_SMALL);
301 }
302 
303 /**
304  * @tc.name: CmGetAuthListTest011
305  * @tc.desc: Test CmGetAuthListTest authlist data NULL
306  * @tc.type: FUNC
307  * @tc.require: AR000H0MIA /SR000H09NA
308  */
309 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest011, TestSize.Level0)
310 {
311     struct CmAppUidList appUidList = { APP_UID_COUNT_ONE, nullptr }; /* setup has granted 1 app uid */
312     int32_t ret = CmGetAuthorizedAppList(&g_keyUri, &appUidList);
313     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
314 }
315 
316 /**
317  * @tc.name: CmGetAuthListTest012
318  * @tc.desc: Test CmGetAuthListTest authlist count too big > MAX_OUT_BLOB_SIZE
319  * @tc.type: FUNC
320  * @tc.require: AR000H0MIA /SR000H09NA
321  */
322 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest012, TestSize.Level0)
323 {
324     struct CmAppUidList appUidList = { MAX_OUT_BLOB_SIZE + 1, nullptr }; /* count too big */
325     int32_t ret = CmGetAuthorizedAppList(&g_keyUri, &appUidList);
326     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
327 }
328 
329 /**
330  * @tc.name: CmGetAuthListTest013
331  * @tc.desc: Test CmGetAuthListTest not grant, get grant list { 0, NULL }
332  * @tc.type: FUNC
333  * @tc.require: AR000H0MIA /SR000H09NA
334  */
335 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest013, TestSize.Level0)
336 {
337     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
338     struct CmBlob keyUri = { sizeof(uriData), uriData };
339     struct CmAppUidList appUidList = { 0, nullptr };
340 
341     int32_t ret = CmGetAuthorizedAppList(&keyUri, &appUidList); /* auth uid not exist */
342     EXPECT_EQ(ret, CM_SUCCESS);
343 
344     uint32_t expectCount = 0;
345     EXPECT_EQ(appUidList.appUidCount, expectCount);
346 }
347 
348 /**
349 * @tc.name: CmGetAuthListTest014
350 * @tc.desc: Test CmGetAuthListTest grant 1, get authlist
351 * @tc.type: FUNC
352 * @tc.require: AR000H0MIA /SR000H09NA
353 */
354 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest014, TestSize.Level0)
355 {
356     uint32_t tempUid = 0;
357     struct CmAppUidList appUidList = { APP_UID_COUNT_ONE, &tempUid };
358 
359     int32_t ret = CmGetAuthorizedAppList(&g_keyUri, &appUidList);
360     EXPECT_EQ(ret, CM_SUCCESS);
361     EXPECT_EQ(appUidList.appUidCount, APP_UID_COUNT_ONE);
362     EXPECT_EQ(*(appUidList.appUid), DEFAULT_BASE_APP_ID);
363 }
364 
365 /**
366  * @tc.name: CmGetAuthListTest015
367  * @tc.desc: Test CmGetAuthListTest grant 10, get authlist
368  * @tc.type: FUNC
369  * @tc.require: AR000H0MIA /SR000H09NA
370  */
371 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest015, TestSize.Level0)
372 {
373     TestGetAuthList(APP_UID_COUNT_MULTI, 0);
374 }
375 
376 /**
377  * @tc.name: CmGetAuthListTest016
378  * @tc.desc: Test CmGetAuthListTest grant 10, remove grant 4, get authlist
379  * @tc.type: FUNC
380  * @tc.require: AR000H0MIA /SR000H09NA
381  */
382 HWTEST_F(CmGetAuthListTest, CmGetAuthListTest016, TestSize.Level0)
383 {
384     TestGetAuthList(APP_UID_COUNT_MULTI, APP_UID_REMOVE_COUNT);
385 }
386 
387 /**
388 * @tc.name: CmGetAuthListTestPerformance017
389 * @tc.desc: 1000 times: grant 1, get authlist
390 * @tc.type: FUNC
391 * @tc.require: AR000H0MIA /SR000H09NA
392 */
393 HWTEST_F(CmGetAuthListTest, CmGetAuthListTestPerformance017, TestSize.Level1)
394 {
395     uint32_t tempUid = 0;
396     struct CmAppUidList appUidList = { APP_UID_COUNT_ONE, &tempUid };
397 
398     int32_t ret;
399     for (uint32_t i = 0; i < PERFORMACE_COUNT; ++i) {
400         ret = CmGetAuthorizedAppList(&g_keyUri, &appUidList);
401         EXPECT_EQ(ret, CM_SUCCESS);
402         EXPECT_EQ(appUidList.appUidCount, APP_UID_COUNT_ONE);
403         EXPECT_EQ(*(appUidList.appUid), DEFAULT_BASE_APP_ID);
404     }
405 }
406 } // end of namespace
407 
408