• 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 #include "verify_code_signature.h"
20 #include "verify_hap.h"
21 #include "hap_utils.h"
22 
23 namespace OHOS {
24 namespace SignatureTools {
25 
26 /*
27  * 测试套件,固定写法
28  */
29 class VerifyCodeSignatureTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase();
33     void SetUp();
34     void TearDown();
35 };
36 
SetUpTestCase(void)37 void VerifyCodeSignatureTest::SetUpTestCase(void)
38 {
39     (void)rename("./codeSigning/hap_no_suffix_err.txt", "./codeSigning/hap_no_suffix_err");
40     (void)rename("./codeSigning/elf_parse_sign_block_err.txt", "./codeSigning/elf_parse_sign_block_err.elf");
41     (void)rename("./codeSigning/hap_merkle_tree_err.txt", "./codeSigning/hap_merkle_tree_err.elf");
42     (void)rename("./codeSigning/hap_profile_is_null_err.txt", "./codeSigning/hap_profile_is_null_err.hap");
43     (void)rename("./codeSigning/hap_invalid_block_header_err.txt", "./codeSigning/hap_invalid_block_header_err.hap");
44     (void)rename("./codeSigning/hap_offset_align_err.txt", "./codeSigning/hap_offset_align_err.hap");
45     (void)rename("./codeSigning/hap_magic_number_err.txt", "./codeSigning/hap_magic_number_err.hap");
46     (void)rename("./codeSigning/hap_segment_header_err.txt", "./codeSigning/hap_segment_header_err.hap");
47     (void)rename("./codeSigning/entry-default-signed-so.txt", "./codeSigning/entry-default-signed-so.hap");
48     (void)rename("./codeSigning/hap_file_type_err.txt", "./codeSigning/hap_file_type_err.elf");
49     (void)rename("./codeSigning/hap_native_libs_err.txt", "./codeSigning/hap_native_libs_err.hap");
50 }
51 
TearDownTestCase(void)52 void VerifyCodeSignatureTest::TearDownTestCase(void)
53 {
54 }
55 
SetUp()56 void VerifyCodeSignatureTest::SetUp()
57 {
58 }
59 
TearDown()60 void VerifyCodeSignatureTest::TearDown()
61 {
62 }
63 
64 /**
65  * @tc.name: AreVectorsEqual001
66  * @tc.desc: Test function of VerifyCodeSignatureTest::AreVectorsEqual() interface for SUCCESS.
67  * @tc.size: MEDIUM
68  * @tc.type: FUNC
69  * @tc.level Level 1
70  * @tc.require: SR000H63TL
71  */
72 HWTEST_F(VerifyCodeSignatureTest, AreVectorsEqual001, testing::ext::TestSize.Level1)
73 {
74     std::vector<int8_t> vec1{ 21, 53, 29, 18, -17, 56, 92, 60, 29, 10, 28, 18, 19, 52, 59, 92 };
75     std::vector<int8_t> vec2{ 21, 53, 29, 18, -17, 56, 92, 60, 29, 10, 28, 18, 19, 52, 59, 92 };
76     bool flag = VerifyCodeSignature::AreVectorsEqual(vec1, vec2);
77 
78     EXPECT_EQ(flag, true);
79 }
80 
81 /**
82  * @tc.name: AreVectorsEqual002
83  * @tc.desc: Test function of VerifyCodeSignatureTest::AreVectorsEqual() interface for FAILED.
84  * @tc.size: MEDIUM
85  * @tc.type: FUNC
86  * @tc.level Level 1
87  * @tc.require: SR000H63TL
88  */
89 HWTEST_F(VerifyCodeSignatureTest, AreVectorsEqual002, testing::ext::TestSize.Level1)
90 {
91     std::vector<int8_t> vec1{ 21, 53, 29, 19, -17, 56, 92, 60, 29, 10, 28, 18, 19 };
92     std::vector<int8_t> vec2{ 21, 53, 29, 18, -17, 56, 92, 60, 29, 10, 28, 18, 19, 52, 59, 92 };
93     bool flag = VerifyCodeSignature::AreVectorsEqual(vec1, vec2);
94 
95     EXPECT_EQ(flag, false);
96 }
97 
98 /**
99  * @tc.name: VerifyCodeSign
100  * @tc.desc: verify code sign without profile content
101  * @tc.size: MEDIUM
102  * @tc.type: FUNC
103  * @tc.level Level 1
104  * @tc.require: SR000H63TL
105  */
106 HWTEST_F(VerifyCodeSignatureTest, VerifyCodeSign, testing::ext::TestSize.Level1)
107 {
108     std::pair<std::string, std::string> pairResult = std::make_pair("", "debug");
109     std::string file = "./codeSigning/entry-default-signed-so.hap";
110     int64_t offset = 1397151;
111     int64_t length = 23193;
112     std::string fileFormat = "hap";
113     std::string profileContent = "";
114     VerifyCodeSignature::VerifyHap(file, offset, length, fileFormat, profileContent);
115     CodeSignBlock csb;
116     bool flag = VerifyCodeSignature::VerifyCodeSign(file, pairResult, csb);
117 
118     EXPECT_EQ(flag, false);
119 }
120 
121 /**
122  * @tc.name: VerifyHap001
123  * @tc.desc: verify hap without profile content
124  * @tc.size: MEDIUM
125  * @tc.type: FUNC
126  * @tc.level Level 1
127  * @tc.require: SR000H63TL
128  */
129 HWTEST_F(VerifyCodeSignatureTest, VerifyHap001, testing::ext::TestSize.Level1)
130 {
131     std::string file = "./codeSigning/entry-default-signed-so.hap";
132     int64_t offset = 1397151;
133     int64_t length = 23193;
134     std::string fileFormat = "hap";
135     std::string profileContent = "";
136     bool flag = VerifyCodeSignature::VerifyHap(file, offset, length, fileFormat, profileContent);
137 
138     EXPECT_EQ(flag, false);
139 }
140 
141 /**
142  * @tc.name: VerifyHap002
143  * @tc.desc: Test function of VerifyCodeSignatureTest::VerifyHap() interface for SUCCESS.
144  * @tc.size: MEDIUM
145  * @tc.type: FUNC
146  * @tc.level Level 1
147  * @tc.require: SR000H63TL
148  */
149 HWTEST_F(VerifyCodeSignatureTest, VerifyHap002, testing::ext::TestSize.Level1)
150 {
151     std::string file = "./codeSigning/entry-default-signed-so.hap";
152     int64_t offset = 1397151;
153     int64_t length = 23193;
154     std::string fileFormat = "";
155     std::string profileContent = "";
156     bool flag = VerifyCodeSignature::VerifyHap(file, offset, length, fileFormat, profileContent);
157 
158     EXPECT_EQ(flag, true);
159 }
160 
161 /**
162  * @tc.name: VerifyHap003
163  * @tc.desc: verify hap with invalid offset
164  * @tc.size: MEDIUM
165  * @tc.type: FUNC
166  * @tc.level Level 1
167  * @tc.require: SR000H63TL
168  */
169 HWTEST_F(VerifyCodeSignatureTest, VerifyHap003, testing::ext::TestSize.Level1)
170 {
171     std::string file = "./codeSigning/entry-default-signed-so.hap";
172     int64_t offset = 1397151;
173     int64_t length = 23194;
174     std::string fileFormat = "hap";
175     std::string profileContent = "";
176     bool flag = VerifyCodeSignature::VerifyHap(file, offset, length, fileFormat, profileContent);
177 
178     EXPECT_EQ(flag, false);
179 }
180 
181 /**
182  * @tc.name: VerifyHap004
183  * @tc.desc: verify hap without input file
184  * @tc.size: MEDIUM
185  * @tc.type: FUNC
186  * @tc.level Level 1
187  * @tc.require: SR000H63TL
188  */
189 HWTEST_F(VerifyCodeSignatureTest, VerifyHap004, testing::ext::TestSize.Level1)
190 {
191     std::string file = "";
192     int64_t offset = 1397151;
193     int64_t length = 23193;
194     std::string fileFormat = "hap";
195     std::string profileContent = "";
196     bool flag = VerifyCodeSignature::VerifyHap(file, offset, length, fileFormat, profileContent);
197 
198     EXPECT_EQ(flag, false);
199 }
200 
201 /**
202  * @tc.name: VerifyHap005
203  * @tc.desc: verify hap with invalid block header
204  * @tc.size: MEDIUM
205  * @tc.type: FUNC
206  * @tc.level Level 1
207  * @tc.require: SR000H63TL
208  */
209 HWTEST_F(VerifyCodeSignatureTest, VerifyHap005, testing::ext::TestSize.Level1)
210 {
211     std::string file = "./codeSigning/hap_invalid_block_header_err.hap";
212     int64_t offset = 1397151;
213     int64_t length = 23221;
214     std::string fileFormat = "hap";
215     std::string profileContent = "{\"version-code\":1,\"version-name\":\"1.0.0\"}";
216     bool flag = VerifyCodeSignature::VerifyHap(file, offset, length, fileFormat, profileContent);
217 
218     EXPECT_EQ(flag, false);
219 }
220 
221 /**
222  * @tc.name: VerifyHap006
223  * @tc.desc: hap offset align error
224  * @tc.size: MEDIUM
225  * @tc.type: FUNC
226  * @tc.level Level 1
227  * @tc.require: SR000H63TL
228  */
229 HWTEST_F(VerifyCodeSignatureTest, VerifyHap006, testing::ext::TestSize.Level1)
230 {
231     std::string file = "./codeSigning/hap_offset_align_err.hap";
232     int64_t offset = 1397151;
233     int64_t length = 23193;
234     std::string fileFormat = "hap";
235     std::string profileContent = "{\"version-code\":1,\"version-name\":\"1.0.0\"}";
236     bool flag = VerifyCodeSignature::VerifyHap(file, offset, length, fileFormat, profileContent);
237 
238     EXPECT_EQ(flag, false);
239 }
240 
241 /**
242  * @tc.name: VerifyHap007
243  * @tc.desc: hap magic number error
244  * @tc.size: MEDIUM
245  * @tc.type: FUNC
246  * @tc.level Level 1
247  * @tc.require: SR000H63TL
248  */
249 HWTEST_F(VerifyCodeSignatureTest, VerifyHap007, testing::ext::TestSize.Level1)
250 {
251     std::string file = "./codeSigning/hap_magic_number_err.hap";
252     int64_t offset = 1397151;
253     int64_t length = 23193;
254     std::string fileFormat = "hap";
255     std::string profileContent = "{\"version-code\":1,\"version-name\":\"1.0.0\"}";
256     bool flag = VerifyCodeSignature::VerifyHap(file, offset, length, fileFormat, profileContent);
257 
258     EXPECT_EQ(flag, false);
259 }
260 
261 /**
262  * @tc.name: VerifyHap008
263  * @tc.desc: hap segment header error
264  * @tc.size: MEDIUM
265  * @tc.type: FUNC
266  * @tc.level Level 1
267  * @tc.require: SR000H63TL
268  */
269 HWTEST_F(VerifyCodeSignatureTest, VerifyHap008, testing::ext::TestSize.Level1)
270 {
271     std::string file = "./codeSigning/hap_segment_header_err.hap";
272     int64_t offset = 1397151;
273     int64_t length = 23193;
274     std::string fileFormat = "hap";
275     std::string profileContent = "{\"version-code\":1,\"version-name\":\"1.0.0\"}";
276     bool flag = VerifyCodeSignature::VerifyHap(file, offset, length, fileFormat, profileContent);
277 
278     EXPECT_EQ(flag, false);
279 }
280 
281 /**
282  * @tc.name: VerifySingleFile
283  * @tc.desc: verify single file with invalid merkle tree offset
284  * @tc.size: MEDIUM
285  * @tc.type: FUNC
286  * @tc.level Level 1
287  * @tc.require: SR000H63TL
288  */
289 HWTEST_F(VerifyCodeSignatureTest, VerifySingleFile, testing::ext::TestSize.Level1)
290 {
291     std::ifstream input;
292     input.open("./codeSigning/entry-default-signed-so.hap", std::ios::binary);
293     int64_t length = 4096;
294     std::vector<int8_t> signature;
295     int64_t merkleTreeOffset = 64;
296     std::vector<int8_t> inMerkleTreeBytes;
297     bool flag = VerifyCodeSignature::VerifySingleFile(input, length, signature, merkleTreeOffset, inMerkleTreeBytes);
298 
299     EXPECT_EQ(flag, false);
300 }
301 
302 /**
303  * @tc.name: VerifyElf001
304  * @tc.desc: elf merkle tree error with elf
305  * @tc.size: MEDIUM
306  * @tc.type: FUNC
307  * @tc.level Level 1
308  * @tc.require: SR000H63TL
309  */
310 HWTEST_F(VerifyCodeSignatureTest, VerifyElf001, testing::ext::TestSize.Level1)
311 {
312     std::string file = "./codeSigning/hap_merkle_tree_err.elf";
313     int64_t offset = 8216;
314     int64_t length = 10516;
315     std::string fileFormat = "elf";
316     std::string profileContent = "{\"version-code\":1,\"version-name\":\"1.0.0\"}";
317     bool flag = VerifyCodeSignature::VerifyElf(file, offset, length, fileFormat, profileContent);
318 
319     EXPECT_EQ(flag, false);
320 }
321 
322 /**
323  * @tc.name: VerifyElf002
324  * @tc.desc: elf merkle tree error with hap
325  * @tc.size: MEDIUM
326  * @tc.type: FUNC
327  * @tc.level Level 1
328  * @tc.require: SR000H63TL
329  */
330 HWTEST_F(VerifyCodeSignatureTest, VerifyElf002, testing::ext::TestSize.Level1)
331 {
332     std::string file = "./codeSigning/hap_merkle_tree_err.elf";
333     int64_t offset = 8216;
334     int64_t length = 10516;
335     std::string fileFormat = "hap";
336     std::string profileContent = "{\"version-code\":1,\"version-name\":\"1.0.0\"}";
337     bool flag = VerifyCodeSignature::VerifyElf(file, offset, length, fileFormat, profileContent);
338 
339     EXPECT_EQ(flag, true);
340 }
341 
342 /**
343  * @tc.name: VerifyElf003
344  * @tc.desc: verify elf with invalid input file
345  * @tc.size: MEDIUM
346  * @tc.type: FUNC
347  * @tc.level Level 1
348  * @tc.require: SR000H63TL
349  */
350 HWTEST_F(VerifyCodeSignatureTest, VerifyElf003, testing::ext::TestSize.Level1)
351 {
352     std::string file = "./codeSigning/hap_merkle_tree_err111.elf";
353     int64_t offset = 8216;
354     int64_t length = 10516;
355     std::string fileFormat = "elf";
356     std::string profileContent = "{\"version-code\":1,\"version-name\":\"1.0.0\"}";
357     bool flag = VerifyCodeSignature::VerifyElf(file, offset, length, fileFormat, profileContent);
358 
359     EXPECT_EQ(flag, false);
360 }
361 
362 /**
363  * @tc.name: VerifyElf004
364  * @tc.desc: verify elf with invalid length
365  * @tc.size: MEDIUM
366  * @tc.type: FUNC
367  * @tc.level Level 1
368  * @tc.require: SR000H63TL
369  */
370 HWTEST_F(VerifyCodeSignatureTest, VerifyElf004, testing::ext::TestSize.Level1)
371 {
372     std::string file = "./codeSigning/hap_merkle_tree_err.elf";
373     int64_t offset = 8216;
374     int64_t length = 2;
375     std::string fileFormat = "elf";
376     std::string profileContent = "{\"version-code\":1,\"version-name\":\"1.0.0\"}";
377     bool flag = VerifyCodeSignature::VerifyElf(file, offset, length, fileFormat, profileContent);
378 
379     EXPECT_EQ(flag, false);
380 }
381 
382 /**
383  * @tc.name: VerifyElf005
384  * @tc.desc: verify elf with invalid app-identifier
385  * @tc.size: MEDIUM
386  * @tc.type: FUNC
387  * @tc.level Level 1
388  * @tc.require: SR000H63TL
389  */
390 HWTEST_F(VerifyCodeSignatureTest, VerifyElf005, testing::ext::TestSize.Level1)
391 {
392     std::string file = "./codeSigning/hap_merkle_tree_err.elf";
393     int64_t offset = 8216;
394     int64_t length = 10516;
395     std::string fileFormat = "elf";
396     std::string profileContent = "{\"version-code\":1,\"app-identifier\":\"111\"}";
397     bool flag = VerifyCodeSignature::VerifyElf(file, offset, length, fileFormat, profileContent);
398 
399     EXPECT_EQ(flag, false);
400 }
401 
402 /**
403  * @tc.name: VerifyElf006
404  * @tc.desc: hap file type error
405  * @tc.size: MEDIUM
406  * @tc.type: FUNC
407  * @tc.level Level 1
408  * @tc.require: SR000H63TL
409  */
410 HWTEST_F(VerifyCodeSignatureTest, VerifyElf006, testing::ext::TestSize.Level1)
411 {
412     std::string file = "./codeSigning/hap_file_type_err.elf";
413     int64_t offset = 8216;
414     int64_t length = 10516;
415     std::string fileFormat = "elf";
416     std::string profileContent = "{\"version-code\":1,\"version-name\":\"1.0.0\"}";
417     bool flag = VerifyCodeSignature::VerifyElf(file, offset, length, fileFormat, profileContent);
418 
419     EXPECT_EQ(flag, false);
420 }
421 
422 /**
423  * @tc.name: VerifyElf007
424  * @tc.desc: parse elf sign block error
425  * @tc.size: MEDIUM
426  * @tc.type: FUNC
427  * @tc.level Level 1
428  * @tc.require: SR000H63TL
429  */
430 HWTEST_F(VerifyCodeSignatureTest, VerifyElf007, testing::ext::TestSize.Level1)
431 {
432     std::string file = "./codeSigning/elf_parse_sign_block_err.elf";
433     int64_t offset = 8216;
434     int64_t length = 10516;
435     std::string fileFormat = "elf";
436     std::string profileContent = "{\"version-code\":1,\"version-name\":\"1.0.0\"}";
437     bool flag = VerifyCodeSignature::VerifyElf(file, offset, length, fileFormat, profileContent);
438 
439     EXPECT_EQ(flag, false);
440 }
441 
442 /**
443  * @tc.name: VerifyNativeLib001
444  * @tc.desc: verify hap native libs error
445  * @tc.size: MEDIUM
446  * @tc.type: FUNC
447  * @tc.level Level 1
448  * @tc.require: SR000H63TL
449  */
450 HWTEST_F(VerifyCodeSignatureTest, VerifyNativeLib001, testing::ext::TestSize.Level1)
451 {
452     std::string file = "./codeSigning/hap_native_libs_err.hap";
453     unzFile zFile = unzOpen(file.c_str());
454     std::pair<std::string, std::string> pairResult;
455     pairResult.first = "111";
456     pairResult.second = "222";
457     CodeSignBlock csb;
458     bool flag = VerifyCodeSignature::VerifyNativeLib(csb, file, zFile, pairResult);
459     unzClose(zFile);
460 
461     EXPECT_EQ(flag, false);
462 }
463 
464 /**
465  * @tc.name: VerifyNativeLib002
466  * @tc.desc: hap profile is null
467  * @tc.size: MEDIUM
468  * @tc.type: FUNC
469  * @tc.level Level 1
470  * @tc.require: SR000H63TL
471  */
472 HWTEST_F(VerifyCodeSignatureTest, VerifyNativeLib002, testing::ext::TestSize.Level1)
473 {
474     std::string file = "./codeSigning/hap_profile_is_null_err.hap";
475     int64_t offset = 1397151;
476     int64_t length = 23221;
477     std::string fileFormat = "hap";
478     std::string profileContent = "{\"version-code\":1,\"version-name\":\"1.0.0\"}";
479     bool flag = VerifyCodeSignature::VerifyHap(file, offset, length, fileFormat, profileContent);
480 
481     EXPECT_EQ(flag, false);
482 }
483 
484 /**
485  * @tc.name: CheckCodeSign001
486  * @tc.desc: Test function of VerifyCodeSignatureTest::CheckCodeSign() interface for SUCCESS.
487  * @tc.size: MEDIUM
488  * @tc.type: FUNC
489  * @tc.level Level 1
490  * @tc.require: SR000H63TL
491  */
492 HWTEST_F(VerifyCodeSignatureTest, CheckCodeSign001, testing::ext::TestSize.Level1)
493 {
494     std::string file = "hap";
495     std::vector<OptionalBlock> optionalBlocks;
496     OptionalBlock block;
497     block.optionalType = HapUtils::HAP_PROPERTY_BLOCK_ID;
498     ByteBuffer bf(8);
499     bf.PutByte('a');
500     block.optionalBlockValue = bf;
501     optionalBlocks.push_back(block);
502 
503     VerifyHap hapVerify;
504     int32_t ret = hapVerify.CheckCodeSign(file, optionalBlocks);
505 
506     EXPECT_EQ(ret, 0);
507 }
508 
509 /**
510  * @tc.name: CheckCodeSign002
511  * @tc.desc: Test function of VerifyCodeSignatureTest::CheckCodeSign() interface for SUCCESS.
512  * @tc.size: MEDIUM
513  * @tc.type: FUNC
514  * @tc.level Level 1
515  * @tc.require: SR000H63TL
516  */
517 HWTEST_F(VerifyCodeSignatureTest, CheckCodeSign002, testing::ext::TestSize.Level1)
518 {
519     std::string file = "test.hap";
520     std::vector<OptionalBlock> optionalBlocks;
521     OptionalBlock block;
522     block.optionalType = HapUtils::HAP_PROPERTY_BLOCK_ID;
523     ByteBuffer bf(8);
524     bf.PutByte('a');
525     block.optionalBlockValue = bf;
526     optionalBlocks.push_back(block);
527 
528     VerifyHap hapVerify;
529     int32_t ret = hapVerify.CheckCodeSign(file, optionalBlocks);
530 
531     EXPECT_EQ(ret, 0);
532 }
533 
534 /**
535  * @tc.name: CheckCodeSign003
536  * @tc.desc: Test function of VerifyCodeSignatureTest::CheckCodeSign() interface for SUCCESS.
537  * @tc.size: MEDIUM
538  * @tc.type: FUNC
539  * @tc.level Level 1
540  * @tc.require: SR000H63TL
541  */
542 HWTEST_F(VerifyCodeSignatureTest, CheckCodeSign003, testing::ext::TestSize.Level1)
543 {
544     std::string file = "test.hap";
545     std::vector<OptionalBlock> optionalBlocks;
546     OptionalBlock block;
547     block.optionalType = HapUtils::HAP_PROPERTY_BLOCK_ID;
548     ByteBuffer bf(16);
549     bf.PutByte('a');
550     block.optionalBlockValue = bf;
551     optionalBlocks.push_back(block);
552 
553     VerifyHap hapVerify;
554     int32_t ret = hapVerify.CheckCodeSign(file, optionalBlocks);
555 
556     EXPECT_EQ(ret, 0);
557 }
558 } // namespace SignatureTools
559 } // namespace OHOS