• 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 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: CmRemoveGrantTest009
209  * @tc.desc: Test CmRemoveGrantTest remove while keyUri not exist
210  * @tc.type: FUNC
211  * @tc.require: AR000H0MIA /SR000H09NA
212  */
213 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest009, TestSize.Level0)
214 {
215     uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0";
216     struct CmBlob keyUri = { sizeof(uriData), uriData };
217     uint32_t appId = DEFAULT_APP_ID;
218     int32_t ret = CmRemoveGrantedApp(&keyUri, appId);
219     EXPECT_EQ(ret, CM_SUCCESS);
220 }
221 
222 /**
223  * @tc.name: CmRemoveGrantTest010
224  * @tc.desc: Test CmRemoveGrantTest remove while app uid not exist
225  * @tc.type: FUNC
226  * @tc.require: AR000H0MIA /SR000H09NA
227  */
228 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest010, TestSize.Level0)
229 {
230     TestRemoveGrant(REMOVE_NOT_EXIST_ID, REMOVE_ONCE); /* remove not exist app uid */
231 }
232 
233 /**
234  * @tc.name: CmRemoveGrantTest011
235  * @tc.desc: Test CmRemoveGrantTest remove while app uid exist
236  * @tc.type: FUNC
237  * @tc.require: AR000H0MIA /SR000H09NA
238  */
239 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest011, TestSize.Level0)
240 {
241     TestRemoveGrant(DEFAULT_APP_ID, REMOVE_ONCE); /* remove exist app uid */
242 }
243 
244 /**
245  * @tc.name: CmRemoveGrantTest012
246  * @tc.desc: Test CmRemoveGrantTest remove same app uid twice
247  * @tc.type: FUNC
248  * @tc.require: AR000H0MIA /SR000H09NA
249  */
250 HWTEST_F(CmRemoveGrantTest, CmRemoveGrantTest012, TestSize.Level0)
251 {
252     TestRemoveGrant(DEFAULT_APP_ID, REMOVE_TWICE); /* remove same app uid twice */
253 }
254 } // end of namespace
255