From 7ec06722923d96d2e51300bafb44b920ca341d58 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Fri, 10 Mar 2023 16:49:58 +0100 Subject: ssb unit tests --- .../uic/barcode/ssbFrame/SsbCommonTicketPart.java | 7 +- .../java/org/uic/barcode/ssbFrame/SsbFrame.java | 102 ++++++++++++++++----- .../java/org/uic/barcode/ssbFrame/SsbGroup.java | 19 ++-- .../java/org/uic/barcode/ssbFrame/SsbHeader.java | 8 +- .../uic/barcode/ssbFrame/SsbNonReservation.java | 14 ++- .../java/org/uic/barcode/ssbFrame/SsbNonUic.java | 41 +++++++-- .../java/org/uic/barcode/ssbFrame/SsbPass.java | 11 ++- .../org/uic/barcode/ssbFrame/SsbReservation.java | 24 +++-- .../org/uic/barcode/ssbFrame/SsbTicketPart.java | 10 +- 9 files changed, 164 insertions(+), 72 deletions(-) (limited to 'src/main/java/org/uic/barcode/ssbFrame') diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java b/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java index 9512fc1..0a158f3 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java @@ -46,14 +46,13 @@ public abstract class SsbCommonTicketPart extends SsbTicketPart { return offset; } - protected int encodeCommonPart(byte[] bytes) { + protected int encodeCommonPart(byte[] bytes, int offset) { BitBuffer bits = new ByteBitBuffer(bytes); - - int offset = 27; // header offset + bits.putInteger(offset,7, numberOfAdults); offset = offset + 7; - bits.putInteger(numberOfChildren,offset, 7); + bits.putInteger(offset, 7, numberOfChildren); offset = offset + 7; bits.put(offset,specimen); offset++; diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbFrame.java b/src/main/java/org/uic/barcode/ssbFrame/SsbFrame.java index 81b5eb4..1ee68bb 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbFrame.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbFrame.java @@ -20,7 +20,7 @@ import org.uic.barcode.utils.SecurityUtils; public class SsbFrame { - private SsbHeader header = null; + private SsbHeader header = new SsbHeader(); private byte[] signaturePart1 = null; @@ -42,33 +42,38 @@ public class SsbFrame { throw new EncodingFormatException("Data size does not fit to SSB"); } - header = new SsbHeader(); - header.decode(bytes); + if (header == null) { + header = new SsbHeader(); + } + + int offset = 0; + + offset = offset + header.decodeContent(bytes,0); if (header.getTicketType().equals(SsbTicketType.UIC_1_IRT_RES_BOA)) { reservationData = new SsbReservation(); - reservationData.decode(bytes); + offset = offset + reservationData.decodeContent(bytes,offset); } else if (header.getTicketType().equals(SsbTicketType.UIC_2_NRT)) { nonReservationData = new SsbNonReservation(); - nonReservationData.decode(bytes); + offset = offset + nonReservationData.decodeContent(bytes,offset); } else if (header.getTicketType().equals(SsbTicketType.UIC_3_GRP)) { groupData = new SsbGroup(); - groupData.decode(bytes); + offset = offset + groupData.decodeContent(bytes, offset); } else if (header.getTicketType().equals(SsbTicketType.UIC_4_RPT)) { passData = new SsbPass(); - passData.decode(bytes); + offset = offset + passData.decodeContent(bytes,offset); } else { nonUicData = new SsbNonUic(); - nonUicData.decode(bytes); + offset = offset + nonUicData.decodeContent(bytes,offset); } @@ -76,8 +81,8 @@ public class SsbFrame { signaturePart2 = new byte[28]; for (int i = 0 ; i < 28;i++) { - signaturePart1[i] = bytes[59 + i]; - signaturePart2[i] = bytes[59 + 28 + i]; + signaturePart1[i] = bytes[58 + i]; + signaturePart2[i] = bytes[58 + 28 + i]; } } @@ -86,27 +91,48 @@ public class SsbFrame { byte[] bytes = new byte[114]; - header.encode(bytes); + int offset = header.encodeContent(bytes,0); + + if (nonUicData != null) { - nonUicData.encode(bytes); + offset = nonUicData.encodeContent(bytes, offset); } else if (nonReservationData != null) { - nonReservationData.encode(bytes); + offset = nonReservationData.encodeContent(bytes, offset); } else if (reservationData != null) { - reservationData.encode(bytes); + offset = reservationData.encodeContent(bytes, offset); } else if (groupData != null) { - groupData.encode(bytes); + offset = groupData.encodeContent(bytes, offset); } else if (passData != null) { - passData.encode(bytes); + offset = passData.encodeContent(bytes, offset); } else { throw new EncodingFormatException("Data Content for SSB missing"); }; - for (int i = 0 ; i < 28;i++) { - bytes[59 + i] = signaturePart1[i]; - bytes[59 + 28 + i] = signaturePart2[i]; + + if (signaturePart1.length > 28) { + throw new EncodingFormatException("Signature too large"); + } + if (signaturePart2.length > 28) { + throw new EncodingFormatException("Signature too large"); } + for (int i = 1 ; i < 29; i++) { + int sigInd = signaturePart1.length - i; + if (sigInd < signaturePart1.length && sigInd >= 0) { + bytes[58 + 28 - i] = signaturePart1[sigInd]; + } else { + bytes[58 + 28 - i] = '\0'; + } + sigInd = signaturePart2.length - i; + if (sigInd < signaturePart2.length && sigInd >= 0) { + bytes[58 + 28 + 28 - i] = signaturePart2[sigInd]; + } else { + bytes[58 + 28 + 28 - i] = '\0'; + } + } + + return bytes; } @@ -115,18 +141,19 @@ public class SsbFrame { byte[] bytes = new byte[58]; - header.encode(bytes); + int offset = header.encodeContent(bytes,0); + if (nonUicData != null) { - nonUicData.encode(bytes); + offset = nonUicData.encodeContent(bytes, offset); } else if (nonReservationData != null) { - nonReservationData.encode(bytes); + offset = nonReservationData.encodeContent(bytes, offset); } else if (reservationData != null) { - reservationData.encode(bytes); + offset = reservationData.encodeContent(bytes, offset); } else if (groupData != null) { - groupData.encode(bytes); + offset = groupData.encodeContent(bytes, offset); } else if (passData != null) { - passData.encode(bytes); + offset = passData.encodeContent(bytes, offset); } else { throw new EncodingFormatException("Data Content for SSB missing"); }; @@ -165,6 +192,10 @@ public class SsbFrame { public void setNonUicData(SsbNonUic nonUicData) { this.nonUicData = nonUicData; + this.nonReservationData = null; + this.reservationData = null; + this.groupData = null; + this.passData = null; } public SsbNonReservation getNonReservationData() { @@ -173,6 +204,11 @@ public class SsbFrame { public void setNonReservationData(SsbNonReservation nonReservationData) { this.nonReservationData = nonReservationData; + header.setTicketType(SsbTicketType.UIC_2_NRT); + this.reservationData = null; + this.nonUicData = null; + this.groupData = null; + this.passData = null; } public SsbReservation getReservationData() { @@ -180,6 +216,11 @@ public class SsbFrame { } public void setReservationData(SsbReservation reservationData) { + header.setTicketType(SsbTicketType.UIC_1_IRT_RES_BOA); + this.nonReservationData = null; + this.nonUicData = null; + this.groupData = null; + this.passData = null; this.reservationData = reservationData; } @@ -189,6 +230,12 @@ public class SsbFrame { public void setGroupData(SsbGroup groupData) { this.groupData = groupData; + header.setTicketType(SsbTicketType.UIC_3_GRP); + this.nonReservationData = null; + this.nonUicData = null; + this.reservationData = null; + this.passData = null; + } public SsbPass getPassData() { @@ -197,6 +244,11 @@ public class SsbFrame { public void setPassData(SsbPass passData) { this.passData = passData; + header.setTicketType(SsbTicketType.UIC_4_RPT); + this.nonReservationData = null; + this.nonUicData = null; + this.groupData = null; + this.reservationData = null; } public void signLevel1(PrivateKey key, Provider prov, String keyId, String algorithmOid) throws Exception { diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbGroup.java b/src/main/java/org/uic/barcode/ssbFrame/SsbGroup.java index 52c3a52..1b2f6e7 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbGroup.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbGroup.java @@ -18,9 +18,9 @@ public class SsbGroup extends SsbCommonTicketPart { @Override - protected void decodeContent(byte[] bytes) { + protected int decodeContent(byte[] bytes, int offset) { - int offset = decodeCommonPart(bytes); + offset = offset + decodeCommonPart(bytes); BitBuffer bits = new ByteBitBuffer(bytes); @@ -44,15 +44,17 @@ public class SsbGroup extends SsbCommonTicketPart { infoCode = bits.getInteger(offset, 14); offset = offset + 14; - text = bits.getChar6String(offset, 222); - offset = offset + 222; + text = bits.getChar6String(offset, 144); + offset = offset + 144; + + return offset; } @Override - protected void encodeContent(byte[] bytes) { + protected int encodeContent(byte[] bytes, int offset) { - int offset = encodeCommonPart(bytes); + offset = offset + encodeCommonPart(bytes, offset); BitBuffer bits = new ByteBitBuffer(bytes); @@ -65,7 +67,7 @@ public class SsbGroup extends SsbCommonTicketPart { bits.putInteger(offset, 9, lastDayOfValidity); offset = offset + 9; - offset = stations.decode(offset, bytes); + offset = stations.encode(offset, bytes); bits.putChar6String(offset, 72,groupName); offset = offset + 72; @@ -77,8 +79,9 @@ public class SsbGroup extends SsbCommonTicketPart { offset = offset + 14; bits.putChar6String(offset, 144, text); - offset = offset + 222; + offset = offset + 144; + return offset; } public int getFirstDayOfValidity() { diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbHeader.java b/src/main/java/org/uic/barcode/ssbFrame/SsbHeader.java index 0d5424b..2ea4a51 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbHeader.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbHeader.java @@ -27,7 +27,7 @@ public class SsbHeader extends SsbTicketPart { public SsbHeader() { } - public void decodeContent(byte[] headerData) { + public int decodeContent(byte[] headerData, int offset) { BitBuffer bits = new ByteBitBuffer(headerData); @@ -36,11 +36,11 @@ public class SsbHeader extends SsbTicketPart { keyId = bits.getInteger(18, 4); ticketType = SsbTicketType.values()[bits.getInteger(22, 5)]; - return; + return 4 + 14 + 4 + 5; } - public void encodeContent(byte[] bytes) { + public int encodeContent(byte[] bytes, int offset) { BitBuffer bits = new ByteBitBuffer(bytes); @@ -49,6 +49,8 @@ public class SsbHeader extends SsbTicketPart { bits.putInteger(18, 4, keyId); bits.putInteger(22, 5, ticketType.ordinal()); + return 4 + 14 + 4 + 5; + } diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbNonReservation.java b/src/main/java/org/uic/barcode/ssbFrame/SsbNonReservation.java index e96e853..a1fb3ae 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbNonReservation.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbNonReservation.java @@ -14,9 +14,9 @@ public class SsbNonReservation extends SsbCommonTicketPart { @Override - protected void decodeContent(byte[] bytes) { + protected int decodeContent(byte[] bytes, int offset) { - int offset = decodeCommonPart(bytes); + offset = offset + decodeCommonPart(bytes); BitBuffer bits = new ByteBitBuffer(bytes); @@ -37,12 +37,14 @@ public class SsbNonReservation extends SsbCommonTicketPart { text = bits.getChar6String(offset, 222); offset = offset + 222; + return offset; + } @Override - protected void encodeContent(byte[] bytes) { + protected int encodeContent(byte[] bytes, int offset) { - int offset = encodeCommonPart(bytes); + offset = offset + encodeCommonPart(bytes, offset); BitBuffer bits = new ByteBitBuffer(bytes); @@ -55,7 +57,7 @@ public class SsbNonReservation extends SsbCommonTicketPart { bits.putInteger(offset, 9, lastDayOfValidity); offset = offset + 9; - offset = stations.decode(offset, bytes); + offset = stations.encode(offset, bytes); bits.putInteger(offset, 14, infoCode); offset = offset + 14; @@ -63,6 +65,8 @@ public class SsbNonReservation extends SsbCommonTicketPart { bits.putChar6String(offset, 222, text); offset = offset + 222; + return offset; + } public int getFirstDayOfValidity() { diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbNonUic.java b/src/main/java/org/uic/barcode/ssbFrame/SsbNonUic.java index 28e5105..1f0049e 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbNonUic.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbNonUic.java @@ -6,31 +6,54 @@ import org.uic.barcode.asn1.uper.ByteBitBuffer; public class SsbNonUic extends SsbTicketPart { + + + byte[] openData = null; @Override - protected void decodeContent(byte[] bytes) { + protected int decodeContent(byte[] bytes, int offset) { + + BitBuffer bits = new ByteBitBuffer(bytes); + + StringBuffer sb = new StringBuffer(); + + + for (int i = offset; i < openDataLength; i++) { + if (bits.get(i) == false) { + sb.append("1"); + } else { + sb.append("0"); + } + } + + for (int i = openDataLength; i < 440; i++) { + sb.append("0"); + } - String bitString = AsnUtils.toBooleanString(bytes); - - openData = AsnUtils.fromBooleanString(bitString); + openData = AsnUtils.fromBooleanString(sb.toString()); + + return offset + openDataLength ; } @Override - protected void encodeContent(byte[] bytes) { + protected int encodeContent(byte[] bytes, int offset) { BitBuffer bits = new ByteBitBuffer(bytes); String bitString = AsnUtils.toBooleanString(openData); - for (int i = 0;i< 58 *8 ;i++) { - if (bitString.charAt(i) == '0') { - bits.put(27 + i, true); + + for (int i = 0; i< openDataLength ; i++) { + if (i < bitString.length() && bitString.charAt(i) == '0') { + bits.put(offset + i, true); } else { - bits.put(27 + i, false); + bits.put(offset + i, false); } } + + return offset + openDataLength; } public byte[] getOpenData() { diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbPass.java b/src/main/java/org/uic/barcode/ssbFrame/SsbPass.java index a38dfaf..3598efd 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbPass.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbPass.java @@ -35,9 +35,9 @@ public class SsbPass extends SsbCommonTicketPart { private String text = null; @Override - protected void decodeContent(byte[] bytes) { + protected int decodeContent(byte[] bytes, int offset) { - int offset = decodeCommonPart(bytes); + offset = offset + decodeCommonPart(bytes); BitBuffer bits = new ByteBitBuffer(bytes); @@ -77,12 +77,13 @@ public class SsbPass extends SsbCommonTicketPart { text = bits.getChar6String(offset, 240); offset = offset + 240; + return offset; } @Override - protected void encodeContent(byte[] bytes) { + protected int encodeContent(byte[] bytes, int offset) { - int offset = encodeCommonPart(bytes); + offset = offset + encodeCommonPart(bytes, offset); BitBuffer bits = new ByteBitBuffer(bytes); @@ -121,6 +122,8 @@ public class SsbPass extends SsbCommonTicketPart { bits.putChar6String(offset, 240,text); offset = offset + 240; + + return offset; } public int getPassSubType() { diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java b/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java index c7f520d..73017d7 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java @@ -30,17 +30,17 @@ public class SsbReservation extends SsbCommonTicketPart { @Override - protected void decodeContent(byte[] bytes) { + protected int decodeContent(byte[] bytes, int offset) { - int offset = decodeCommonPart(bytes); + offset = offset + decodeCommonPart(bytes); BitBuffer bits = new ByteBitBuffer(bytes); ticketSubType = bits.getInteger(offset, 2); - offset = offset + 4; + offset = offset + 2; stations = new SsbStations(); - stations.decode(offset, bytes); + offset = stations.decode(offset, bytes); /* * Departure date : First day of validity from the issuing date Num (<367) 9,000 @@ -60,7 +60,7 @@ public class SsbReservation extends SsbCommonTicketPart { offset = offset + 11; train = bits.getChar6String(offset, 30); - offset = offset +30; + offset = offset + 30; coach = bits.getInteger(offset, 10); offset = offset + 10; @@ -76,17 +76,19 @@ public class SsbReservation extends SsbCommonTicketPart { text = bits.getChar6String(offset, 162); offset = offset + 162; + + return offset; } @Override - protected void encodeContent(byte[] bytes) { + protected int encodeContent(byte[] bytes, int offset) { - int offset = encodeCommonPart(bytes); + offset = offset + encodeCommonPart(bytes, offset); BitBuffer bits = new ByteBitBuffer(bytes); bits.putInteger(offset, 2,ticketSubType); - offset = offset + 4; + offset = offset + 2; offset = stations.encode(offset, bytes); @@ -108,7 +110,7 @@ public class SsbReservation extends SsbCommonTicketPart { offset = offset + 11; bits.putChar6String(offset, 30,train); - offset = offset +30; + offset = offset + 30; bits.putInteger(offset, 10,coach); offset = offset + 10; @@ -117,7 +119,7 @@ public class SsbReservation extends SsbCommonTicketPart { offset = offset + 18; bits.put(offset, overbooking); - offset++; + offset++; bits.putInteger(offset, 14, infoCode); offset = offset + 14; @@ -125,6 +127,8 @@ public class SsbReservation extends SsbCommonTicketPart { bits.putChar6String(offset, 162, text); offset = offset + 162; + return offset; + } public SsbStations getStations() { diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbTicketPart.java b/src/main/java/org/uic/barcode/ssbFrame/SsbTicketPart.java index 3855c5c..5583e66 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbTicketPart.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbTicketPart.java @@ -4,23 +4,25 @@ import org.uic.barcode.ticket.EncodingFormatException; public abstract class SsbTicketPart { + public static int openDataLength = 437; + public void decode(byte[] bytes) throws EncodingFormatException { if (bytes.length != 114) { throw new EncodingFormatException("Data size does not fit to SSB"); } - decodeContent(bytes); + decodeContent(bytes, 0); }; - protected abstract void decodeContent(byte[] bytes); + protected abstract int decodeContent(byte[] bytes , int offset); public void encode(byte[] bytes) throws EncodingFormatException { if (bytes.length != 114) { throw new EncodingFormatException("Data size does not fit to SSB"); } - encodeContent(bytes); + encodeContent(bytes, 0); } - protected abstract void encodeContent(byte[] bytes); + protected abstract int encodeContent(byte[] bytes, int offset); -- cgit v1.2.3