• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <memory>
17 #include <fstream>
18 #include <gtest/gtest.h>
19 
20 #include "verify_elf.h"
21 #include "block_data.h"
22 #include "sign_provider.h"
23 #include "verify_hap.h"
24 
25 namespace OHOS {
26 namespace SignatureTools {
27 
28 class VerifyElfTest : public testing::Test {
29 public:
30     static void SetUpTestCase(void);
31     static void TearDownTestCase();
32     void SetUp();
33     void TearDown();
34 };
35 
SetUpTestCase(void)36 void VerifyElfTest::SetUpTestCase(void)
37 {
38     (void)rename("./elfVerify/elf_verify_data_err_package.txt", "./elfVerify/elf_verify_data_err_package.elf");
39     (void)rename("./elfVerify/elf_verify_pkcs7_err_package.txt", "./elfVerify/elf_verify_pkcs7_err_package.elf");
40     (void)rename("./elfVerify/elf_check_ownerid_err_package.txt", "./elfVerify/elf_check_ownerid_err_package.elf");
41     (void)rename("./elfVerify/elf_signed_package.txt", "./elfVerify/elf_signed_package.elf");
42     (void)rename("./elfVerify/elf_unsigned_package.txt", "./elfVerify/elf_unsigned_package.elf");
43     (void)rename("./elfVerify/elf_check_file_err_package.txt", "./elfVerify/elf_check_file_err_package.elf");
44     (void)rename("./elfVerify/elf_signed_with_profile_json.txt", "./elfVerify/elf_signed_with_profile_json.elf");
45 }
46 
TearDownTestCase(void)47 void VerifyElfTest::TearDownTestCase(void)
48 {
49 }
50 
SetUp()51 void VerifyElfTest::SetUp()
52 {
53 }
54 
TearDown()55 void VerifyElfTest::TearDown()
56 {
57 }
58 
59 /**
60  * @tc.name: Verify001
61  * @tc.desc: Test function of VerifyElfTest::Verify() interface for SUCCESS.
62  * @tc.size: MEDIUM
63  * @tc.type: FUNC
64  * @tc.level Level 1
65  * @tc.require: SR000H63TL
66  */
67 HWTEST_F(VerifyElfTest, Verify001, testing::ext::TestSize.Level1)
68 {
69     Options options;
70     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_signed_package.elf"));
71     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
72     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
73 
74     VerifyElf verifyElf;
75     bool flag = verifyElf.Verify(&options);
76 
77     EXPECT_EQ(flag, true);
78 }
79 
80 /**
81  * @tc.name: Verify002
82  * @tc.desc: param not exist
83  * @tc.size: MEDIUM
84  * @tc.type: FUNC
85  * @tc.level Level 1
86  * @tc.require: SR000H63TL
87  */
88 HWTEST_F(VerifyElfTest, Verify002, testing::ext::TestSize.Level1)
89 {
90     VerifyElf verifyElf;
91     bool flag = verifyElf.Verify(nullptr);
92 
93     EXPECT_EQ(flag, false);
94 }
95 
96 /**
97  * @tc.name: Verify003
98  * @tc.desc: param outCertChain not exist
99  * @tc.size: MEDIUM
100  * @tc.type: FUNC
101  * @tc.level Level 1
102  * @tc.require: SR000H63TL
103  */
104 HWTEST_F(VerifyElfTest, Verify003, testing::ext::TestSize.Level1)
105 {
106     Options options;
107     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_signed_package.elf"));
108     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
109 
110     VerifyElf verifyElf;
111     bool flag = verifyElf.Verify(&options);
112 
113     EXPECT_EQ(flag, false);
114 }
115 
116 /**
117  * @tc.name: Verify004
118  * @tc.desc: param outProfile not exist
119  * @tc.size: MEDIUM
120  * @tc.type: FUNC
121  * @tc.level Level 1
122  * @tc.require: SR000H63TL
123  */
124 HWTEST_F(VerifyElfTest, Verify004, testing::ext::TestSize.Level1)
125 {
126     Options options;
127     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_signed_package.elf"));
128     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
129 
130     VerifyElf verifyElf;
131     bool flag = verifyElf.Verify(&options);
132 
133     EXPECT_EQ(flag, false);
134 }
135 
136 /**
137  * @tc.name: Verify005
138  * @tc.desc: param inFile not exist
139  * @tc.size: MEDIUM
140  * @tc.type: FUNC
141  * @tc.level Level 1
142  * @tc.require: SR000H63TL
143  */
144 HWTEST_F(VerifyElfTest, Verify005, testing::ext::TestSize.Level1)
145 {
146     Options options;
147     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
148     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
149 
150     VerifyElf verifyElf;
151     bool flag = verifyElf.Verify(&options);
152 
153     EXPECT_EQ(flag, false);
154 }
155 
156 /**
157  * @tc.name: Verify006
158  * @tc.desc: input file not exist
159  * @tc.size: MEDIUM
160  * @tc.type: FUNC
161  * @tc.level Level 1
162  * @tc.require: SR000H63TL
163  */
164 HWTEST_F(VerifyElfTest, Verify006, testing::ext::TestSize.Level1)
165 {
166     Options options;
167     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_signed_package111.elf"));
168     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
169     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
170 
171     VerifyElf verifyElf;
172     bool flag = verifyElf.Verify(&options);
173 
174     EXPECT_EQ(flag, false);
175 }
176 
177 /**
178  * @tc.name: Verify007
179  * @tc.desc: wrong input file
180  * @tc.size: MEDIUM
181  * @tc.type: FUNC
182  * @tc.level Level 1
183  * @tc.require: SR000H63TL
184  */
185 HWTEST_F(VerifyElfTest, Verify007, testing::ext::TestSize.Level1)
186 {
187     Options options;
188     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_unsigned_package.elf"));
189     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
190     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
191 
192     VerifyElf verifyElf;
193     bool flag = verifyElf.Verify(&options);
194 
195     EXPECT_EQ(flag, false);
196 }
197 
198 /**
199  * @tc.name: Verify008
200  * @tc.desc: Test function of VerifyElfTest::Verify() interface for SUCCESS.
201  * @tc.size: MEDIUM
202  * @tc.type: FUNC
203  * @tc.level Level 1
204  * @tc.require: SR000H63TL
205  */
206 HWTEST_F(VerifyElfTest, Verify008, testing::ext::TestSize.Level1)
207 {
208     Options options;
209     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_signed_package.elf"));
210     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/readonly.cer"));
211     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
212 
213     VerifyElf verifyElf;
214     bool flag = verifyElf.Verify(&options);
215 
216     EXPECT_EQ(flag, true);
217 }
218 
219 /**
220  * @tc.name: Verify009
221  * @tc.desc: check owner id error
222  * @tc.size: MEDIUM
223  * @tc.type: FUNC
224  * @tc.level Level 1
225  * @tc.require: SR000H63TL
226  */
227 HWTEST_F(VerifyElfTest, Verify009, testing::ext::TestSize.Level1)
228 {
229     Options options;
230     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_check_ownerid_err_package.elf"));
231     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
232     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
233 
234     VerifyElf verifyElf;
235     bool flag = verifyElf.Verify(&options);
236 
237     EXPECT_EQ(flag, false);
238 }
239 
240 /**
241  * @tc.name: Verify010
242  * @tc.desc: verify elf data error
243  * @tc.size: MEDIUM
244  * @tc.type: FUNC
245  * @tc.level Level 1
246  * @tc.require: SR000H63TL
247  */
248 HWTEST_F(VerifyElfTest, Verify010, testing::ext::TestSize.Level1)
249 {
250     Options options;
251     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_verify_data_err_package.elf"));
252     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
253     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
254 
255     VerifyElf verifyElf;
256     bool flag = verifyElf.Verify(&options);
257 
258     EXPECT_EQ(flag, false);
259 }
260 
261 /**
262  * @tc.name: Verify011
263  * @tc.desc: verify pkcs7 error
264  * @tc.size: MEDIUM
265  * @tc.type: FUNC
266  * @tc.level Level 1
267  * @tc.require: SR000H63TL
268  */
269 HWTEST_F(VerifyElfTest, Verify011, testing::ext::TestSize.Level1)
270 {
271     Options options;
272     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_verify_pkcs7_err_package.elf"));
273     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
274     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
275 
276     VerifyElf verifyElf;
277     bool flag = verifyElf.Verify(&options);
278 
279     EXPECT_EQ(flag, false);
280 }
281 
282 /**
283  * @tc.name: Verify013
284  * @tc.desc: check elf file error
285  * @tc.size: MEDIUM
286  * @tc.type: FUNC
287  * @tc.level Level 1
288  * @tc.require: SR000H63TL
289  */
290 HWTEST_F(VerifyElfTest, Verify013, testing::ext::TestSize.Level1)
291 {
292     Options options;
293     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_check_file_err_package.elf"));
294     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
295     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
296 
297     VerifyElf verifyElf;
298     bool flag = verifyElf.Verify(&options);
299 
300     EXPECT_EQ(flag, true);
301 }
302 
303 /**
304  * @tc.name: Verify014
305  * @tc.desc: verify elf with unsigned profile
306  * @tc.size: MEDIUM
307  * @tc.type: FUNC
308  * @tc.level Level 1
309  * @tc.require: SR000H63TL
310  */
311 HWTEST_F(VerifyElfTest, Verify014, testing::ext::TestSize.Level1)
312 {
313     Options options;
314     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_signed_with_profile_json.elf"));
315     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
316     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
317 
318     VerifyElf verifyElf;
319     bool flag = verifyElf.Verify(&options);
320 
321     EXPECT_EQ(flag, true);
322 }
323 
324 /**
325  * @tc.name: CheckParams
326  * @tc.desc: Test function of VerifyElfTest::CheckParams() interface for SUCCESS.
327  * @tc.size: MEDIUM
328  * @tc.type: FUNC
329  * @tc.level Level 1
330  * @tc.require: SR000H63TL
331  */
332 HWTEST_F(VerifyElfTest, CheckParams, testing::ext::TestSize.Level1)
333 {
334     Options options;
335     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_signed_package.elf"));
336     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
337     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
338 
339     bool flag = VerifyElf::CheckParams(&options);
340 
341     EXPECT_EQ(flag, true);
342 }
343 
344 /**
345  * @tc.name: CheckSignFile
346  * @tc.desc: Test function of VerifyElfTest::CheckSignFile() interface for SUCCESS.
347  * @tc.size: MEDIUM
348  * @tc.type: FUNC
349  * @tc.level Level 1
350  * @tc.require: SR000H63TL
351  */
352 HWTEST_F(VerifyElfTest, CheckSignFile, testing::ext::TestSize.Level1)
353 {
354     std::string file = "./elfVerify/elf_signed_package.elf";
355 
356     bool flag = VerifyElf::CheckSignFile(file);
357 
358     EXPECT_EQ(flag, true);
359 }
360 
361 /**
362  * @tc.name: GetSignBlockData
363  * @tc.desc: Test function of VerifyElfTest::GetSignBlockData() interface for FAILED.
364  * @tc.size: MEDIUM
365  * @tc.type: FUNC
366  * @tc.level Level 1
367  * @tc.require: SR000H63TL
368  */
369 HWTEST_F(VerifyElfTest, GetSignBlockData, testing::ext::TestSize.Level1)
370 {
371     std::vector<int8_t> bytes = { 1, 1, 1, 1, 1, 1, 1, 1 };
372     BlockData blockData(0, 0);
373 
374     bool flag = VerifyElf::GetSignBlockData(bytes, blockData, "elf");
375 
376     EXPECT_EQ(flag, false);
377 }
378 
379 /**
380  * @tc.name: GetSignBlockInfo
381  * @tc.desc: Test function of VerifyElfTest::GetSignBlockInfo() interface for SUCCESS.
382  * @tc.size: MEDIUM
383  * @tc.type: FUNC
384  * @tc.level Level 1
385  * @tc.require: SR000H63TL
386  */
387 HWTEST_F(VerifyElfTest, GetSignBlockInfo, testing::ext::TestSize.Level1)
388 {
389     std::string file = "./elfVerify/elf_signed_package.elf";
390     SignBlockInfo signBlockInfo(false);
391 
392     bool flag = VerifyElf::GetSignBlockInfo(file, signBlockInfo, "elf");
393 
394     EXPECT_EQ(flag, true);
395 }
396 
397 /**
398  * @tc.name: GetFileDigest
399  * @tc.desc: Test function of VerifyElfTest::GetFileDigest() interface for FAILED.
400  * @tc.size: MEDIUM
401  * @tc.type: FUNC
402  * @tc.level Level 1
403  * @tc.require: SR000H63TL
404  */
405 HWTEST_F(VerifyElfTest, GetFileDigest, testing::ext::TestSize.Level1)
406 {
407     std::string file = "./elfVerify/elf_signed_package.elf";
408     SignBlockInfo signBlockInfo(false);
409     VerifyElf::GetSignBlockInfo(file, signBlockInfo, "elf");
410     std::vector<int8_t> fileBytes = { 1, 1, 1, 1, 1, 1, 1, 1 };
411     std::vector<int8_t> signatrue = { 1, 1, 1, 1, 1, 1, 1, 1 };
412 
413     bool flag = VerifyElf::GetFileDigest(fileBytes, signatrue, signBlockInfo);
414 
415     EXPECT_EQ(flag, false);
416 }
417 
418 /**
419  * @tc.name: GetRawContent
420  * @tc.desc: Test function of VerifyElfTest::GetRawContent() interface for FAILED.
421  * @tc.size: MEDIUM
422  * @tc.type: FUNC
423  * @tc.level Level 1
424  * @tc.require: SR000H63TL
425  */
426 HWTEST_F(VerifyElfTest, GetRawContent, testing::ext::TestSize.Level1)
427 {
428     std::vector<int8_t> contentVec = { 1, 1, 1, 1, 1, 1, 1, 1 };
429     std::string rawContent;
430 
431     bool flag = VerifyElf::GetRawContent(contentVec, rawContent);
432 
433     EXPECT_EQ(flag, false);
434 }
435 
436 /**
437  * @tc.name: VerifyP7b
438  * @tc.desc: Test function of VerifyElfTest::VerifyP7b() interface for SUCCESS.
439  * @tc.size: MEDIUM
440  * @tc.type: FUNC
441  * @tc.level Level 1
442  * @tc.require: SR000H63TL
443  */
444 HWTEST_F(VerifyElfTest, VerifyP7b, testing::ext::TestSize.Level1)
445 {
446     std::unordered_map<int8_t, SigningBlock> signBlockMap;
447     Options options;
448     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_signed_package.elf"));
449     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
450     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
451     Pkcs7Context pkcs7Context;
452     std::vector<int8_t> profileVec;
453     std::string profileJson;
454 
455     bool flag = VerifyElf::VerifyP7b(signBlockMap, &options, pkcs7Context, profileVec, profileJson);
456 
457     EXPECT_EQ(flag, true);
458 }
459 
460 /**
461  * @tc.name: SignElf001
462  * @tc.desc: sign elf with code sign
463  * @tc.size: MEDIUM
464  * @tc.type: FUNC
465  * @tc.level Level 1
466  * @tc.require: SR000H63TL
467  */
468 HWTEST_F(VerifyElfTest, SignElf001, testing::ext::TestSize.Level1)
469 {
470     Options options;
471     options.emplace("mode", std::string("localSign"));
472     options.emplace("keyPwd", std::string("123456"));
473     options.emplace("outFile", std::string("./hapSign/entry-default-signed.elf"));
474     options.emplace("keyAlias", std::string("oh-app1-key-v1"));
475     options.emplace("profileFile", std::string("./hapSign/signed-profile.p7b"));
476     options.emplace("signAlg", std::string("SHA256withECDSA"));
477     options.emplace("keystorePwd", std::string("123456"));
478     options.emplace("keystoreFile", std::string("./hapSign/ohtest.jks"));
479     options.emplace("appCertFile", std::string("./hapSign/app-release1.pem"));
480     options.emplace("inFile", std::string("./elfVerify/elf_unsigned_package.elf"));
481     options.emplace("signCode", std::string("1"));
482     options.emplace("inForm", std::string("elf"));
483 
484     SignProvider signProvider;
485     bool flag = signProvider.SignElf(&options);
486 
487     EXPECT_EQ(flag, false);
488 }
489 
490 /**
491  * @tc.name: SignElf002
492  * @tc.desc: sign elf without code sign
493  * @tc.size: MEDIUM
494  * @tc.type: FUNC
495  * @tc.level Level 1
496  * @tc.require: SR000H63TL
497  */
498 HWTEST_F(VerifyElfTest, SignElf002, testing::ext::TestSize.Level1)
499 {
500     Options options;
501     options.emplace("mode", std::string("localSign"));
502     options.emplace("keyPwd", std::string("123456"));
503     options.emplace("outFile", std::string("./hapSign/entry-default-signed.elf"));
504     options.emplace("keyAlias", std::string("oh-app1-key-v1"));
505     options.emplace("profileFile", std::string("./hapSign/signed-profile.p7b"));
506     options.emplace("signAlg", std::string("SHA256withECDSA"));
507     options.emplace("keystorePwd", std::string("123456"));
508     options.emplace("keystoreFile", std::string("./hapSign/ohtest.jks"));
509     options.emplace("appCertFile", std::string("./hapSign/app-release1.pem"));
510     options.emplace("inFile", std::string("./elfVerify/elf_unsigned_package.elf"));
511     options.emplace("signCode", std::string("0"));
512 
513     SignProvider signProvider;
514     bool flag = signProvider.SignElf(&options);
515 
516     EXPECT_EQ(flag, false);
517 }
518 
519 /**
520  * @tc.name: SignElf003
521  * @tc.desc: invalid param signCode
522  * @tc.size: MEDIUM
523  * @tc.type: FUNC
524  * @tc.level Level 1
525  * @tc.require: SR000H63TL
526  */
527 HWTEST_F(VerifyElfTest, SignElf003, testing::ext::TestSize.Level1)
528 {
529     Options options;
530     options.emplace("mode", std::string("localSign"));
531     options.emplace("keyPwd", std::string("123456"));
532     options.emplace("outFile", std::string("./hapSign/entry-default-signed.elf"));
533     options.emplace("keyAlias", std::string("oh-app1-key-v1"));
534     options.emplace("profileFile", std::string("./hapSign/signed-profile.p7b"));
535     options.emplace("signAlg", std::string("SHA256withECDSA"));
536     options.emplace("keystorePwd", std::string("123456"));
537     options.emplace("keystoreFile", std::string("./hapSign/ohtest.jks"));
538     options.emplace("appCertFile", std::string("./hapSign/app-release1.pem"));
539     options.emplace("inFile", std::string("./elfVerify/elf_unsigned_package.elf"));
540     options.emplace("signCode", std::string("2"));
541     options.emplace("inForm", std::string("elf"));
542 
543     SignProvider signProvider;
544     bool flag = signProvider.SignElf(&options);
545 
546     EXPECT_EQ(flag, false);
547 }
548 
549 /**
550  * @tc.name: SignElf004
551  * @tc.desc: input file without suffix
552  * @tc.size: MEDIUM
553  * @tc.type: FUNC
554  * @tc.level Level 1
555  * @tc.require: SR000H63TL
556  */
557 HWTEST_F(VerifyElfTest, SignElf004, testing::ext::TestSize.Level1)
558 {
559     Options options;
560     options.emplace("mode", std::string("localSign"));
561     options.emplace("keyPwd", std::string("123456"));
562     options.emplace("outFile", std::string("./hapSign/entry-default-signed.elf"));
563     options.emplace("keyAlias", std::string("oh-app1-key-v1"));
564     options.emplace("profileFile", std::string("./hapSign/signed-profile.p7b"));
565     options.emplace("signAlg", std::string("SHA256withECDSA"));
566     options.emplace("keystorePwd", std::string("123456"));
567     options.emplace("keystoreFile", std::string("./hapSign/ohtest.jks"));
568     options.emplace("appCertFile", std::string("./hapSign/app-release1.pem"));
569     options.emplace("inFile", std::string("./elfVerify/elf_unsigned_package"));
570     options.emplace("signCode", std::string("1"));
571     options.emplace("inForm", std::string("elf"));
572 
573     SignProvider signProvider;
574     bool flag = signProvider.SignElf(&options);
575 
576     EXPECT_EQ(flag, false);
577 }
578 
579 /**
580  * @tc.name: SignElf005
581  * @tc.desc: input file not exist
582  * @tc.size: MEDIUM
583  * @tc.type: FUNC
584  * @tc.level Level 1
585  * @tc.require: SR000H63TL
586  */
587 HWTEST_F(VerifyElfTest, SignElf005, testing::ext::TestSize.Level1)
588 {
589     Options options;
590     options.emplace("mode", std::string("localSign"));
591     options.emplace("keyPwd", std::string("123456"));
592     options.emplace("outFile", std::string("./hapSign/entry-default-signed.elf"));
593     options.emplace("keyAlias", std::string("oh-app1-key-v1"));
594     options.emplace("profileFile", std::string("./hapSign/signed-profile.p7b"));
595     options.emplace("signAlg", std::string("SHA256withECDSA"));
596     options.emplace("keystorePwd", std::string("123456"));
597     options.emplace("keystoreFile", std::string("./hapSign/ohtest.jks"));
598     options.emplace("appCertFile", std::string("./hapSign/app-release1.pem"));
599     options.emplace("inFile", std::string("./elfVerify/elf_unsigned_package111.elf"));
600     options.emplace("signCode", std::string("1"));
601     options.emplace("inForm", std::string("elf"));
602 
603     SignProvider signProvider;
604     bool flag = signProvider.SignElf(&options);
605 
606     EXPECT_EQ(flag, false);
607 }
608 
609 /**
610  * @tc.name: SignElf006
611  * @tc.desc: pem file not exist
612  * @tc.size: MEDIUM
613  * @tc.type: FUNC
614  * @tc.level Level 1
615  * @tc.require: SR000H63TL
616  */
617 HWTEST_F(VerifyElfTest, SignElf006, testing::ext::TestSize.Level1)
618 {
619     Options options;
620     options.emplace("mode", std::string("localSign"));
621     options.emplace("keyPwd", std::string("123456"));
622     options.emplace("outFile", std::string("./hapSign/entry-default-signed.elf"));
623     options.emplace("keyAlias", std::string("oh-app1-key-v1"));
624     options.emplace("profileFile", std::string("./hapSign/signed-profile.p7b"));
625     options.emplace("signAlg", std::string("SHA512withECDSA"));
626     options.emplace("keystorePwd", std::string("123456"));
627     options.emplace("keystoreFile", std::string("./hapSign/ohtest.jks"));
628     options.emplace("appCertFile", std::string("./hapSign/app-release1.pem"));
629     options.emplace("inFile", std::string("./elfVerify/elf_unsigned_package.elf"));
630     options.emplace("signCode", std::string("1"));
631     options.emplace("inForm", std::string("elf"));
632 
633     SignProvider signProvider;
634     bool flag = signProvider.SignElf(&options);
635 
636     EXPECT_EQ(flag, false);
637 }
638 
639 /**
640  * @tc.name: SignElf007
641  * @tc.desc: invalid compatible version
642  * @tc.size: MEDIUM
643  * @tc.type: FUNC
644  * @tc.level Level 1
645  * @tc.require: SR000H63TL
646  */
647 HWTEST_F(VerifyElfTest, SignElf007, testing::ext::TestSize.Level1)
648 {
649     Options options;
650     options.emplace("mode", std::string("localSign"));
651     options.emplace("keyPwd", std::string("123456"));
652     options.emplace("outFile", std::string("./hapSign/entry-default-signed.elf"));
653     options.emplace("keyAlias", std::string("oh-app1-key-v1"));
654     options.emplace("profileFile", std::string("./hapSign/signed-profile.p7b"));
655     options.emplace("signAlg", std::string("SHA256withECDSA"));
656     options.emplace("keystorePwd", std::string("123456"));
657     options.emplace("keystoreFile", std::string("./hapSign/ohtest.jks"));
658     options.emplace("appCertFile", std::string("./hapSign/app-release1.pem"));
659     options.emplace("inFile", std::string("./elfVerify/elf_unsigned_package.elf"));
660     options.emplace("signCode", std::string("1"));
661     options.emplace("inForm", std::string("elf"));
662     options.emplace("compatibleVersion", std::string("a"));
663 
664     SignProvider signProvider;
665     bool flag = signProvider.SignElf(&options);
666 
667     EXPECT_EQ(flag, false);
668 }
669 
670 /**
671  * @tc.name: SignElf008
672  * @tc.desc: p7b file not exist
673  * @tc.size: MEDIUM
674  * @tc.type: FUNC
675  * @tc.level Level 1
676  * @tc.require: SR000H63TL
677  */
678 HWTEST_F(VerifyElfTest, SignElf008, testing::ext::TestSize.Level1)
679 {
680     Options options;
681     options.emplace("mode", std::string("localSign"));
682     options.emplace("keyPwd", std::string("123456"));
683     options.emplace("outFile", std::string("./hapSign/entry-default-signed.elf"));
684     options.emplace("keyAlias", std::string("oh-app1-key-v1"));
685     options.emplace("profileFile", std::string("./hapSign/signed-profile111.p7b"));
686     options.emplace("signAlg", std::string("SHA256withECDSA"));
687     options.emplace("keystorePwd", std::string("123456"));
688     options.emplace("keystoreFile", std::string("./hapSign/ohtest.jks"));
689     options.emplace("appCertFile", std::string("./hapSign/app-release1.pem"));
690     options.emplace("inFile", std::string("./elfVerify/elf_unsigned_package.elf"));
691     options.emplace("signCode", std::string("1"));
692     options.emplace("inForm", std::string("elf"));
693 
694     SignProvider signProvider;
695     bool flag = signProvider.SignElf(&options);
696 
697     EXPECT_EQ(flag, false);
698 }
699 
700 /**
701  * @tc.name: SignBin001
702  * @tc.desc: invalid alg SHA512withECDSA
703  * @tc.size: MEDIUM
704  * @tc.type: FUNC
705  * @tc.level Level 1
706  * @tc.require: SR000H63TL
707  */
708 HWTEST_F(VerifyElfTest, SignBin001, testing::ext::TestSize.Level1)
709 {
710     Options options;
711     options.emplace("mode", std::string("localSign"));
712     options.emplace("keyPwd", std::string("123456"));
713     options.emplace("outFile", std::string("./hapSign/entry-default-signed.elf"));
714     options.emplace("keyAlias", std::string("oh-app1-key-v1"));
715     options.emplace("profileFile", std::string("./hapSign/signed-profile.p7b"));
716     options.emplace("signAlg", std::string("SHA512withECDSA"));
717     options.emplace("keystorePwd", std::string("123456"));
718     options.emplace("keystoreFile", std::string("./hapSign/ohtest.jks"));
719     options.emplace("appCertFile", std::string("./hapSign/app-release1.pem"));
720     options.emplace("inFile", std::string("./elfVerify/elf_unsigned_package.elf"));
721     options.emplace("inForm", std::string("bin"));
722 
723     SignProvider signProvider;
724     bool flag = signProvider.SignBin(&options);
725 
726     EXPECT_EQ(flag, false);
727 }
728 
729 /**
730  * @tc.name: WriteVerifyOutput001
731  * @tc.desc: cer file not exist
732  * @tc.size: MEDIUM
733  * @tc.type: FUNC
734  * @tc.level Level 1
735  * @tc.require: SR000H63TL
736  */
737 HWTEST_F(VerifyElfTest, WriteVerifyOutput001, testing::ext::TestSize.Level1)
738 {
739     Options options;
740     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_signed_package.elf"));
741     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
742 
743     SignBlockInfo signBlockInfo(false);
744     VerifyElf::GetSignBlockInfo("./elfVerify/elf_signed_package.elf", signBlockInfo, "elf");
745     std::unordered_map<int8_t, SigningBlock> signBlockMap = signBlockInfo.GetSignBlockMap();
746     SigningBlock profileSign = signBlockMap.find(2)->second;
747     std::vector<int8_t> profileByte = profileSign.GetValue();
748     std::vector<int8_t> profileVec;
749     Pkcs7Context pkcs7Context;
750     VerifyHap hapVerifyV2;
751     std::unique_ptr<ByteBuffer> profileBuffer =
752         std::make_unique<ByteBuffer>((char*)profileByte.data(), profileByte.size());
753     hapVerifyV2.VerifyAppPkcs7(pkcs7Context, *profileBuffer);
754     int32_t flag = hapVerifyV2.WriteVerifyOutput(pkcs7Context, profileVec, &options);
755 
756     EXPECT_NE(flag, 0);
757 }
758 
759 /**
760  * @tc.name: WriteVerifyOutput002
761  * @tc.desc: p7b file not exist
762  * @tc.size: MEDIUM
763  * @tc.type: FUNC
764  * @tc.level Level 1
765  * @tc.require: SR000H63TL
766  */
767 HWTEST_F(VerifyElfTest, WriteVerifyOutput002, testing::ext::TestSize.Level1)
768 {
769     Options options;
770     options.emplace(Options::IN_FILE, std::string("./elfVerify/elf_signed_package.elf"));
771     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
772 
773     SignBlockInfo signBlockInfo(false);
774     VerifyElf::GetSignBlockInfo("./elfVerify/elf_signed_package.elf", signBlockInfo, "elf");
775     std::unordered_map<int8_t, SigningBlock> signBlockMap = signBlockInfo.GetSignBlockMap();
776     SigningBlock profileSign = signBlockMap.find(2)->second;
777     std::vector<int8_t> profileByte = profileSign.GetValue();
778     std::vector<int8_t> profileVec;
779     Pkcs7Context pkcs7Context;
780     VerifyHap hapVerifyV2;
781     std::unique_ptr<ByteBuffer> profileBuffer =
782         std::make_unique<ByteBuffer>((char*)profileByte.data(), profileByte.size());
783     hapVerifyV2.VerifyAppPkcs7(pkcs7Context, *profileBuffer);
784     int32_t flag = hapVerifyV2.WriteVerifyOutput(pkcs7Context, profileVec, &options);
785 
786     EXPECT_NE(flag, 0);
787 }
788 } // namespace SignatureTools
789 } // namespace OHOS