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 "sign_bin.h"
18#include "sign_provider.h"
19#include "local_sign_provider.h"
20
21#define VERSION 9
22#define BYTE_NUMBER 32
23
24namespace OHOS {
25namespace SignatureTools {
26void ConstructSignerConfig(SignerConfig& signerConfig, Options& options)
27{
28    signerConfig.SetCompatibleVersion(VERSION);
29
30    std::map<std::string, std::string> params;
31    params["keyPwd"] = "123456";
32    params["mode"] = "localSign";
33    params["keyAlias"] = "oh-app1-key-v1";
34    params["signAlg"] = "SHA256withECDSA";
35    params["appCertFile"] = "./hapSign/app-release1.pem";
36    params["signCode"] = "1";
37    params["compatibleVersion"] = "9";
38    params["outFile"] = "./hapSign/signed.bin";
39    params["profileFile"] = "./hapSign/app1-profile1.p7b";
40    params["keystorePwd"] = "123456";
41    params["keystoreFile"] = "./hapSign/ohtest.p12";
42    params["inFile"] = "./hapSign/unsigned.bin";
43    params["profileSigned"] = "1";
44    signerConfig.FillParameters(params);
45
46    ContentDigestAlgorithm contentDigestAlgorithm("SHA-256", BYTE_NUMBER);
47    std::pair<std::string, void*> signatureAlgAndParams("SHA256withECDSA", nullptr);
48    SignatureAlgorithmHelper signatureAlgorithm(SignatureAlgorithmId::ECDSA_WITH_SHA256, "ECDSA_WITH_SHA256",
49                                                contentDigestAlgorithm, signatureAlgAndParams);
50    std::vector<SignatureAlgorithmHelper> signatureAlgorithms;
51    signatureAlgorithms.push_back(signatureAlgorithm);
52    signerConfig.SetSignatureAlgorithms(signatureAlgorithms);
53
54    options.emplace("mode", std::string("localSign"));
55    char keyPwd[] = "123456";
56    options.emplace("keyPwd", keyPwd);
57    options.emplace("outFile", std::string("./hapSign/signed.bin"));
58    options.emplace("keyAlias", std::string("oh-app1-key-v1"));
59    options.emplace("profileFile", std::string("./hapSign/app1-profile1.p7b"));
60    options.emplace("signAlg", std::string("SHA256withECDSA"));
61    char keystorePwd[] = "123456";
62    options.emplace("keystorePwd", keystorePwd);
63    options.emplace("keystoreFile", std::string("./hapSign/ohtest.p12"));
64    options.emplace("appCertFile", std::string("./hapSign/app-release1.pem"));
65    options.emplace("inFile", std::string("./hapSign/unsigned.bin"));
66    signerConfig.SetOptions(&options);
67}
68
69void ConstructSignParams(std::map<std::string, std::string>& signParams)
70{
71    signParams["a"] = "4";
72    signParams["appCertFile"] = "./hapSign/app-release1.pem";
73    signParams["compatibleVersion"] = "9";
74    signParams["inFile"] = "./hapSign/unsigned.bin";
75    signParams["inForm"] = "bin";
76    signParams["keyAlias"] = "oh-app1-key-v1";
77    signParams["keyPwd"] = "123456";
78    signParams["keystoreFile"] = "./hapSign/ohtest.p12";
79    signParams["keystorePwd"] = "123456";
80    signParams["outFile"] = "./hapSign/signed.bin";
81    signParams["profileFile"] = "./hapSign/app1-profile1.p7b";
82    signParams["profileSigned"] = "1";
83    signParams["signAlg"] = "SHA256withECDSA";
84    signParams["signCode"] = "1";
85}
86
87class SignBinTest : public testing::Test {
88public:
89    static void SetUpTestCase(void)
90    {
91        (void)rename("./hapSign/unsigned.txt", "./hapSign/unsigned.bin");
92    };
93    static void TearDownTestCase()
94    {
95    };
96    void SetUp()
97    {
98    };
99    void TearDown()
100    {
101    };
102};
103
104/**
105 * @tc.name: GenerateFileDigest001
106 * @tc.desc: The return digest size is 0, because the algorithm does not support.
107 * @tc.size: MEDIUM
108 * @tc.type: FUNC
109 * @tc.level Level 1
110 * @tc.require: SR000H63TL
111 */
112HWTEST_F(SignBinTest, GenerateFileDigest001, testing::ext::TestSize.Level1)
113{
114    std::shared_ptr<SignBin> api = std::make_shared<SignBin>();
115    std::vector<int8_t> generateFileDigest = api->GenerateFileDigest("./signed.bin", "SHA266");
116    EXPECT_EQ(generateFileDigest.size(), 0);
117}
118
119/**
120 * @tc.name: GenerateFileDigest002
121 * @tc.desc: The return digest size is 0, because the input file does not exist.
122 * @tc.size: MEDIUM
123 * @tc.type: FUNC
124 * @tc.level Level 1
125 * @tc.require: SR000H63TL
126 */
127HWTEST_F(SignBinTest, GenerateFileDigest002, testing::ext::TestSize.Level1)
128{
129    std::shared_ptr<SignBin> api = std::make_shared<SignBin>();
130    std::vector<int8_t> generateFileDigest = api->GenerateFileDigest("./signed.bin", "SHA384withECDSA");
131    EXPECT_EQ(generateFileDigest.size(), 0);
132}
133
134/**
135 * @tc.name: Sign001
136 * @tc.desc: The block head data made failed
137 * @tc.size: MEDIUM
138 * @tc.type: FUNC
139 * @tc.level Level 1
140 * @tc.require: SR000H63TL
141 */
142HWTEST_F(SignBinTest, Sign001, testing::ext::TestSize.Level1)
143{
144    // go to branch "The block head data made failed"
145    std::shared_ptr<SignBin> api = std::make_shared<SignBin>();
146
147    // 1.construct SignerConfig
148    SignerConfig signerConfig;
149    Options options;
150    ConstructSignerConfig(signerConfig, options);
151
152    // 2.construct signParams
153    std::string appCertFile = "./hapSign/app-release1.pem";
154    std::string compatibleVersion = "9";
155    std::string inFile = "./hapSign/unvalid.bin";
156    std::string inForm = "bin";
157    std::string keyAlias = "oh-app1-key-v1";
158    static char keyPwd[] = "123456";
159    std::string keystoreFile = "./hapSign/ohtest.p12";
160    static char keystorePwd[] = "123456";
161    std::string outFile = "./hapSign/signed.bin";
162    std::string profileFile = "./hapSign/app1-profile1.p7b";
163    std::string profileSigned = "1";
164    std::string signAlg = "SHA256withECDSA";
165    std::string signCode = "1";
166    std::string mode = "localSign";
167
168    std::map<std::string, std::string> signParams;
169    signParams["a"] = "4";
170    signParams["appCertFile"] = appCertFile;
171    signParams["compatibleVersion"] = compatibleVersion;
172    signParams["inFile"] = inFile;
173    signParams["inForm"] = inForm;
174    signParams["keyAlias"] = keyAlias;
175    signParams["keyPwd"] = keyPwd;
176    signParams["keystoreFile"] = keystoreFile;
177    signParams["keystorePwd"] = keystorePwd;
178    signParams["outFile"] = outFile;
179    signParams["profileFile"] = profileFile;
180    signParams["profileSigned"] = profileSigned;
181    signParams["signAlg"] = signAlg;
182    signParams["signCode"] = signCode;
183    signParams["mode"] = mode;
184
185    bool flag = api->Sign(signerConfig, signParams);
186    EXPECT_EQ(flag, false);
187}
188
189/**
190 * @tc.name: Sign002
191 * @tc.desc: The sign data made failed.
192 * @tc.size: MEDIUM
193 * @tc.type: FUNC
194 * @tc.level Level 1
195 * @tc.require: SR000H63TL
196 */
197HWTEST_F(SignBinTest, Sign002, testing::ext::TestSize.Level1)
198{
199    // go to branch "The sign data made failed"
200    std::shared_ptr<SignBin> api = std::make_shared<SignBin>();
201
202    // 1.construct SignerConfig
203    SignerConfig signerConfig;
204    Options options;
205    ConstructSignerConfig(signerConfig, options);
206
207    // 2.construct sign params
208    std::map<std::string, std::string> signParams;
209    ConstructSignParams(signParams);
210
211    bool flag = api->Sign(signerConfig, signParams);
212    EXPECT_EQ(flag, false);
213}
214
215/**
216 * @tc.name: SignBin001
217 * @tc.desc: check Compatible Version failed.
218 * @tc.size: MEDIUM
219 * @tc.type: FUNC
220 * @tc.level Level 1
221 * @tc.require: SR000H63TL
222 */
223HWTEST_F(SignBinTest, SignBin001, testing::ext::TestSize.Level1)
224{
225    // go to branch "check Compatible Version failed"
226    std::unique_ptr<SignProvider> signProvider = std::make_unique<LocalSignProvider>();
227    std::shared_ptr<Options> params = std::make_shared<Options>();
228
229    std::string mode = "localSign";
230    std::string keyAlias = "oh-app1-key-v1";
231    std::string signAlg = "SHA256withECDSA";
232    std::string signCode = "1";
233    std::string appCertFile = "./hapSign/app-release1.pem";
234    std::string profileFile = "./hapSign/app1-profile1.p7b";
235    std::string inFile = "./hapSign/unsigned.bin";
236    std::string keystoreFile = "./hapSign/ohtest.p12";
237    std::string outFile = "./hapSign/signed.bin";
238    std::string inForm = "bin";
239    char keyPwd[] = "123456";
240    char keystorePwd[] = "123456";
241    std::string compatibleVersion = "";
242
243    (*params)["mode"] = mode;
244    (*params)["keyAlias"] = keyAlias;
245    (*params)["signAlg"] = signAlg;
246    (*params)["signCode"] = signCode;
247    (*params)["appCertFile"] = appCertFile;
248    (*params)["profileFile"] = profileFile;
249    (*params)["inFile"] = inFile;
250    (*params)["keystoreFile"] = keystoreFile;
251    (*params)["outFile"] = outFile;
252    (*params)["inForm"] = inForm;
253    (*params)["keyPwd"] = keyPwd;
254    (*params)["keystorePwd"] = keystorePwd;
255    (*params)["compatibleVersion"] = compatibleVersion;
256    bool ret = signProvider->SignBin(params.get());
257    EXPECT_EQ(ret, false);
258}
259
260/**
261 * @tc.name: SignBin002
262 * @tc.desc: Test function result of SignBin002 will be SUCCESS.
263 * @tc.size: MEDIUM
264 * @tc.type: FUNC
265 * @tc.level Level 1
266 * @tc.require: SR000H63TL
267 */
268HWTEST_F(SignBinTest, SignBin002, testing::ext::TestSize.Level1)
269{
270    std::unique_ptr<SignProvider> signProvider = std::make_unique<LocalSignProvider>();
271    std::shared_ptr<Options> params = std::make_shared<Options>();
272
273    std::string mode = "localSign";
274    std::string keyAlias = "oh-app1-key-v1";
275    std::string signAlg = "SHA256withECDSA";
276    std::string signCode = "1";
277    std::string appCertFile = "./hapSign/app-release1.pem";
278    std::string profileFile = "./hapSign/app1-profile1.p7b";
279    std::string inFile = "./hapSign/unsigned.bin";
280    std::string keystoreFile = "./hapSign/ohtest.p12";
281    std::string outFile = "./hapSign/signed.bin";
282    std::string inForm = "bin";
283    char keyPwd[] = "123456";
284    char keystorePwd[] = "123456";
285    std::string compatibleVersion = "9";
286
287    (*params)["mode"] = mode;
288    (*params)["keyAlias"] = keyAlias;
289    (*params)["signAlg"] = signAlg;
290    (*params)["signCode"] = signCode;
291    (*params)["appCertFile"] = appCertFile;
292    (*params)["profileFile"] = profileFile;
293    (*params)["inFile"] = inFile;
294    (*params)["keystoreFile"] = keystoreFile;
295    (*params)["outFile"] = outFile;
296    (*params)["inForm"] = inForm;
297    (*params)["keyPwd"] = keyPwd;
298    (*params)["keystorePwd"] = keystorePwd;
299    (*params)["compatibleVersion"] = compatibleVersion;
300    bool ret = signProvider->SignBin(params.get());
301    EXPECT_EQ(ret, true);
302}
303
304} // namespace SignatureTools
305} // namespace OHOS
306