1 /* 2 * Copyright (c) 2021-2022 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 package com.ohos.hapsigntoolcmd; 17 18 import static org.junit.jupiter.api.Assertions.assertArrayEquals; 19 import static org.junit.jupiter.api.Assertions.assertFalse; 20 import static org.junit.jupiter.api.Assertions.assertTrue; 21 22 import com.ohos.hapsigntool.HapSignTool; 23 import com.ohos.hapsigntool.codesigning.utils.HapUtils; 24 import com.ohos.hapsigntool.entity.ParamConstants; 25 import com.ohos.hapsigntool.error.CustomException; 26 import com.ohos.hapsigntool.error.ProfileException; 27 import com.ohos.hapsigntool.utils.KeyPairTools; 28 import com.ohos.hapsigntool.utils.FileUtils; 29 import com.ohos.hapsigntool.zip.Zip; 30 31 import org.junit.jupiter.api.AfterAll; 32 import org.junit.jupiter.api.BeforeAll; 33 import org.junit.jupiter.api.MethodOrderer; 34 import org.junit.jupiter.api.Order; 35 import org.junit.jupiter.api.Test; 36 import org.junit.jupiter.api.TestMethodOrder; 37 import org.junit.platform.commons.logging.Logger; 38 import org.junit.platform.commons.logging.LoggerFactory; 39 40 import java.io.File; 41 import java.io.FileOutputStream; 42 import java.io.IOException; 43 import java.math.BigInteger; 44 import java.net.URL; 45 import java.nio.file.Files; 46 import java.nio.file.Path; 47 import java.nio.file.Paths; 48 import java.nio.file.StandardCopyOption; 49 import java.util.ArrayList; 50 import java.util.List; 51 import java.util.Map; 52 import java.util.Random; 53 import java.util.jar.JarFile; 54 import java.util.zip.CRC32; 55 import java.util.zip.ZipEntry; 56 import java.util.zip.ZipFile; 57 import java.util.zip.ZipOutputStream; 58 59 /** 60 * CmdUnitTest. 61 * 62 * @since 2021/12/28 63 */ 64 @TestMethodOrder(MethodOrderer.OrderAnnotation.class) 65 public class CmdUnitTest { 66 /** 67 * Command line parameter signCode. 68 */ 69 public static final String CMD_SIGN_CODE = "-signCode"; 70 71 /** 72 * Command line parameter appCaCertFile. 73 */ 74 public static final String CMD_SUB_CA_CERT_FILE = "-subCaCertFile"; 75 76 /** 77 * Command line parameter outFile. 78 */ 79 public static final String CMD_OUT_FILE = "-outFile"; 80 81 /** 82 * Command line parameter basicConstraints. 83 */ 84 public static final String CMD_BASIC_CONSTRAINTS = "-basicConstraints"; 85 86 /** 87 * Command line parameter basicConstraintsCa. 88 */ 89 public static final String CMD_BASIC_CONSTRAINTS_CA = "-basicConstraintsCa"; 90 91 /** 92 * Command line parameter basicConstraintsCritical. 93 */ 94 public static final String CMD_BASIC_CONSTRAINTS_CRITICAL = "-basicConstraintsCritical"; 95 96 /** 97 * Command line parameter basicConstraintsPathLen. 98 */ 99 public static final String CMD_BASIC_CONSTRAINTS_PATH_LEN = "-basicConstraintsPathLen"; 100 101 /** 102 * Command line parameter caCertFile. 103 */ 104 public static final String CMD_ROOT_CA_CERT_FILE = "-rootCaCertFile"; 105 106 /** 107 * Command line parameter outForm. 108 */ 109 public static final String CMD_OUT_FORM = "-outForm"; 110 111 /** 112 * Command line parameter cert. 113 */ 114 public static final String CMD_CERT_CHAIN = "certChain"; 115 116 /** 117 * Command line parameter digitalSignature. 118 */ 119 public static final String CMD_DIGITAL_SIGNATURE = "digitalSignature"; 120 121 /** 122 * Command line parameter codeSignature and emailProtection. 123 */ 124 public static final String CMD_CODE_AND_EMAIL = "codeSignature,emailProtection"; 125 126 /** 127 * Command line parameter mode. 128 */ 129 public static final String CMD_MODE = "-mode"; 130 131 /** 132 * Command line parameter keystoreFile. 133 */ 134 public static final String CMD_KEY_STORE_FILE = "-keystoreFile"; 135 136 /** 137 * Command line parameter keystorePwd. 138 */ 139 public static final String CMD_KEY_STORE_RIGHTS = "-keystorePwd"; 140 141 /** 142 * Command line parameter keyAlg. 143 */ 144 public static final String CMD_KEY_ALG = "-keyAlg"; 145 146 /** 147 * Command line parameter keyAlias. 148 */ 149 public static final String CMD_KEY_ALIAS = "-keyAlias"; 150 151 /** 152 * Command line parameter keyPwd. 153 */ 154 public static final String CMD_KEY_RIGHTS = "-keyPwd"; 155 156 /** 157 * Command line parameter keySize. 158 */ 159 public static final String CMD_KEY_SIZE = "-keySize"; 160 161 /** 162 * Command line parameter keyUsage. 163 */ 164 public static final String CMD_KEY_USAGE = "-keyUsage"; 165 166 /** 167 * Command line parameter keyUsageCritical. 168 */ 169 public static final String CMD_KEY_USAGE_CRITICAL = "-keyUsageCritical"; 170 171 /** 172 * Command line parameter extKeyUsage. 173 */ 174 public static final String CMD_EXT_KEY_USAGE = "-extKeyUsage"; 175 176 /** 177 * Command line parameter extKeyUsageCritical. 178 */ 179 public static final String CMD_EXT_KEY_USAGE_CRITICAL = "-extKeyUsageCritical"; 180 181 /** 182 * Command line parameter profileCertFile. 183 */ 184 public static final String CMD_PROFILE_CERT_FILE = "-profileCertFile"; 185 186 /** 187 * Command line parameter subject. 188 */ 189 public static final String CMD_SUBJECT = "-subject"; 190 191 /** 192 * Command line parameter signAlg. 193 */ 194 public static final String CMD_SIGN_ALG = "-signAlg"; 195 196 /** 197 * Command line parameter inFile. 198 */ 199 public static final String CMD_IN_FILE = "-inFile"; 200 201 /** 202 * Command line parameter issuer. 203 */ 204 public static final String CMD_ISSUER = "-issuer"; 205 206 /** 207 * Command line parameter issuerKeyAlias. 208 */ 209 public static final String CMD_ISSUER_KEY_ALIAS = "-issuerKeyAlias"; 210 211 /** 212 * Command line parameter issuerKeyPwd. 213 */ 214 public static final String CMD_ISSUER_KEY_RIGHTS = "-issuerKeyPwd"; 215 216 /** 217 * Command line parameter validity. 218 */ 219 public static final String CMD_VALIDITY = "-validity"; 220 221 /** 222 * Command line parameter appCertFile. 223 */ 224 public static final String CMD_APP_CERT_FILE = "-appCertFile"; 225 226 /** 227 * Command line parameter appCertFile. 228 */ 229 public static final String CMD_PROFILE_FILE = "-profileFile"; 230 231 /** 232 * Command line parameter appCertFile. 233 */ 234 public static final String CMD_OUT_CERT_CHAIN = "-outCertChain"; 235 236 /** 237 * Command line parameter appCertFile. 238 */ 239 public static final String CMD_OUT_PROFILE = "-outProfile"; 240 241 /** 242 * Command line parameter false. 243 */ 244 public static final String CMD_FALSE = "false"; 245 246 /** 247 * Command line parameter true. 248 */ 249 public static final String CMD_TRUE = "true"; 250 251 /** 252 * Command line parameter basicConstraintsPathLen is 0. 253 */ 254 public static final String CMD_BC_PATH_LEN_0 = "0"; 255 256 /** 257 * Command line parameter password is 123456. 258 */ 259 public static final String CMD_RIGHTS_123456 = "123456"; 260 261 /** 262 * Command line parameter RSA is 2048. 263 */ 264 public static final String CMD_RSA_2048 = "2048"; 265 266 /** 267 * Command line parameter ECC is 256. 268 */ 269 public static final String CMD_ECC_256 = "NIST-P-256"; 270 271 /** 272 * Command line parameter validity is 365. 273 */ 274 public static final String CMD_VALIDITY_365 = "365"; 275 276 /** 277 * Command line parameter json file is UnsgnedDebugProfileTemplate. 278 */ 279 public static final String CMD_JSON_FILE = "UnsgnedDebugProfileTemplate.json"; 280 281 /** 282 * Command line parameter json file is UnsgnedReleaseProfileTemplate. 283 */ 284 public static final String CMD_RELEASE_JSON_FILE = "UnsgnedReleaseProfileTemplate.json"; 285 286 /** 287 * Command line parameter localSign. 288 */ 289 public static final String CMD_LOCAL_SIGN = "localSign"; 290 291 /** 292 * Command line parameter SHA256withECDSA. 293 */ 294 public static final String CMD_SHA_256_WITH_ECDSA = "SHA256withECDSA"; 295 296 /** 297 * Command line parameter cer file is test_app-debug-cert. 298 */ 299 public static final String CMD_APP_DEBUG_CERT_PATH = "test_app-debug-cert.pem"; 300 301 /** 302 * Command line parameter cer file is test_app-release-cert. 303 */ 304 public static final String CMD_APP_RELEASE_CERT_PATH = "test_app-release-cert.pem"; 305 306 /** 307 * Command line parameter cer file is test_cert. 308 */ 309 public static final String CMD_CERT_PATH = "test_cert.cer"; 310 311 /** 312 * Command line parameter csr file is test_csr. 313 */ 314 public static final String CMD_CSR_PATH = "test_csr.csr"; 315 316 /** 317 * Command line parameter jks file is test_app_csr. 318 */ 319 public static final String CMD_KEY_APP_STORE_PATH = "test_app_keypair.jks"; 320 321 /** 322 * Command line parameter jks file is test_profile_csr. 323 */ 324 public static final String CMD_KEY_PROFILE_STORE_PATH = "test_profile_keypair.jks"; 325 326 /** 327 * Command line parameter cer file is test_root_app_ca. 328 */ 329 public static final String CMD_ROOT_APP_CA_PATH = "test_root_app_ca.cer"; 330 331 /** 332 * Command line parameter cer file is test_root_profile_ca. 333 */ 334 public static final String CMD_ROOT_PROFILE_CA_PATH = "test_root_profile_ca.cer"; 335 336 /** 337 * Command line parameter cer file is test_sub_app_ca. 338 */ 339 public static final String CMD_SUB_APP_CA_PATH = "test_sub_app_ca.cer"; 340 341 /** 342 * Command line parameter cer file is test_sub_profile_ca. 343 */ 344 public static final String CMD_SUB_PROFILE_CA_PATH = "test_sub_profile_ca.cer"; 345 346 /** 347 * Command line parameter p7b file is test_sign_profile. 348 */ 349 public static final String CMD_SIGN_PROFILE_PATH = "test_sign_profile.p7b"; 350 351 /** 352 * Command line parameter p7b file is test_sign_profile. 353 */ 354 public static final String CMD_SIGN_RELEASE_PROFILE_PATH = "test_sign_release_profile.p7b"; 355 356 /** 357 * Command line parameter cer file is test_profile-debug-cert. 358 */ 359 public static final String CMD_PROFILE_DEBUG_CERT_PATH = "test_profile-debug-cert.pem"; 360 361 /** 362 * Command line parameter cer file is test_profile-release-cert. 363 */ 364 public static final String CMD_PROFILE_RELEASE_CERT_PATH = "test_profile-release-cert.pem"; 365 366 /** 367 * Command line parameter cer file is test_verify_profile. 368 */ 369 public static final String CMD_VERIFY_PROFILE_RESULT_PATH = "test_verify_profile_result.json"; 370 371 /** 372 * Command line parameter cer file is test_verify_profile. 373 */ 374 public static final String CMD_TEST_HAP_FILE_PATH = "entry-default-unsigned.hap"; 375 376 /** 377 * Command line parameter oh-profile-key-v1. 378 */ 379 public static final String CMD_OH_PROFILE_KEY_V1 = "oh-profile-key-v1"; 380 381 /** 382 * Command line parameter oh-app1-key-v1. 383 */ 384 public static final String CMD_OH_APP1_KEY_V1 = "oh-app1-key-v1"; 385 386 /** 387 * Command line parameter oh-root-ca-key-v1. 388 */ 389 public static final String CMD_OH_ROOT_CA_KEY_V1 = "oh-root-ca-key-v1"; 390 391 /** 392 * Command line parameter oh-sub-app-ca-key-v1. 393 */ 394 public static final String CMD_OH_SUB_APP_CA_KEY_V1 = "oh-sub-app-ca-key-v1"; 395 396 /** 397 * Command line parameter oh-sub-profile-ca-key-v1. 398 */ 399 public static final String CMD_OH_SUB_PROFILE_CA_KEY_V1 = "oh-sub-profile-ca-key-v1"; 400 401 /** 402 * Command line parameter CN=ROOT CA. 403 */ 404 public static final String CMD_ROOT_CA = "C=CN,O=OpenHarmony,OU=OpenHarmony Community,CN=ROOT CA"; 405 406 /** 407 * Command line parameter CN=App1 Release. 408 */ 409 public static final String CMD_APP1_RELEASE = "C=CN,O=OpenHarmony,OU=OpenHarmony Community,CN=App1 Release"; 410 411 /** 412 * Command line parameter CN=Provision Profile Release. 413 */ 414 public static final String CMD_PROFILE_RELEASE = "C=CN,O=OpenHarmony," 415 + "OU=OpenHarmony Community,CN=Provision Profile Release"; 416 417 /** 418 * Command line parameter CN=Provision Profile Signature Service CA. 419 */ 420 public static final String CMD_PROFILE_CA = "C=CN,O=OpenHarmony,OU=OpenHarmony Community," 421 + "CN=Provision Profile Signature Service CA"; 422 423 /** 424 * Command line parameter CN=Application Signature Service CA. 425 */ 426 public static final String CMD_APP_CA = "C=CN," 427 + "O=OpenHarmony,OU=OpenHarmony Community,CN=Application Signature Service CA"; 428 429 /** 430 * Add log info. 431 */ 432 private static final Logger LOGGER = LoggerFactory.getLogger(CmdUnitTest.class); 433 434 /** 435 * create test dir 436 */ 437 @BeforeAll mkTestDir()438 public static void mkTestDir() { 439 File dir = new File("test"); 440 dir.mkdir(); 441 } 442 443 /** 444 * delete test dir 445 */ 446 @AfterAll delTestDir()447 public static void delTestDir() { 448 File dir = new File("test"); 449 if (dir.exists()) { 450 for (File file : dir.listFiles()) { 451 file.delete(); 452 } 453 dir.delete(); 454 } 455 } 456 457 /** 458 * test cmdKeypair 459 * 460 * @throws IOException io error 461 */ 462 @Order(1) 463 @Test testCmdKeypair()464 public void testCmdKeypair() throws IOException { 465 try { 466 deleteFile(CMD_KEY_APP_STORE_PATH); 467 deleteFile(CMD_KEY_PROFILE_STORE_PATH); 468 boolean result = HapSignTool.processCmd(new String[]{CmdUtil.Method.GENERATE_KEYPAIR}); 469 assertFalse(result); 470 assertFalse(FileUtils.isFileExist(CMD_KEY_APP_STORE_PATH)); 471 assertFalse(FileUtils.isFileExist(CMD_KEY_PROFILE_STORE_PATH)); 472 } catch (CustomException exception) { 473 LOGGER.info(exception, () -> exception.getMessage()); 474 } 475 476 deleteFile(CMD_KEY_APP_STORE_PATH); 477 boolean result = HapSignTool.processCmd(new String[]{ 478 CmdUtil.Method.GENERATE_KEYPAIR, 479 CMD_KEY_ALIAS, CMD_OH_APP1_KEY_V1, 480 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 481 CMD_KEY_ALG, KeyPairTools.ECC_INPUT, 482 CMD_KEY_SIZE, CMD_ECC_256, 483 CMD_KEY_STORE_FILE, CMD_KEY_APP_STORE_PATH, 484 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456}); 485 assertTrue(result); 486 assertTrue(FileUtils.isFileExist(CMD_KEY_APP_STORE_PATH)); 487 deleteFile(CMD_KEY_PROFILE_STORE_PATH); 488 result = HapSignTool.processCmd(new String[]{ 489 CmdUtil.Method.GENERATE_KEYPAIR, 490 CMD_KEY_ALIAS, CMD_OH_PROFILE_KEY_V1, 491 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 492 CMD_KEY_ALG, KeyPairTools.ECC_INPUT, 493 CMD_KEY_SIZE, CMD_ECC_256, 494 CMD_KEY_STORE_FILE, CMD_KEY_PROFILE_STORE_PATH, 495 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456}); 496 assertTrue(result); 497 assertTrue(FileUtils.isFileExist(CMD_KEY_PROFILE_STORE_PATH)); 498 } 499 500 /** 501 * Csr test case. 502 * 503 * @throws IOException Error 504 */ 505 @Order(2) 506 @Test testCmdCsr()507 public void testCmdCsr() throws IOException { 508 try { 509 deleteFile(CMD_CSR_PATH); 510 boolean result = HapSignTool.processCmd(new String[]{CmdUtil.Method.GENERATE_CSR}); 511 assertFalse(result); 512 assertFalse(FileUtils.isFileExist(CMD_CSR_PATH)); 513 } catch (CustomException exception) { 514 LOGGER.info(exception, () -> exception.getMessage()); 515 } 516 517 deleteFile(CMD_CSR_PATH); 518 boolean result = HapSignTool.processCmd(new String[]{ 519 CmdUtil.Method.GENERATE_CSR, 520 CMD_KEY_ALIAS, CMD_OH_APP1_KEY_V1, 521 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 522 CMD_SUBJECT, CMD_APP1_RELEASE, 523 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA, 524 CMD_KEY_STORE_FILE, CMD_KEY_APP_STORE_PATH, 525 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 526 CMD_OUT_FILE, CMD_CSR_PATH}); 527 assertTrue(result); 528 assertTrue(FileUtils.isFileExist(CMD_CSR_PATH)); 529 } 530 531 /** 532 * Cert test case 533 * 534 * @throws IOException Error 535 */ 536 @Order(3) 537 @Test testCmdCert()538 public void testCmdCert() throws IOException { 539 try { 540 deleteFile(CMD_CERT_PATH); 541 boolean result = HapSignTool.processCmd(new String[]{CmdUtil.Method.GENERATE_CERT}); 542 assertFalse(result); 543 assertFalse(FileUtils.isFileExist(CMD_CERT_PATH)); 544 } catch (CustomException exception) { 545 LOGGER.info(exception, () -> exception.getMessage()); 546 } 547 548 deleteFile(CMD_CERT_PATH); 549 boolean result = HapSignTool.processCmd(new String[]{ 550 CmdUtil.Method.GENERATE_CERT, 551 CMD_KEY_ALIAS, CMD_OH_APP1_KEY_V1, 552 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 553 CMD_ISSUER, CMD_APP_CA, 554 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA, 555 CMD_KEY_STORE_FILE, CMD_KEY_APP_STORE_PATH, 556 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 557 CMD_OUT_FILE, CMD_CERT_PATH, 558 CMD_ISSUER_KEY_ALIAS, CMD_OH_APP1_KEY_V1, 559 CMD_ISSUER_KEY_RIGHTS, CMD_RIGHTS_123456, 560 CMD_SUBJECT, CMD_APP1_RELEASE, 561 CMD_VALIDITY, CMD_VALIDITY_365, 562 CMD_KEY_USAGE, CMD_DIGITAL_SIGNATURE, 563 CMD_KEY_USAGE_CRITICAL, CMD_FALSE, 564 CMD_EXT_KEY_USAGE, CMD_CODE_AND_EMAIL, 565 CMD_EXT_KEY_USAGE_CRITICAL, CMD_TRUE, 566 CMD_BASIC_CONSTRAINTS, CMD_FALSE, 567 CMD_BASIC_CONSTRAINTS_CRITICAL, CMD_TRUE, 568 CMD_BASIC_CONSTRAINTS_CA, CMD_FALSE, 569 CMD_BASIC_CONSTRAINTS_PATH_LEN, CMD_BC_PATH_LEN_0}); 570 assertTrue(result); 571 assertTrue(FileUtils.isFileExist(CMD_CERT_PATH)); 572 } 573 574 /** 575 * Ca test case. 576 * 577 * @throws IOException Error 578 */ 579 @Order(4) 580 @Test testCmdCa()581 public void testCmdCa() throws IOException { 582 try { 583 deleteFile(CMD_ROOT_APP_CA_PATH); 584 deleteFile(CMD_ROOT_PROFILE_CA_PATH); 585 deleteFile(CMD_SUB_APP_CA_PATH); 586 deleteFile(CMD_SUB_PROFILE_CA_PATH); 587 boolean result = HapSignTool.processCmd(new String[]{CmdUtil.Method.GENERATE_CA}); 588 assertFalse(result); 589 assertFalse(FileUtils.isFileExist(CMD_ROOT_APP_CA_PATH)); 590 assertFalse(FileUtils.isFileExist(CMD_ROOT_PROFILE_CA_PATH)); 591 assertFalse(FileUtils.isFileExist(CMD_SUB_APP_CA_PATH)); 592 assertFalse(FileUtils.isFileExist(CMD_SUB_PROFILE_CA_PATH)); 593 } catch (CustomException exception) { 594 LOGGER.info(exception, () -> exception.getMessage()); 595 } 596 deleteFile(CMD_ROOT_APP_CA_PATH); 597 boolean result = generateAppRootCa(); 598 assertTrue(result); 599 assertTrue(FileUtils.isFileExist(CMD_ROOT_APP_CA_PATH)); 600 deleteFile(CMD_ROOT_PROFILE_CA_PATH); 601 result = generateProfileRootCa(); 602 assertTrue(result); 603 assertTrue(FileUtils.isFileExist(CMD_ROOT_PROFILE_CA_PATH)); 604 deleteFile(CMD_SUB_APP_CA_PATH); 605 result = generateAppSubCa(); 606 assertTrue(result); 607 assertTrue(FileUtils.isFileExist(CMD_SUB_APP_CA_PATH)); 608 deleteFile(CMD_SUB_PROFILE_CA_PATH); 609 result = generateProfileSubCa(); 610 assertTrue(result); 611 assertTrue(FileUtils.isFileExist(CMD_SUB_PROFILE_CA_PATH)); 612 } 613 614 /** 615 * App cert test case. 616 * 617 * @throws IOException Error 618 */ 619 @Order(5) 620 @Test testCmdAppCert()621 public void testCmdAppCert() throws IOException { 622 try { 623 deleteFile(CMD_APP_DEBUG_CERT_PATH); 624 deleteFile(CMD_APP_RELEASE_CERT_PATH); 625 boolean result = HapSignTool.processCmd(new String[]{CmdUtil.Method.GENERATE_APP_CERT}); 626 assertFalse(result); 627 assertFalse(FileUtils.isFileExist(CMD_APP_DEBUG_CERT_PATH)); 628 assertFalse(FileUtils.isFileExist(CMD_APP_RELEASE_CERT_PATH)); 629 } catch (CustomException exception) { 630 LOGGER.info(exception, () -> exception.getMessage()); 631 } 632 deleteFile(CMD_APP_DEBUG_CERT_PATH); 633 boolean result = HapSignTool.processCmd(new String[]{ 634 CmdUtil.Method.GENERATE_APP_CERT, 635 CMD_KEY_ALIAS, CMD_OH_APP1_KEY_V1, 636 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 637 CMD_ISSUER, CMD_APP_CA, 638 CMD_KEY_STORE_FILE, CMD_KEY_APP_STORE_PATH, 639 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 640 CMD_OUT_FILE, CMD_APP_DEBUG_CERT_PATH, 641 CMD_ISSUER_KEY_ALIAS, CMD_OH_SUB_APP_CA_KEY_V1, 642 CMD_ISSUER_KEY_RIGHTS, CMD_RIGHTS_123456, 643 CMD_SUBJECT, CMD_APP1_RELEASE, 644 CMD_VALIDITY, CMD_VALIDITY_365, 645 CMD_OUT_FORM, CMD_CERT_CHAIN, 646 CMD_ROOT_CA_CERT_FILE, CMD_ROOT_APP_CA_PATH, 647 CMD_SUB_CA_CERT_FILE, CMD_SUB_APP_CA_PATH, 648 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA}); 649 assertTrue(result); 650 assertTrue(FileUtils.isFileExist(CMD_APP_DEBUG_CERT_PATH)); 651 deleteFile(CMD_APP_RELEASE_CERT_PATH); 652 result = HapSignTool.processCmd(new String[]{ 653 CmdUtil.Method.GENERATE_APP_CERT, 654 CMD_KEY_ALIAS, CMD_OH_APP1_KEY_V1, 655 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 656 CMD_ISSUER, CMD_APP_CA, 657 CMD_KEY_STORE_FILE, CMD_KEY_APP_STORE_PATH, 658 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 659 CMD_OUT_FILE, CMD_APP_RELEASE_CERT_PATH, 660 CMD_ISSUER_KEY_ALIAS, CMD_OH_SUB_APP_CA_KEY_V1, 661 CMD_ISSUER_KEY_RIGHTS, CMD_RIGHTS_123456, 662 CMD_SUBJECT, CMD_APP1_RELEASE, 663 CMD_VALIDITY, CMD_VALIDITY_365, 664 CMD_OUT_FORM, CMD_CERT_CHAIN, 665 CMD_ROOT_CA_CERT_FILE, CMD_ROOT_APP_CA_PATH, 666 CMD_SUB_CA_CERT_FILE, CMD_SUB_APP_CA_PATH, 667 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA}); 668 assertTrue(result); 669 assertTrue(FileUtils.isFileExist(CMD_APP_RELEASE_CERT_PATH)); 670 } 671 672 /** 673 * Profile cert test case 674 * 675 * @throws IOException Error 676 */ 677 @Order(6) 678 @Test testCmdProfileCert()679 public void testCmdProfileCert() throws IOException { 680 try { 681 deleteFile(CMD_PROFILE_DEBUG_CERT_PATH); 682 deleteFile(CMD_PROFILE_RELEASE_CERT_PATH); 683 boolean result = HapSignTool.processCmd(new String[]{CmdUtil.Method.GENERATE_PROFILE_CERT}); 684 assertFalse(result); 685 assertFalse(FileUtils.isFileExist(CMD_PROFILE_DEBUG_CERT_PATH)); 686 assertFalse(FileUtils.isFileExist(CMD_PROFILE_RELEASE_CERT_PATH)); 687 } catch (CustomException exception) { 688 LOGGER.info(exception, () -> exception.getMessage()); 689 } 690 deleteFile(CMD_PROFILE_DEBUG_CERT_PATH); 691 boolean result = HapSignTool.processCmd(new String[]{ 692 CmdUtil.Method.GENERATE_PROFILE_CERT, 693 CMD_KEY_ALIAS, CMD_OH_PROFILE_KEY_V1, 694 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 695 CMD_ISSUER, CMD_PROFILE_CA, 696 CMD_KEY_STORE_FILE, CMD_KEY_PROFILE_STORE_PATH, 697 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 698 CMD_OUT_FILE, CMD_PROFILE_DEBUG_CERT_PATH, 699 CMD_ISSUER_KEY_ALIAS, CMD_OH_SUB_PROFILE_CA_KEY_V1, 700 CMD_ISSUER_KEY_RIGHTS, CMD_RIGHTS_123456, 701 CMD_SUBJECT, CMD_PROFILE_RELEASE, 702 CMD_VALIDITY, CMD_VALIDITY_365, 703 CMD_OUT_FORM, CMD_CERT_CHAIN, 704 CMD_ROOT_CA_CERT_FILE, CMD_ROOT_PROFILE_CA_PATH, 705 CMD_SUB_CA_CERT_FILE, CMD_SUB_PROFILE_CA_PATH, 706 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA}); 707 assertTrue(result); 708 assertTrue(FileUtils.isFileExist(CMD_PROFILE_DEBUG_CERT_PATH)); 709 deleteFile(CMD_PROFILE_RELEASE_CERT_PATH); 710 result = HapSignTool.processCmd(new String[]{ 711 CmdUtil.Method.GENERATE_PROFILE_CERT, 712 CMD_KEY_ALIAS, CMD_OH_PROFILE_KEY_V1, 713 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 714 CMD_ISSUER, CMD_PROFILE_CA, 715 CMD_KEY_STORE_FILE, CMD_KEY_PROFILE_STORE_PATH, 716 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 717 CMD_OUT_FILE, CMD_PROFILE_RELEASE_CERT_PATH, 718 CMD_ISSUER_KEY_ALIAS, CMD_OH_SUB_PROFILE_CA_KEY_V1, 719 CMD_ISSUER_KEY_RIGHTS, CMD_RIGHTS_123456, 720 CMD_SUBJECT, CMD_PROFILE_RELEASE, 721 CMD_VALIDITY, CMD_VALIDITY_365, 722 CMD_OUT_FORM, CMD_CERT_CHAIN, 723 CMD_ROOT_CA_CERT_FILE, CMD_ROOT_PROFILE_CA_PATH, 724 CMD_SUB_CA_CERT_FILE, CMD_SUB_PROFILE_CA_PATH, 725 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA}); 726 assertTrue(result); 727 assertTrue(FileUtils.isFileExist(CMD_PROFILE_RELEASE_CERT_PATH)); 728 } 729 730 /** 731 * Sign profile test case. 732 * 733 * @throws IOException error 734 */ 735 @Order(7) 736 @Test testCmdSignProfile()737 public void testCmdSignProfile() throws IOException { 738 try { 739 deleteFile(CMD_SIGN_PROFILE_PATH); 740 boolean result = HapSignTool.processCmd(new String[]{CmdUtil.Method.SIGN_PROFILE}); 741 assertFalse(result); 742 assertFalse(FileUtils.isFileExist(CMD_SIGN_PROFILE_PATH)); 743 } catch (CustomException exception) { 744 LOGGER.info(exception, () -> exception.getMessage()); 745 } 746 747 deleteFile(CMD_SIGN_PROFILE_PATH); 748 loadFile(CMD_JSON_FILE); 749 boolean result = HapSignTool.processCmd(new String[]{ 750 CmdUtil.Method.SIGN_PROFILE, 751 CMD_MODE, CMD_LOCAL_SIGN, 752 CMD_KEY_ALIAS, CMD_OH_PROFILE_KEY_V1, 753 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 754 CMD_PROFILE_CERT_FILE, CMD_PROFILE_DEBUG_CERT_PATH, 755 CMD_IN_FILE, CMD_JSON_FILE, 756 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA, 757 CMD_KEY_STORE_FILE, CMD_KEY_PROFILE_STORE_PATH, 758 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 759 CMD_OUT_FILE, CMD_SIGN_PROFILE_PATH}); 760 assertTrue(result); 761 assertTrue(FileUtils.isFileExist(CMD_SIGN_PROFILE_PATH)); 762 763 deleteFile(CMD_SIGN_RELEASE_PROFILE_PATH); 764 loadFile(CMD_RELEASE_JSON_FILE); 765 result = HapSignTool.processCmd(new String[]{ 766 CmdUtil.Method.SIGN_PROFILE, 767 CMD_MODE, CMD_LOCAL_SIGN, 768 CMD_KEY_ALIAS, CMD_OH_PROFILE_KEY_V1, 769 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 770 CMD_PROFILE_CERT_FILE, CMD_PROFILE_RELEASE_CERT_PATH, 771 CMD_IN_FILE, CMD_RELEASE_JSON_FILE, 772 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA, 773 CMD_KEY_STORE_FILE, CMD_KEY_PROFILE_STORE_PATH, 774 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 775 CMD_OUT_FILE, CMD_SIGN_RELEASE_PROFILE_PATH}); 776 assertTrue(result); 777 assertTrue(FileUtils.isFileExist(CMD_SIGN_RELEASE_PROFILE_PATH)); 778 } 779 780 /** 781 * Verify profile test case. 782 */ 783 @Order(8) 784 @Test testVerifyProfile()785 public void testVerifyProfile() { 786 try { 787 boolean result = HapSignTool.processCmd(new String[]{CmdUtil.Method.VERIFY_PROFILE}); 788 assertFalse(result); 789 } catch (CustomException exception) { 790 LOGGER.info(exception, () -> exception.getMessage()); 791 } 792 793 boolean result = HapSignTool.processCmd(new String[]{ 794 CmdUtil.Method.VERIFY_PROFILE, 795 CMD_IN_FILE, CMD_SIGN_PROFILE_PATH, 796 CMD_OUT_FILE, CMD_VERIFY_PROFILE_RESULT_PATH}); 797 assertTrue(result); 798 } 799 800 /** 801 * Sign hap test case. 802 */ 803 @Order(9) 804 @Test testCmdSignApp()805 public void testCmdSignApp() { 806 try { 807 boolean result = HapSignTool.processCmd(new String[]{CmdUtil.Method.SIGN_APP}); 808 assertFalse(result); 809 } catch (CustomException exception) { 810 LOGGER.info(exception, () -> exception.getMessage()); 811 } 812 } 813 814 /** 815 * Verify signed app test case. 816 */ 817 @Order(10) 818 @Test testCmdVerifyApp()819 public void testCmdVerifyApp() { 820 try { 821 boolean result = HapSignTool.processCmd(new String[]{CmdUtil.Method.VERIFY_APP}); 822 assertFalse(result); 823 } catch (CustomException exception) { 824 LOGGER.info(exception, () -> exception.getMessage()); 825 } 826 } 827 828 /** 829 * test sign and verify hap file include multi lib 830 * 831 * @throws IOException error 832 */ 833 @Order(11) 834 @Test testCmdMultiHap()835 public void testCmdMultiHap() throws IOException { 836 multiBundleTest(".hap"); 837 } 838 839 /** 840 * test sign and verify hqf file include multi lib 841 * 842 * @throws IOException error 843 */ 844 @Order(12) 845 @Test testCmdMultiHqf()846 public void testCmdMultiHqf() throws IOException { 847 multiBundleTest(".hqf"); 848 } 849 850 @Order(13) 851 @Test testCmdMultiHnp()852 public void testCmdMultiHnp() throws IOException { 853 loadFile(CMD_TEST_HAP_FILE_PATH); 854 File unsignedHap = new File(CMD_TEST_HAP_FILE_PATH); 855 signAndVerifyHap(unsignedHap.getAbsolutePath(), ".hap"); 856 } 857 858 @Order(14) 859 @Test testNoCodeSignHap()860 public void testNoCodeSignHap() throws IOException { 861 File unsignedHap = generateHapFile(FileType.FILE_UNCOMPRESSED, FileType.FILE_UNCOMPRESSED, 862 FileType.FILE_UNCOMPRESSED, FileType.FILE_UNCOMPRESSED, ".hap"); 863 String signedHap = File.createTempFile("signed-", ".hap", new File("test")).getAbsolutePath(); 864 865 boolean result = HapSignTool.processCmd(new String[] { 866 CmdUtil.Method.SIGN_APP, 867 CMD_MODE, CMD_LOCAL_SIGN, 868 CMD_KEY_ALIAS, CMD_OH_APP1_KEY_V1, 869 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 870 CMD_APP_CERT_FILE, CMD_APP_DEBUG_CERT_PATH, 871 CMD_PROFILE_FILE, CMD_SIGN_PROFILE_PATH, 872 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA, 873 CMD_KEY_STORE_FILE, CMD_KEY_APP_STORE_PATH, 874 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 875 CMD_IN_FILE, unsignedHap.getAbsolutePath(), 876 CMD_OUT_FILE, signedHap, 877 CMD_SIGN_CODE, ParamConstants.SignCodeFlag.DISABLE_SIGN_CODE.getSignCodeFlag() 878 }); 879 assertTrue(result); 880 result = HapSignTool.processCmd(new String[] { 881 CmdUtil.Method.VERIFY_APP, CMD_IN_FILE, signedHap, CMD_OUT_CERT_CHAIN, "test" + File.separator + "1.cer", 882 CMD_OUT_PROFILE, "test" + File.separator + "1.p7b" 883 }); 884 assertTrue(result); 885 assertFalse(existPagesInfoFile(signedHap)); 886 } 887 888 @Order(15) 889 @Test testNoRunnableFileHap()890 public void testNoRunnableFileHap() throws IOException { 891 File unsignedHap = generateHapFile(FileType.FILE_NOT_EXISTED, FileType.FILE_NOT_EXISTED, 892 FileType.FILE_UNCOMPRESSED, FileType.FILE_UNCOMPRESSED, ".hap"); 893 String signedHap = signAndVerifyHap(unsignedHap.getAbsolutePath(), ".hap"); 894 assertFalse(existPagesInfoFile(signedHap)); 895 } 896 897 @Order(16) 898 @Test testUncompressedRunnableFileHap()899 public void testUncompressedRunnableFileHap() throws IOException { 900 File unsignedHap = generateHapFile(FileType.FILE_UNCOMPRESSED, FileType.FILE_UNCOMPRESSED, 901 FileType.FILE_UNCOMPRESSED, FileType.FILE_UNCOMPRESSED, ".hap"); 902 String signedHap = signAndVerifyHap(unsignedHap.getAbsolutePath(), ".hap"); 903 assertTrue(existPagesInfoFile(signedHap)); 904 } 905 906 @Order(17) 907 @Test testCompressedRunnableFileHap()908 public void testCompressedRunnableFileHap() throws IOException { 909 File unsignedHap = generateHapFile(FileType.FILE_COMPRESSED, FileType.FILE_COMPRESSED, 910 FileType.FILE_COMPRESSED, FileType.FILE_UNCOMPRESSED, ".hap"); 911 String signedHap = signAndVerifyHap(unsignedHap.getAbsolutePath(), ".hap"); 912 assertFalse(existPagesInfoFile(signedHap)); 913 } 914 multiBundleTest(String bundleSuffix)915 private void multiBundleTest(String bundleSuffix) throws IOException { 916 for (FileType abcFile : FileType.values()) { 917 for (FileType soFile : FileType.values()) { 918 for (FileType anFile : FileType.values()) { 919 File unsignedHap = generateHapFile(abcFile, soFile, anFile, FileType.FILE_NOT_EXISTED, 920 bundleSuffix); 921 signAndVerifyHap(unsignedHap.getAbsolutePath(), bundleSuffix); 922 923 unsignedHap = generateHapFile(abcFile, soFile, anFile, FileType.FILE_UNCOMPRESSED, bundleSuffix); 924 signAndVerifyHap(unsignedHap.getAbsolutePath(), bundleSuffix); 925 926 unsignedHap = generateHapFile(abcFile, soFile, anFile, FileType.FILE_COMPRESSED, bundleSuffix); 927 signAndVerifyHap(unsignedHap.getAbsolutePath(), bundleSuffix); 928 } 929 } 930 } 931 } 932 generateHapFile(FileType abc, FileType so, FileType an, FileType otherFile, String bundleSuffix)933 private File generateHapFile(FileType abc, FileType so, FileType an, FileType otherFile, String bundleSuffix) 934 throws IOException { 935 File unsignedHap = File.createTempFile("unsigned-", bundleSuffix, new File("test")); 936 try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(unsignedHap))) { 937 for (int i = 0; i < 10; i++) { 938 fillZipEntryFile(abc, ".abc", out); 939 if (".hqf".equals(bundleSuffix)) { 940 fillZipEntryFile(so, ".so.diff", out); 941 } 942 fillZipEntryFile(so, ".solib", out); 943 fillZipEntryFile(so, ".so", out); 944 fillZipEntryFile(so, ".so.111", out); 945 fillZipEntryFile(so, ".so.111.111", out); 946 fillZipEntryFile(so, ".so.111.111.111", out); 947 fillZipEntryFile(an, ".an", out); 948 fillZipEntryFile(otherFile, ".json", out); 949 } 950 } 951 return unsignedHap; 952 } 953 fillZipEntryFile(FileType ft, String suffix, ZipOutputStream out)954 private void fillZipEntryFile(FileType ft, String suffix, ZipOutputStream out) throws IOException { 955 String tempSuffix = suffix; 956 if (FileType.FILE_NOT_EXISTED.equals(ft)) { 957 tempSuffix = ""; 958 } 959 String fileName = new BigInteger(Long.SIZE, new Random()).toString() + tempSuffix; 960 if (tempSuffix.startsWith(".so")) { 961 fileName = "libs/" + fileName; 962 } 963 if (tempSuffix.startsWith(".an")) { 964 fileName = "an/" + fileName; 965 } 966 ZipEntry zipEntry = new ZipEntry(fileName); 967 byte[] bytes = generateChunkBytes(); 968 if (FileType.FILE_UNCOMPRESSED.equals(ft)) { 969 zipEntry.setMethod(ZipEntry.STORED); 970 zipEntry.setSize(bytes.length); 971 CRC32 crc32 = new CRC32(); 972 crc32.reset(); 973 crc32.update(bytes, 0, bytes.length); 974 zipEntry.setCrc(crc32.getValue()); 975 } else { 976 zipEntry.setMethod(ZipEntry.DEFLATED); 977 } 978 out.putNextEntry(zipEntry); 979 out.write(bytes); 980 out.closeEntry(); 981 } 982 generateChunkBytes()983 private byte[] generateChunkBytes() { 984 Random random = new Random(); 985 int size = Math.max(4096, random.nextInt(1024 * 1024 * 2)); 986 byte[] bytes = new byte[size]; 987 random.nextBytes(bytes); 988 return bytes; 989 } 990 signAndVerifyHap(String unsignedHap, String bundleSuffix)991 private String signAndVerifyHap(String unsignedHap, String bundleSuffix) throws IOException { 992 String signedHap = File.createTempFile("signed-", bundleSuffix, new File("test")).getAbsolutePath(); 993 // debug 994 boolean result = HapSignTool.processCmd(new String[] { 995 CmdUtil.Method.SIGN_APP, 996 CMD_MODE, CMD_LOCAL_SIGN, 997 CMD_KEY_ALIAS, CMD_OH_APP1_KEY_V1, 998 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 999 CMD_APP_CERT_FILE, CMD_APP_DEBUG_CERT_PATH, 1000 CMD_PROFILE_FILE, CMD_SIGN_PROFILE_PATH, 1001 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA, 1002 CMD_KEY_STORE_FILE, CMD_KEY_APP_STORE_PATH, 1003 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 1004 CMD_IN_FILE, unsignedHap, 1005 CMD_OUT_FILE, signedHap 1006 }); 1007 assertTrue(result); 1008 1009 result = HapSignTool.processCmd(new String[] { 1010 CmdUtil.Method.VERIFY_APP, CMD_IN_FILE, signedHap, CMD_OUT_CERT_CHAIN, "test" + File.separator + "1.cer", 1011 CMD_OUT_PROFILE, "test" + File.separator + "1.p7b" 1012 }); 1013 assertTrue(result); 1014 // release 1015 result = HapSignTool.processCmd(new String[] { 1016 CmdUtil.Method.SIGN_APP, 1017 CMD_MODE, CMD_LOCAL_SIGN, 1018 CMD_KEY_ALIAS, CMD_OH_APP1_KEY_V1, 1019 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 1020 CMD_APP_CERT_FILE, CMD_APP_RELEASE_CERT_PATH, 1021 CMD_PROFILE_FILE, CMD_SIGN_RELEASE_PROFILE_PATH, 1022 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA, 1023 CMD_KEY_STORE_FILE, CMD_KEY_APP_STORE_PATH, 1024 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 1025 CMD_IN_FILE, signedHap, 1026 CMD_OUT_FILE, signedHap 1027 }); 1028 assertTrue(result); 1029 1030 result = HapSignTool.processCmd(new String[] { 1031 CmdUtil.Method.VERIFY_APP, CMD_IN_FILE, signedHap, CMD_OUT_CERT_CHAIN, "test" + File.separator + "1.cer", 1032 CMD_OUT_PROFILE, "test" + File.separator + "1.p7b" 1033 }); 1034 assertTrue(result); 1035 return signedHap; 1036 } 1037 existPagesInfoFile(String signedHap)1038 private boolean existPagesInfoFile(String signedHap) throws IOException { 1039 try (ZipFile inputHap = new ZipFile(signedHap)) { 1040 ZipEntry entry = inputHap.getEntry(FileUtils.BIT_MAP_FILENAME); 1041 return entry != null; 1042 } 1043 } 1044 1045 /** 1046 * Test Method: isRunnableFile() 1047 */ 1048 @Test testIsRunnableFile()1049 public void testIsRunnableFile() { 1050 List<String> correctName = new ArrayList<>(); 1051 correctName.add("libs/中文.so"); 1052 correctName.add("srtjdwrtj.an"); 1053 correctName.add("srtjdwrtj.abc"); 1054 correctName.add("libs/srtjdwrtj.so"); 1055 correctName.add("libs/srtjdwrtj.so.1"); 1056 correctName.add("libs/srtjdwrtj.so.1.1"); 1057 correctName.add("libs/srtjdwrtj.so.1.1.1"); 1058 correctName.add("libs/srtjdwrtj.aaaa.111.111.1111"); 1059 correctName.add("libs/srtjdwrtj.so.111.111.1111"); 1060 correctName.add("libs/中文.so.111.111.1111"); 1061 correctName.add("libs/srtjdwrtj.so.111.%%%.1111"); 1062 for (String name : correctName) { 1063 assertTrue(FileUtils.isRunnableFile(name)); 1064 } 1065 1066 List<String> incorrectName = new ArrayList<>(); 1067 incorrectName.add("srtjdwrtj.so"); 1068 incorrectName.add("srtjdwrtj.so.111.111.1111.54645"); 1069 incorrectName.add("srtjdwrtjso.111.111.11111"); 1070 incorrectName.add("srtjdwrtj.so.abc.111.111.1111"); 1071 incorrectName.add("srtjdwrtj.so.111.111.json"); 1072 incorrectName.add("srtjdwrtj.abc.json"); 1073 incorrectName.add("srtjdwrtj.an.json"); 1074 incorrectName.add("中文.so.111.111.json"); 1075 for (String name : incorrectName) { 1076 assertFalse(FileUtils.isRunnableFile(name)); 1077 } 1078 } 1079 1080 @Test testGetHnpsFromHap()1081 public void testGetHnpsFromHap() throws IOException, ProfileException { 1082 loadFile(CMD_TEST_HAP_FILE_PATH); 1083 File file = new File(CMD_TEST_HAP_FILE_PATH); 1084 try (JarFile inputJar = new JarFile(file, false)) { 1085 Map<String, String> hnpMap = HapUtils.getHnpsFromJson(inputJar); 1086 assertTrue(hnpMap.size() == 4); 1087 assertTrue("private".equals(hnpMap.get("entry-default-unsigned.hnp"))); 1088 assertTrue("public".equals(hnpMap.get("entry-default-unsigned1.hnp"))); 1089 assertTrue("private".equals(hnpMap.get("dd/entry-default-unsigned.hnp"))); 1090 assertTrue("public".equals(hnpMap.get("dd/entry-default-unsigned1.hnp"))); 1091 } 1092 } 1093 1094 /** 1095 * Test Method: testByteToZip() 1096 * 1097 * @throws IOException read file exception 1098 */ 1099 @Test testByteToZip()1100 public void testByteToZip() throws IOException { 1101 for (int i = 0; i < 10; i++) { 1102 File file = generateHapFile(FileType.FILE_UNCOMPRESSED, FileType.FILE_UNCOMPRESSED, 1103 FileType.FILE_UNCOMPRESSED, FileType.FILE_UNCOMPRESSED, ".hap"); 1104 Zip zip = new Zip(file); 1105 String outFileName = "test" + File.separator + "testOut.hap"; 1106 zip.toFile(outFileName); 1107 File outFile = new File(outFileName); 1108 byte[] bytes = FileUtils.readFile(file); 1109 byte[] outBytes = FileUtils.readFile(outFile); 1110 assertArrayEquals(outBytes, bytes); 1111 1112 deleteFile(file.getCanonicalPath()); 1113 deleteFile(outFileName); 1114 } 1115 } 1116 generateAppRootCa()1117 private boolean generateAppRootCa() { 1118 return HapSignTool.processCmd(new String[]{ 1119 CmdUtil.Method.GENERATE_CA, 1120 CMD_KEY_ALIAS, CMD_OH_ROOT_CA_KEY_V1, 1121 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 1122 CMD_KEY_ALG, KeyPairTools.ECC_INPUT, 1123 CMD_KEY_SIZE, CMD_ECC_256, 1124 CMD_KEY_STORE_FILE, CMD_KEY_APP_STORE_PATH, 1125 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 1126 CMD_OUT_FILE, CMD_ROOT_APP_CA_PATH, 1127 CMD_SUBJECT, CMD_ROOT_CA, 1128 CMD_VALIDITY, CMD_VALIDITY_365, 1129 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA, 1130 CMD_BASIC_CONSTRAINTS_PATH_LEN, CMD_BC_PATH_LEN_0}); 1131 } 1132 generateProfileRootCa()1133 private boolean generateProfileRootCa() { 1134 return HapSignTool.processCmd(new String[]{ 1135 CmdUtil.Method.GENERATE_CA, 1136 CMD_KEY_ALIAS, CMD_OH_ROOT_CA_KEY_V1, 1137 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 1138 CMD_KEY_ALG, KeyPairTools.ECC_INPUT, 1139 CMD_KEY_SIZE, CMD_ECC_256, 1140 CMD_KEY_STORE_FILE, CMD_KEY_PROFILE_STORE_PATH, 1141 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 1142 CMD_OUT_FILE, CMD_ROOT_PROFILE_CA_PATH, 1143 CMD_SUBJECT, CMD_ROOT_CA, 1144 CMD_VALIDITY, CMD_VALIDITY_365, 1145 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA, 1146 CMD_BASIC_CONSTRAINTS_PATH_LEN, CMD_BC_PATH_LEN_0}); 1147 } 1148 generateAppSubCa()1149 private boolean generateAppSubCa() { 1150 return HapSignTool.processCmd(new String[]{ 1151 CmdUtil.Method.GENERATE_CA, 1152 CMD_KEY_ALIAS, CMD_OH_SUB_APP_CA_KEY_V1, 1153 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 1154 CMD_ISSUER, CMD_ROOT_CA, 1155 CMD_KEY_ALG, KeyPairTools.ECC_INPUT, 1156 CMD_KEY_SIZE, CMD_ECC_256, 1157 CMD_KEY_STORE_FILE, CMD_KEY_APP_STORE_PATH, 1158 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 1159 CMD_OUT_FILE, CMD_SUB_APP_CA_PATH, 1160 CMD_ISSUER_KEY_ALIAS, CMD_OH_ROOT_CA_KEY_V1, 1161 CMD_ISSUER_KEY_RIGHTS, CMD_RIGHTS_123456, 1162 CMD_SUBJECT, CMD_APP_CA, 1163 CMD_VALIDITY, CMD_VALIDITY_365, 1164 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA, 1165 CMD_BASIC_CONSTRAINTS_PATH_LEN, CMD_BC_PATH_LEN_0}); 1166 } 1167 generateProfileSubCa()1168 private boolean generateProfileSubCa() { 1169 return HapSignTool.processCmd(new String[]{ 1170 CmdUtil.Method.GENERATE_CA, 1171 CMD_KEY_ALIAS, CMD_OH_SUB_PROFILE_CA_KEY_V1, 1172 CMD_KEY_RIGHTS, CMD_RIGHTS_123456, 1173 CMD_ISSUER, CMD_ROOT_CA, 1174 CMD_KEY_ALG, KeyPairTools.ECC_INPUT, 1175 CMD_KEY_SIZE, CMD_ECC_256, 1176 CMD_KEY_STORE_FILE, CMD_KEY_PROFILE_STORE_PATH, 1177 CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, 1178 CMD_OUT_FILE, CMD_SUB_PROFILE_CA_PATH, 1179 CMD_ISSUER_KEY_ALIAS, CMD_OH_ROOT_CA_KEY_V1, 1180 CMD_ISSUER_KEY_RIGHTS, CMD_RIGHTS_123456, 1181 CMD_SUBJECT, CMD_PROFILE_CA, 1182 CMD_VALIDITY, CMD_VALIDITY_365, 1183 CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA, 1184 CMD_BASIC_CONSTRAINTS_PATH_LEN, CMD_BC_PATH_LEN_0}); 1185 } 1186 loadFile(String filePath)1187 private void loadFile(String filePath) throws IOException { 1188 ClassLoader classLoader = CmdUnitTest.class.getClassLoader(); 1189 URL resource = classLoader.getResource(filePath); 1190 assert resource != null; 1191 Files.copy(new File(resource.getPath()).toPath(), new File(filePath).toPath(), 1192 StandardCopyOption.REPLACE_EXISTING); 1193 } 1194 deleteFile(String filePath)1195 private void deleteFile(String filePath) throws IOException { 1196 if (FileUtils.isFileExist(filePath)) { 1197 Path path = Paths.get(filePath); 1198 Files.delete(path); 1199 } 1200 } 1201 1202 /** 1203 * Enumerated value of file type in zip. 1204 */ 1205 public enum FileType { 1206 FILE_NOT_EXISTED, 1207 FILE_UNCOMPRESSED, 1208 FILE_COMPRESSED; 1209 } 1210 } 1211