From ac0d52f82484484febdedd614ec1748241905862 Mon Sep 17 00:00:00 2001 From: zfeixiang Date: Thu, 6 Mar 2025 10:00:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=97=A0=E5=8F=AF=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E6=97=B6=E4=B8=8D=E7=94=9F=E6=88=90.page.inf?= =?UTF-8?q?o=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zfeixiang --- .../com/ohos/hapsigntoolcmd/CmdUnitTest.java | 64 ++++++++++++++++++- .../codesigning/sign/PageInfoGenerator.java | 3 + .../hap/provider/SignProvider.java | 4 +- 3 files changed, 69 insertions(+), 2 deletions(-) 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 64d917ea..234b14e9 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; @@ -61,6 +62,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 +854,54 @@ 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 testExistRunnableFileHap() 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)); + } + private void multiBundleTest(String bundleSuffix) throws IOException { for (FileType abcFile : FileType.values()) { for (FileType soFile : FileType.values()) { @@ -924,7 +978,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 +1022,14 @@ public class CmdUnitTest { CMD_OUT_PROFILE, "test" + File.separator + "1.p7b" }); assertTrue(result); + return signedHap; + } + + private boolean existPagesInfoFile(String signedHap) throws IOException { + try (JarFile inputHap = new JarFile(signedHap, false)) { + 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 dbcfda66..14465945 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 8c779e76..df61a76f 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) { -- Gitee From 5c5acef243f657b7f3a84eba054e4f4cb7e7e3a9 Mon Sep 17 00:00:00 2001 From: zfeixiang Date: Thu, 6 Mar 2025 16:34:15 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=B8=8D=E7=94=9F=E6=88=90.page.info?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E5=9C=BA=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zfeixiang --- .../java/com/ohos/hapsigntoolcmd/CmdUnitTest.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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 234b14e9..41417e75 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 @@ -53,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; /** @@ -895,13 +896,22 @@ public class CmdUnitTest { @Order(16) @Test - public void testExistRunnableFileHap() throws IOException { + 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()) { @@ -1026,7 +1036,7 @@ public class CmdUnitTest { } private boolean existPagesInfoFile(String signedHap) throws IOException { - try (JarFile inputHap = new JarFile(signedHap, false)) { + try (ZipFile inputHap = new ZipFile(signedHap)) { ZipEntry entry = inputHap.getEntry(FileUtils.BIT_MAP_FILENAME); return entry != null; } -- Gitee