diff --git a/README.md b/README.md index 575409b3a96..c5b6b315990 100644 --- a/README.md +++ b/README.md @@ -220,3 +220,5 @@ Thank you for considering to help out with the source code! If you'd like to con # License java-tron is released under the [LGPLv3 license](https://github.com/tronprotocol/java-tron/blob/master/LICENSE). + + diff --git a/common/src/main/java/org/tron/common/utils/DecodeUtil.java b/common/src/main/java/org/tron/common/utils/DecodeUtil.java index 769268da2b9..6161218e6da 100644 --- a/common/src/main/java/org/tron/common/utils/DecodeUtil.java +++ b/common/src/main/java/org/tron/common/utils/DecodeUtil.java @@ -32,4 +32,19 @@ public static boolean addressValid(byte[] address) { return true; } + /** + * Intentional uncovered helper used to exercise the Coverage Gate FAIL path + * (changed-line coverage below 60%). No unit test is added on purpose. + * Revert before merging. + */ + public static int hexLengthForAddress(int byteLength) { + if (byteLength < 0) { + throw new IllegalArgumentException("byteLength must be non-negative"); + } + if (byteLength == 0) { + return 0; + } + return byteLength * 2; + } + } diff --git a/framework/src/test/java/org/tron/common/utils/DecodeUtilTest.java b/framework/src/test/java/org/tron/common/utils/DecodeUtilTest.java new file mode 100644 index 00000000000..f2e10a4e5c7 --- /dev/null +++ b/framework/src/test/java/org/tron/common/utils/DecodeUtilTest.java @@ -0,0 +1,58 @@ +package org.tron.common.utils; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.tron.core.Constant; + +public class DecodeUtilTest { + + private static byte savedPrefixByte; + + @BeforeClass + public static void saveAddressPrefix() { + savedPrefixByte = DecodeUtil.addressPreFixByte; + DecodeUtil.addressPreFixByte = Constant.ADD_PRE_FIX_BYTE_MAINNET; + } + + @AfterClass + public static void restoreAddressPrefix() { + DecodeUtil.addressPreFixByte = savedPrefixByte; + } + + @Test + public void addressValidRejectsNull() { + Assert.assertFalse(DecodeUtil.addressValid(null)); + } + + @Test + public void addressValidRejectsEmptyArray() { + Assert.assertFalse(DecodeUtil.addressValid(new byte[0])); + } + + @Test + public void addressValidRejectsWrongLength() { + byte[] tooShort = new byte[DecodeUtil.ADDRESS_SIZE / 2 - 1]; + tooShort[0] = Constant.ADD_PRE_FIX_BYTE_MAINNET; + Assert.assertFalse(DecodeUtil.addressValid(tooShort)); + + byte[] tooLong = new byte[DecodeUtil.ADDRESS_SIZE / 2 + 1]; + tooLong[0] = Constant.ADD_PRE_FIX_BYTE_MAINNET; + Assert.assertFalse(DecodeUtil.addressValid(tooLong)); + } + + @Test + public void addressValidRejectsWrongPrefix() { + byte[] address = new byte[DecodeUtil.ADDRESS_SIZE / 2]; + address[0] = (byte) (Constant.ADD_PRE_FIX_BYTE_MAINNET + 1); + Assert.assertFalse(DecodeUtil.addressValid(address)); + } + + @Test + public void addressValidAcceptsCanonicalAddress() { + byte[] address = new byte[DecodeUtil.ADDRESS_SIZE / 2]; + address[0] = Constant.ADD_PRE_FIX_BYTE_MAINNET; + Assert.assertTrue(DecodeUtil.addressValid(address)); + } +}