1 /*
2 * Copyright (c) 2022-2025 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 INVALID_AUTH_URI_LEN = 100;
29 static constexpr uint32_t DEFAULT_APP_ID = 1000;
30 static constexpr uint32_t GRANT_ONE_APP_ID = 1;
31 static constexpr uint32_t GRANT_MULTIPLE_APP_ID = 10;
32 static constexpr uint32_t GRANT_MAX_APP_ID = 256;
33
34 class CmGrantTest : 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 CmGrantTest::SetUpTestCase(void)
46 {
47 g_selfTokenId = GetSelfTokenID();
48 CmTestCommon::SetTestEnvironment(g_selfTokenId);
49 }
50
TearDownTestCase(void)51 void CmGrantTest::TearDownTestCase(void)
52 {
53 CmTestCommon::ResetTestEnvironment();
54 }
55
SetUp()56 void CmGrantTest::SetUp()
57 {
58 g_MockHap = new (std::nothrow) MockHapToken();
59 }
60
TearDown()61 void CmGrantTest::TearDown()
62 {
63 if (g_MockHap != nullptr) {
64 delete g_MockHap;
65 g_MockHap = nullptr;
66 }
67 }
68
TestNormalGrant(uint32_t count,bool isSameUid)69 static void TestNormalGrant(uint32_t count, bool isSameUid)
70 {
71 uint8_t aliasData[] = "TestNormalGrant";
72 struct CmBlob alias = { sizeof(aliasData), aliasData };
73 int32_t ret = TestGenerateAppCert(&alias, CERT_KEY_ALG_RSA, CM_CREDENTIAL_STORE);
74 EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert failed, retcode:" << ret;
75
76 uint8_t uriData[] = "oh:t=ak;o=TestNormalGrant;u=0;a=0";
77 struct CmBlob keyUri = { sizeof(uriData), uriData };
78 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
79 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
80 uint32_t appId = DEFAULT_APP_ID;
81
82 for (uint32_t i = 0; i < count; ++i) {
83 if (!isSameUid) {
84 appId += i;
85 }
86 authUri.size = DEFAULT_AUTH_URI_LEN; /* clear authUri size */
87 ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
88 EXPECT_EQ(ret, CM_SUCCESS) << "CmGrantAppCertificate failed, retcode:" << ret;
89 }
90
91 ret = CmUninstallAppCert(&keyUri, CM_CREDENTIAL_STORE);
92 EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
93 }
94
95 /**
96 * @tc.name: CmGrantTest001
97 * @tc.desc: Test CmGrantTest keyUri is NULL
98 * @tc.type: FUNC
99 * @tc.require: AR000H0MIA /SR000H09NA
100 */
101 HWTEST_F(CmGrantTest, CmGrantTest001, TestSize.Level0)
102 {
103 struct CmBlob *keyUri = nullptr; /* keyUri is NULL */
104
105 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
106 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
107
108 uint32_t appId = DEFAULT_APP_ID;
109
110 int32_t ret = CmGrantAppCertificate(keyUri, appId, &authUri);
111 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
112 }
113
114 /**
115 * @tc.name: CmGrantTest002
116 * @tc.desc: Test CmGrantTest keyUri size is 0
117 * @tc.type: FUNC
118 * @tc.require: AR000H0MIA /SR000H09NA
119 */
120 HWTEST_F(CmGrantTest, CmGrantTest002, TestSize.Level0)
121 {
122 uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
123 struct CmBlob keyUri = { 0, uriData }; /* keyUri size is 0 */
124
125 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
126 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
127
128 uint32_t appId = DEFAULT_APP_ID;
129
130 int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
131 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
132 }
133
134 /**
135 * @tc.name: CmGrantTest003
136 * @tc.desc: Test CmGrantTest keyUri data is null
137 * @tc.type: FUNC
138 * @tc.require: AR000H0MIA /SR000H09NA
139 */
140 HWTEST_F(CmGrantTest, CmGrantTest003, TestSize.Level0)
141 {
142 uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
143 struct CmBlob keyUri = { sizeof(uriData), nullptr }; /* keyUri data is null */
144
145 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
146 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
147
148 uint32_t appId = DEFAULT_APP_ID;
149
150 int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
151 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
152 }
153
154 /**
155 * @tc.name: CmGrantTest004
156 * @tc.desc: Test CmGrantTest keyUri data not end of '\0'
157 * @tc.type: FUNC
158 * @tc.require: AR000H0MIA /SR000H09NA
159 */
160 HWTEST_F(CmGrantTest, CmGrantTest004, TestSize.Level0)
161 {
162 uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
163 struct CmBlob keyUri = { strlen((char *)uriData), uriData }; /* keyUri data not end of '\0' */
164
165 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
166 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
167
168 uint32_t appId = DEFAULT_APP_ID;
169
170 int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
171 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
172 }
173
174 /**
175 * @tc.name: CmGrantTest005
176 * @tc.desc: Test CmGrantTest keyUri data has no app: can't find cert
177 * @tc.type: FUNC
178 * @tc.require: AR000H0MIA /SR000H09NA
179 */
180 HWTEST_F(CmGrantTest, CmGrantTest005, TestSize.Level0)
181 {
182 /* keyUri data has no app */
183 uint8_t uriData[] = "oh:t=ak;o=keyA;u=0";
184 struct CmBlob keyUri = { sizeof(uriData), uriData };
185
186 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
187 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
188
189 uint32_t appId = DEFAULT_APP_ID;
190
191 int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
192 EXPECT_EQ(ret, CMR_ERROR_NOT_EXIST);
193 }
194
195 /**
196 * @tc.name: CmGrantTest006
197 * @tc.desc: Test CmGrantTest keyUri data has no user: can't find cert
198 * @tc.type: FUNC
199 * @tc.require: AR000H0MIA /SR000H09NA
200 */
201 HWTEST_F(CmGrantTest, CmGrantTest006, TestSize.Level0)
202 {
203 /* keyUri data has no user */
204 uint8_t uriData[] = "oh:t=ak;o=keyA;a=0";
205 struct CmBlob keyUri = { sizeof(uriData), uriData };
206
207 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
208 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
209
210 uint32_t appId = DEFAULT_APP_ID;
211
212 int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
213 EXPECT_EQ(ret, CMR_ERROR_NOT_EXIST);
214 }
215
216 /**
217 * @tc.name: CmGrantTest007
218 * @tc.desc: Test CmGrantTest keyUri data has no object: can't find cert
219 * @tc.type: FUNC
220 * @tc.require: AR000H0MIA /SR000H09NA
221 */
222 HWTEST_F(CmGrantTest, CmGrantTest007, TestSize.Level0)
223 {
224 /* keyUri data has no object */
225 uint8_t uriData[] = "oh:t=ak;u=0;a=0";
226 struct CmBlob keyUri = { sizeof(uriData), uriData };
227
228 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
229 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
230
231 uint32_t appId = DEFAULT_APP_ID;
232
233 int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
234 EXPECT_EQ(ret, CMR_ERROR_NOT_EXIST);
235 }
236
237 /**
238 * @tc.name: CmGrantTest008
239 * @tc.desc: Test CmGrantTest keyUri data type not ak: can't find cert
240 * @tc.type: FUNC
241 * @tc.require: AR000H0MIA /SR000H09NA
242 */
243 HWTEST_F(CmGrantTest, CmGrantTest008, TestSize.Level0)
244 {
245 /* keyUri data type not ak */
246 uint8_t uriData[] = "oh:t=m;o=keyA;u=0;a=0";
247 struct CmBlob keyUri = { sizeof(uriData), uriData };
248
249 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
250 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
251
252 uint32_t appId = DEFAULT_APP_ID;
253
254 int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
255 EXPECT_EQ(ret, CMR_ERROR_NOT_EXIST);
256 }
257
258 /**
259 * @tc.name: CmGrantTest009
260 * @tc.desc: Test CmGrantTest authUri null
261 * @tc.type: FUNC
262 * @tc.require: AR000H0MIA /SR000H09NA
263 */
264 HWTEST_F(CmGrantTest, CmGrantTest009, TestSize.Level0)
265 {
266 uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
267 struct CmBlob keyUri = { sizeof(uriData), uriData };
268 uint32_t appId = DEFAULT_APP_ID;
269 struct CmBlob *authUri = nullptr; /* authUri nullptr */
270
271 int32_t ret = CmGrantAppCertificate(&keyUri, appId, authUri);
272 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
273 }
274
275 /**
276 * @tc.name: CmGrantTest010
277 * @tc.desc: Test CmGrantTest authUri size is 0
278 * @tc.type: FUNC
279 * @tc.require: AR000H0MIA /SR000H09NA
280 */
281 HWTEST_F(CmGrantTest, CmGrantTest010, TestSize.Level0)
282 {
283 uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
284 struct CmBlob keyUri = { sizeof(uriData), uriData };
285
286 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
287 struct CmBlob authUri = { 0, authUriData }; /* authUri size is 0 */
288
289 uint32_t appId = DEFAULT_APP_ID;
290
291 int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
292 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
293 }
294
295 /**
296 * @tc.name: CmGrantTest011
297 * @tc.desc: Test CmGrantTest authUri data is NULL
298 * @tc.type: FUNC
299 * @tc.require: AR000H0MIA /SR000H09NA
300 */
301 HWTEST_F(CmGrantTest, CmGrantTest011, TestSize.Level0)
302 {
303 uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
304 struct CmBlob keyUri = { sizeof(uriData), uriData };
305 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, nullptr }; /* authUri data is NULL */
306 uint32_t appId = DEFAULT_APP_ID;
307
308 int32_t ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
309 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
310 }
311
312 /**
313 * @tc.name: CmGrantTest012
314 * @tc.desc: Test CmGrantTest normal case: grant 1 app id
315 * @tc.type: FUNC
316 * @tc.require: AR000H0MIA /SR000H09NA
317 */
318 HWTEST_F(CmGrantTest, CmGrantTest012, TestSize.Level0)
319 {
320 TestNormalGrant(GRANT_ONE_APP_ID, true); /* grant 1 app id */
321 }
322
323 /**
324 * @tc.name: CmGrantTest013
325 * @tc.desc: Test CmGrantTest normal case: grant 10 same app id
326 * @tc.type: FUNC
327 * @tc.require: AR000H0MIA /SR000H09NA
328 */
329 HWTEST_F(CmGrantTest, CmGrantTest013, TestSize.Level0)
330 {
331 TestNormalGrant(GRANT_MULTIPLE_APP_ID, true); /* grant 10 same app id */
332 }
333
334 /**
335 * @tc.name: CmGrantTest014
336 * @tc.desc: Test CmGrantTest normal case: grant 10 different app id
337 * @tc.type: FUNC
338 * @tc.require: AR000H0MIA /SR000H09NA
339 */
340 HWTEST_F(CmGrantTest, CmGrantTest014, TestSize.Level0)
341 {
342 TestNormalGrant(GRANT_MULTIPLE_APP_ID, false); /* grant 10 different app id */
343 }
344
345 /**
346 * @tc.name: CmGrantTest015
347 * @tc.desc: Test CmGrantTest authUri size too small
348 * @tc.type: FUNC
349 * @tc.require: AR000H0MIA /SR000H09NA
350 */
351 HWTEST_F(CmGrantTest, CmGrantTest015, TestSize.Level0)
352 {
353 uint8_t aliasData[] = "CmGrantTest015";
354 struct CmBlob alias = { sizeof(aliasData), aliasData };
355 int32_t ret = TestGenerateAppCert(&alias, CERT_KEY_ALG_RSA, CM_CREDENTIAL_STORE);
356 EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert failed, retcode:" << ret;
357
358 uint8_t uriData[] = "oh:t=ak;o=CmGrantTest015;u=0;a=0";
359 struct CmBlob keyUri = { sizeof(uriData), uriData };
360 uint8_t authUriData[INVALID_AUTH_URI_LEN] = {0}; /* size too small */
361 struct CmBlob authUri = { INVALID_AUTH_URI_LEN, authUriData };
362 uint32_t appId = DEFAULT_APP_ID;
363
364 ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
365 EXPECT_EQ(ret, CMR_ERROR_BUFFER_TOO_SMALL) << "CmGrantAppCertificate failed, retcode:" << ret;
366
367 ret = CmUninstallAppCert(&keyUri, CM_CREDENTIAL_STORE);
368 EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
369 }
370
371 /**
372 * @tc.name: CmGrantTest016
373 * @tc.desc: Test CmGrantTest max grant test
374 * @tc.type: FUNC
375 * @tc.require: AR000H0MIA /SR000H09NA
376 */
377 HWTEST_F(CmGrantTest, CmGrantTest016, TestSize.Level0)
378 {
379 uint8_t aliasData[] = "CmGrantTest016";
380 struct CmBlob alias = { sizeof(aliasData), aliasData };
381 int32_t ret = TestGenerateAppCert(&alias, CERT_KEY_ALG_ECC, CM_CREDENTIAL_STORE);
382 EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert ecc failed, retcode:" << ret;
383
384 uint8_t uriData[] = "oh:t=ak;o=CmGrantTest016;u=0;a=0";
385 struct CmBlob keyUri = { sizeof(uriData), uriData };
386 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
387 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
388 uint32_t appId = DEFAULT_APP_ID;
389
390 for (uint32_t i = 0; i < GRANT_MAX_APP_ID; ++i) {
391 appId += i;
392 authUri.size = DEFAULT_AUTH_URI_LEN; /* clear authUri size */
393 ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
394 EXPECT_EQ(ret, CM_SUCCESS) << "CmGrantAppCertificate failed, retcode:" << ret;
395 }
396
397 /* grant exist appUid: success */
398 authUri.size = DEFAULT_AUTH_URI_LEN; /* clear authUri size */
399 ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
400 EXPECT_EQ(ret, CM_SUCCESS) << "CmGrantAppCertificate failed, retcode:" << ret;
401
402 /* grant 257 appUid: failed */
403 appId += 1;
404 authUri.size = DEFAULT_AUTH_URI_LEN; /* clear authUri size */
405 ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
406 EXPECT_EQ(ret, CMR_ERROR_MAX_GRANT_COUNT_REACHED) << "CmGrantAppCertificate failed, retcode:" << ret;
407
408 ret = CmUninstallAppCert(&keyUri, CM_CREDENTIAL_STORE);
409 EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
410 }
411
412 /**
413 * @tc.name: CmGrantTestPerformance016
414 * @tc.desc: 1000 times: grant and remove grant
415 * @tc.type: FUNC
416 * @tc.require: AR000H0MIA /SR000H09NA
417 */
418 HWTEST_F(CmGrantTest, CmGrantTestPerformance016, TestSize.Level1)
419 {
420 uint8_t aliasData[] = "TestGrantPer";
421 struct CmBlob alias = { sizeof(aliasData), aliasData };
422 int32_t ret = TestGenerateAppCert(&alias, CERT_KEY_ALG_RSA, CM_CREDENTIAL_STORE);
423 EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert failed, retcode:" << ret;
424
425 uint8_t uriData[] = "oh:t=ak;o=TestGrantPer;u=0;a=0";
426 struct CmBlob keyUri = { sizeof(uriData), uriData };
427 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
428 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
429 uint32_t appId = DEFAULT_APP_ID;
430
431 for (uint32_t i = 0; i < PERFORMACE_COUNT; ++i) {
432 ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
433 EXPECT_EQ(ret, CM_SUCCESS) << "CmGrantAppCertificate failed, retcode:" << ret;
434
435 ret = CmRemoveGrantedApp(&keyUri, appId);
436 EXPECT_EQ(ret, CM_SUCCESS) << "CmRemoveGrantedApp failed, retcode:" << ret;
437 }
438
439 ret = CmUninstallAppCert(&keyUri, CM_CREDENTIAL_STORE);
440 EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
441 }
442 } // end of namespace
443
444