1 /*
2 * Copyright (c) 2024 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 "cert_crl_common.h"
19 #include "certificate_openssl_common.h"
20 #include "cf_blob.h"
21 #include "cf_log.h"
22 #include "cf_mock.h"
23 #include "cf_object_base.h"
24 #include "cf_result.h"
25 #include "crypto_x509_test_common.h"
26 #include "fwk_class.h"
27 #include "memory_mock.h"
28 #include "securec.h"
29 #include "string"
30 #include "x509_cert_chain.h"
31 #include "x509_cert_chain_openssl.h"
32 #include "x509_certificate_openssl.h"
33
34 using namespace std;
35 using namespace testing::ext;
36 using namespace CFMock;
37
38 using ::testing::_;
39 using ::testing::AnyNumber;
40 using ::testing::Invoke;
41 using ::testing::Return;
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 int __real_X509_print(BIO *bp, X509 *x);
48 BIO *__real_BIO_new(const BIO_METHOD *type);
49 int __real_i2d_X509_bio(BIO *bp, X509 *x509);
50
51 #ifdef __cplusplus
52 }
53 #endif
54
55 namespace {
56 class CryptoX509CertChainTestEx : public testing::Test {
57 public:
58 static void SetUpTestCase();
59 static void TearDownTestCase();
60 void SetUp();
61 void TearDown();
62 };
63
64 static HcfCertChain *g_certChainP7b = nullptr;
65 static HcfX509CertChainSpi *g_certChainP7bSpi = nullptr;
66
GetInvalidCertChainClass(void)67 static const char *GetInvalidCertChainClass(void)
68 {
69 return "HcfInvalidCertChain";
70 }
71
SetUpTestCase()72 void CryptoX509CertChainTestEx::SetUpTestCase()
73 {
74 CfResult ret = HcfCertChainCreate(&g_inStreamChainDataP7b, nullptr, &g_certChainP7b);
75 ASSERT_EQ(ret, CF_SUCCESS);
76 ASSERT_NE(g_certChainP7b, nullptr);
77
78 HcfX509CertChainSpi *certChainSpi = nullptr;
79 ret = HcfX509CertChainByEncSpiCreate(&g_inStreamChainDataP7b, &certChainSpi);
80 ASSERT_EQ(ret, CF_SUCCESS);
81 ASSERT_NE(certChainSpi, nullptr);
82 g_certChainP7bSpi = certChainSpi;
83 }
84
TearDownTestCase()85 void CryptoX509CertChainTestEx::TearDownTestCase()
86 {
87 CfObjDestroy(g_certChainP7b);
88 CfObjDestroy(g_certChainP7bSpi);
89 }
90
SetUp()91 void CryptoX509CertChainTestEx::SetUp() {}
92
TearDown()93 void CryptoX509CertChainTestEx::TearDown() {}
94
95 HWTEST_F(CryptoX509CertChainTestEx, ToStringTest001, TestSize.Level0)
96 {
97 CF_LOG_I("CryptoX509CertChainTestEx - ToStringTest001");
98 ASSERT_NE(g_certChainP7b, nullptr);
99
100 CfBlob blob = { 0, nullptr };
101 CfResult ret = g_certChainP7b->toString(g_certChainP7b, &blob);
102 EXPECT_EQ(ret, CF_SUCCESS);
103 CfBlobDataFree(&blob);
104
105 HcfCertChain certChain;
106 certChain.base.getClass = GetInvalidCertChainClass;
107
108 ret = g_certChainP7b->toString(&certChain, &blob);
109 EXPECT_EQ(ret, CF_INVALID_PARAMS);
110
111 ret = g_certChainP7b->toString(NULL, &blob);
112 EXPECT_EQ(ret, CF_INVALID_PARAMS);
113
114 ret = g_certChainP7b->toString(g_certChainP7b, NULL);
115 EXPECT_EQ(ret, CF_INVALID_PARAMS);
116
117 ret = g_certChainP7b->toString(NULL, NULL);
118 EXPECT_EQ(ret, CF_INVALID_PARAMS);
119
120 X509OpensslMock::SetMockFlag(true);
121 EXPECT_CALL(X509OpensslMock::GetInstance(), BIO_new(_))
122 .WillOnce(Return(NULL))
123 .WillRepeatedly(Invoke(__real_BIO_new));
124 ret = g_certChainP7b->toString(g_certChainP7b, &blob);
125 EXPECT_EQ(ret, CF_ERR_MALLOC);
126 X509OpensslMock::SetMockFlag(false);
127
128 X509OpensslMock::SetMockFlag(true);
129 EXPECT_CALL(X509OpensslMock::GetInstance(), X509_print(_, _))
130 .WillOnce(Return(-1))
131 .WillRepeatedly(Invoke(__real_X509_print));
132 ret = g_certChainP7b->toString(g_certChainP7b, &blob);
133 EXPECT_EQ(ret, CF_ERR_CRYPTO_OPERATION);
134 X509OpensslMock::SetMockFlag(false);
135
136 X509OpensslMock::SetMockFlag(true);
137 EXPECT_CALL(X509OpensslMock::GetInstance(), BIO_ctrl(_, _, _, _)).WillRepeatedly(Return(0));
138 ret = g_certChainP7b->toString(g_certChainP7b, &blob);
139 EXPECT_EQ(ret, CF_ERR_CRYPTO_OPERATION);
140 X509OpensslMock::SetMockFlag(false);
141 }
142
143 HWTEST_F(CryptoX509CertChainTestEx, HashCodeTest001, TestSize.Level0)
144 {
145 CF_LOG_I("CryptoX509CertChainTestEx - HashCodeTest001");
146 ASSERT_NE(g_certChainP7b, nullptr);
147
148 CfBlob blob = { 0, nullptr };
149 CfResult ret = g_certChainP7b->hashCode(g_certChainP7b, &blob);
150 EXPECT_EQ(ret, CF_SUCCESS);
151 CfBlobDataFree(&blob);
152
153 SetMockFlag(true);
154 ret = g_certChainP7b->hashCode(g_certChainP7b, &blob);
155 EXPECT_EQ(ret, CF_ERR_MALLOC);
156 SetMockFlag(false);
157
158 HcfCertChain certChain;
159 certChain.base.getClass = GetInvalidCertChainClass;
160
161 ret = g_certChainP7b->hashCode(&certChain, &blob);
162 EXPECT_EQ(ret, CF_INVALID_PARAMS);
163
164 ret = g_certChainP7b->hashCode(NULL, &blob);
165 EXPECT_EQ(ret, CF_INVALID_PARAMS);
166
167 ret = g_certChainP7b->hashCode(g_certChainP7b, NULL);
168 EXPECT_EQ(ret, CF_INVALID_PARAMS);
169
170 ret = g_certChainP7b->hashCode(NULL, NULL);
171 EXPECT_EQ(ret, CF_INVALID_PARAMS);
172
173 X509OpensslMock::SetMockFlag(true);
174 EXPECT_CALL(X509OpensslMock::GetInstance(), BIO_new(_))
175 .WillOnce(Return(NULL))
176 .WillRepeatedly(Invoke(__real_BIO_new));
177 ret = g_certChainP7b->hashCode(g_certChainP7b, &blob);
178 EXPECT_EQ(ret, CF_ERR_MALLOC);
179 X509OpensslMock::SetMockFlag(false);
180
181 X509OpensslMock::SetMockFlag(true);
182 EXPECT_CALL(X509OpensslMock::GetInstance(), i2d_X509_bio(_, _))
183 .WillOnce(Return(-1))
184 .WillRepeatedly(Invoke(__real_i2d_X509_bio));
185 ret = g_certChainP7b->hashCode(g_certChainP7b, &blob);
186 EXPECT_EQ(ret, CF_ERR_CRYPTO_OPERATION);
187 X509OpensslMock::SetMockFlag(false);
188
189 X509OpensslMock::SetMockFlag(true);
190 EXPECT_CALL(X509OpensslMock::GetInstance(), BIO_ctrl(_, _, _, _)).WillRepeatedly(Return(0));
191 ret = g_certChainP7b->hashCode(g_certChainP7b, &blob);
192 EXPECT_EQ(ret, CF_ERR_CRYPTO_OPERATION);
193 X509OpensslMock::SetMockFlag(false);
194 }
195
196 HWTEST_F(CryptoX509CertChainTestEx, HcfX509CertChainSpiEngineToStringTest001, TestSize.Level0)
197 {
198 CF_LOG_I("HcfX509CertChainSpiEngineToStringTest001");
199 ASSERT_NE(g_certChainP7bSpi, nullptr);
200
201 CfBlob blob = { 0, nullptr };
202 CfResult ret = g_certChainP7bSpi->engineToString(g_certChainP7bSpi, &blob);
203 EXPECT_EQ(ret, CF_SUCCESS);
204 CfBlobDataFree(&blob);
205
206 HcfX509CertChainSpi InvalidCertChainSpi;
207 InvalidCertChainSpi.base.getClass = GetInvalidCertChainClass;
208
209 ret = g_certChainP7bSpi->engineToString(&InvalidCertChainSpi, &blob);
210 EXPECT_EQ(ret, CF_INVALID_PARAMS);
211
212 ret = g_certChainP7bSpi->engineToString(NULL, &blob);
213 EXPECT_EQ(ret, CF_INVALID_PARAMS);
214
215 ret = g_certChainP7bSpi->engineToString(g_certChainP7bSpi, NULL);
216 EXPECT_EQ(ret, CF_INVALID_PARAMS);
217
218 ret = g_certChainP7bSpi->engineToString(NULL, NULL);
219 EXPECT_EQ(ret, CF_INVALID_PARAMS);
220 }
221
222 HWTEST_F(CryptoX509CertChainTestEx, HcfX509CertChainSpiEngineHashCodeTest001, TestSize.Level0)
223 {
224 CF_LOG_I("HcfX509CertChainSpiEngineHashCodeTest001");
225 ASSERT_NE(g_certChainP7bSpi, nullptr);
226
227 CfBlob blob = { 0, nullptr };
228 CfResult ret = g_certChainP7bSpi->engineHashCode(g_certChainP7bSpi, &blob);
229 EXPECT_EQ(ret, CF_SUCCESS);
230 CfBlobDataFree(&blob);
231
232 HcfX509CertChainSpi InvalidCertChainSpi;
233 InvalidCertChainSpi.base.getClass = GetInvalidCertChainClass;
234
235 ret = g_certChainP7bSpi->engineHashCode(&InvalidCertChainSpi, &blob);
236 EXPECT_EQ(ret, CF_INVALID_PARAMS);
237
238 ret = g_certChainP7bSpi->engineHashCode(NULL, &blob);
239 EXPECT_EQ(ret, CF_INVALID_PARAMS);
240
241 ret = g_certChainP7bSpi->engineHashCode(g_certChainP7bSpi, NULL);
242 EXPECT_EQ(ret, CF_INVALID_PARAMS);
243
244 ret = g_certChainP7bSpi->engineHashCode(NULL, NULL);
245 EXPECT_EQ(ret, CF_INVALID_PARAMS);
246 }
247
248 } // namespace
249