• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include <gtest/hwext/gtest-multithread.h>
18 
19 #include "cm_test_common.h"
20 
21 #include "cert_manager_api.h"
22 
23 using namespace testing::ext;
24 using namespace testing::mt;
25 using namespace CertmanagerTest;
26 namespace {
27 static uint32_t g_selfTokenId = 0;
28 static MockHapToken* g_MockHap = nullptr;
29 static constexpr uint32_t DEFAULT_AUTH_URI_LEN = 256;
30 static constexpr uint32_t DEFAULT_APP_ID = 1000;
31 static constexpr uint32_t REMOVE_TWICE = 2;
32 static constexpr uint32_t REMOVE_ONCE = 1;
33 static constexpr uint32_t REMOVE_NOT_EXIST_ID = 1001;
34 static constexpr uint32_t MULTI_THREAD_NUM = 10;
35 static uint32_t g_removeAppUid = 0;
36 static uint32_t g_removeCount = 0;
37 
38 class CmRemoveGrantMultiThreadTest : public testing::Test {
39 public:
40     static void SetUpTestCase(void);
41 
42     static void TearDownTestCase(void);
43 
44     void SetUp();
45 
46     void TearDown();
47 };
48 
SetUpTestCase(void)49 void CmRemoveGrantMultiThreadTest::SetUpTestCase(void)
50 {
51     g_selfTokenId = GetSelfTokenID();
52     CmTestCommon::SetTestEnvironment(g_selfTokenId);
53 }
54 
TearDownTestCase(void)55 void CmRemoveGrantMultiThreadTest::TearDownTestCase(void)
56 {
57     CmTestCommon::ResetTestEnvironment();
58 }
59 
SetUp()60 void CmRemoveGrantMultiThreadTest::SetUp()
61 {
62     g_MockHap = new (std::nothrow) MockHapToken();
63 }
64 
TearDown()65 void CmRemoveGrantMultiThreadTest::TearDown()
66 {
67     if (g_MockHap != nullptr) {
68         delete g_MockHap;
69         g_MockHap = nullptr;
70     }
71 }
72 
TestRemoveGrantPreAction(void)73 static void TestRemoveGrantPreAction(void)
74 {
75     uint8_t aliasData[] = "TestRemoveGrant";
76     struct CmBlob alias = { sizeof(aliasData), aliasData };
77     int32_t ret = TestGenerateAppCert(&alias, CERT_KEY_ALG_RSA, CM_CREDENTIAL_STORE);
78     EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert failed, retcode:" << ret;
79 
80     uint8_t uriData[] = "oh:t=ak;o=TestRemoveGrant;u=0;a=0";
81     struct CmBlob keyUri = { sizeof(uriData), uriData };
82     uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
83     struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
84     uint32_t appId = DEFAULT_APP_ID;
85 
86     ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
87     EXPECT_EQ(ret, CM_SUCCESS) << "CmGrantAppCertificate failed, retcode:" << ret;
88 }
89 
TestRemoveGrant(void)90 static void TestRemoveGrant(void)
91 {
92     int32_t ret = 0;
93     uint8_t uriData[] = "oh:t=ak;o=TestRemoveGrant;u=0;a=0";
94     struct CmBlob keyUri = { sizeof(uriData), uriData };
95 
96     for (uint32_t i = 0; i < g_removeCount; ++i) {
97         ret = CmRemoveGrantedApp(&keyUri, g_removeAppUid);
98         EXPECT_EQ(ret, CM_SUCCESS) << "CmRemoveGrantedApp failed, index:" << i << ", retcode:" << ret;
99     }
100 }
101 
TestRemoveGrantAfterAction(void)102 static void TestRemoveGrantAfterAction(void)
103 {
104     uint8_t uriData[] = "oh:t=ak;o=TestRemoveGrant;u=0;a=0";
105     struct CmBlob keyUri = { sizeof(uriData), uriData };
106 
107     int32_t ret = CmUninstallAppCert(&keyUri, CM_CREDENTIAL_STORE);
108     EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
109 }
110 
111 /**
112  * @tc.name: CmRemoveGrantMultiThreadTest001
113  * @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri is NULL
114  * @tc.type: FUNC
115  * @tc.require: AR000H0MIA /SR000H09NA
116  */
117 HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest001, TestSize.Level0, MULTI_THREAD_NUM)
118 {
119     struct CmBlob *keyUri = nullptr; /* keyUri is NULL */
120     uint32_t appId = 0;
121     int32_t ret = CmRemoveGrantedApp(keyUri, appId);
122     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
123 }
124 
125 /**
126  * @tc.name: CmRemoveGrantMultiThreadTest002
127  * @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri size is 0
128  * @tc.type: FUNC
129  * @tc.require: AR000H0MIA /SR000H09NA
130  */
131 HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest002, TestSize.Level0, MULTI_THREAD_NUM)
132 {
133     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
134     struct CmBlob keyUri = { 0, uriData }; /* keyUri size is 0 */
135     uint32_t appId = 0;
136     int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
137     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
138 }
139 
140 /**
141  * @tc.name: CmRemoveGrantMultiThreadTest003
142  * @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri data is null
143  * @tc.type: FUNC
144  * @tc.require: AR000H0MIA /SR000H09NA
145  */
146 HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest003, TestSize.Level0, MULTI_THREAD_NUM)
147 {
148     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
149     struct CmBlob keyUri = { sizeof(uriData), nullptr }; /* keyUri data is null */
150     uint32_t appId = 0;
151     int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
152     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
153 }
154 
155 /**
156  * @tc.name: CmRemoveGrantMultiThreadTest004
157  * @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri data not end of '\0'
158  * @tc.type: FUNC
159  * @tc.require: AR000H0MIA /SR000H09NA
160  */
161 HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest004, TestSize.Level0, MULTI_THREAD_NUM)
162 {
163     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
164     struct CmBlob keyUri = { strlen((char *)uriData), uriData }; /* keyUri data not end of '\0' */
165     uint32_t appId = 0;
166     int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
167     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
168 }
169 
170 /**
171  * @tc.name: CmRemoveGrantMultiThreadTest005
172  * @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri data has no app
173  * @tc.type: FUNC
174  * @tc.require: AR000H0MIA /SR000H09NA
175  */
176 HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest005, TestSize.Level0, MULTI_THREAD_NUM)
177 {
178     /* keyUri data has no app */
179     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0";
180     struct CmBlob keyUri = { sizeof(uriData), uriData };
181     uint32_t appId = 0;
182     int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
183     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
184 }
185 
186 /**
187  * @tc.name: CmRemoveGrantMultiThreadTest006
188  * @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri data has no user
189  * @tc.type: FUNC
190  * @tc.require: AR000H0MIA /SR000H09NA
191  */
192 HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest006, TestSize.Level0, MULTI_THREAD_NUM)
193 {
194     /* keyUri data has no user */
195     uint8_t uriData[] = "oh:t=ak;o=keyA;a=0";
196     struct CmBlob keyUri = { sizeof(uriData), uriData };
197     uint32_t appId = 0;
198     int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
199     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
200 }
201 
202 /**
203  * @tc.name: CmRemoveGrantMultiThreadTest007
204  * @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri data has no object
205  * @tc.type: FUNC
206  * @tc.require: AR000H0MIA /SR000H09NA
207  */
208 HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest007, TestSize.Level0, MULTI_THREAD_NUM)
209 {
210     /* keyUri data has no object */
211     uint8_t uriData[] = "oh:t=ak;u=0;a=0";
212     struct CmBlob keyUri = { sizeof(uriData), uriData };
213     uint32_t appId = 0;
214     int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
215     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
216 }
217 
218 /**
219  * @tc.name: CmRemoveGrantMultiThreadTest008
220  * @tc.desc: Test CmRemoveGrantMultiThreadTest keyUri data type not ak
221  * @tc.type: FUNC
222  * @tc.require: AR000H0MIA /SR000H09NA
223  */
224 HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest008, TestSize.Level0, MULTI_THREAD_NUM)
225 {
226     /* keyUri data type not ak */
227     uint8_t uriData[] = "oh:t=m;o=keyA;u=0;a=0";
228     struct CmBlob keyUri = { sizeof(uriData), uriData };
229     uint32_t appId = 0;
230     int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
231     EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
232 }
233 
234 /**
235  * @tc.name: CmRemoveGrantMultiThreadTest009
236  * @tc.desc: Test CmRemoveGrantMultiThreadTest remove while keyUri not exist
237  * @tc.type: FUNC
238  * @tc.require: AR000H0MIA /SR000H09NA
239  */
240 HWMTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest009, TestSize.Level0, MULTI_THREAD_NUM)
241 {
242     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
243     struct CmBlob keyUri = { sizeof(uriData), uriData };
244     uint32_t appId = DEFAULT_APP_ID;
245     int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
246     EXPECT_EQ(ret, CM_SUCCESS);
247 }
248 
249 /**
250  * @tc.name: CmRemoveGrantMultiThreadTest010
251  * @tc.desc: Test CmRemoveGrantMultiThreadTest remove while app uid not exist
252  * @tc.type: FUNC
253  * @tc.require: AR000H0MIA /SR000H09NA
254  */
255 HWTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest010, TestSize.Level0)
256 {
257     TestRemoveGrantPreAction();
258     g_removeAppUid = REMOVE_NOT_EXIST_ID;
259     g_removeCount = REMOVE_ONCE;
260     SET_THREAD_NUM(MULTI_THREAD_NUM);
261     GTEST_RUN_TASK(TestRemoveGrant); /* remove not exist app uid */
262     TestRemoveGrantAfterAction();
263 }
264 
265 /**
266  * @tc.name: CmRemoveGrantMultiThreadTest011
267  * @tc.desc: Test CmRemoveGrantMultiThreadTest remove while app uid exist
268  * @tc.type: FUNC
269  * @tc.require: AR000H0MIA /SR000H09NA
270  */
271 HWTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest011, TestSize.Level0)
272 {
273     TestRemoveGrantPreAction();
274     g_removeAppUid = DEFAULT_APP_ID;
275     g_removeCount = REMOVE_ONCE;
276     SET_THREAD_NUM(MULTI_THREAD_NUM);
277     GTEST_RUN_TASK(TestRemoveGrant); /* remove exist app uid */
278     TestRemoveGrantAfterAction();
279 }
280 
281 /**
282  * @tc.name: CmRemoveGrantMultiThreadTest012
283  * @tc.desc: Test CmRemoveGrantMultiThreadTest remove same app uid twice
284  * @tc.type: FUNC
285  * @tc.require: AR000H0MIA /SR000H09NA
286  */
287 HWTEST_F(CmRemoveGrantMultiThreadTest, CmRemoveGrantMultiThreadTest012, TestSize.Level0)
288 {
289     TestRemoveGrantPreAction();
290     g_removeAppUid = DEFAULT_APP_ID;
291     g_removeCount = REMOVE_TWICE;
292     SET_THREAD_NUM(MULTI_THREAD_NUM);
293     GTEST_RUN_TASK(TestRemoveGrant); /* remove same app uid twice */
294     TestRemoveGrantAfterAction();
295 }
296 } // end of namespace
297