summaryrefslogtreecommitdiffstats
path: root/src/org/uic/barcode/test/DynamicFrameSimpleTest.java
diff options
context:
space:
mode:
authorCGantert345 <57003061+CGantert345@users.noreply.github.com>2020-07-28 17:57:25 +0200
committerCGantert345 <57003061+CGantert345@users.noreply.github.com>2020-07-28 17:57:25 +0200
commitb17ce13cc31abd088088bf8ab26cc924a6a36585 (patch)
tree5588e1ccc212213097500f4cb43fa0e3e68e323b /src/org/uic/barcode/test/DynamicFrameSimpleTest.java
parentDraft of the new DOSIPAS included (diff)
downloadUIC-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/DynamicFrameSimpleTest.java')
-rw-r--r--src/org/uic/barcode/test/DynamicFrameSimpleTest.java171
1 files changed, 171 insertions, 0 deletions
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();
+ }
+
+
+}