1 /*
2  * Copyright (c) 2024-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 #include <memory>
16 #include <gtest/gtest.h>
17 #include "cert_tools.h"
18 
19 namespace OHOS {
20 namespace SignatureTools {
21 class CertToolsTest : public testing::Test {
22 public:
SetUpTestCase(void)23     static void SetUpTestCase(void)
24     {
25     };
TearDownTestCase()26     static void TearDownTestCase()
27     {
28     };
SetUp()29     void SetUp()
30     {
31     };
TearDown()32     void TearDown()
33     {
34     };
35 };
36 
37 /**
38  * @tc.name: cert_tools_test_001
39  * @tc.desc: parameter basicConstraintsCritical connot convert bool
40  * @tc.type: FUNC
41  */
HWTEST_F(CertToolsTest, cert_tools_test_001, testing::ext::TestSize.Level1)42 HWTEST_F(CertToolsTest, cert_tools_test_001, testing::ext::TestSize.Level1)
43 {
44     Options options;
45     options[Options::BASIC_CONSTRAINTS_CRITICAL] = std::string("a");
46     options[Options::BASIC_CONSTRAINTS] = std::string("1");
47     bool result = CertTools::SetBisicConstraints(&options, nullptr);
48     EXPECT_EQ(result, false);
49 }
50 
51 /**
52  * @tc.name: cert_tools_test_002
53  * @tc.desc: parameter basicConstraintsCa connot convert bool
54  * @tc.type: FUNC
55  */
HWTEST_F(CertToolsTest, cert_tools_test_002, testing::ext::TestSize.Level1)56 HWTEST_F(CertToolsTest, cert_tools_test_002, testing::ext::TestSize.Level1)
57 {
58     Options options;
59     options[Options::BASIC_CONSTRAINTS_CRITICAL] = std::string("1");
60     options[Options::BASIC_CONSTRAINTS] = std::string("1");
61     options[Options::BASIC_CONSTRAINTS_CA] = std::string("a");
62     bool result = CertTools::SetBisicConstraints(&options, nullptr);
63     EXPECT_EQ(result, false);
64 }
65 
66 /**
67  * @tc.name: cert_tools_test_003
68  * @tc.desc: This function tests success for interface SetBisicConstraints
69  * @tc.type: FUNC
70  */
HWTEST_F(CertToolsTest, cert_tools_test_003, testing::ext::TestSize.Level1)71 HWTEST_F(CertToolsTest, cert_tools_test_003, testing::ext::TestSize.Level1)
72 {
73     Options options;
74     options[Options::BASIC_CONSTRAINTS_CRITICAL] = std::string("1");
75     options[Options::BASIC_CONSTRAINTS] = std::string("1");
76     options[Options::BASIC_CONSTRAINTS_CA] = std::string("1");
77     X509* cert = X509_new();
78     bool ret = CertTools::SetBisicConstraints(&options, cert);
79     EXPECT_EQ(ret, true);
80     X509_free(cert);
81 }
82 
83 /**
84  * @tc.name: cert_tools_test_004
85  * @tc.desc: This function tests failed for interface SignForSubCert due to parameter caPrikey is null
86  * @tc.type: FUNC
87  */
HWTEST_F(CertToolsTest, cert_tools_test_004, testing::ext::TestSize.Level1)88 HWTEST_F(CertToolsTest, cert_tools_test_004, testing::ext::TestSize.Level1)
89 {
90     Options options;
91     KeyStoreHelper stroe;
92     EVP_PKEY* caPrikey = stroe.GenerateKeyPair("ECC", 256);
93     X509* cert = X509_new();
94     X509_REQ* subcsr = X509_REQ_new();
95     X509_REQ_set_pubkey(subcsr, caPrikey);
96     X509_REQ* rootcsr = X509_REQ_new();
97     bool ret = CertTools::SignForSubCert(cert, subcsr, rootcsr, nullptr, &options);
98     EXPECT_EQ(ret, false);
99 }
100 
101 /**
102  * @tc.name: cert_tools_test_005
103  * @tc.desc: This function tests failed for interface SetSubjectForCert due to parameter cert is null
104  * @tc.type: FUNC
105  */
HWTEST_F(CertToolsTest, cert_tools_test_005, testing::ext::TestSize.Level1)106 HWTEST_F(CertToolsTest, cert_tools_test_005, testing::ext::TestSize.Level1)
107 {
108     X509_REQ* certReq = X509_REQ_new();
109     bool ret = CertTools::SetSubjectForCert(certReq, nullptr);
110     EXPECT_EQ(ret, false);
111     X509_REQ_free(certReq);
112 }
113 
114 /**
115  * @tc.name: cert_tools_test_006
116  * @tc.desc: This function tests failed for interface GenerateSubCert due to parameter subject is error
117  * @tc.type: FUNC
118  */
HWTEST_F(CertToolsTest, cert_tools_test_006, testing::ext::TestSize.Level1)119 HWTEST_F(CertToolsTest, cert_tools_test_006, testing::ext::TestSize.Level1)
120 {
121     std::shared_ptr<Options> params = std::make_shared<Options>();
122     std::string keyAlias = "alias";
123     std::string issuerkeyAlias = "oh-app1-key-v1";
124     char keyPwd[] = "123456";
125     std::string keyAlg = "ECC";
126     int keySize = 256;
127     std::string keystoreFile = "/data/test/generateKeyPair/keypair.p12";
128     char keystorePwd[] = "123456";
129     std::string signAlg = "SHA384withECDSA";
130     std::string subject = "C=,O=OpenHarmony,OU=OpenHarmony Community,CN= Openharmony Application CA";
131     std::string issuer = "C=CN,O=OpenHarmony_test,OU=OpenHarmony Community,CN= Openharmony Application SUB  CA";
132     char isksPwd[] = "123456";
133     (*params)["keystorePwd"] = keystorePwd;
134     (*params)["issuerKeystorePwd"] = isksPwd;
135     (*params)["keyAlias"] = keyAlias;
136     (*params)["keyPwd"] = keyPwd;
137     (*params)["keyAlg"] = keyAlg;
138     (*params)["keySize"] = keySize;
139     (*params)["keystoreFile"] = keystoreFile;
140     (*params)["signAlg"] = signAlg;
141     (*params)["subject"] = subject;
142     (*params)["issuer"] = issuer;
143     (*params)["issuerKeyAlias"] = issuerkeyAlias;
144     std::unique_ptr<LocalizationAdapter> adaptePtr = std::make_unique<LocalizationAdapter>(params.get());
145     EVP_PKEY* keyPair = nullptr;
146     keyPair = adaptePtr->GetAliasKey(true);
147     X509_REQ* csr = CertTools::GenerateCsr(keyPair, signAlg, subject);
148     bool ret = CertTools::GenerateSubCert(keyPair, csr, params.get());
149     EXPECT_EQ(ret, false);
150 }
151 
152 /**
153  * @tc.name: cert_tools_test_007
154  * @tc.desc: This function tests failed for interface SetCertPublickKey due to parameter subjectCsr no set PublickKey
155  * @tc.type: FUNC
156  */
HWTEST_F(CertToolsTest, cert_tools_test_007, testing::ext::TestSize.Level1)157 HWTEST_F(CertToolsTest, cert_tools_test_007, testing::ext::TestSize.Level1)
158 {
159     X509* cert = X509_new();
160     X509_REQ* certReq = X509_REQ_new();
161     bool ret = CertTools::SetCertPublickKey(cert, certReq);
162     EXPECT_EQ(ret, false);
163     X509_REQ_free(certReq);
164     X509_free(cert);
165 }
166 
167 /**
168  * @tc.name: cert_tools_test_008
169  * @tc.desc: This function tests failed for interface SetCertPublickKey due to parameter cert is null
170  * @tc.type: FUNC
171  */
HWTEST_F(CertToolsTest, cert_tools_test_008, testing::ext::TestSize.Level1)172 HWTEST_F(CertToolsTest, cert_tools_test_008, testing::ext::TestSize.Level1)
173 {
174     X509_REQ* certReq = X509_REQ_new();
175     KeyStoreHelper stroe;
176     EVP_PKEY* Prikey = stroe.GenerateKeyPair("ECC", 256);
177     X509_REQ_set_pubkey(certReq, Prikey);
178     bool ret = CertTools::SetCertPublickKey(nullptr, certReq);
179     EXPECT_EQ(ret, false);
180     X509_REQ_free(certReq);
181 }
182 } // namespace SignatureTools
183 } // namespace OHOS