diff --git a/jpos/src/main/java/org/jpos/iso/ISOUtil.java b/jpos/src/main/java/org/jpos/iso/ISOUtil.java index d92f9264c7..52349c7c25 100644 --- a/jpos/src/main/java/org/jpos/iso/ISOUtil.java +++ b/jpos/src/main/java/org/jpos/iso/ISOUtil.java @@ -32,6 +32,10 @@ import java.util.StringTokenizer; import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.regex.Pattern; +import java.math.BigInteger; +import java.util.regex.Pattern; +import java.util.regex.Matcher; /** * various functions needed to pack/unpack ISO-8583 fields @@ -177,7 +181,7 @@ public static String trim (String s) { public static String zeropad(String s, int len) throws ISOException { return padleft(s, len, '0'); } - + /** * zeropads a long without throwing an ISOException (performs modulus operation) * @@ -680,8 +684,26 @@ public static byte[] hex2byte (byte[] b, int offset, int len) { * @return byte array */ public static byte[] hex2byte (String s) { - return hex2byte (s, CHARSET); + + if (s.isEmpty() || s.length() % 2 != 0) //verificar si el string es vacio + { + throw new IllegalArgumentException("The string is empty or null, invalid hexadecimal"); + } + // Verificar si cada carácter es un dígito hexadecimal válido + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (!isDigitHexadecimal(c)) { + throw new IllegalArgumentException("The string is not hexadecimal"); + } + } + return hex2byte(s, CHARSET); + } + + public static boolean isDigitHexadecimal(char c) { + return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); + } + /** * Converts a hex string into a byte array * @param s source string (with Hex representation) diff --git a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java index 921d624fe0..e1b124344f 100644 --- a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java +++ b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java @@ -34,6 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.Assert.assertArrayEquals; import java.math.BigDecimal; import java.math.MathContext; @@ -43,13 +44,36 @@ import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; +import java.util.regex.Pattern; -import org.junit.jupiter.api.Assertions; +import org.fest.assertions.Assert; import org.junit.jupiter.api.Test; +import org.junit.Assert; +import java.util.Arrays; public class ISOUtilTest { final String lineSep = System.getProperty("line.separator"); + + + @Test + public void testHex2byte_ValidInput() { + byte[] result = ISOUtil.hex2byte("1A2B3C"); + byte [] expected = {0x1A, 0x2B, 0x3C}; + assertArrayEquals(expected, result); + } + + @Test + public void testHex2byte_InvalidInput_ThrowsExceptionWithMessage() { + + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + ISOUtil.hex2byte("1A2B3C"); + }, "Not hex"); + + assertEquals("Not hex", exception.getMessage()); + } + + @Test public void testAsciiToEbcdic() throws Throwable { byte[] a = new byte[0];