From 2949fbd789fc3c35d340d17cb96e26a7a2ef7d02 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Wed, 24 Nov 2021 11:54:10 +0100 Subject: extended tests --- .../test/DynamicFrameDynamicContentTest.java | 16 ++ .../barcode/test/StaticFrameBarcodeTestFCB3.java | 193 +++++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB3.java (limited to 'src/test/java/org') diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java index 53b6af8..f3e4f54 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java @@ -24,6 +24,8 @@ import org.junit.Before; import org.junit.Test; import org.uic.barcode.Decoder; import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicContent.fdc1.GeoCoordinateSystemType; +import org.uic.barcode.dynamicContent.fdc1.GeoCoordinateType; import org.uic.barcode.dynamicContent.fdc1.TimeStamp; import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1; import org.uic.barcode.dynamicFrame.Constants; @@ -104,6 +106,11 @@ public class DynamicFrameDynamicContentTest { ts.setDateTime(Date.from(originalTimeStamp.toInstant())); dcd.setTimeStamp(ts); + GeoCoordinateType geo = new GeoCoordinateType(); + geo.setLatitude(123456L); + geo.setLongitude(823456L); + dcd.setGeoCoordinate(geo); + enc.setDynamicContentDataUIC1(dcd); enc.signLevel2(keyPairLevel2.getPrivate()); } catch (Exception e) { @@ -152,6 +159,12 @@ public class DynamicFrameDynamicContentTest { dcd.setAppId("MyApp"); dcd.setPhoneIdHash(phoneIdHash); dcd.setPassIdHash(passIdHash); + + GeoCoordinateType geo = new GeoCoordinateType(); + geo.setLatitude(123456L); + geo.setLongitude(823456L); + dcd.setGeoCoordinate(geo); + TimeStamp ts = new TimeStamp(); ts.setDateTime(Date.from(originalTimeStamp.toInstant())); dcd.setTimeStamp(ts); @@ -215,6 +228,9 @@ public class DynamicFrameDynamicContentTest { assert(Arrays.equals(dynamicData.getPhoneIdHash(),phoneIdHash)); + assert(dynamicData.getGeoCoordinate().getLatitude() == 123456L); + assert(dynamicData.getGeoCoordinate().getLongitude() == 823456L); + Date timeStamp = dynamicData.getTimeStamp().getTimeAsDate(); ZonedDateTime retrievedTimeStamp = timeStamp.toInstant().atZone(ZoneId.of("UTC")); long diff = ChronoUnit.SECONDS.between(originalTimeStamp, retrievedTimeStamp); diff --git a/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB3.java b/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB3.java new file mode 100644 index 0000000..41ce365 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB3.java @@ -0,0 +1,193 @@ +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.staticFrame.ticketLayoutBarcode.TicketLayout; +import org.uic.barcode.test.utils.SimpleTestTicketLayout; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +/** + * The Class StaticFrameBarcodeTest. + */ +public class StaticFrameBarcodeTestFCB3 { + + /** The algorithm OID. */ + public String algorithmOID = Constants.DSA_SHA224; + + public int keySize = 2048; + + /** The key pair. */ + public KeyPair keyPair = null; + + + public IUicRailTicket testFCBticket = null; + + public TicketLayout testLayout = null; + + + /** + * Initialize. + * + * set the signature algorithm + * generate a key pair + * set the test content + * for ticket and layout + */ + @Before public void initialize() { + + algorithmOID = Constants.DSA_SHA224; + keySize = 2048; + testFCBticket = SimpleUICTestTicket.getUicTestTicket(); + testLayout = SimpleTestTicketLayout.getSimpleTestTicketLayout(); + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = generateDSAKeys(keySize); + } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) { + e.printStackTrace(); + } + + assert(keyPair != null); + + } + + + /** + * Test dynamic header barcode encoding. + */ + @Test public void testStaticHeaderBarcodeEncoding() { + + IUicRailTicket ticket = testFCBticket; + + TicketLayout layout = testLayout; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, layout, Encoder.UIC_BARCODE_TYPE_CLASSIC, 2, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setStaticHeaderParams("123456789012", "de"); + + 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); + + } + + /** + * Test dynamic header barcode decoding. + */ + @Test public void testStaticHeaderBarcodeDecoding() { + + + IUicRailTicket ticket = testFCBticket; + + TicketLayout layout = testLayout; + + + Encoder enc = null; + + try { + enc = new Encoder(ticket, layout, Encoder.UIC_BARCODE_TYPE_CLASSIC, 2, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setStaticHeaderParams("123456789012", "de"); + + 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); + + 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); + + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); + + SimpleTestTicketLayout.compare(layout, dec.getLayout()); + + } + + /** + * 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(); + } + +} -- cgit v1.2.3