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
27 class CmIsAuthedTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30
31 static void TearDownTestCase(void);
32
33 void SetUp();
34
35 void TearDown();
36 };
37
SetUpTestCase(void)38 void CmIsAuthedTest::SetUpTestCase(void)
39 {
40 SetATPermission();
41 }
42
TearDownTestCase(void)43 void CmIsAuthedTest::TearDownTestCase(void)
44 {
45 }
46
SetUp()47 void CmIsAuthedTest::SetUp()
48 {
49 uint8_t aliasData[] = "TestNormalGrant";
50 struct CmBlob alias = { sizeof(aliasData), aliasData };
51
52 int32_t ret = TestGenerateAppCert(&alias, CERT_KEY_ALG_RSA, CM_CREDENTIAL_STORE);
53 EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert failed, retcode:" << ret;
54 }
55
TearDown()56 void CmIsAuthedTest::TearDown()
57 {
58 uint8_t uriData[] = "oh:t=ak;o=TestNormalGrant;u=0;a=0";
59 struct CmBlob keyUri = { sizeof(uriData), uriData };
60
61 int32_t ret = CmUninstallAppCert(&keyUri, CM_CREDENTIAL_STORE);
62 EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
63 }
64
TestGrantApp(struct CmBlob * authUri)65 static void TestGrantApp(struct CmBlob *authUri)
66 {
67 uint32_t appId = 0;
68 uint8_t uriData[] = "oh:t=ak;o=TestNormalGrant;u=0;a=0";
69 struct CmBlob keyUri = { sizeof(uriData), uriData };
70
71 int32_t ret = CmGrantAppCertificate(&keyUri, appId, authUri);
72 EXPECT_EQ(ret, CM_SUCCESS) << "CmGrantAppCertificate failed, retcode:" << ret;
73 }
74
75 /**
76 * @tc.name: CmIsAuthedTest001
77 * @tc.desc: Test CmIsAuthorizedApp authUri is NULL
78 * @tc.type: FUNC
79 * @tc.require: AR000H0MIA /SR000H09NA
80 */
81 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest001, TestSize.Level0)
82 {
83 struct CmBlob *authUri = nullptr; /* authUri is NULL */
84 int32_t ret = CmIsAuthorizedApp(authUri);
85 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
86 }
87
88 /**
89 * @tc.name: CmIsAuthedTest002
90 * @tc.desc: Test CmIsAuthorizedApp authUri size is 0
91 * @tc.type: FUNC
92 * @tc.require: AR000H0MIA /SR000H09NA
93 */
94 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest002, TestSize.Level0)
95 {
96 uint8_t uriData[] =
97 "oh:t=ak;o=keyA;u=0;a=0?ca=1000&m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FAFE";
98 struct CmBlob authUri = { 0, uriData }; /* authUri size is 0 */
99 int32_t ret = CmIsAuthorizedApp(&authUri);
100 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
101 }
102
103 /**
104 * @tc.name: CmIsAuthedTest003
105 * @tc.desc: Test CmIsAuthorizedApp authUri data is null
106 * @tc.type: FUNC
107 * @tc.require: AR000H0MIA /SR000H09NA
108 */
109 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest003, TestSize.Level0)
110 {
111 uint8_t uriData[] =
112 "oh:t=ak;o=keyA;u=0;a=0?ca=1000&m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FAFE";
113 struct CmBlob authUri = { sizeof(uriData), nullptr }; /* authUri data is null */
114 int32_t ret = CmIsAuthorizedApp(&authUri);
115 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
116 }
117
118 /**
119 * @tc.name: CmIsAuthedTest004
120 * @tc.desc: Test CmIsAuthorizedApp authUri data not end of '\0'
121 * @tc.type: FUNC
122 * @tc.require: AR000H0MIA /SR000H09NA
123 */
124 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest004, TestSize.Level0)
125 {
126 uint8_t uriData[] =
127 "oh:t=ak;o=keyA;u=0;a=0?ca=1000&m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FAFE";
128 struct CmBlob authUri = { strlen((char *)uriData), uriData }; /* authUri data not end of '\0' */
129 int32_t ret = CmIsAuthorizedApp(&authUri);
130 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
131 }
132
133 /**
134 * @tc.name: CmIsAuthedTest005
135 * @tc.desc: Test CmIsAuthorizedApp authUri data has no app
136 * @tc.type: FUNC
137 * @tc.require: AR000H0MIA /SR000H09NA
138 */
139 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest005, TestSize.Level0)
140 {
141 /* authUri data has no app */
142 uint8_t uriData[] =
143 "oh:t=ak;o=keyA;u=0?ca=1000&m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FAFE";
144 struct CmBlob authUri = { sizeof(uriData), uriData };
145 int32_t ret = CmIsAuthorizedApp(&authUri);
146 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
147 }
148
149 /**
150 * @tc.name: CmIsAuthedTest006
151 * @tc.desc: Test CmIsAuthorizedApp authUri data has no user
152 * @tc.type: FUNC
153 * @tc.require: AR000H0MIA /SR000H09NA
154 */
155 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest006, TestSize.Level0)
156 {
157 /* authUri data has no user */
158 uint8_t uriData[] =
159 "oh:t=ak;o=keyA;a=0?ca=1000&m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FAFE";
160 struct CmBlob authUri = { sizeof(uriData), uriData };
161 int32_t ret = CmIsAuthorizedApp(&authUri);
162 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
163 }
164
165 /**
166 * @tc.name: CmIsAuthedTest007
167 * @tc.desc: Test CmIsAuthorizedApp authUri data has no object
168 * @tc.type: FUNC
169 * @tc.require: AR000H0MIA /SR000H09NA
170 */
171 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest007, TestSize.Level0)
172 {
173 /* authUri data has no object */
174 uint8_t uriData[] =
175 "oh:t=ak;u=0;a=0?ca=1000&m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FAFE";
176 struct CmBlob authUri = { sizeof(uriData), uriData };
177 int32_t ret = CmIsAuthorizedApp(&authUri);
178 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
179 }
180
181 /**
182 * @tc.name: CmIsAuthedTest008
183 * @tc.desc: Test CmIsAuthorizedApp authUri data type not ak
184 * @tc.type: FUNC
185 * @tc.require: AR000H0MIA /SR000H09NA
186 */
187 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest008, TestSize.Level0)
188 {
189 /* authUri data type not ak */
190 uint8_t uriData[] =
191 "oh:t=m;o=keyA;u=0;a=0?ca=1000&m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FAFE";
192 struct CmBlob authUri = { sizeof(uriData), uriData };
193 int32_t ret = CmIsAuthorizedApp(&authUri);
194 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
195 }
196
197 /**
198 * @tc.name: CmIsAuthedTest009
199 * @tc.desc: Test CmIsAuthorizedApp authUri data has no clientapp
200 * @tc.type: FUNC
201 * @tc.require: AR000H0MIA /SR000H09NA
202 */
203 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest009, TestSize.Level0)
204 {
205 /* authUri data has no clientapp */
206 uint8_t uriData[] =
207 "oh:t=ak;o=keyA;u=0;a=0?m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FAFE";
208 struct CmBlob authUri = { sizeof(uriData), uriData };
209 int32_t ret = CmIsAuthorizedApp(&authUri);
210 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
211 }
212
213 /**
214 * @tc.name: CmIsAuthedTest010
215 * @tc.desc: Test CmIsAuthorizedApp authUri data has no macData
216 * @tc.type: FUNC
217 * @tc.require: AR000H0MIA /SR000H09NA
218 */
219 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest010, TestSize.Level0)
220 {
221 uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0?ca=1000"; /* authUri data has no macData */
222 struct CmBlob authUri = { sizeof(uriData), uriData };
223 int32_t ret = CmIsAuthorizedApp(&authUri);
224 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
225 }
226
227 /**
228 * @tc.name: CmIsAuthedTest011
229 * @tc.desc: Test CmIsAuthorizedApp normal test
230 * @tc.type: FUNC
231 * @tc.require: AR000H0MIA /SR000H09NA
232 */
233 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest011, TestSize.Level0)
234 {
235 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
236 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
237 TestGrantApp(&authUri);
238
239 int32_t ret = CmIsAuthorizedApp(&authUri);
240 EXPECT_EQ(ret, CM_SUCCESS);
241 }
242
243 /**
244 * @tc.name: CmIsAuthedTest012
245 * @tc.desc: Test CmIsAuthorizedApp authUri macData size not 32
246 * @tc.type: FUNC
247 * @tc.require: AR000H0MIA /SR000H09NA
248 */
249 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest012, TestSize.Level0)
250 {
251 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
252 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
253 TestGrantApp(&authUri);
254
255 /* authUri macData size 31 */
256 uint8_t uriDataFail[] =
257 "oh:t=ak;o=TestNormalGrant;u=0;a=0?ca=0&m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FA";
258 struct CmBlob authUriFail = { sizeof(uriDataFail), uriDataFail };
259 int32_t ret = CmIsAuthorizedApp(&authUriFail);
260 EXPECT_EQ(ret, CMR_ERROR_AUTH_CHECK_FAILED);
261 }
262
263 /**
264 * @tc.name: CmIsAuthedTest013
265 * @tc.desc: Test CmIsAuthorizedApp mac invalid
266 * @tc.type: FUNC
267 * @tc.require: AR000H0MIA /SR000H09NA
268 */
269 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest013, TestSize.Level0)
270 {
271 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
272 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
273 TestGrantApp(&authUri);
274
275 /* authUri macData invalid */
276 uint8_t uriDataFail[] =
277 "oh:t=ak;o=TestNormalGrant;u=0;a=0?ca=0&m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FAFE";
278 struct CmBlob authUriFail = { sizeof(uriDataFail), uriDataFail };
279 int32_t ret = CmIsAuthorizedApp(&authUriFail);
280 EXPECT_EQ(ret, CMR_ERROR_AUTH_CHECK_FAILED);
281 }
282
283 /**
284 * @tc.name: CmIsAuthedTest014
285 * @tc.desc: Test CmIsAuthorizedApp mac size is odd number
286 * @tc.type: FUNC
287 * @tc.require: AR000H0MIA /SR000H09NA
288 */
289 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest014, TestSize.Level0)
290 {
291 /* authUri mac size is odd number */
292 uint8_t uriDataFail[] =
293 "oh:t=ak;o=TestNormalGrant;u=0;a=0?ca=0&m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FAF";
294 struct CmBlob authUriFail = { sizeof(uriDataFail), uriDataFail };
295 int32_t ret = CmIsAuthorizedApp(&authUriFail);
296 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
297 }
298
299 /**
300 * @tc.name: CmIsAuthedTest015
301 * @tc.desc: Test CmIsAuthorizedApp mac data can not change to hex
302 * @tc.type: FUNC
303 * @tc.require: AR000H0MIA /SR000H09NA
304 */
305 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest015, TestSize.Level0)
306 {
307 /* authUri mac data can not change to hex */
308 uint8_t uriDataFail[] =
309 "oh:t=ak;o=TestNormalGrant;u=0;a=0?ca=0&m=BA632421B76F1059BC28184FB9E50D57mm232B6D5C535E0DCAC0114A7AD8FAFE";
310 struct CmBlob authUriFail = { sizeof(uriDataFail), uriDataFail };
311 int32_t ret = CmIsAuthorizedApp(&authUriFail);
312 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
313 }
314
315 /**
316 * @tc.name: CmIsAuthedTest016
317 * @tc.desc: Test CmIsAuthorizedApp can not find mac key
318 * @tc.type: FUNC
319 * @tc.require: AR000H0MIA /SR000H09NA
320 */
321 HWTEST_F(CmIsAuthedTest, CmIsAuthedTest016, TestSize.Level0)
322 {
323 uint8_t uriDataFail[] =
324 "oh:t=ak;o=keyA;u=0;a=0?ca=0&m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FAFE";
325 struct CmBlob authUriFail = { sizeof(uriDataFail), uriDataFail };
326 int32_t ret = CmIsAuthorizedApp(&authUriFail);
327 EXPECT_EQ(ret, CMR_ERROR_KEY_OPERATION_FAILED);
328 }
329
330 /**
331 * @tc.name: CmIsAuthedTestPerformance017
332 * @tc.desc: 1000 times: Test CmIsAuthorizedApp normal test
333 * @tc.type: FUNC
334 * @tc.require: AR000H0MIA /SR000H09NA
335 */
336 HWTEST_F(CmIsAuthedTest, CmIsAuthedTestPerformance017, TestSize.Level1)
337 {
338 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
339 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
340 TestGrantApp(&authUri);
341
342 int32_t ret;
343 for (uint32_t i = 0; i < PERFORMACE_COUNT; ++i) {
344 ret = CmIsAuthorizedApp(&authUri);
345 EXPECT_EQ(ret, CM_SUCCESS);
346 }
347 }
348 } // end of namespace
349
350