diff --git a/src/main/java/com/solab/iso8583/IsoValue.java b/src/main/java/com/solab/iso8583/IsoValue.java index 3fed509..4e57de9 100644 --- a/src/main/java/com/solab/iso8583/IsoValue.java +++ b/src/main/java/com/solab/iso8583/IsoValue.java @@ -277,7 +277,7 @@ public class IsoValue implements Cloneable { } if (binary) { if (digits == 4) { - outs.write((((l % 1000) / 100) << 4) | ((l % 1000)/100)); + outs.write((((l % 10000) / 1000) << 4) | ((l % 1000)/100)); } else if (digits == 3) { outs.write(l / 100); //00 to 09 automatically in BCD } diff --git a/src/main/java/com/solab/iso8583/parse/LlllvarParseInfo.java b/src/main/java/com/solab/iso8583/parse/LlllvarParseInfo.java index 0e415c4..17339cb 100644 --- a/src/main/java/com/solab/iso8583/parse/LlllvarParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LlllvarParseInfo.java @@ -74,7 +74,7 @@ public class LlllvarParseInfo extends FieldParseInfo { "Insufficient data for bin LLLLVAR header, field %d pos %d", field, pos), pos); } - final int len = ((buf[pos] & 0xf0) * 1000) + ((buf[pos] & 0x0f) * 100) + final int len = (((buf[pos] & 0xf0) >> 4) * 1000) + ((buf[pos] & 0x0f) * 100) + (((buf[pos + 1] & 0xf0) >> 4) * 10) + (buf[pos + 1] & 0x0f); if (len < 0) { throw new ParseException(String.format( diff --git a/src/test/java/com/solab/iso8583/TestLlllPatch.java b/src/test/java/com/solab/iso8583/TestLlllPatch.java new file mode 100644 index 0000000..76541b4 --- /dev/null +++ b/src/test/java/com/solab/iso8583/TestLlllPatch.java @@ -0,0 +1,93 @@ +package com.solab.iso8583; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.*; + +/** + * Extended test for LLLLVar field + * + * @author Peter Margetiak + */ +public class TestLlllPatch { + + private MessageFactory mfact = new MessageFactory<>(); + + @Before + public void setup() throws IOException { + mfact.setConfigPath("issue50.xml"); + mfact.setAssignDate(false); + } + + @Test + public void testParsingLength() throws Exception { + final int LENGTH = 1234; + + // prepare + String LLLLVar = makeLLLLVar(LENGTH); + StringBuilder LLLLVarBuilder = new StringBuilder(); + LLLLVarBuilder.append("01004000000000000000") + .append(String.format("%04d", LENGTH)).append(LLLLVar); + + // parse + IsoMessage m = mfact.parseMessage(LLLLVarBuilder.toString().getBytes(), 0); + Assert.assertNotNull(m); + Assert.assertEquals(LLLLVar, m.getObjectValue(2)); + } + + @Test + public void testSerialiseParseSmall() throws Exception { + testSerialiseParse(88); + } + + @Test + public void testSerialiseParseMedium() throws Exception { + testSerialiseParse(258); + } + + @Test + public void testSerialiseParseLarge() throws Exception { + testSerialiseParse(9919); + } + + private void testSerialiseParse(final int LENGTH) throws Exception { + // prepare + String LLLLVar = makeLLLLVar(LENGTH); + IsoMessage m = mfact.newMessage(0x100); + m.setValue(2, LLLLVar, IsoType.LLLLVAR, 0); + m.setBinary(true); + + // write + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + m.write(bout, 2); + bout.close(); + + // read + byte[] buf = new byte[2]; + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); + bin.read(buf); + Assert.assertNotEquals(buf, new byte[2]); + + int len = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff); + buf = new byte[len]; + bin.read(buf); + bin.close(); + + // parse + mfact.setUseBinaryMessages(true); + m = mfact.parseMessage(buf, mfact.getIsoHeader(0x100).length()); + Assert.assertNotNull(m); + Assert.assertEquals(LLLLVar, m.getObjectValue(2)); + } + + private String makeLLLLVar(final int length) { + final StringBuilder sb = new StringBuilder(); + for (int i = 0; i < length; i++) { + sb.append("a"); + } + + return sb.toString(); + } +} diff --git a/src/test/resources/issue50.xml b/src/test/resources/issue50.xml new file mode 100644 index 0000000..c97c871 --- /dev/null +++ b/src/test/resources/issue50.xml @@ -0,0 +1,7 @@ + + +
ISO999999999
+ + + +
\ No newline at end of file