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(nullptr))
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(), BIO_ctrl(_, _, _, _)).WillRepeatedly(Return(0));
130 ret = g_certChainP7b->toString(g_certChainP7b, &blob);
131 EXPECT_EQ(ret, CF_ERR_CRYPTO_OPERATION);
132 X509OpensslMock::SetMockFlag(false);
133 }
134
135 HWTEST_F(CryptoX509CertChainTestEx, HashCodeTest001, TestSize.Level0)
136 {
137 CF_LOG_I("CryptoX509CertChainTestEx - HashCodeTest001");
138 ASSERT_NE(g_certChainP7b, nullptr);
139
140 CfBlob blob = { 0, nullptr };
141 CfResult ret = g_certChainP7b->hashCode(g_certChainP7b, &blob);
142 EXPECT_EQ(ret, CF_SUCCESS);
143 CfBlobDataFree(&blob);
144
145 SetMockFlag(true);
146 ret = g_certChainP7b->hashCode(g_certChainP7b, &blob);
147 EXPECT_EQ(ret, CF_ERR_MALLOC);
148 SetMockFlag(false);
149
150 HcfCertChain certChain;
151 certChain.base.getClass = GetInvalidCertChainClass;
152
153 ret = g_certChainP7b->hashCode(&certChain, &blob);
154 EXPECT_EQ(ret, CF_INVALID_PARAMS);
155
156 ret = g_certChainP7b->hashCode(NULL, &blob);
157 EXPECT_EQ(ret, CF_INVALID_PARAMS);
158
159 ret = g_certChainP7b->hashCode(g_certChainP7b, NULL);
160 EXPECT_EQ(ret, CF_INVALID_PARAMS);
161
162 ret = g_certChainP7b->hashCode(NULL, NULL);
163 EXPECT_EQ(ret, CF_INVALID_PARAMS);
164
165 X509OpensslMock::SetMockFlag(true);
166 EXPECT_CALL(X509OpensslMock::GetInstance(), BIO_new(_))
167 .WillOnce(Return(nullptr))
168 .WillRepeatedly(Invoke(__real_BIO_new));
169 ret = g_certChainP7b->hashCode(g_certChainP7b, &blob);
170 EXPECT_EQ(ret, CF_ERR_MALLOC);
171 X509OpensslMock::SetMockFlag(false);
172
173 X509OpensslMock::SetMockFlag(true);
174 EXPECT_CALL(X509OpensslMock::GetInstance(), i2d_X509_bio(_, _))
175 .WillOnce(Return(-1))
176 .WillRepeatedly(Invoke(__real_i2d_X509_bio));
177 ret = g_certChainP7b->hashCode(g_certChainP7b, &blob);
178 EXPECT_EQ(ret, CF_ERR_CRYPTO_OPERATION);
179 X509OpensslMock::SetMockFlag(false);
180
181 X509OpensslMock::SetMockFlag(true);
182 EXPECT_CALL(X509OpensslMock::GetInstance(), BIO_ctrl(_, _, _, _)).WillRepeatedly(Return(0));
183 ret = g_certChainP7b->hashCode(g_certChainP7b, &blob);
184 EXPECT_EQ(ret, CF_ERR_CRYPTO_OPERATION);
185 X509OpensslMock::SetMockFlag(false);
186 }
187
188 HWTEST_F(CryptoX509CertChainTestEx, HcfX509CertChainSpiEngineToStringTest001, TestSize.Level0)
189 {
190 CF_LOG_I("HcfX509CertChainSpiEngineToStringTest001");
191 ASSERT_NE(g_certChainP7bSpi, nullptr);
192
193 CfBlob blob = { 0, nullptr };
194 CfResult ret = g_certChainP7bSpi->engineToString(g_certChainP7bSpi, &blob);
195 EXPECT_EQ(ret, CF_SUCCESS);
196 CfBlobDataFree(&blob);
197
198 HcfX509CertChainSpi InvalidCertChainSpi;
199 InvalidCertChainSpi.base.getClass = GetInvalidCertChainClass;
200
201 ret = g_certChainP7bSpi->engineToString(&InvalidCertChainSpi, &blob);
202 EXPECT_EQ(ret, CF_INVALID_PARAMS);
203
204 ret = g_certChainP7bSpi->engineToString(NULL, &blob);
205 EXPECT_EQ(ret, CF_INVALID_PARAMS);
206
207 ret = g_certChainP7bSpi->engineToString(g_certChainP7bSpi, NULL);
208 EXPECT_EQ(ret, CF_INVALID_PARAMS);
209
210 ret = g_certChainP7bSpi->engineToString(NULL, NULL);
211 EXPECT_EQ(ret, CF_INVALID_PARAMS);
212 }
213
214 HWTEST_F(CryptoX509CertChainTestEx, HcfX509CertChainSpiEngineHashCodeTest001, TestSize.Level0)
215 {
216 CF_LOG_I("HcfX509CertChainSpiEngineHashCodeTest001");
217 ASSERT_NE(g_certChainP7bSpi, nullptr);
218
219 CfBlob blob = { 0, nullptr };
220 CfResult ret = g_certChainP7bSpi->engineHashCode(g_certChainP7bSpi, &blob);
221 EXPECT_EQ(ret, CF_SUCCESS);
222 CfBlobDataFree(&blob);
223
224 HcfX509CertChainSpi InvalidCertChainSpi;
225 InvalidCertChainSpi.base.getClass = GetInvalidCertChainClass;
226
227 ret = g_certChainP7bSpi->engineHashCode(&InvalidCertChainSpi, &blob);
228 EXPECT_EQ(ret, CF_INVALID_PARAMS);
229
230 ret = g_certChainP7bSpi->engineHashCode(NULL, &blob);
231 EXPECT_EQ(ret, CF_INVALID_PARAMS);
232
233 ret = g_certChainP7bSpi->engineHashCode(g_certChainP7bSpi, NULL);
234 EXPECT_EQ(ret, CF_INVALID_PARAMS);
235
236 ret = g_certChainP7bSpi->engineHashCode(NULL, NULL);
237 EXPECT_EQ(ret, CF_INVALID_PARAMS);
238 }
239
240 } // namespace
241