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