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