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 --- src/main/java/org/uic/barcode/Decoder.java | 9 +- src/main/java/org/uic/barcode/Encoder.java | 2 + .../org/uic/barcode/asn1/uper/ByteBitBuffer.java | 1 + src/main/java/org/uic/barcode/package.html | 30 +-- .../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 +- .../java/org/uic/barcode/staticFrame/package.html | 29 +-- .../org/uic/barcode/ticket/api/asn/package.html | 8 +- .../org/uic/barcode/ticket/api/impl/package.html | 7 +- .../org/uic/barcode/ticket/api/spec/package.html | 10 +- .../org/uic/barcode/ticket/api/utils/package.html | 7 +- src/main/java/org/uic/barcode/ticket/package.html | 12 +- src/main/java/org/uic/barcode/utils/package.html | 21 +- .../uic/barcode/test/SsbFrameBarcodeTestGroup.java | 220 ++++++++++++++++++++ .../barcode/test/SsbFrameBarcodeTestNonUic.java | 207 +++++++++++++++++++ .../uic/barcode/test/SsbFrameBarcodeTestNrt.java | 214 ++++++++++++++++++++ .../uic/barcode/test/SsbFrameBarcodeTestPass.java | 215 ++++++++++++++++++++ .../test/SsbFrameBarcodeTestReservation.java | 222 +++++++++++++++++++++ .../uic/barcode/test/utils/SsbTicketFactory.java | 193 ++++++++++++++++++ 26 files changed, 1520 insertions(+), 123 deletions(-) create mode 100644 src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestGroup.java create mode 100644 src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestNonUic.java create mode 100644 src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestNrt.java create mode 100644 src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestPass.java create mode 100644 src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestReservation.java create mode 100644 src/test/java/org/uic/barcode/test/utils/SsbTicketFactory.java diff --git a/src/main/java/org/uic/barcode/Decoder.java b/src/main/java/org/uic/barcode/Decoder.java index 1b3fb0c..85faa4a 100644 --- a/src/main/java/org/uic/barcode/Decoder.java +++ b/src/main/java/org/uic/barcode/Decoder.java @@ -213,6 +213,13 @@ public class Decoder { } else { throw e; } + } catch (AssertionError e) { + dynamicFrame = null; + if (isSsbFrame(data)) { + decodeSsbFrame(data); + } else { + throw new EncodingFormatException(e.getMessage()); + } } } else if (isStaticHeader(data)){ @@ -292,7 +299,7 @@ public class Decoder { * @return true, if is static header */ private boolean isSsbFrame(byte[] data) { - if (data.length == 144) { + if (data.length == 114) { return true; } return false; diff --git a/src/main/java/org/uic/barcode/Encoder.java b/src/main/java/org/uic/barcode/Encoder.java index cf2d4d2..f2b9b0c 100644 --- a/src/main/java/org/uic/barcode/Encoder.java +++ b/src/main/java/org/uic/barcode/Encoder.java @@ -462,6 +462,8 @@ public class Encoder { return DynamicFrameCoder.encode(dynamicFrame); } else if (staticFrame != null) { return staticFrame.encode(); + } else if (ssbFrame != null) { + return ssbFrame.encode(); } return null; } diff --git a/src/main/java/org/uic/barcode/asn1/uper/ByteBitBuffer.java b/src/main/java/org/uic/barcode/asn1/uper/ByteBitBuffer.java index e409005..ce5ca60 100644 --- a/src/main/java/org/uic/barcode/asn1/uper/ByteBitBuffer.java +++ b/src/main/java/org/uic/barcode/asn1/uper/ByteBitBuffer.java @@ -125,6 +125,7 @@ public class ByteBitBuffer implements BitBuffer { public ByteBitBuffer(byte[] backingArray) { this.bytes = backingArray; this.isFinite = true; + this.limit = backingArray.length * 8; } private ByteBitBuffer(int initialCapacity) { diff --git a/src/main/java/org/uic/barcode/package.html b/src/main/java/org/uic/barcode/package.html index 4a6ee0d..075af29 100644 --- a/src/main/java/org/uic/barcode/package.html +++ b/src/main/java/org/uic/barcode/package.html @@ -3,19 +3,21 @@ -

Provides the decoder and encoder for a UIC barcode

- - -

Included features:

-

-

- - +

Provides the decoder and encoder for a UIC barcode

+ + +

+ Included features:
+
+

+

+ + \ No newline at end of file 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); diff --git a/src/main/java/org/uic/barcode/staticFrame/package.html b/src/main/java/org/uic/barcode/staticFrame/package.html index b76540b..5ad7515 100644 --- a/src/main/java/org/uic/barcode/staticFrame/package.html +++ b/src/main/java/org/uic/barcode/staticFrame/package.html @@ -3,19 +3,24 @@ -

static bar code header frame

+

static bar code header frame

-

Provides an implementation of the static bar code header frame specified in UIC IRS 90918-9 including:

- -

-

+

+ Provides an implementation of the static bar code header frame + specified in UIC IRS 90918-9 including:
+
+

+

\ No newline at end of file diff --git a/src/main/java/org/uic/barcode/ticket/api/asn/package.html b/src/main/java/org/uic/barcode/ticket/api/asn/package.html index 307075e..214572b 100644 --- a/src/main/java/org/uic/barcode/ticket/api/asn/package.html +++ b/src/main/java/org/uic/barcode/ticket/api/asn/package.html @@ -1,7 +1,9 @@ -asn - - Provides code generated from the asn.1 specification using the openAsn compiler to implement the asn.1 encoduing and decoding using unaligned PER encoding. +asn + +Provides code generated from the asn.1 specification using + the openAsn compiler to implement the asn.1 encoduing and decoding + using unaligned PER encoding. \ No newline at end of file diff --git a/src/main/java/org/uic/barcode/ticket/api/impl/package.html b/src/main/java/org/uic/barcode/ticket/api/impl/package.html index 587b741..f2ef54e 100644 --- a/src/main/java/org/uic/barcode/ticket/api/impl/package.html +++ b/src/main/java/org/uic/barcode/ticket/api/impl/package.html @@ -1,7 +1,10 @@ -Ticket Data Implementation +Ticket Data Implementation + - Provides a simple implementation of the ticket data interface specified in package spec. + Provides a simple implementation of the ticket data interface specified + in package + spec. \ No newline at end of file diff --git a/src/main/java/org/uic/barcode/ticket/api/spec/package.html b/src/main/java/org/uic/barcode/ticket/api/spec/package.html index 18d8b53..f3961b8 100644 --- a/src/main/java/org/uic/barcode/ticket/api/spec/package.html +++ b/src/main/java/org/uic/barcode/ticket/api/spec/package.html @@ -1,7 +1,13 @@ -UIC ticket interface +UIC ticket interface + - Provides the interface specification of the ticket data. Any ticket data implementation which wants to use the provided encoder / decoder function must implement this interface. A simple implementation is provided in package impl

. + Provides the interface specification of the ticket data. Any ticket + data implementation which wants to use the provided encoder / decoder + function must implement this interface. A simple implementation is + provided in package + impl +

. \ No newline at end of file diff --git a/src/main/java/org/uic/barcode/ticket/api/utils/package.html b/src/main/java/org/uic/barcode/ticket/api/utils/package.html index a926c2d..baf156e 100644 --- a/src/main/java/org/uic/barcode/ticket/api/utils/package.html +++ b/src/main/java/org/uic/barcode/ticket/api/utils/package.html @@ -1,7 +1,8 @@ -Utilities - - Provides utilities for the asn.1 encoding and decoding of ticket data. +Utilities + +Provides utilities for the asn.1 encoding and decoding of + ticket data. \ No newline at end of file diff --git a/src/main/java/org/uic/barcode/ticket/package.html b/src/main/java/org/uic/barcode/ticket/package.html index 993e0c4..6f10316 100644 --- a/src/main/java/org/uic/barcode/ticket/package.html +++ b/src/main/java/org/uic/barcode/ticket/package.html @@ -1,9 +1,13 @@ -UIC ticket data API +UIC ticket data API + - This API provides a specification of ticket data as an interface and an implementation of an encoder/decoder to encode and decode ticket data to an asn.1 PER encoded byte stream according to the UIC specification. -
- Any ticket data implementing the interface defined in package spec can be encoded/decoded. The package impl provides a simple implementation of the ticket. + This API provides a specification of ticket data as an interface and an + implementation of an encoder/decoder to encode and decode ticket data + to an asn.1 PER encoded byte stream according to the UIC specification. +
Any ticket data implementing the interface defined in package + spec can be encoded/decoded. The package + impl provides a simple implementation of the ticket. \ No newline at end of file diff --git a/src/main/java/org/uic/barcode/utils/package.html b/src/main/java/org/uic/barcode/utils/package.html index 45a73a9..73917d7 100644 --- a/src/main/java/org/uic/barcode/utils/package.html +++ b/src/main/java/org/uic/barcode/utils/package.html @@ -3,18 +3,19 @@ -

Implementation of the header frames to bundle all data for a bar code.

+

Implementation of the header frames to bundle all data for a + bar code.

-

+

- Provides a decoding and encoding of the header data frames for:

- -

-

- \ No newline at end of file diff --git a/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestGroup.java b/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestGroup.java new file mode 100644 index 0000000..f6ec58b --- /dev/null +++ b/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestGroup.java @@ -0,0 +1,220 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.logger.LoggerFactory; +import org.uic.barcode.ssbFrame.SsbFrame; +import org.uic.barcode.test.utils.SsbTicketFactory; +import org.uic.barcode.ticket.EncodingFormatException; + +/** + * The Class StaticFrameBarcodeTest. + */ +public class SsbFrameBarcodeTestGroup { + + /** The algorithm OID. */ + public String algorithmOID = Constants.DSA_SHA224; + + public int keySize = 2048; + + /** The key pair. */ + public KeyPair keyPair = null; + + + public SsbFrame ssbFrame = null; + + + /** + * Initialize. + * + * set the signature algorithm + * generate a key pair + * set the test content + * for ticket and layout + */ + @Before public void initialize() { + + LoggerFactory.setActivateConsoleLog(true); + + algorithmOID = Constants.DSA_SHA224; + keySize = 1024; + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = generateDSAKeys(keySize); + } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) { + e.printStackTrace(); + } + + assert(keyPair != null); + + } + + + + + + /** + * Test ssb pass encoding. + */ + @Test public void testSsbEncoding() { + + + Encoder enc = null; + + try { + enc = new Encoder(null, null, Encoder.UIC_BARCODE_TYPE_SSB, 0, 0); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setSsbFrame(SsbTicketFactory.getSsbGroup()); + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + assert(encoded.length == 114); + + } + + /** + * Test ssb barcode decoding. + */ + @Test public void testSsbDecoding() { + + + + + Encoder enc = null; + + try { + enc = new Encoder(null, null, Encoder.UIC_BARCODE_TYPE_SSB, 1, 0); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setSsbFrame( SsbTicketFactory.getSsbGroup()); + + assert(enc != null); + + try { + enc.signLevel1("4711", keyPair.getPrivate(), algorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPair.getPublic(),algorithmOID); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + assert(dec.getSsbFrame() != null); + + assert(dec.getSsbFrame().getHeader() != null); + + SsbFrame ref = SsbTicketFactory.getSsbGroup(); + + assert(dec.getSsbFrame().getHeader().getKeyId() == 1); + assert(dec.getSsbFrame().getHeader().getIssuer() == 4711); + assert(dec.getSsbFrame().getHeader().getTicketType().equals(ref.getHeader().getTicketType())); + assert(dec.getSsbFrame().getHeader().getVersion() == 1); + + assert(dec.getSsbFrame().getGroupData() != null); + + assert(dec.getSsbFrame().getGroupData().getClassCode().equals(ref.getGroupData().getClassCode())); + assert(dec.getSsbFrame().getGroupData().isSpecimen() == ref.getGroupData().isSpecimen()); + assert(dec.getSsbFrame().getGroupData().getFirstDayOfValidity() == ref.getGroupData().getFirstDayOfValidity()); + assert(dec.getSsbFrame().getGroupData().getLastDayOfValidity() == ref.getGroupData().getLastDayOfValidity()); + + + assert(dec.getSsbFrame().getGroupData().getStations() != null); + + SsbTicketFactory.compareStations(dec.getSsbFrame().getGroupData().getStations(), ref.getGroupData().getStations()); + + SsbTicketFactory.compareCommonTicketPart(dec.getSsbFrame().getGroupData(), ref.getGroupData()); + + + assert(dec.getSsbFrame().getGroupData().getFirstDayOfValidity() == ref.getGroupData().getFirstDayOfValidity()); + assert(dec.getSsbFrame().getGroupData().getLastDayOfValidity() == ref.getGroupData().getLastDayOfValidity()); + + assert(dec.getSsbFrame().getGroupData().getInfoCode() == ref.getGroupData().getInfoCode()); + assert(dec.getSsbFrame().getGroupData().getText().equalsIgnoreCase(ref.getGroupData().getText())); + + assert(dec.getSsbFrame().getGroupData().getCounterMarkNumber() == ref.getGroupData().getCounterMarkNumber()); + assert(dec.getSsbFrame().getGroupData().getGroupName().equalsIgnoreCase(ref.getGroupData().getGroupName())); + + } + + /** + * Generate DSA keys. + * + * @return the key pair + * @throws NoSuchAlgorithmException the no such algorithm exception + * @throws NoSuchProviderException the no such provider exception + * @throws InvalidAlgorithmParameterException the invalid algorithm parameter exception + */ + public KeyPair generateDSAKeys(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + KeyPairGenerator g = KeyPairGenerator.getInstance("DSA", "BC"); + g.initialize(keySize, new SecureRandom()); + return g.generateKeyPair(); + } + +} diff --git a/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestNonUic.java b/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestNonUic.java new file mode 100644 index 0000000..2a28d80 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestNonUic.java @@ -0,0 +1,207 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.logger.LoggerFactory; +import org.uic.barcode.ssbFrame.SsbFrame; +import org.uic.barcode.test.utils.SsbTicketFactory; +import org.uic.barcode.ticket.EncodingFormatException; + +/** + * The Class StaticFrameBarcodeTest. + */ +public class SsbFrameBarcodeTestNonUic { + + /** The algorithm OID. */ + public String algorithmOID = Constants.DSA_SHA224; + + public int keySize = 2048; + + /** The key pair. */ + public KeyPair keyPair = null; + + + public SsbFrame ssbFrame = null; + + + /** + * Initialize. + * + * set the signature algorithm + * generate a key pair + * set the test content + * for ticket and layout + */ + @Before public void initialize() { + + LoggerFactory.setActivateConsoleLog(true); + + algorithmOID = Constants.DSA_SHA224; + keySize = 1024; + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = generateDSAKeys(keySize); + } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) { + e.printStackTrace(); + } + + assert(keyPair != null); + + } + + + + + + /** + * Test ssb pass encoding. + */ + @Test public void testSsbEncoding() { + + + Encoder enc = null; + + try { + enc = new Encoder(null, null, Encoder.UIC_BARCODE_TYPE_SSB, 0, 0); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setSsbFrame(SsbTicketFactory.getSsbNonUic()); + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + assert(encoded.length == 114); + + } + + /** + * Test dynamic header barcode decoding. + */ + @Test public void testSsbDecoding() { + + + + + Encoder enc = null; + + try { + enc = new Encoder(null, null, Encoder.UIC_BARCODE_TYPE_SSB, 1, 0); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setSsbFrame( SsbTicketFactory.getSsbNonUic()); + + assert(enc != null); + + try { + enc.signLevel1("4711", keyPair.getPrivate(), algorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPair.getPublic(),algorithmOID); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + assert(dec.getSsbFrame() != null); + + assert(dec.getSsbFrame().getHeader() != null); + + SsbFrame ref = SsbTicketFactory.getSsbNonUic(); + + assert(dec.getSsbFrame().getHeader().getKeyId() == 1); + assert(dec.getSsbFrame().getHeader().getIssuer() == 4711); + assert(dec.getSsbFrame().getHeader().getTicketType().equals(ref.getHeader().getTicketType())); + assert(dec.getSsbFrame().getHeader().getVersion() == 1); + + assert(dec.getSsbFrame().getNonUicData() != null); + + assert(dec.getSsbFrame().getNonUicData().getOpenData() != null); + + + + for (int i = 0; i < ref.getNonUicData().getOpenData().length; i++) { + assert (dec.getSsbFrame().getNonUicData().getOpenData()[i] == (ref.getNonUicData().getOpenData())[i] ); + } + + + } + + /** + * Generate DSA keys. + * + * @return the key pair + * @throws NoSuchAlgorithmException the no such algorithm exception + * @throws NoSuchProviderException the no such provider exception + * @throws InvalidAlgorithmParameterException the invalid algorithm parameter exception + */ + public KeyPair generateDSAKeys(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + KeyPairGenerator g = KeyPairGenerator.getInstance("DSA", "BC"); + g.initialize(keySize, new SecureRandom()); + return g.generateKeyPair(); + } + +} diff --git a/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestNrt.java b/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestNrt.java new file mode 100644 index 0000000..2000ee1 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestNrt.java @@ -0,0 +1,214 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.logger.LoggerFactory; +import org.uic.barcode.ssbFrame.SsbFrame; +import org.uic.barcode.test.utils.SsbTicketFactory; +import org.uic.barcode.ticket.EncodingFormatException; + +/** + * The Class StaticFrameBarcodeTest. + */ +public class SsbFrameBarcodeTestNrt { + + /** The algorithm OID. */ + public String algorithmOID = Constants.DSA_SHA224; + + public int keySize = 1024; + + /** The key pair. */ + public KeyPair keyPair = null; + + + public SsbFrame ssbFrame = null; + + + /** + * Initialize. + * + * set the signature algorithm + * generate a key pair + * set the test content + * for ticket and layout + */ + @Before public void initialize() { + + LoggerFactory.setActivateConsoleLog(true); + + algorithmOID = Constants.DSA_SHA224; + keySize = 1024; + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = generateDSAKeys(keySize); + } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) { + e.printStackTrace(); + } + + assert(keyPair != null); + + } + + + + + + /** + * Test ssb pass encoding. + */ + @Test public void testSsbEncoding() { + + + Encoder enc = null; + + try { + enc = new Encoder(null, null, Encoder.UIC_BARCODE_TYPE_SSB, 0, 0); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setSsbFrame(SsbTicketFactory.getSsbNonReservation()); + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + assert(encoded.length == 114); + + } + + /** + * Test dynamic header barcode decoding. + */ + @Test public void testSsbDecoding() { + + + + + Encoder enc = null; + + try { + enc = new Encoder(null, null, Encoder.UIC_BARCODE_TYPE_SSB, 1, 0); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setSsbFrame( SsbTicketFactory.getSsbNonReservation()); + + assert(enc != null); + + try { + enc.signLevel1("4711", keyPair.getPrivate(), algorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPair.getPublic(),algorithmOID); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + assert(dec.getSsbFrame() != null); + + assert(dec.getSsbFrame().getHeader() != null); + + SsbFrame ref = SsbTicketFactory.getSsbNonReservation(); + + assert(dec.getSsbFrame().getHeader().getKeyId() == 1); + assert(dec.getSsbFrame().getHeader().getIssuer() == 4711); + assert(dec.getSsbFrame().getHeader().getTicketType().equals(ref.getHeader().getTicketType())); + assert(dec.getSsbFrame().getHeader().getVersion() == 1); + + assert(dec.getSsbFrame().getNonReservationData() != null); + + assert(dec.getSsbFrame().getNonReservationData().getClassCode().equals(ref.getNonReservationData().getClassCode())); + assert(dec.getSsbFrame().getNonReservationData().isSpecimen() == ref.getNonReservationData().isSpecimen()); + assert(dec.getSsbFrame().getNonReservationData().getFirstDayOfValidity() == ref.getNonReservationData().getFirstDayOfValidity()); + assert(dec.getSsbFrame().getNonReservationData().getLastDayOfValidity() == ref.getNonReservationData().getLastDayOfValidity()); + + + assert(dec.getSsbFrame().getNonReservationData().getStations() != null); + + SsbTicketFactory.compareStations(dec.getSsbFrame().getNonReservationData().getStations(), ref.getNonReservationData().getStations()); + + SsbTicketFactory.compareCommonTicketPart(dec.getSsbFrame().getNonReservationData(), ref.getNonReservationData()); + + + assert(dec.getSsbFrame().getNonReservationData().getInfoCode() == ref.getNonReservationData().getInfoCode()); + assert(dec.getSsbFrame().getNonReservationData().getText().equalsIgnoreCase(ref.getNonReservationData().getText())); + + } + + /** + * Generate DSA keys. + * + * @return the key pair + * @throws NoSuchAlgorithmException the no such algorithm exception + * @throws NoSuchProviderException the no such provider exception + * @throws InvalidAlgorithmParameterException the invalid algorithm parameter exception + */ + public KeyPair generateDSAKeys(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + KeyPairGenerator g = KeyPairGenerator.getInstance("DSA", "BC"); + g.initialize(keySize, new SecureRandom()); + return g.generateKeyPair(); + } + +} diff --git a/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestPass.java b/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestPass.java new file mode 100644 index 0000000..dd2101a --- /dev/null +++ b/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestPass.java @@ -0,0 +1,215 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.logger.LoggerFactory; +import org.uic.barcode.ssbFrame.SsbFrame; +import org.uic.barcode.test.utils.SsbTicketFactory; +import org.uic.barcode.ticket.EncodingFormatException; + +/** + * The Class StaticFrameBarcodeTest. + */ +public class SsbFrameBarcodeTestPass { + + /** The algorithm OID. */ + public String algorithmOID = Constants.DSA_SHA224; + + public int keySize = 1024; + + /** The key pair. */ + public KeyPair keyPair = null; + + + public SsbFrame ssbFrame = null; + + + /** + * Initialize. + * + * set the signature algorithm + * generate a key pair + * set the test content + * for ticket and layout + */ + @Before public void initialize() { + + LoggerFactory.setActivateConsoleLog(true); + + algorithmOID = Constants.DSA_SHA224; + keySize = 1024; + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = generateDSAKeys(keySize); + } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) { + e.printStackTrace(); + } + + assert(keyPair != null); + + } + + + + + + /** + * Test ssb pass encoding. + */ + @Test public void testSsbPassEncoding() { + + + Encoder enc = null; + + try { + enc = new Encoder(null, null, Encoder.UIC_BARCODE_TYPE_SSB, 0, 0); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setSsbFrame(SsbTicketFactory.getSsbPass()); + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + assert(encoded.length == 114); + + } + + /** + * Test dynamic header barcode decoding. + */ + @Test public void testSsbPassDecoding() { + + + + + Encoder enc = null; + + try { + enc = new Encoder(null, null, Encoder.UIC_BARCODE_TYPE_SSB, 1, 0); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setSsbFrame( SsbTicketFactory.getSsbPass()); + + assert(enc != null); + + try { + enc.signLevel1("4711", keyPair.getPrivate(), algorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPair.getPublic(),algorithmOID); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + assert(dec.getSsbFrame() != null); + + assert(dec.getSsbFrame().getHeader() != null); + + SsbFrame ref = SsbTicketFactory.getSsbPass(); + + assert(dec.getSsbFrame().getHeader().getKeyId() == 1); + assert(dec.getSsbFrame().getHeader().getIssuer() == 4711); + assert(dec.getSsbFrame().getHeader().getTicketType().equals(ref.getHeader().getTicketType())); + assert(dec.getSsbFrame().getHeader().getVersion() == 1); + + assert(dec.getSsbFrame().getPassData() != null); + assert(dec.getSsbFrame().getPassData().getClassCode().equals(ref.getPassData().getClassCode())); + assert(dec.getSsbFrame().getPassData().isSpecimen() == ref.getPassData().isSpecimen()); + assert(dec.getSsbFrame().getPassData().isHasSecondPage() == ref.getPassData().isHasSecondPage()); + assert(dec.getSsbFrame().getPassData().getCountry_1() == ref.getPassData().getCountry_1()); + assert(dec.getSsbFrame().getPassData().getCountry_2() == ref.getPassData().getCountry_2()); + assert(dec.getSsbFrame().getPassData().getCountry_3() == ref.getPassData().getCountry_3()); + assert(dec.getSsbFrame().getPassData().getCountry_4() == ref.getPassData().getCountry_4()); + assert(dec.getSsbFrame().getPassData().getCountry_5() == ref.getPassData().getCountry_5()); + assert(dec.getSsbFrame().getPassData().getDay() == ref.getPassData().getDay()); + assert(dec.getSsbFrame().getPassData().getFirstDayOfValidity() == ref.getPassData().getFirstDayOfValidity()); + assert(dec.getSsbFrame().getPassData().getInfoCode() == ref.getPassData().getInfoCode()); + assert(dec.getSsbFrame().getPassData().getMaximumValidityDuration() == ref.getPassData().getMaximumValidityDuration()); + assert(dec.getSsbFrame().getPassData().getNumberOfAdults() == ref.getPassData().getNumberOfAdults()); + assert(dec.getSsbFrame().getPassData().getNumberOfChildren() == ref.getPassData().getNumberOfChildren()); + assert(dec.getSsbFrame().getPassData().getNumberOfTravels() == ref.getPassData().getNumberOfTravels()); + assert(dec.getSsbFrame().getPassData().getText().equalsIgnoreCase(ref.getPassData().getText())); + assert(dec.getSsbFrame().getPassData().getTicketNumber().equals(ref.getPassData().getTicketNumber())); + assert(dec.getSsbFrame().getPassData().getYear() == ref.getPassData().getYear()); + } + + /** + * Generate DSA keys. + * + * @return the key pair + * @throws NoSuchAlgorithmException the no such algorithm exception + * @throws NoSuchProviderException the no such provider exception + * @throws InvalidAlgorithmParameterException the invalid algorithm parameter exception + */ + public KeyPair generateDSAKeys(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + KeyPairGenerator g = KeyPairGenerator.getInstance("DSA", "BC"); + g.initialize(keySize, new SecureRandom()); + return g.generateKeyPair(); + } + +} diff --git a/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestReservation.java b/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestReservation.java new file mode 100644 index 0000000..5f9e11e --- /dev/null +++ b/src/test/java/org/uic/barcode/test/SsbFrameBarcodeTestReservation.java @@ -0,0 +1,222 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.logger.LoggerFactory; +import org.uic.barcode.ssbFrame.SsbFrame; +import org.uic.barcode.test.utils.SsbTicketFactory; +import org.uic.barcode.ticket.EncodingFormatException; + +/** + * The Class StaticFrameBarcodeTest. + */ +public class SsbFrameBarcodeTestReservation { + + /** The algorithm OID. */ + public String algorithmOID = Constants.DSA_SHA224; + + public int keySize = 1024; + + /** The key pair. */ + public KeyPair keyPair = null; + + + public SsbFrame ssbFrame = null; + + + /** + * Initialize. + * + * set the signature algorithm + * generate a key pair + * set the test content + * for ticket and layout + */ + @Before public void initialize() { + + LoggerFactory.setActivateConsoleLog(true); + + algorithmOID = Constants.DSA_SHA224; + keySize = 1024; + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = generateDSAKeys(keySize); + } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) { + e.printStackTrace(); + } + + assert(keyPair != null); + + } + + + + + + /** + * Test ssb pass encoding. + */ + @Test public void testSsbEncoding() { + + + Encoder enc = null; + + try { + enc = new Encoder(null, null, Encoder.UIC_BARCODE_TYPE_SSB, 0, 0); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setSsbFrame(SsbTicketFactory.getSsbReservation()); + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + assert(encoded.length == 114); + + } + + /** + * Test dynamic header barcode decoding. + */ + @Test public void testSsbDecoding() { + + + + + Encoder enc = null; + + try { + enc = new Encoder(null, null, Encoder.UIC_BARCODE_TYPE_SSB, 1, 0); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setSsbFrame( SsbTicketFactory.getSsbReservation()); + + assert(enc != null); + + try { + enc.signLevel1("4711", keyPair.getPrivate(), algorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPair.getPublic(),algorithmOID); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + assert(dec.getSsbFrame() != null); + + assert(dec.getSsbFrame().getHeader() != null); + + SsbFrame ref = SsbTicketFactory.getSsbReservation(); + + assert(dec.getSsbFrame().getHeader().getKeyId() == 1); + assert(dec.getSsbFrame().getHeader().getIssuer() == 4711); + assert(dec.getSsbFrame().getHeader().getTicketType().equals(ref.getHeader().getTicketType())); + assert(dec.getSsbFrame().getHeader().getVersion() == 1); + + assert(dec.getSsbFrame().getReservationData() != null); + + assert(dec.getSsbFrame().getReservationData().getClassCode().equals(ref.getReservationData().getClassCode())); + assert(dec.getSsbFrame().getReservationData().isSpecimen() == ref.getReservationData().isSpecimen()); + assert(dec.getSsbFrame().getReservationData().getCoach() == ref.getReservationData().getCoach()); + assert(dec.getSsbFrame().getReservationData().getDepartureDate() == ref.getReservationData().getDepartureDate()); + assert(dec.getSsbFrame().getReservationData().getDepartureTime() == ref.getReservationData().getDepartureTime()); + + + assert(dec.getSsbFrame().getReservationData().getStations() != null); + + SsbTicketFactory.compareStations(dec.getSsbFrame().getReservationData().getStations(), ref.getReservationData().getStations()); + + SsbTicketFactory.compareCommonTicketPart(dec.getSsbFrame().getReservationData(), ref.getReservationData()); + + + assert(dec.getSsbFrame().getReservationData().getPlace().equals(ref.getReservationData().getPlace())); + assert(dec.getSsbFrame().getReservationData().getTicketSubType() == ref.getReservationData().getTicketSubType()); + assert(dec.getSsbFrame().getReservationData().getTrain().equals(ref.getReservationData().getTrain())); + assert(dec.getSsbFrame().getReservationData().isOverbooking() == ref.getReservationData().isOverbooking()); + + assert(dec.getSsbFrame().getReservationData().getInfoCode() == ref.getReservationData().getInfoCode()); + assert(dec.getSsbFrame().getReservationData().getText().equalsIgnoreCase(ref.getReservationData().getText())); + + + + } + + /** + * Generate DSA keys. + * + * @return the key pair + * @throws NoSuchAlgorithmException the no such algorithm exception + * @throws NoSuchProviderException the no such provider exception + * @throws InvalidAlgorithmParameterException the invalid algorithm parameter exception + */ + public KeyPair generateDSAKeys(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + KeyPairGenerator g = KeyPairGenerator.getInstance("DSA", "BC"); + g.initialize(keySize, new SecureRandom()); + return g.generateKeyPair(); + } + +} diff --git a/src/test/java/org/uic/barcode/test/utils/SsbTicketFactory.java b/src/test/java/org/uic/barcode/test/utils/SsbTicketFactory.java new file mode 100644 index 0000000..539fced --- /dev/null +++ b/src/test/java/org/uic/barcode/test/utils/SsbTicketFactory.java @@ -0,0 +1,193 @@ +package org.uic.barcode.test.utils; + +import org.uic.barcode.ssbFrame.SsbClass; +import org.uic.barcode.ssbFrame.SsbCommonTicketPart; +import org.uic.barcode.ssbFrame.SsbFrame; +import org.uic.barcode.ssbFrame.SsbGroup; +import org.uic.barcode.ssbFrame.SsbHeader; +import org.uic.barcode.ssbFrame.SsbNonReservation; +import org.uic.barcode.ssbFrame.SsbNonUic; +import org.uic.barcode.ssbFrame.SsbPass; +import org.uic.barcode.ssbFrame.SsbReservation; +import org.uic.barcode.ssbFrame.SsbStationCodeTable; +import org.uic.barcode.ssbFrame.SsbStations; +import org.uic.barcode.ssbFrame.SsbTicketType; + +public class SsbTicketFactory { + + public static SsbFrame getSsbPass() { + + SsbFrame ssbPass = new SsbFrame(); + + ssbPass.setHeader(new SsbHeader()); + ssbPass.getHeader().setIssuer(4711); + ssbPass.getHeader().setTicketType(SsbTicketType.UIC_4_RPT); + ssbPass.getHeader().setVersion(1); + + + ssbPass.setPassData(new SsbPass()); + ssbPass.getPassData().setClassCode(SsbClass.FIRST); + ssbPass.getPassData().setCountry_1(10); + ssbPass.getPassData().setCountry_2(12); + ssbPass.getPassData().setDay(1); + ssbPass.getPassData().setFirstDayOfValidity(120); + ssbPass.getPassData().setHasSecondPage(false); + ssbPass.getPassData().setInfoCode(12); + ssbPass.getPassData().setMaximumValidityDuration(2); + ssbPass.getPassData().setNumberOfAdults(2); + ssbPass.getPassData().setNumberOfChildren(3); + ssbPass.getPassData().setNumberOfTravels(3); + ssbPass.getPassData().setPassSubType(1); + ssbPass.getPassData().setSpecimen(true); + ssbPass.getPassData().setText("Test"); + ssbPass.getPassData().setTicketNumber("SKCTS86"); + ssbPass.getPassData().setYear(3); + + return ssbPass; + } + + public static SsbFrame getSsbGroup() { + + SsbFrame ssb = new SsbFrame(); + + ssb.setHeader(new SsbHeader()); + ssb.getHeader().setIssuer(4711); + ssb.getHeader().setTicketType(SsbTicketType.UIC_3_GRP); + ssb.getHeader().setVersion(1); + + + ssb.setGroupData(new SsbGroup()); + ssb.getGroupData().setClassCode(SsbClass.FIRST); + ssb.getGroupData().setCounterMarkNumber(1); + ssb.getGroupData().setDay(1); + ssb.getGroupData().setFirstDayOfValidity(10); + ssb.getGroupData().setFirstDayOfValidity(120); + ssb.getGroupData().setGroupName("GroupName"); + ssb.getGroupData().setInfoCode(12); + ssb.getGroupData().setLastDayOfValidity(3); + ssb.getGroupData().setNumberOfAdults(2); + ssb.getGroupData().setNumberOfChildren(3); + ssb.getGroupData().setReturnJourney(false); + ssb.getGroupData().setSpecimen(true); + ssb.getGroupData().setText("Test"); + ssb.getGroupData().setTicketNumber("SKCTS86"); + ssb.getGroupData().setYear(3); + + ssb.getGroupData().getStations().setArrivalStationCode("8012345"); + ssb.getGroupData().getStations().setDepartureStationCode("8054321"); + ssb.getGroupData().getStations().setCodeTable(SsbStationCodeTable.NRT); + + return ssb; + } + + public static SsbFrame getSsbNonReservation() { + + SsbFrame ssb = new SsbFrame(); + + ssb.setHeader(new SsbHeader()); + ssb.getHeader().setIssuer(4711); + ssb.getHeader().setTicketType(SsbTicketType.UIC_2_NRT); + ssb.getHeader().setVersion(1); + + + ssb.setNonReservationData(new SsbNonReservation()); + ssb.getNonReservationData().setClassCode(SsbClass.FIRST); + ssb.getNonReservationData().setDay(1); + ssb.getNonReservationData().setFirstDayOfValidity(10); + ssb.getNonReservationData().setFirstDayOfValidity(120); + ssb.getNonReservationData().setInfoCode(12); + ssb.getNonReservationData().setLastDayOfValidity(3); + ssb.getNonReservationData().setNumberOfAdults(2); + ssb.getNonReservationData().setNumberOfChildren(3); + ssb.getNonReservationData().setReturnJourney(false); + ssb.getNonReservationData().setSpecimen(true); + ssb.getNonReservationData().setText("Test"); + ssb.getNonReservationData().setTicketNumber("SKCTS86"); + ssb.getNonReservationData().setYear(3); + + + ssb.getNonReservationData().getStations().setArrivalStationCode("8012345"); + ssb.getNonReservationData().getStations().setDepartureStationCode("8054321"); + ssb.getNonReservationData().getStations().setCodeTable(SsbStationCodeTable.NRT); + + return ssb; + } + + public static SsbFrame getSsbReservation() { + + SsbFrame ssb = new SsbFrame(); + + ssb.setHeader(new SsbHeader()); + ssb.getHeader().setIssuer(4711); + ssb.getHeader().setTicketType(SsbTicketType.UIC_1_IRT_RES_BOA); + ssb.getHeader().setVersion(1); + + + ssb.setReservationData(new SsbReservation()); + ssb.getReservationData().setClassCode(SsbClass.FIRST); + ssb.getReservationData().setDay(1); + ssb.getReservationData().setCoach(123); + ssb.getReservationData().setDepartureDate(120); + ssb.getReservationData().setDepartureTime(500); + ssb.getReservationData().setOverbooking(false); + ssb.getReservationData().setNumberOfAdults(2); + ssb.getReservationData().setNumberOfChildren(3); + ssb.getReservationData().setPlace("05B"); + ssb.getReservationData().setTicketSubType(2); + ssb.getReservationData().setTrain("1234B"); + + ssb.getReservationData().setSpecimen(true); + ssb.getReservationData().setText("Test"); + ssb.getReservationData().setTicketNumber("SKCTS86"); + ssb.getReservationData().setYear(3); + + + ssb.getReservationData().getStations().setArrivalStationCode("8012345"); + ssb.getReservationData().getStations().setDepartureStationCode("8054321"); + ssb.getReservationData().getStations().setCodeTable(SsbStationCodeTable.NRT); + + return ssb; + } + + public static SsbFrame getSsbNonUic() { + + SsbFrame ssb = new SsbFrame(); + + ssb.setHeader(new SsbHeader()); + ssb.getHeader().setIssuer(4711); + ssb.getHeader().setTicketType(SsbTicketType.NONUIC_23_BILATERAL); + ssb.getHeader().setVersion(1); + + + ssb.setNonUicData(new SsbNonUic()); + ssb.getNonUicData().setOpenData("TestData".getBytes()); + + + return ssb; + } + + public static void compareStations(SsbStations stations, SsbStations stations2) { + + assert (stations.getCodeTable().equals(stations2.getCodeTable())); + + assert (stations.getArrivalStationCode().equals(stations2.getArrivalStationCode())); + + assert (stations.getDepartureStationCode().equals(stations2.getDepartureStationCode())); + + } + + public static void compareCommonTicketPart(SsbCommonTicketPart part, SsbCommonTicketPart part2) { + + assert(part.isSpecimen() == part2.isSpecimen()); + assert(part.getDay() == part2.getDay()); + assert(part.getNumberOfAdults() == part2.getNumberOfAdults()); + assert(part.getNumberOfChildren() == part2.getNumberOfChildren()); + assert(part.getTicketNumber().equals(part2.getTicketNumber())); + assert(part.getYear() == part2.getYear()); + + } + + + + +} -- cgit v1.2.3