diff --git a/hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/CmdUnitTest.java b/hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/CmdUnitTest.java index 64d917ea69d0cc1cbef2ad487522c536f7f35d08..41417e7537edf41e777ebc6cbf0cc0bf0e600d38 100644 --- a/hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/CmdUnitTest.java +++ b/hapsigntool/hap_sign_tool/src/test/java/com/ohos/hapsigntoolcmd/CmdUnitTest.java @@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import com.ohos.hapsigntool.HapSignTool; import com.ohos.hapsigntool.codesigning.utils.HapUtils; +import com.ohos.hapsigntool.entity.ParamConstants; import com.ohos.hapsigntool.error.CustomException; import com.ohos.hapsigntool.error.ProfileException; import com.ohos.hapsigntool.utils.KeyPairTools; @@ -52,6 +53,7 @@ import java.util.Random; import java.util.jar.JarFile; import java.util.zip.CRC32; import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; /** @@ -61,6 +63,11 @@ import java.util.zip.ZipOutputStream; */ @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class CmdUnitTest { + /** + * Command line parameter signCode. + */ + public static final String CMD_SIGN_CODE = "-signCode"; + /** * Command line parameter appCaCertFile. */ @@ -848,6 +855,63 @@ public class CmdUnitTest { signAndVerifyHap(unsignedHap.getAbsolutePath(), ".hap"); } + @Order(14) + @Test + public void testNoCodeSignHap() throws IOException { + File unsignedHap = generateHapFile(FileType.FILE_UNCOMPRESSED, FileType.FILE_UNCOMPRESSED, + FileType.FILE_UNCOMPRESSED, FileType.FILE_UNCOMPRESSED, ".hap"); + String signedHap = File.createTempFile("signed-", ".hap", new File("test")).getAbsolutePath(); + + boolean result = HapSignTool.processCmd(new String[] { + CmdUtil.Method.SIGN_APP, + CMD_MODE, CMD_LOCAL_SIGN, + CMD_KEY_ALIAS, CMD_OH_APP1_KEY_V1, + CMD_KEY_RIGHTS, CMD_RIGHTS_123456, + CMD_APP_CERT_FILE, CMD_APP_DEBUG_CERT_PATH, + CMD_PROFILE_FILE, CMD_SIGN_PROFILE_PATH, + CMD_SIGN_ALG, CMD_SHA_256_WITH_ECDSA, + CMD_KEY_STORE_FILE, CMD_KEY_APP_STORE_PATH, + CMD_KEY_STORE_RIGHTS, CMD_RIGHTS_123456, + CMD_IN_FILE, unsignedHap.getAbsolutePath(), + CMD_OUT_FILE, signedHap, + CMD_SIGN_CODE, ParamConstants.SignCodeFlag.DISABLE_SIGN_CODE.getSignCodeFlag() + }); + assertTrue(result); + result = HapSignTool.processCmd(new String[] { + CmdUtil.Method.VERIFY_APP, CMD_IN_FILE, signedHap, CMD_OUT_CERT_CHAIN, "test" + File.separator + "1.cer", + CMD_OUT_PROFILE, "test" + File.separator + "1.p7b" + }); + assertTrue(result); + assertFalse(existPagesInfoFile(signedHap)); + } + + @Order(15) + @Test + public void testNoRunnableFileHap() throws IOException { + File unsignedHap = generateHapFile(FileType.FILE_NOT_EXISTED, FileType.FILE_NOT_EXISTED, + FileType.FILE_UNCOMPRESSED, FileType.FILE_UNCOMPRESSED, ".hap"); + String signedHap = signAndVerifyHap(unsignedHap.getAbsolutePath(), ".hap"); + assertFalse(existPagesInfoFile(signedHap)); + } + + @Order(16) + @Test + public void testUncompressedRunnableFileHap() throws IOException { + File unsignedHap = generateHapFile(FileType.FILE_UNCOMPRESSED, FileType.FILE_UNCOMPRESSED, + FileType.FILE_UNCOMPRESSED, FileType.FILE_UNCOMPRESSED, ".hap"); + String signedHap = signAndVerifyHap(unsignedHap.getAbsolutePath(), ".hap"); + assertTrue(existPagesInfoFile(signedHap)); + } + + @Order(17) + @Test + public void testCompressedRunnableFileHap() throws IOException { + File unsignedHap = generateHapFile(FileType.FILE_COMPRESSED, FileType.FILE_COMPRESSED, + FileType.FILE_COMPRESSED, FileType.FILE_UNCOMPRESSED, ".hap"); + String signedHap = signAndVerifyHap(unsignedHap.getAbsolutePath(), ".hap"); + assertFalse(existPagesInfoFile(signedHap)); + } + private void multiBundleTest(String bundleSuffix) throws IOException { for (FileType abcFile : FileType.values()) { for (FileType soFile : FileType.values()) { @@ -924,7 +988,7 @@ public class CmdUnitTest { return bytes; } - private void signAndVerifyHap(String unsignedHap, String bundleSuffix) throws IOException { + private String signAndVerifyHap(String unsignedHap, String bundleSuffix) throws IOException { String signedHap = File.createTempFile("signed-", bundleSuffix, new File("test")).getAbsolutePath(); // debug boolean result = HapSignTool.processCmd(new String[] { @@ -968,6 +1032,14 @@ public class CmdUnitTest { CMD_OUT_PROFILE, "test" + File.separator + "1.p7b" }); assertTrue(result); + return signedHap; + } + + private boolean existPagesInfoFile(String signedHap) throws IOException { + try (ZipFile inputHap = new ZipFile(signedHap)) { + ZipEntry entry = inputHap.getEntry(FileUtils.BIT_MAP_FILENAME); + return entry != null; + } } /** diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/codesigning/sign/PageInfoGenerator.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/codesigning/sign/PageInfoGenerator.java index dbcfda66f03cf63c42eba38b1687aa1634311d5c..14465945c5b004b6d0fd2999a8bdb6eb197c2d20 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/codesigning/sign/PageInfoGenerator.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/codesigning/sign/PageInfoGenerator.java @@ -125,6 +125,9 @@ public class PageInfoGenerator { * @throws HapFormatException hap format error */ public byte[] generateBitMap() throws HapFormatException { + if (excSegmentList.isEmpty()) { + return new byte[0]; + } if (!NumberUtils.isMultiple4K(maxEntryDataOffset)) { throw new HapFormatException(CodeSignErrMsg.FILE_4K_ALIGNMENT_ERROR.toString(maxEntryDataOffset)); } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/hap/provider/SignProvider.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/hap/provider/SignProvider.java index 8c779e764882e2611261c6eaac3a3233b6f476c7..df61a76f19e190062b3f3422aaf1197257dfe07e 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/hap/provider/SignProvider.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/hap/provider/SignProvider.java @@ -503,7 +503,9 @@ public abstract class SignProvider { throws IOException, HapFormatException, ElfFormatException { Zip zip = new Zip(input); zip.alignment(alignment); - if (StringUtils.containsIgnoreCase(CodeSigning.SUPPORT_FILE_FORM, suffix)) { + if (StringUtils.containsIgnoreCase(CodeSigning.SUPPORT_FILE_FORM, suffix) + && ParamConstants.SignCodeFlag.ENABLE_SIGN_CODE.getSignCodeFlag() + .equals(signParams.get(ParamConstants.PARAM_SIGN_CODE))) { PageInfoGenerator pageInfoGenerator = new PageInfoGenerator(zip); byte[] bitMap = pageInfoGenerator.generateBitMap(); if (bitMap != null && bitMap.length > 0) {