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