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 static constexpr uint32_t DEFAULT_APP_ID = 1000;
27 static constexpr uint32_t REMOVE_TWICE = 2;
28 static constexpr uint32_t REMOVE_ONCE = 1;
29 static constexpr uint32_t REMOVE_NOT_EXIST_ID = 1001;
30
31 class CmRemoveGrantTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34
35 static void TearDownTestCase(void);
36
37 void SetUp();
38
39 void TearDown();
40 };
41
SetUpTestCase(void)42 void CmRemoveGrantTest::SetUpTestCase(void)
43 {
44 SetATPermission();
45 }
46
TearDownTestCase(void)47 void CmRemoveGrantTest::TearDownTestCase(void)
48 {
49 }
50
SetUp()51 void CmRemoveGrantTest::SetUp()
52 {
53 }
54
TearDown()55 void CmRemoveGrantTest::TearDown()
56 {
57 }
58
TestRemoveGrant(uint32_t removeAppUid,uint32_t removeCount)59 static void TestRemoveGrant(uint32_t removeAppUid, uint32_t removeCount)
60 {
61 uint8_t aliasData[] = "TestRemoveGrant";
62 struct CmBlob alias = { sizeof(aliasData), aliasData };
63 int32_t ret = TestGenerateAppCert(&alias, CERT_KEY_ALG_RSA, CM_CREDENTIAL_STORE);
64 EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert failed, retcode:" << ret;
65
66 uint8_t uriData[] = "oh:t=ak;o=TestRemoveGrant;u=0;a=0";
67 struct CmBlob keyUri = { sizeof(uriData), uriData };
68 uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0};
69 struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData };
70 uint32_t appId = DEFAULT_APP_ID;
71
72 ret = CmGrantAppCertificate(&keyUri, appId, &authUri);
73 EXPECT_EQ(ret, CM_SUCCESS) << "CmGrantAppCertificate failed, retcode:" << ret;
74
75 for (uint32_t i = 0; i < removeCount; ++i) {
76 ret = CmRemoveGrantedApp(&keyUri, removeAppUid);
77 EXPECT_EQ(ret, CM_SUCCESS) << "CmRemoveGrantedApp failed, index:" << i << ", retcode:" << ret;
78 }
79
80 ret = CmUninstallAppCert(&keyUri, CM_CREDENTIAL_STORE);
81 EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
82 }
83
84 /**
85 * @tc.name: CmRemoveGrantTest001
86 * @tc.desc: Test CmRemoveGrantTest keyUri is NULL
87 * @tc.type: FUNC
88 * @tc.require: AR000H0MIA /SR000H09NA
89 */
90 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest001, TestSize.Level0)
91 {
92 struct CmBlob *keyUri = nullptr; /* keyUri is NULL */
93 uint32_t appId = 0;
94 int32_t ret = CmRemoveGrantedApp(keyUri, appId);
95 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
96 }
97
98 /**
99 * @tc.name: CmRemoveGrantTest002
100 * @tc.desc: Test CmRemoveGrantTest keyUri size is 0
101 * @tc.type: FUNC
102 * @tc.require: AR000H0MIA /SR000H09NA
103 */
104 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest002, TestSize.Level0)
105 {
106 uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
107 struct CmBlob keyUri = { 0, uriData }; /* keyUri size is 0 */
108 uint32_t appId = 0;
109 int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
110 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
111 }
112
113 /**
114 * @tc.name: CmRemoveGrantTest003
115 * @tc.desc: Test CmRemoveGrantTest keyUri data is null
116 * @tc.type: FUNC
117 * @tc.require: AR000H0MIA /SR000H09NA
118 */
119 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest003, TestSize.Level0)
120 {
121 uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
122 struct CmBlob keyUri = { sizeof(uriData), nullptr }; /* keyUri data is null */
123 uint32_t appId = 0;
124 int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
125 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
126 }
127
128 /**
129 * @tc.name: CmRemoveGrantTest004
130 * @tc.desc: Test CmRemoveGrantTest keyUri data not end of '\0'
131 * @tc.type: FUNC
132 * @tc.require: AR000H0MIA /SR000H09NA
133 */
134 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest004, TestSize.Level0)
135 {
136 uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
137 struct CmBlob keyUri = { strlen((char *)uriData), uriData }; /* keyUri data not end of '\0' */
138 uint32_t appId = 0;
139 int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
140 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
141 }
142
143 /**
144 * @tc.name: CmRemoveGrantTest005
145 * @tc.desc: Test CmRemoveGrantTest keyUri data has no app
146 * @tc.type: FUNC
147 * @tc.require: AR000H0MIA /SR000H09NA
148 */
149 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest005, TestSize.Level0)
150 {
151 /* keyUri data has no app */
152 uint8_t uriData[] = "oh:t=ak;o=keyA;u=0";
153 struct CmBlob keyUri = { sizeof(uriData), uriData };
154 uint32_t appId = 0;
155 int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
156 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
157 }
158
159 /**
160 * @tc.name: CmRemoveGrantTest006
161 * @tc.desc: Test CmRemoveGrantTest keyUri data has no user
162 * @tc.type: FUNC
163 * @tc.require: AR000H0MIA /SR000H09NA
164 */
165 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest006, TestSize.Level0)
166 {
167 /* keyUri data has no user */
168 uint8_t uriData[] = "oh:t=ak;o=keyA;a=0";
169 struct CmBlob keyUri = { sizeof(uriData), uriData };
170 uint32_t appId = 0;
171 int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
172 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
173 }
174
175 /**
176 * @tc.name: CmRemoveGrantTest007
177 * @tc.desc: Test CmRemoveGrantTest keyUri data has no object
178 * @tc.type: FUNC
179 * @tc.require: AR000H0MIA /SR000H09NA
180 */
181 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest007, TestSize.Level0)
182 {
183 /* keyUri data has no object */
184 uint8_t uriData[] = "oh:t=ak;u=0;a=0";
185 struct CmBlob keyUri = { sizeof(uriData), uriData };
186 uint32_t appId = 0;
187 int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
188 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
189 }
190
191 /**
192 * @tc.name: CmRemoveGrantTest008
193 * @tc.desc: Test CmRemoveGrantTest keyUri data type not ak
194 * @tc.type: FUNC
195 * @tc.require: AR000H0MIA /SR000H09NA
196 */
197 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest008, TestSize.Level0)
198 {
199 /* keyUri data type not ak */
200 uint8_t uriData[] = "oh:t=m;o=keyA;u=0;a=0";
201 struct CmBlob keyUri = { sizeof(uriData), uriData };
202 uint32_t appId = 0;
203 int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
204 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
205 }
206
207 /**
208 * @tc.name: CmRemoveGrantTest0081
209 * @tc.desc: Test CmRemoveGrantTest keyUri data invalid
210 * @tc.type: FUNC
211 * @tc.require: AR000H0MIA /SR000H09NA
212 */
213 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest0081, TestSize.Level0)
214 {
215 /* keyUri data invalid */
216 uint8_t uriData[] = "oh:t=%;o=%1;u=%22;a=%3";
217 struct CmBlob keyUri = { sizeof(uriData), uriData };
218 uint32_t appId = 0;
219 int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
220 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
221 }
222
223 /**
224 * @tc.name: CmRemoveGrantTest009
225 * @tc.desc: Test CmRemoveGrantTest remove while keyUri not exist
226 * @tc.type: FUNC
227 * @tc.require: AR000H0MIA /SR000H09NA
228 */
229 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest009, TestSize.Level0)
230 {
231 uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
232 struct CmBlob keyUri = { sizeof(uriData), uriData };
233 uint32_t appId = DEFAULT_APP_ID;
234 int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
235 EXPECT_EQ(ret, CM_SUCCESS);
236 }
237
238 /**
239 * @tc.name: CmRemoveGrantTest010
240 * @tc.desc: Test CmRemoveGrantTest remove while app uid not exist
241 * @tc.type: FUNC
242 * @tc.require: AR000H0MIA /SR000H09NA
243 */
244 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest010, TestSize.Level0)
245 {
246 TestRemoveGrant(REMOVE_NOT_EXIST_ID, REMOVE_ONCE); /* remove not exist app uid */
247 }
248
249 /**
250 * @tc.name: CmRemoveGrantTest011
251 * @tc.desc: Test CmRemoveGrantTest remove while app uid exist
252 * @tc.type: FUNC
253 * @tc.require: AR000H0MIA /SR000H09NA
254 */
255 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest011, TestSize.Level0)
256 {
257 TestRemoveGrant(DEFAULT_APP_ID, REMOVE_ONCE); /* remove exist app uid */
258 }
259
260 /**
261 * @tc.name: CmRemoveGrantTest012
262 * @tc.desc: Test CmRemoveGrantTest remove same app uid twice
263 * @tc.type: FUNC
264 * @tc.require: AR000H0MIA /SR000H09NA
265 */
266 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest012, TestSize.Level0)
267 {
268 TestRemoveGrant(DEFAULT_APP_ID, REMOVE_TWICE); /* remove same app uid twice */
269 }
270 } // end of namespace
271