|
| 1 | +using NetCore8583.Extensions; |
| 2 | +using NetCore8583.Parse; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace NetCore8583.Test |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Tests for issue #15: conditional fields beyond bit 64 when secondary bitmap is absent. |
| 9 | + /// When a parse template defines fields > 64 (e.g. field 125) but a message only contains |
| 10 | + /// fields within the primary bitmap, parsing must not throw. |
| 11 | + /// </summary> |
| 12 | + public class TestIssue15 |
| 13 | + { |
| 14 | + // Message type 9800 as ASCII: '9','8','0','0' |
| 15 | + private static readonly byte[] Mti = { 0x39, 0x38, 0x30, 0x30 }; |
| 16 | + |
| 17 | + // Primary-only BCD bitmap: bit 24 (field 24) and bit 31 (field 31) set, bit 1 = 0 (no secondary) |
| 18 | + // Byte 3 (bits 17-24): 0x01 sets bit 24 |
| 19 | + // Byte 4 (bits 25-32): 0x02 sets bit 31 |
| 20 | + private static readonly byte[] PrimaryBitmap = { 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00 }; |
| 21 | + |
| 22 | + // Primary bitmap with secondary indicator: same as above but bit 1 (MSB of byte 1) = 1 |
| 23 | + private static readonly byte[] PrimaryBitmapWithSecondary = { 0x80, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00 }; |
| 24 | + |
| 25 | + // Secondary bitmap: only bit 125 set. |
| 26 | + // Bit 125 is in secondary byte 8 (bits 121-128): value 0x08 |
| 27 | + private static readonly byte[] SecondaryBitmapWith125 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08 }; |
| 28 | + |
| 29 | + // Field 24 NUMERIC n3: "089" |
| 30 | + private static readonly byte[] Field24 = { 0x30, 0x38, 0x39 }; |
| 31 | + |
| 32 | + // Field 31 LLVAR: length "02", data "AB" |
| 33 | + private static readonly byte[] Field31 = { 0x30, 0x32, 0x41, 0x42 }; |
| 34 | + |
| 35 | + // Field 125 LLVAR: length "03", data "XYZ" |
| 36 | + private static readonly byte[] Field125 = { 0x30, 0x33, 0x58, 0x59, 0x5A }; |
| 37 | + |
| 38 | + private static sbyte[] BuildMessage(params byte[][] parts) |
| 39 | + { |
| 40 | + var totalLength = 0; |
| 41 | + foreach (var part in parts) totalLength += part.Length; |
| 42 | + var buf = new byte[totalLength]; |
| 43 | + var pos = 0; |
| 44 | + foreach (var part in parts) |
| 45 | + { |
| 46 | + part.CopyTo(buf, pos); |
| 47 | + pos += part.Length; |
| 48 | + } |
| 49 | + return buf.ToInt8(); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void TestConditionalField125_NoPrimaryBitmapAbsent_ShouldParseWithoutCrash() |
| 54 | + { |
| 55 | + // Field 125 is in the parse template but the message has no secondary bitmap. |
| 56 | + // Parsing must succeed and field 125 must not be present in the result. |
| 57 | + var mf = new MessageFactory<IsoMessage> { UseBinaryBitmap = true }; |
| 58 | + ConfigParser.ConfigureFromClasspathConfig(mf, @"/Resources/issue15.xml"); |
| 59 | + |
| 60 | + var buf = BuildMessage(Mti, PrimaryBitmap, Field24, Field31); |
| 61 | + var msg = mf.ParseMessage(buf, 0); |
| 62 | + |
| 63 | + Assert.NotNull(msg); |
| 64 | + Assert.Equal(0x9800, msg.Type); |
| 65 | + Assert.True(msg.HasField(24), "Field 24 should be present"); |
| 66 | + Assert.True(msg.HasField(31), "Field 31 should be present"); |
| 67 | + Assert.False(msg.HasField(125), "Field 125 should be absent when secondary bitmap is not present"); |
| 68 | + Assert.Equal("089", msg.GetField(24).ToString()); |
| 69 | + Assert.Equal("AB", msg.GetField(31).ToString()); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void TestConditionalField125_SecondaryBitmapPresent_ShouldParseField125() |
| 74 | + { |
| 75 | + // Field 125 is in the parse template and the message has a secondary bitmap with bit 125 set. |
| 76 | + var mf = new MessageFactory<IsoMessage> { UseBinaryBitmap = true }; |
| 77 | + ConfigParser.ConfigureFromClasspathConfig(mf, @"/Resources/issue15.xml"); |
| 78 | + |
| 79 | + var buf = BuildMessage(Mti, PrimaryBitmapWithSecondary, SecondaryBitmapWith125, Field24, Field31, Field125); |
| 80 | + var msg = mf.ParseMessage(buf, 0); |
| 81 | + |
| 82 | + Assert.NotNull(msg); |
| 83 | + Assert.Equal(0x9800, msg.Type); |
| 84 | + Assert.True(msg.HasField(24), "Field 24 should be present"); |
| 85 | + Assert.True(msg.HasField(31), "Field 31 should be present"); |
| 86 | + Assert.True(msg.HasField(125), "Field 125 should be present when secondary bitmap is set"); |
| 87 | + Assert.Equal("089", msg.GetField(24).ToString()); |
| 88 | + Assert.Equal("AB", msg.GetField(31).ToString()); |
| 89 | + Assert.Equal("XYZ", msg.GetField(125).ToString()); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments