• 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 #include "cm_test_common.h"
18 #include "cm_test_log.h"
19 #include "cert_manager_api.h"
20 
21 using namespace testing::ext;
22 using namespace CertmanagerTest;
23 namespace {
24 static uint32_t g_selfTokenId = 0;
25 static MockHapToken* g_MockHap = nullptr;
26 struct CertInfoResult {
27     struct CertInfo CertInfo;
28     bool bExpectResult;
29 };
30 
31 struct CertInfoResult g_listCertInfoexpectResult[] = {
32     {
33         {
34             "1d3472b9.0",
35             "GlobalSign",
36             true,
37             "CN=GlobalSign,OU=GlobalSign ECC Root CA - R5,O=GlobalSign",
38             "CN=GlobalSign,OU=GlobalSign ECC Root CA - R5,O=GlobalSign",
39             "605949E0262EBB55F90A778A71F94AD86C",
40             "2012-11-13",
41             "2038-1-19",
42             "17:9F:BC:14:8A:3D:D0:0F:D2:4E:A1:34:58:CC:43:BF:A7:F5:9C:81:82:D7:83:A5:13:F6:EB:EC:10:0C:89:24"
43         },
44         true
45     },
46     {
47         {
48             "4f316efb.0",
49             "SwissSign AG",
50             true,
51             "CN=SwissSign Gold CA - G2,OU=,O=SwissSign AG",
52             "CN=SwissSign Gold CA - G2,OU=,O=SwissSign AG",
53             "BB401C43F55E4FB0",
54             "2006-10-25",
55             "2036-10-25",
56             "62:DD:0B:E9:B9:F5:0A:16:3E:A0:F8:E7:5C:05:3B:1E:CA:57:EA:55:C8:68:8F:64:7C:68:81:F2:C8:35:7B:95"
57         },
58         true
59     },
60     {
61         {
62             "5ad8a5d6.0",
63             "GlobalSign nv-sa",
64             true,
65             "CN=GlobalSign Root CA,OU=Root CA,O=GlobalSign nv-sa",
66             "CN=GlobalSign Root CA,OU=Root CA,O=GlobalSign nv-sa",
67             "040000000001154B5AC394",
68             "1998-9-1",
69             "2028-1-28",
70             "EB:D4:10:40:E4:BB:3E:C7:42:C9:E3:81:D3:1E:F2:A4:1A:48:B6:68:5C:96:E7:CE:F3:C1:DF:6C:D4:33:1C:99"
71         },
72         true
73     }
74 };
75 
76 class CmGetCertInfoTest : public testing::Test {
77 public:
78     static void SetUpTestCase(void);
79 
80     static void TearDownTestCase(void);
81 
82     void SetUp();
83 
84     void TearDown();
85 
86     struct CertList *lstCert;
87 };
88 
SetUpTestCase(void)89 void CmGetCertInfoTest::SetUpTestCase(void)
90 {
91     g_selfTokenId = GetSelfTokenID();
92     CmTestCommon::SetTestEnvironment(g_selfTokenId);
93 }
94 
TearDownTestCase(void)95 void CmGetCertInfoTest::TearDownTestCase(void)
96 {
97     CmTestCommon::ResetTestEnvironment();
98 }
99 
SetUp()100 void CmGetCertInfoTest::SetUp()
101 {
102     InitCertList(&lstCert);
103     g_MockHap = new (std::nothrow) MockHapToken();
104 }
105 
TearDown()106 void CmGetCertInfoTest::TearDown()
107 {
108     FreeCertList(lstCert);
109     if (g_MockHap != nullptr) {
110         delete g_MockHap;
111         g_MockHap = nullptr;
112     }
113 }
114 
115 /**
116  * @tc.name: SimpleCmGetCertInfo001
117  * @tc.desc: Test CertManager get cert info interface base function
118  * @tc.type: FUNC
119  * @tc.require: AR000H0MJA /SR000H096P
120  */
121 HWTEST_F(CmGetCertInfoTest, SimpleCmGetCertInfo001, TestSize.Level0)
122 {
123     char *uri = g_listCertInfoexpectResult[0].CertInfo.uri;
124     struct CmBlob uriBlob = {strlen(uri) + 1, (uint8_t *)(uri)};
125     struct CertInfo certInfo;
126     unsigned int len = sizeof(struct CertInfo);
127     (void)memset_s(&certInfo, len, 0, len);
128     int32_t ret = InitCertInfo(&certInfo);
129     EXPECT_EQ(ret, CM_SUCCESS) << "CertInfo malloc faild, retcode:" << ret;
130 
131     ret = CmGetCertInfo(&uriBlob, CM_SYSTEM_TRUSTED_STORE, &certInfo);
132     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetCertInfo failed, retcode:" << ret;
133 
134     EXPECT_EQ(CompareCertInfo(&certInfo, &(g_listCertInfoexpectResult[0].CertInfo)), true) <<DumpCertInfo(&certInfo);
135 
136     FreeCMBlobData(&(certInfo.certInfo));
137 }
138 
139 /**
140  * @tc.name: AppGetCertInfoCompare002
141  * @tc.desc: Test CertManager get cert info compare interface function
142  * @tc.type: FUNC
143  * @tc.require: AR000H0MJA /SR000H096P
144  */
145 HWTEST_F(CmGetCertInfoTest, AppGetCertInfoCompare002, TestSize.Level0)
146 {
147     int length = sizeof(g_listCertInfoexpectResult) / sizeof(g_listCertInfoexpectResult[0]);
148 
149     char *uri = g_listCertInfoexpectResult[length - 1].CertInfo.uri;
150     struct CmBlob uriBlob = {strlen(uri) + 1, (uint8_t *)(uri)};
151 
152     struct CertInfo firstCertInfo, secondCertInfo;
153     unsigned int len = sizeof(struct CertInfo);
154 
155     (void)memset_s(&firstCertInfo, len, 0, len);
156     int32_t ret = InitCertInfo(&firstCertInfo);
157     EXPECT_EQ(ret, CM_SUCCESS) << "firstCertInfo malloc faild, retcode:" << ret;
158 
159     (void)memset_s(&secondCertInfo, len, 0, len);
160     ret = InitCertInfo(&secondCertInfo);
161     EXPECT_EQ(ret, CM_SUCCESS) << "secondCertInfo malloc faild, retcode:" << ret;
162 
163     ret = CmGetCertInfo(&uriBlob, CM_SYSTEM_TRUSTED_STORE, &firstCertInfo);
164     EXPECT_EQ(ret, CM_SUCCESS) << "first CmGetCertInfo failed,retcode:" << ret;
165 
166     ret = CmGetCertInfo(&uriBlob, CM_SYSTEM_TRUSTED_STORE, &secondCertInfo);
167     EXPECT_EQ(ret, CM_SUCCESS) << "second CmGetCertInfo failed,retcode:" << ret;
168 
169     EXPECT_EQ(CompareCertInfo(&firstCertInfo, &secondCertInfo), true) << "Diffrent app do not get the same cert.";
170     FreeCMBlobData(&(firstCertInfo.certInfo));
171     FreeCMBlobData(&(secondCertInfo.certInfo));
172 }
173 
174 /**
175  * @tc.name: AppGetAllCertInfo003
176  * @tc.desc: Test CertManager get all cert info interface function
177  * @tc.type: FUNC
178  * @tc.require: AR000H0MJA /SR000H096P
179  */
180 HWTEST_F(CmGetCertInfoTest, AppGetAllCertInfo003, TestSize.Level0)
181 {
182     unsigned int len = sizeof(struct CertInfo);;
183     struct CertInfo certInfo;
184     int32_t ret = CmGetCertList(CM_SYSTEM_TRUSTED_STORE, lstCert);
185 
186     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetCertList failed,retcode:" << ret;
187 
188     for (uint32_t i = 0; i < lstCert->certsCount; ++i) {
189         (void)memset_s(&certInfo, len, 0, len);
190         ret = InitCertInfo(&certInfo);
191         EXPECT_EQ(ret, CM_SUCCESS) << "CertInfo malloc faild, retcode:" << ret;
192 
193         struct CertAbstract *ptr = &(lstCert->certAbstract[i]);
194         ASSERT_TRUE(ptr != NULL);
195 
196         struct CmBlob uriBlob = {strlen(ptr->uri) + 1, (uint8_t *)(ptr->uri)};
197 
198         ret = CmGetCertInfo(&uriBlob, CM_SYSTEM_TRUSTED_STORE, &certInfo);
199         EXPECT_EQ(ret, CM_SUCCESS) << " CmGetCertInfo failed,retcode:" << ptr->uri;
200         FreeCMBlobData(&(certInfo.certInfo));
201     }
202 }
203 
204 /**
205  * @tc.name: ExceptionGetCertInfoTest004
206  * @tc.desc: Test CertManager get cert info interface abnormal function
207  * @tc.type: FUNC
208  * @tc.require: AR000H0MJA /SR000H096P
209  */
210 HWTEST_F(CmGetCertInfoTest, ExceptionGetCertInfoTest004, TestSize.Level0)
211 {
212     char *uri = g_listCertInfoexpectResult[1].CertInfo.uri;
213     struct CmBlob uriBlob = {strlen(uri) + 1, (uint8_t *)(uri)};
214     struct CertInfo certInfo;
215     unsigned int len = sizeof(struct CertInfo);
216     (void)memset_s(&certInfo, len, 0, len);
217     int32_t ret = InitCertInfo(&certInfo);
218     EXPECT_EQ(ret, CM_SUCCESS) << "CertInfo malloc faild, retcode:" << ret;
219 
220     EXPECT_EQ(CmGetCertInfo(NULL, CM_SYSTEM_TRUSTED_STORE, &certInfo), CMR_ERROR_NULL_POINTER);
221 
222     EXPECT_EQ(CmGetCertInfo(&uriBlob, 10,  &certInfo), CMR_ERROR_INVALID_ARGUMENT);
223 
224     EXPECT_EQ(CmGetCertInfo(&uriBlob, CM_SYSTEM_TRUSTED_STORE, NULL), CMR_ERROR_NULL_POINTER);
225 
226     const char *invalidUri = "INVALID";
227     struct CmBlob invalidUriBlob = {strlen(invalidUri), (uint8_t *)invalidUri};
228     EXPECT_EQ(CmGetCertInfo(&invalidUriBlob, CM_SYSTEM_TRUSTED_STORE, &certInfo),
229         CMR_ERROR_INVALID_ARGUMENT);
230 
231     FreeCMBlobData(&(certInfo.certInfo));
232 }
233 }