diff options
author | CGantert345 <57003061+CGantert345@users.noreply.github.com> | 2020-07-28 17:57:25 +0200 |
---|---|---|
committer | CGantert345 <57003061+CGantert345@users.noreply.github.com> | 2020-07-28 17:57:25 +0200 |
commit | b17ce13cc31abd088088bf8ab26cc924a6a36585 (patch) | |
tree | 5588e1ccc212213097500f4cb43fa0e3e68e323b /src/org/uic/barcode/test | |
parent | Draft of the new DOSIPAS included (diff) | |
download | UIC-barcode-b17ce13cc31abd088088bf8ab26cc924a6a36585.tar UIC-barcode-b17ce13cc31abd088088bf8ab26cc924a6a36585.tar.gz UIC-barcode-b17ce13cc31abd088088bf8ab26cc924a6a36585.tar.bz2 UIC-barcode-b17ce13cc31abd088088bf8ab26cc924a6a36585.tar.lz UIC-barcode-b17ce13cc31abd088088bf8ab26cc924a6a36585.tar.xz UIC-barcode-b17ce13cc31abd088088bf8ab26cc924a6a36585.tar.zst UIC-barcode-b17ce13cc31abd088088bf8ab26cc924a6a36585.zip |
Diffstat (limited to 'src/org/uic/barcode/test')
11 files changed, 1238 insertions, 0 deletions
diff --git a/src/org/uic/barcode/test/DynamicFrameDoubleSignatureTest.java b/src/org/uic/barcode/test/DynamicFrameDoubleSignatureTest.java new file mode 100644 index 0000000..f74c40f --- /dev/null +++ b/src/org/uic/barcode/test/DynamicFrameDoubleSignatureTest.java @@ -0,0 +1,212 @@ +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.Arrays;
+import java.util.zip.DataFormatException;
+
+import org.bouncycastle.jce.ECNamedCurveTable;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.jce.spec.ECParameterSpec;
+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.dynamicFrame.DataType;
+import org.uic.barcode.test.utils.Level2TestDataFactory;
+import org.uic.barcode.test.utils.SimpleUICTestTicket;
+import org.uic.barcode.utils.AlgorithmNameResolver;
+import org.uic.ticket.EncodingFormatException;
+import org.uic.ticket.api.spec.IUicRailTicket;
+
+public class DynamicFrameDoubleSignatureTest {
+
+ public String signatureAlgorithmOID = null;
+ public String elipticCurve = null;
+ public String keyPairAlgorithmOID = null;
+
+ public KeyPair keyPairLevel1 = null;
+ public KeyPair keyPairLevel2 = null;
+
+ public IUicRailTicket testFCBticket = null;
+
+
+ @Before public void initialize() {
+
+ signatureAlgorithmOID = Constants.ECDSA_SHA256;
+ keyPairAlgorithmOID = Constants.KG_EC_256;
+ elipticCurve = "secp256k1";
+
+ testFCBticket = SimpleUICTestTicket.getUicTestTicket();
+
+ Security.addProvider(new BouncyCastleProvider());
+
+ try {
+ keyPairLevel1 = generateECKeys(keyPairAlgorithmOID, elipticCurve);
+ keyPairLevel2 = generateECKeys(keyPairAlgorithmOID, elipticCurve);
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(keyPairLevel1 != null);
+
+ }
+
+
+ @Test public void testDynamicHeaderBarcodeEncoding() {
+
+ IUicRailTicket ticket = testFCBticket;
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 1);
+ } catch (IOException | EncodingFormatException e1) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+ try {
+ enc.setLevel1Algs(signatureAlgorithmOID, keyPairAlgorithmOID);
+ enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID,keyPairLevel2.getPublic());
+ enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+ try {
+ enc.setLevel2Data(Level2TestDataFactory.getLevel2SimpleTestData());
+ enc.signLevel2(keyPairLevel2.getPrivate());
+ } catch (Exception e) {
+ assert(false);
+ }
+
+
+ byte[] encoded = null;
+ try {
+ encoded = enc.encode();
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(encoded != null);
+
+
+
+ }
+
+ @Test public void testDynamicHeaderBarcodeDecoding() {
+
+ IUicRailTicket ticket = testFCBticket;
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 1);
+ } catch (IOException | EncodingFormatException e1) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+ try {
+ enc.setLevel1Algs(signatureAlgorithmOID, keyPairAlgorithmOID);
+ enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID,keyPairLevel2.getPublic());
+ enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+
+ DataType level2Data = Level2TestDataFactory.getLevel2SimpleTestData();
+ try {
+ enc.setLevel2Data(level2Data);
+ enc.signLevel2(keyPairLevel2.getPrivate());
+ } 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(keyPairLevel1.getPublic(),null);
+ } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException
+ | UnsupportedOperationException | IOException | EncodingFormatException e) {
+ assert(false);
+ }
+ assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK);
+
+ signatureCheck = 0;
+ try {
+ signatureCheck = dec.validateLevel2();
+ } catch (IllegalArgumentException | UnsupportedOperationException e) {
+ assert(false);
+ }
+ assert(signatureCheck == Constants.LEVEL2_VALIDATION_OK);
+
+
+ DataType level2DataDec = dec.getLevel2Data();
+
+ assert(level2Data.getFormat().equals(level2DataDec.getFormat()));
+ assert(Arrays.equals(level2Data.getData().toByteArray(),level2DataDec.getData().toByteArray()));
+
+ SimpleUICTestTicket.compare(ticket, dec.getUicTicket());
+
+
+ }
+
+ public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{
+ ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName);
+ KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC");
+ g.initialize(ecSpec, new SecureRandom());
+ return g.generateKeyPair();
+ }
+
+ public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{
+
+ String keyAlgorithmName = AlgorithmNameResolver.getName(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, keyAlgorithmOid, "BC");
+
+ keyAlgorithmName = "ECDSA";
+ ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve);
+ KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC");
+ g.initialize(ecSpec, new SecureRandom());
+ return g.generateKeyPair();
+ }
+
+
+}
diff --git a/src/org/uic/barcode/test/DynamicFrameLOwLevelTest.java b/src/org/uic/barcode/test/DynamicFrameLOwLevelTest.java new file mode 100644 index 0000000..d3148b2 --- /dev/null +++ b/src/org/uic/barcode/test/DynamicFrameLOwLevelTest.java @@ -0,0 +1,116 @@ +package org.uic.barcode.test;
+
+import java.security.InvalidAlgorithmParameterException;
+import java.security.KeyFactory;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.PrivateKey;
+import java.security.PublicKey;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.X509EncodedKeySpec;
+
+import org.bouncycastle.jce.ECNamedCurveTable;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.jce.spec.ECParameterSpec;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.dynamicFrame.Constants;
+import org.uic.barcode.dynamicFrame.DynamicFrame;
+import org.uic.barcode.test.utils.SimpleDynamicFrameTestBarcode;
+
+import net.gcdc.asn1.uper.UperEncoder;
+
+public class DynamicFrameLOwLevelTest {
+
+ public String algorithmOID = Constants.ECDSA_SHA256;
+ public KeyPair keyPair = null;
+ public String publicKeyHex = "3081A7301006072A8648CE3D020106052B81040027038192000405B2797BB27F96EC3769B81E563EEB97A4CE3B5BB4EE19BC90A3F496B805644AA042736E5FA9B3A5FBEA5B01CD1D9EC13C009F9655B31FFF9AA52AC6D90B5D6220B58853A1D18BF20779BE5C52356AE70B19242065F82B76961E2A079F42CA9A41A1AB4D5518446AC3721953AE6323C60E15389498DE5F592A24DDDA45F736D93695C797C0F28A712EC25B9CD8078457";
+ public String privateKeyHex = "30820109020100301006072A8648CE3D020106052B810400270481F13081EE020101044801EF44914319A5DD528C06419D7B0FD0CDD7F62A231BEB197E45A0074C02E11FE82ABAD916BE94FD8256260AA9191F19241CFC7E372B3A4E0ADA06CCA51678C54198667DFC9B0DA8A00706052B81040027A18195038192000405B2797BB27F96EC3769B81E563EEB97A4CE3B5BB4EE19BC90A3F496B805644AA042736E5FA9B3A5FBEA5B01CD1D9EC13C009F9655B31FFF9AA52AC6D90B5D6220B58853A1D18BF20779BE5C52356AE70B19242065F82B76961E2A079F42CA9A41A1AB4D5518446AC3721953AE6323C60E15389498DE5F592A24DDDA45F736D93695C797C0F28A712EC25B9CD8078457";
+
+ public String hexReferenceContent = "400EAC986010DF80A021DE008808014374F3E7D72F2A9979F4A13A90086280B40020894DED0DC0688DEEE0AC593368D60220DCF2EED3BF903B6BCA3B937BAB801280EB44AC0505B4200000000100E6F70656E5469636B6574496E666F120220103B830B9B9B0B3B28084A0B6B9BA32B93230B680202F698040100B20004C6C8020404E9979F40201620505B402C80A0F68020AA192338F4100C08008097308194024800DA0C61105BAD7E13ADF9D5A00CBC47732865EA67E8371A5FBE38B4FABBBABD37459D12048DA6664700E787C32962A607A784FD2FC669A9A8EC9F91CD53AF2B922EFECE24FF3D68024800A1F7CF1C0625FB19CF089E74D668F5E8C15179BEF7BA79D9D169A12FA47F6340ED50BADB57CD83110060FEC08B1EF978C6DB08A172B0DE20C442D4507442623A74A624457590040";
+
+
+ @Before public void initialize() {
+
+ Security.addProvider(new BouncyCastleProvider());
+
+ try {
+ keyPair = generateECDSAKeys();
+ } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
+ e.printStackTrace();
+ }
+ privateKeyHex = UperEncoder.hexStringFromBytes(keyPair.getPrivate().getEncoded());
+ publicKeyHex = UperEncoder.hexStringFromBytes(keyPair.getPublic().getEncoded());
+
+ assert(keyPair != null);
+
+ try {
+ PublicKey publicKey = KeyFactory.getInstance("ECDSA").generatePublic(new X509EncodedKeySpec(UperEncoder.bytesFromHexString(publicKeyHex)));
+ PrivateKey privateKey = KeyFactory.getInstance("ECDSA").generatePrivate(new PKCS8EncodedKeySpec(UperEncoder.bytesFromHexString(privateKeyHex)));
+ keyPair = new KeyPair(publicKey,privateKey);
+ } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ }
+ assert(keyPair != null);
+
+ }
+
+
+ @Test public void testDynamicHeaderBarcodeEncoding() {
+
+ DynamicFrame barcode1 = SimpleDynamicFrameTestBarcode.getSimpleDynamicHeaderBarcode(algorithmOID, keyPair);
+
+ byte[] encoded = barcode1.encode();
+
+ //String hex = UperEncoder.hexStringFromBytes(encoded);
+
+ /*
+ * check the available implementations
+ String s = null;
+ try {
+ s = AlgorithmNameResolver.getSecurityNames();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ */
+
+
+ //note: hex is different each time due to the random seed in the signature
+ assert(encoded != null);
+
+ }
+
+ @Test public void testDynamicHeaderBarcodeDecoding() {
+
+ DynamicFrame barcode1 = SimpleDynamicFrameTestBarcode.getSimpleDynamicHeaderBarcode(algorithmOID, keyPair);
+
+ byte[] encoded = barcode1.encode();
+
+ DynamicFrame barcode = DynamicFrame.decode(encoded);
+
+ int signatureCheck = barcode.validateLevel1(keyPair.getPublic());
+
+ assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK);
+
+ SimpleDynamicFrameTestBarcode.compareFrame(barcode1, barcode);
+
+
+
+
+ }
+
+ public KeyPair generateECDSAKeys() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{
+ ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("B-571");
+ KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
+ g.initialize(ecSpec, new SecureRandom());
+ return g.generateKeyPair();
+ }
+
+
+
+}
diff --git a/src/org/uic/barcode/test/DynamicFrameSimpleTest.java b/src/org/uic/barcode/test/DynamicFrameSimpleTest.java new file mode 100644 index 0000000..72b137e --- /dev/null +++ b/src/org/uic/barcode/test/DynamicFrameSimpleTest.java @@ -0,0 +1,171 @@ +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.ECNamedCurveTable;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.jce.spec.ECParameterSpec;
+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.test.utils.SimpleUICTestTicket;
+import org.uic.barcode.utils.AlgorithmNameResolver;
+import org.uic.ticket.EncodingFormatException;
+import org.uic.ticket.api.spec.IUicRailTicket;
+
+public class DynamicFrameSimpleTest {
+
+ public String signatureAlgorithmOID = null;
+ public String elipticCurve = null;
+ public String keyPairAlgorithmOID = null;
+
+ public KeyPair keyPair = null;
+
+ public IUicRailTicket testFCBticket = null;
+
+
+ @Before public void initialize() {
+
+ signatureAlgorithmOID = Constants.ECDSA_SHA256;
+ keyPairAlgorithmOID = Constants.KG_EC_256;
+ elipticCurve = "secp256k1";
+
+ testFCBticket = SimpleUICTestTicket.getUicTestTicket();
+
+ Security.addProvider(new BouncyCastleProvider());
+
+ try {
+ keyPair = generateECKeys(Constants.KG_EC, elipticCurve);
+ //keyPair = generateECDSAKeys("ECDSA", "B-571");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(keyPair != null);
+
+ }
+
+
+ @Test public void testDynamicHeaderBarcodeEncoding() {
+
+ IUicRailTicket ticket = testFCBticket;
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 1);
+ } catch (IOException | EncodingFormatException e1) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+ try {
+ enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+
+ byte[] encoded = null;
+ try {
+ encoded = enc.encode();
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(encoded != null);
+
+
+
+ }
+
+ @Test public void testDynamicHeaderBarcodeDecoding() {
+
+ IUicRailTicket ticket = testFCBticket;
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 1);
+ } catch (IOException | EncodingFormatException e1) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+ try {
+ enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "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(),null);
+ } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException
+ | UnsupportedOperationException | IOException | EncodingFormatException e) {
+ assert(false);
+ }
+
+ assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK);
+
+ SimpleUICTestTicket.compare(ticket, dec.getUicTicket());
+
+
+ }
+
+ public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{
+ ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName);
+ KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC");
+ g.initialize(ecSpec, new SecureRandom());
+ return g.generateKeyPair();
+ }
+
+ public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{
+
+ String keyAlgorithmName = AlgorithmNameResolver.getName(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, keyAlgorithmOid, "BC");
+
+ keyAlgorithmName = "ECDSA";
+ ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve);
+ KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC");
+ g.initialize(ecSpec, new SecureRandom());
+ return g.generateKeyPair();
+ }
+
+
+}
diff --git a/src/org/uic/barcode/test/SignatureSplitTest.java b/src/org/uic/barcode/test/SignatureSplitTest.java new file mode 100644 index 0000000..2e51525 --- /dev/null +++ b/src/org/uic/barcode/test/SignatureSplitTest.java @@ -0,0 +1,76 @@ +package org.uic.barcode.test;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.math.BigInteger;
+
+import org.junit.Test;
+import org.uic.barcode.staticFrame.StaticFrame;
+import org.uic.barcode.test.utils.TestUtils;
+import org.uic.ticket.EncodingFormatException;
+
+public class SignatureSplitTest {
+
+
+ /*
+ *
+ *
+ * World-Schema DEFINITIONS AUTOMATIC TAGS ::=
+ BEGIN
+ Signature ::= SEQUENCE OF INTEGER
+ END
+
+
+ value Signature ::= {
+ 340282366920938463,
+ 134515671861986
+ }
+
+
+ Encoding using DER encoding rule
+ Signature SEQUENCE OF: tag = [UNIVERSAL 16] constructed; length = 18
+ INTEGER: tag = [UNIVERSAL 2] primitive; length = 8
+ 340282366920938463
+ INTEGER: tag = [UNIVERSAL 2] primitive; length = 6
+ 134515671861986
+ Encoded successfully in 20 bytes: 30120208 04B8ED02 83A6D3DF 02067A57 5ED68AE2
+
+
+ *
+ *
+ */
+
+
+ @Test public void testSplitSignature() throws IOException, EncodingFormatException{
+
+ BigInteger i1 = BigInteger.valueOf(340282366920938463L);
+ BigInteger i2 = BigInteger.valueOf(134515671861986L);
+
+ byte[] encoded = StaticFrame.encodeSignatureIntegerSequence(i1,i2);
+
+
+ String hex = TestUtils.hexStringFromBytes(encoded);
+
+ assertEquals(hex,"3012020804B8ED0283A6D3DF02067A575ED68AE2");
+
+ BigInteger[] ints = null;
+ try {
+ ints = StaticFrame.decodeSignatureIntegerSequence(encoded);
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ assert(ints != null);
+
+ assert(ints.length == 2);
+
+ assert(i1.equals(ints[0]));
+
+ assert(i2.equals(ints[1]));
+
+ }
+
+
+}
\ No newline at end of file diff --git a/src/org/uic/barcode/test/StaticFrameBarcodeTest.java b/src/org/uic/barcode/test/StaticFrameBarcodeTest.java new file mode 100644 index 0000000..a0855de --- /dev/null +++ b/src/org/uic/barcode/test/StaticFrameBarcodeTest.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.staticHeader.ticketLayoutBarcode.TicketLayout;
+import org.uic.barcode.test.utils.SimpleTestTicketLayout;
+import org.uic.barcode.test.utils.SimpleUICTestTicket;
+import org.uic.ticket.EncodingFormatException;
+import org.uic.ticket.api.spec.IUicRailTicket;
+
+/**
+ * The Class StaticFrameBarcodeTest.
+ */
+public class StaticFrameBarcodeTest {
+
+ /** 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 testDynamicHeaderBarcodeEncoding() {
+
+ IUicRailTicket ticket = testFCBticket;
+
+ TicketLayout layout = testLayout;
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, layout, Encoder.UIC_BARCODE_TYPE_CLASSIC, 2, 1);
+ } 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 testDynamicHeaderBarcodeDecoding() {
+
+
+ IUicRailTicket ticket = testFCBticket;
+
+ TicketLayout layout = testLayout;
+
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, layout, Encoder.UIC_BARCODE_TYPE_CLASSIC, 2, 1);
+ } 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();
+ }
+
+}
diff --git a/src/org/uic/barcode/test/TicketLayoutTest.java b/src/org/uic/barcode/test/TicketLayoutTest.java new file mode 100644 index 0000000..0210896 --- /dev/null +++ b/src/org/uic/barcode/test/TicketLayoutTest.java @@ -0,0 +1,45 @@ +package org.uic.barcode.test;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.junit.Test;
+import org.uic.barcode.staticFrame.UTLAYDataRecord;
+import org.uic.barcode.staticHeader.ticketLayoutBarcode.TicketLayout;
+import org.uic.barcode.test.utils.SimpleTestTicketLayout;
+import org.uic.barcode.test.utils.TestUtils;
+import org.uic.ticket.EncodingFormatException;
+
+
+public class TicketLayoutTest {
+
+
+ @Test public void testTicketLayout() throws IOException, EncodingFormatException{
+
+ UTLAYDataRecord tl1 = new UTLAYDataRecord();
+
+ TicketLayout layout = SimpleTestTicketLayout.getSimpleTestTicketLayout();
+ tl1.setLayout(layout);
+
+ byte[] encoded = null;
+ try {
+ encoded = tl1.encode();
+ } catch (IOException e) {
+ throw (e);
+ }
+
+ String hex = TestUtils.hexStringFromBytes(encoded);
+
+ assertEquals(hex,"555F544C41593031303034305243543230303031303130313031323030303030374DC3BC6C6C6572");
+
+ UTLAYDataRecord tl2 = new UTLAYDataRecord();
+
+ tl2.decode(tl1.encode());
+
+
+ assertEquals(tl1.toString(),tl2.toString());
+
+ }
+
+}
diff --git a/src/org/uic/barcode/test/utils/Level2TestDataFactory.java b/src/org/uic/barcode/test/utils/Level2TestDataFactory.java new file mode 100644 index 0000000..a361561 --- /dev/null +++ b/src/org/uic/barcode/test/utils/Level2TestDataFactory.java @@ -0,0 +1,20 @@ +package org.uic.barcode.test.utils;
+
+import org.uic.barcode.dynamicFrame.DataType;
+
+import net.gcdc.asn1.datatypesimpl.OctetString;
+
+public class Level2TestDataFactory {
+
+ public static DataType getLevel2SimpleTestData() {
+
+ String testContent = "2020.10.01T12:12.20";
+
+ DataType level2Data = new DataType();
+ level2Data.setFormat("TEST");
+ level2Data.setData(new OctetString(testContent.getBytes()));
+
+ return level2Data;
+ }
+
+}
diff --git a/src/org/uic/barcode/test/utils/SimpleDynamicFrameTestBarcode.java b/src/org/uic/barcode/test/utils/SimpleDynamicFrameTestBarcode.java new file mode 100644 index 0000000..1782ca6 --- /dev/null +++ b/src/org/uic/barcode/test/utils/SimpleDynamicFrameTestBarcode.java @@ -0,0 +1,80 @@ +package org.uic.barcode.test.utils;
+
+import java.security.KeyPair;
+
+import org.uic.barcode.dynamicFrame.Constants;
+import org.uic.barcode.dynamicFrame.DataType;
+import org.uic.barcode.dynamicFrame.DynamicFrame;
+import org.uic.barcode.dynamicFrame.Level1DataType;
+import org.uic.barcode.dynamicFrame.Level2DataType;
+import org.uic.barcode.dynamicFrame.SequenceOfDataType;
+import org.uic.ticket.EncodingFormatException;
+import org.uic.ticket.api.asn.omv1.UicRailTicketData;
+import org.uic.ticket.api.test.SimpleUicTestTicket;
+
+
+public class SimpleDynamicFrameTestBarcode {
+
+ public static DynamicFrame getSimpleDynamicHeaderBarcode(String algorithm, KeyPair keyPair) {
+
+
+
+ DynamicFrame barcode = null;
+
+
+
+ try {
+ barcode = new DynamicFrame();
+ barcode.setFormat(Constants.DYNAMIC_BARCODE_FORMAT_DEFAULT);
+ Level2DataType level2Data = new Level2DataType();
+ barcode.setLevel2SignedData(level2Data);
+
+ Level1DataType level1Data = new Level1DataType();
+ level2Data.setLevel1Data(level1Data);
+
+ level1Data.setSecurityProvider("1080");
+ level1Data.setKeyId(1L);
+
+ level1Data.setLevel1SigningAlg(Constants.ECDSA_SHA256);
+
+ DataType data = new DataType();
+ UicRailTicketData ticket = SimpleUicTestTicket.getUicTestTicket();
+ byte[] ticketData = ticket.encode();
+ data.setByteData(ticketData);
+ data.setFormat(Constants.DATA_TYPE_FCB_VERSION_1);
+ SequenceOfDataType dataSequence = new SequenceOfDataType();
+ level1Data.setData(dataSequence);
+ level1Data.getData().add(data);
+
+ try {
+ level2Data.signLevel1(keyPair.getPrivate());
+ } catch (Exception e) {
+ assert(false);
+ }
+
+
+ } catch (EncodingFormatException e) {
+ e.printStackTrace();
+ return null;
+ }
+
+ return barcode;
+ }
+
+
+ public static void compareFrame(DynamicFrame frame1, DynamicFrame frame2) {
+
+ assert(frame1.getLevel2SignedData().getLevel1Data().getKeyId() == frame2.getLevel2SignedData().getLevel1Data().getKeyId());
+
+ assert(frame1.getLevel2SignedData().getLevel1Data().getLevel1SigningAlg().equals(frame2.getLevel2SignedData().getLevel1Data().level1SigningAlg));
+
+ assert(frame1.getLevel2SignedData().getLevel1Data().getSecurityProvider().equals(frame2.getLevel2SignedData().getLevel1Data().getSecurityProvider()));
+
+ DataType data1 = frame1.getLevel2SignedData().getLevel1Data().getData().get(0);
+ DataType data2 = frame2.getLevel2SignedData().getLevel1Data().getData().get(0);
+
+ assert(data1.getFormat().equals(data2.getFormat()));
+
+ }
+
+}
diff --git a/src/org/uic/barcode/test/utils/SimpleTestTicketLayout.java b/src/org/uic/barcode/test/utils/SimpleTestTicketLayout.java new file mode 100644 index 0000000..24cbf50 --- /dev/null +++ b/src/org/uic/barcode/test/utils/SimpleTestTicketLayout.java @@ -0,0 +1,33 @@ +package org.uic.barcode.test.utils;
+
+import org.uic.barcode.staticHeader.ticketLayoutBarcode.LayoutElement;
+import org.uic.barcode.staticHeader.ticketLayoutBarcode.TicketLayout;
+
+public class SimpleTestTicketLayout {
+
+
+ public static TicketLayout getSimpleTestTicketLayout() {
+
+ TicketLayout layout = new TicketLayout();
+
+ layout.setLayoutStandard("RCT2");
+
+ LayoutElement element = new LayoutElement();
+ element.setColumn(1);
+ element.setLine(1);
+ element.setHeight(1);
+ element.setWidth(20);
+ element.setText("Müller");
+ layout.addLayoutElement(element);
+
+ return layout;
+
+ }
+
+ public static void compare(TicketLayout layout1, TicketLayout layout2) {
+
+ assert(layout1.getLayoutStandard().equals(layout2.getLayoutStandard()));
+
+ }
+
+}
diff --git a/src/org/uic/barcode/test/utils/SimpleUICTestTicket.java b/src/org/uic/barcode/test/utils/SimpleUICTestTicket.java new file mode 100644 index 0000000..3f38515 --- /dev/null +++ b/src/org/uic/barcode/test/utils/SimpleUICTestTicket.java @@ -0,0 +1,254 @@ +package org.uic.barcode.test.utils;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.uic.ticket.api.impl.SimpleCardReference;
+import org.uic.ticket.api.impl.SimpleControlDetail;
+import org.uic.ticket.api.impl.SimpleCustomerStatusDescription;
+import org.uic.ticket.api.impl.SimpleExtension;
+import org.uic.ticket.api.impl.SimpleIssuingDetail;
+import org.uic.ticket.api.impl.SimpleOpenTicket;
+import org.uic.ticket.api.impl.SimpleStationPassage;
+import org.uic.ticket.api.impl.SimpleTicketLink;
+import org.uic.ticket.api.impl.SimpleToken;
+import org.uic.ticket.api.impl.SimpleTraveler;
+import org.uic.ticket.api.impl.SimpleTravelerDetail;
+import org.uic.ticket.api.impl.SimpleUicRailTicket;
+import org.uic.ticket.api.spec.ICardReference;
+import org.uic.ticket.api.spec.IControlDetail;
+import org.uic.ticket.api.spec.ICustomerStatusDescription;
+import org.uic.ticket.api.spec.IExtension;
+import org.uic.ticket.api.spec.IIssuingDetail;
+import org.uic.ticket.api.spec.ILinkMode;
+import org.uic.ticket.api.spec.IOpenTicket;
+import org.uic.ticket.api.spec.IStationPassage;
+import org.uic.ticket.api.spec.ITicketLink;
+import org.uic.ticket.api.spec.IToken;
+import org.uic.ticket.api.spec.ITraveler;
+import org.uic.ticket.api.spec.IUicRailTicket;
+
+public class SimpleUICTestTicket {
+
+
+ public static IUicRailTicket getUicTestTicket() {
+ IUicRailTicket ticket = new SimpleUicRailTicket();
+ populateTicket(ticket);
+ return ticket;
+ }
+
+
+ private static void populateTicket(IUicRailTicket ticket) {
+
+ ticket.setControlDetails(new SimpleControlDetail());
+ populate(ticket.getControlDetails());
+
+
+ ticket.setIssuerDetails(new SimpleIssuingDetail());
+ populateIssuingData(ticket.getIssuerDetails());
+
+ SimpleTravelerDetail td = new SimpleTravelerDetail();
+ populateTravelerData(td);
+ ticket.setTravelerDetails(td);
+
+
+ //OpenTicket
+ IOpenTicket do1 = new SimpleOpenTicket();
+ populate(do1);
+ ticket.addOpenTicket(do1);
+
+ //StationPassage
+ IStationPassage do2 = new SimpleStationPassage();
+ populateStationPassage(do2);
+ ticket.addStationPassage(do2);
+
+ //token
+ IToken to = new SimpleToken();
+ to.setTokenProvider("VDV");
+ byte[] ba = { (byte) 0x82, (byte) 0xDA };
+ to.setToken(ba);
+
+
+ ticket.addExtension(populateExtension());
+
+ }
+
+ /*
+ ticket stationPassage : {
+ productName "passage"
+ ,station {8312345}
+ ,stationNameUTF8 { "Amsterdam" }
+ ,validFromDay 0
+ ,validUntilDay 4
+ }
+ */
+ private static void populateStationPassage(IStationPassage sp) {
+ sp.setProductName("passage");
+
+ try {
+ Date date = new SimpleDateFormat("dd/MM/yyyy").parse("01/01/2018");
+ sp.setValidFrom(date);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ try {
+ Date date = new SimpleDateFormat("dd/MM/yyyy").parse("04/01/2018");
+ sp.setValidUntil(date);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+
+ sp.addStation("8312345");
+ sp.addStationName("Amsterdam");
+
+ }
+
+ /*
+ {
+ token {tokenProviderIA5 "VDV", token '82DA'H }
+ ,ticket openTicket : {
+ returnIncluded FALSE
+ infoText "openTicketInfo"
+ }
+ }
+ */
+
+ private static void populate(IOpenTicket otd) {
+ otd.setInfoText("openTicketInfo");
+ otd.setReturnIncluded(false);
+ }
+
+
+ /*
+ ,travelerDetail{
+ traveler {
+ {
+ firstName "John"
+ ,secondName "Dow"
+ ,idCard "12345"
+ ,ticketHolder TRUE
+ ,status {{customerStatusDescr "senior" }}
+ }
+ }
+ ,groupName "myGroup"
+ }
+ */
+
+ private static void populateTravelerData(SimpleTravelerDetail td) {
+ td.setGroupName("myGroup");
+ ITraveler tr = new SimpleTraveler();
+ tr.setIdCard("12345");
+ tr.setFirstName("John");
+ tr.setSecondName("Dow");
+ tr.setTicketHolder(true);
+ ICustomerStatusDescription cst = new SimpleCustomerStatusDescription();
+ tr.addStatusDescription(cst);
+ cst.setDescription("senior");
+ td.addTraveler(tr);
+ }
+
+ /*
+ ,issuingDetail {
+ issuerNum 1080
+ issuingYear 2018
+ issuingDay 1
+ specimen TRUE,
+ securePaperTicket FALSE,
+ activated TRUE,
+ issuerPNR "issuerTestPNR",
+ issuedOnLine 12
+ }
+ */
+ private static void populateIssuingData(IIssuingDetail iIssuingDetail) {
+
+ try {
+ Date date = new SimpleDateFormat("dd/MM/yyyy").parse("01/01/2018");
+ iIssuingDetail.setIssuer("1080");
+ iIssuingDetail.setIssuingDate(date);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+
+
+ iIssuingDetail.setIssuerPNR("issuerTestPNR");
+ iIssuingDetail.setSpecimen(true);
+ iIssuingDetail.setSecurePaperTicket(false);
+ iIssuingDetail.setActivated(true);
+ iIssuingDetail.setIssuedOnLine(12);
+ }
+
+ /*
+ ,extension {
+ { extensionId "1", extensionData '82DA'H }
+ ,{ extensionId "2", extensionData '83DA'H }
+ }
+ */
+ private static IExtension populateExtension() {
+ IExtension ed1 = new SimpleExtension();
+ ed1.setId("1");
+ byte[] ba1 = { (byte) 0x82, (byte) 0xDA };
+ ed1.setBinarydata(ba1);
+ return ed1;
+ }
+
+ /*
+ ,controlDetail {
+ identificationByCardReference {
+ { trailingCardIdNum 100 }
+ }
+ ,identificationByIdCard FALSE
+ ,identificationByPassportId FALSE
+ ,passportValidationRequired FALSE
+ ,onlineValidationRequired FALSE
+ ,ageCheckRequired FALSE
+ ,reductionCardCheckRequired FALSE
+ ,infoText "cd"
+ ,includedTickets {
+ { productOwnerIA5 "test" }
+ }
+ }
+ */
+ private static void populate(IControlDetail iControlDetail) {
+ iControlDetail.setInfoText("cd");
+ iControlDetail.setAgeCheckRequired(false);
+ iControlDetail.setIdentificationByIdCard(false);
+ iControlDetail.setIdentificationByPassportId(false);
+ iControlDetail.setOnlineValidationRequired(false);
+ iControlDetail.setPassportValidationRequired(false);
+ iControlDetail.setReductionCardCheckRequired(false);
+ iControlDetail.getIdentificationByCardReference().add(populateCardRefrence());
+ iControlDetail.addLinkedTicket(populateLinkedTicket());
+ }
+
+
+ /*
+ *
+ */
+ private static ITicketLink populateLinkedTicket() {
+ ITicketLink it = new SimpleTicketLink();
+ it.setProductOwner("test");
+ it.setLinkMode(ILinkMode.issuedTogether);
+ return it;
+ }
+
+ /*
+ {
+ trailingCardIdNum 100
+ }
+ */
+ private static ICardReference populateCardRefrence() {
+ ICardReference cr = new SimpleCardReference();
+ cr.setTrailingCardId("100");
+ return cr;
+ }
+
+
+ public static void compare(IUicRailTicket ticket1, IUicRailTicket ticket2) {
+
+ assert(ticket1.getIssuerDetails().getIssuer().equals(ticket2.getIssuerDetails().getIssuer()));
+
+ }
+
+
+}
diff --git a/src/org/uic/barcode/test/utils/TestUtils.java b/src/org/uic/barcode/test/utils/TestUtils.java new file mode 100644 index 0000000..7311ae4 --- /dev/null +++ b/src/org/uic/barcode/test/utils/TestUtils.java @@ -0,0 +1,38 @@ +package org.uic.barcode.test.utils;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public class TestUtils {
+
+ final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
+
+ public static String hexStringFromBytes(byte[] bytes) {
+ char[] hexChars = new char[bytes.length * 2];
+ for (int j = 0; j < bytes.length; j++) {
+ int v = bytes[j] & 0xFF;
+ hexChars[j * 2] = hexArray[v >>> 4];
+ hexChars[j * 2 + 1] = hexArray[v & 0x0F];
+ }
+ return new String(hexChars);
+ }
+
+
+ public static Date parseDate (String source){
+
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
+
+ try {
+ return formatter.parse(source);
+ } catch (ParseException e) {
+ try {
+ return formatter.parse("2001-01-01");
+ } catch (ParseException e1) {
+ return null;
+ }
+ }
+
+ }
+
+}
|