From cd76118fb6737eb241cab60207262617e12d237d Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 23 Nov 2021 11:16:40 +0100 Subject: test added --- .../org/uic/barcode/test/DynamicFrameFCB3Test.java | 175 +++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java new file mode 100644 index 0000000..2ac7353 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java @@ -0,0 +1,175 @@ +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.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameFCB3Test { + + 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 testDynamicHeaderBarcodeEncodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } 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 testDynamicHeaderBarcodeDecodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } 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); + + assert(dec.getDynamicHeader().getFormat().equals("U1")); + + assert(dec.getDynamicHeader().getDynamicDataFDC1().getDataType().getFormat().equals("FCB3")); + + + 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 = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} -- cgit v1.2.3 From 47133b358a65723b1b131f66d2e340a12685f9e6 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 23 Nov 2021 11:23:55 +0100 Subject: renamed --- .../org/uic/barcode/test/DynamicFrameFCB3Test.java | 175 --------------------- .../org/uic/barcode/test/DynamicFrameFcb3Test.java | 175 +++++++++++++++++++++ 2 files changed, 175 insertions(+), 175 deletions(-) delete mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java deleted file mode 100644 index 2ac7353..0000000 --- a/src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java +++ /dev/null @@ -1,175 +0,0 @@ -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.ticket.EncodingFormatException; -import org.uic.barcode.ticket.api.spec.IUicRailTicket; - -public class DynamicFrameFCB3Test { - - 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 testDynamicHeaderBarcodeEncodingFCB3() { - - IUicRailTicket ticket = testFCBticket; - - Encoder enc = null; - - try { - enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); - } 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 testDynamicHeaderBarcodeDecodingFCB3() { - - IUicRailTicket ticket = testFCBticket; - - Encoder enc = null; - - try { - enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); - } 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); - - assert(dec.getDynamicHeader().getFormat().equals("U1")); - - assert(dec.getDynamicHeader().getDynamicDataFDC1().getDataType().getFormat().equals("FCB3")); - - - 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 = "ECDSA"; - ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); - KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); - g.initialize(ecSpec, new SecureRandom()); - return g.generateKeyPair(); - } - - -} diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java new file mode 100644 index 0000000..cb252a2 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java @@ -0,0 +1,175 @@ +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.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameFcb3Test { + + 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 testDynamicHeaderBarcodeEncodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } 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 testDynamicHeaderBarcodeDecodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } 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); + + assert(dec.getDynamicHeader().getFormat().equals("U1")); + + assert(dec.getDynamicHeader().getDynamicDataFDC1().getDataType().getFormat().equals("FCB3")); + + + 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 = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} -- cgit v1.2.3 From 900acb165c53e9801112c3f1b8f57ff4d9ad531d Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 23 Nov 2021 13:28:56 +0100 Subject: additional tests for DOSIPAS --- .../test/DynamicFrameDynamicContentTest.java | 208 +++++++++++++++++++++ .../org/uic/barcode/test/DynamicFrameFcb3Test.java | 175 ----------------- .../barcode/test/DynamicFrameFcbVersion1Test.java | 174 +++++++++++++++++ .../barcode/test/DynamicFrameFcbVersion3Test.java | 174 +++++++++++++++++ 4 files changed, 556 insertions(+), 175 deletions(-) create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java delete mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java new file mode 100644 index 0000000..93ecdfb --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java @@ -0,0 +1,208 @@ +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.dynamicContent.fdc1.UicDynamicContentDataFDC1; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameDynamicContentTest { + + 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); + + assert(keyPairLevel2 != null); + + } + + + @Test public void testDynamicContentEncoding() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 13); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID, keyPairLevel2.getPublic()); + + try { + enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + try { + UicDynamicContentDataFDC1 dcd = new UicDynamicContentDataFDC1(); + dcd.setChallengeString("CHALLENGE"); + dcd.setAppId("MyApp"); + enc.setDynamicContentDataUIC1(dcd); + 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 testDynamicContentDecoding() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 13); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID, keyPairLevel2.getPublic()); + + try { + enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + try { + UicDynamicContentDataFDC1 dcd = new UicDynamicContentDataFDC1(); + dcd.setChallengeString("CHALLENGE"); + dcd.setAppId("MyApp"); + enc.setDynamicContentDataUIC1(dcd); + 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); + + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); + + int level2check = 0; + try { + level2check = dec.validateLevel2(); + } catch (Exception e) { + assert(false); + } + + assert(level2check == Constants.LEVEL2_VALIDATION_OK); + + assert(dec.getDynamicHeader().getDynamicDataFDC1().getChallengeString().equals("CHALLENGE")); + + assert(dec.getDynamicHeader().getDynamicDataFDC1().getAppId().equals("MyApp")); + + } + + 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 = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java deleted file mode 100644 index cb252a2..0000000 --- a/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java +++ /dev/null @@ -1,175 +0,0 @@ -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.ticket.EncodingFormatException; -import org.uic.barcode.ticket.api.spec.IUicRailTicket; - -public class DynamicFrameFcb3Test { - - 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 testDynamicHeaderBarcodeEncodingFCB3() { - - IUicRailTicket ticket = testFCBticket; - - Encoder enc = null; - - try { - enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); - } 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 testDynamicHeaderBarcodeDecodingFCB3() { - - IUicRailTicket ticket = testFCBticket; - - Encoder enc = null; - - try { - enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); - } 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); - - assert(dec.getDynamicHeader().getFormat().equals("U1")); - - assert(dec.getDynamicHeader().getDynamicDataFDC1().getDataType().getFormat().equals("FCB3")); - - - 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 = "ECDSA"; - ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); - KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); - g.initialize(ecSpec, new SecureRandom()); - return g.generateKeyPair(); - } - - -} diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java new file mode 100644 index 0000000..63ba68e --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java @@ -0,0 +1,174 @@ +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.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameFcbVersion1Test { + + 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 testDynamicHeaderBarcodeEncodingFCB3() { + + 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 testDynamicHeaderBarcodeDecodingFCB3() { + + 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); + + assert(dec.getDynamicHeader().getFormat().equals("U1")); + + assert(dec.getDynamicHeader().getLevel2SignedData().getLevel1Data().getData().get(0).getFormat().equals("FCB1")); + + + 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 = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java new file mode 100644 index 0000000..4f0897e --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java @@ -0,0 +1,174 @@ +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.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameFcbVersion3Test { + + 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 testDynamicHeaderBarcodeEncodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } 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 testDynamicHeaderBarcodeDecodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } 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); + + assert(dec.getDynamicHeader().getFormat().equals("U1")); + + assert(dec.getDynamicHeader().getLevel2SignedData().getLevel1Data().getData().get(0).getFormat().equals("FCB3")); + + + 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 = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} -- cgit v1.2.3 From 12ca8d77e0a0d63ce03886d38c079c686122253a Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 23 Nov 2021 15:39:05 +0100 Subject: dynamic content time stamp fixed and test added --- .../test/DynamicFrameDynamicContentTest.java | 38 ++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java index 93ecdfb..53b6af8 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java @@ -10,6 +10,11 @@ import java.security.NoSuchProviderException; import java.security.SecureRandom; import java.security.Security; import java.security.SignatureException; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoUnit; +import java.util.Arrays; +import java.util.Date; import java.util.zip.DataFormatException; import org.bouncycastle.jce.ECNamedCurveTable; @@ -19,6 +24,7 @@ import org.junit.Before; import org.junit.Test; import org.uic.barcode.Decoder; import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicContent.fdc1.TimeStamp; import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1; import org.uic.barcode.dynamicFrame.Constants; import org.uic.barcode.test.utils.SimpleUICTestTicket; @@ -34,6 +40,11 @@ public class DynamicFrameDynamicContentTest { public KeyPair keyPairLevel1 = null; public KeyPair keyPairLevel2 = null; + public byte[] passIdHash = "PassId".getBytes(); + public byte[] phoneIdHash = "myPhone".getBytes(); + + ZonedDateTime originalTimeStamp = ZonedDateTime.now(ZoneId.of("UTC")); + public IUicRailTicket testFCBticket = null; @@ -87,6 +98,12 @@ public class DynamicFrameDynamicContentTest { UicDynamicContentDataFDC1 dcd = new UicDynamicContentDataFDC1(); dcd.setChallengeString("CHALLENGE"); dcd.setAppId("MyApp"); + dcd.setPhoneIdHash(phoneIdHash); + dcd.setPassIdHash(passIdHash); + TimeStamp ts = new TimeStamp(); + ts.setDateTime(Date.from(originalTimeStamp.toInstant())); + dcd.setTimeStamp(ts); + enc.setDynamicContentDataUIC1(dcd); enc.signLevel2(keyPairLevel2.getPrivate()); } catch (Exception e) { @@ -133,6 +150,12 @@ public class DynamicFrameDynamicContentTest { UicDynamicContentDataFDC1 dcd = new UicDynamicContentDataFDC1(); dcd.setChallengeString("CHALLENGE"); dcd.setAppId("MyApp"); + dcd.setPhoneIdHash(phoneIdHash); + dcd.setPassIdHash(passIdHash); + TimeStamp ts = new TimeStamp(); + ts.setDateTime(Date.from(originalTimeStamp.toInstant())); + dcd.setTimeStamp(ts); + enc.setDynamicContentDataUIC1(dcd); enc.signLevel2(keyPairLevel2.getPrivate()); } catch (Exception e) { @@ -182,10 +205,21 @@ public class DynamicFrameDynamicContentTest { assert(level2check == Constants.LEVEL2_VALIDATION_OK); - assert(dec.getDynamicHeader().getDynamicDataFDC1().getChallengeString().equals("CHALLENGE")); + UicDynamicContentDataFDC1 dynamicData = dec.getDynamicHeader().getDynamicDataFDC1(); + + assert(dynamicData.getChallengeString().equals("CHALLENGE")); + + assert(dynamicData.getAppId().equals("MyApp")); + + assert(Arrays.equals(dynamicData.getPassIdHash(),passIdHash)); - assert(dec.getDynamicHeader().getDynamicDataFDC1().getAppId().equals("MyApp")); + assert(Arrays.equals(dynamicData.getPhoneIdHash(),phoneIdHash)); + Date timeStamp = dynamicData.getTimeStamp().getTimeAsDate(); + ZonedDateTime retrievedTimeStamp = timeStamp.toInstant().atZone(ZoneId.of("UTC")); + long diff = ChronoUnit.SECONDS.between(originalTimeStamp, retrievedTimeStamp); + + assert(diff == 0); } public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ -- cgit v1.2.3 From b51f825080e3535b259c673af7053ad43fc1e674 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 23 Nov 2021 16:44:15 +0100 Subject: unused code removed test on security with designated provider --- ...cFrameDoubleSignatureProviderSelectionTest.java | 169 +++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureProviderSelectionTest.java (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureProviderSelectionTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureProviderSelectionTest.java new file mode 100644 index 0000000..84f7795 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureProviderSelectionTest.java @@ -0,0 +1,169 @@ +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.Provider; +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.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameDoubleSignatureProviderSelectionTest { + + public String signatureAlgorithmOID = null; + public String elipticCurve = null; + public String keyPairAlgorithmOID = null; + + public KeyPair keyPairLevel1 = null; + public KeyPair keyPairLevel2 = null; + + public IUicRailTicket testFCBticket = null; + + public Provider prov = null; + + @Before public void initialize() { + + signatureAlgorithmOID = Constants.ECDSA_SHA256; + keyPairAlgorithmOID = Constants.KG_EC_256; + elipticCurve = "secp256k1"; + + testFCBticket = SimpleUICTestTicket.getUicTestTicket(); + + prov = new BouncyCastleProvider(); + + Security.addProvider(prov); + + try { + keyPairLevel1 = generateECKeys(keyPairAlgorithmOID, elipticCurve); + keyPairLevel2 = generateECKeys(keyPairAlgorithmOID, elipticCurve); + } catch (Exception e) { + assert(false); + } + + assert(keyPairLevel1 != null); + + assert(keyPairLevel2 != null); + + } + + + + @Test public void testDynamicHeaderBarcodeDecoding() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 13); + } 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",prov); + } catch (Exception e) { + assert(false); + } + + assert(enc != null); + + + DataType level2Data = Level2TestDataFactory.getLevel2SimpleTestData(); + try { + enc.setLevel2Data(level2Data); + enc.signLevel2(keyPairLevel2.getPrivate(),prov); + } 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, prov); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + signatureCheck = 0; + try { + signatureCheck = dec.validateLevel2(prov); + } 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 = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} -- cgit v1.2.3 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/uic/barcode') 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 From 3062dbc6b6df9c7ef824eeea8d31ad5702506518 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Wed, 24 Nov 2021 12:40:11 +0100 Subject: more tests --- .../barcode/asn1/test/UperEncodeBitStringTest.java | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/test/java/org/uic/barcode/asn1/test/UperEncodeBitStringTest.java (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeBitStringTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeBitStringTest.java new file mode 100644 index 0000000..574a9cf --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeBitStringTest.java @@ -0,0 +1,81 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.Asn1VarSizeBitstring; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeBitStringTest { + + /** + * Example from the Standard on UPER. +
+ World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value BIT STRING OPTIONAL, + } + END + + + rec1value TestRecord ::= { + value '001'B + } ++ + + + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @Asn1Optional() Asn1VarSizeBitstring value; + + public TestRecord() { + this(false,false,true); + } + + public TestRecord(Boolean value1,Boolean value2,Boolean value3 ) { + List
+ World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value1 PrintableString ( FROM ("ACGT") ) OPTIONAL, + value2 PrintableString ( FROM ("ACGT") ) OPTIONAL + } + END + + rec1value TestRecord ::= { + value1 "ACGT", + value2 "ACTGCATCGA" + } ++ */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @RestrictedString(value = CharacterRestriction.VisibleString, alphabet = GenAlphabet.class) + @Asn1Optional() String value1; + + @FieldOrder(order = 1) + @RestrictedString(value = CharacterRestriction.VisibleString, alphabet = GenAlphabet.class) + @Asn1Optional() String value2; + + public TestRecord() { + } + + public TestRecord(String v1, String v2) { + this.value1 = v1; + this.value2 = v2; + } + } + + + @Test public void testEncode() throws IllegalArgumentException, IllegalAccessException { + + + TestRecord record = new TestRecord("ACGT", "ACTGCATCGA"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C106C2879360",hex); + + } + + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord("ACGT", "ACTGCATCGA"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C106C2879360",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1,record.value1); + assertEquals(result.value2,record.value2); + } + + + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeVarBitStringTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeVarBitStringTest.java new file mode 100644 index 0000000..d6212e2 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeVarBitStringTest.java @@ -0,0 +1,81 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.Asn1VarSizeBitstring; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeVarBitStringTest { + + /** + * Example from the Standard on UPER. +
+ World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value BIT STRING OPTIONAL, + } + END + + + rec1value TestRecord ::= { + value '001'B + } ++ + + + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @Asn1Optional() Asn1VarSizeBitstring value; + + public TestRecord() { + this(false,false,true); + } + + public TestRecord(Boolean value1,Boolean value2,Boolean value3 ) { + List