summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/uic/barcode/ticketTestDB/EncodeSparpreisTicketDBTest.java
blob: 787abf5830902a3a683404cc360da733ff10478b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package org.uic.barcode.ticketTestDB;

import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.TimeZone;
import java.util.zip.DataFormatException;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.uic.barcode.Decoder;
import org.uic.barcode.Encoder;
import org.uic.barcode.asn1.uper.UperEncoder;
import org.uic.barcode.dynamicFrame.Constants;
import org.uic.barcode.staticFrame.StaticFrame;
import org.uic.barcode.test.utils.TestUtils;
import org.uic.barcode.ticket.EncodingFormatException;
import org.uic.barcode.ticket.api.spec.IUicRailTicket;

public class EncodeSparpreisTicketDBTest {
	
	/** The algorithm OID. */
	public String algorithmOID = Constants.DSA_SHA1;
	
	public int keySize = 1024;
	
	/** The key pair. */
	public KeyPair keyPair = null;
	
	public String securityProvider = null;
	
	/** the test ticket **/
	public IUicRailTicket ticket = null;
	
    TimeZone defaulttimeZone = null;
	
	/**
	 * Prepare tickets.
	 * @throws DataFormatException 
	 * @throws EncodingFormatException 
	 * @throws IOException 
	 */
	@Before public void prepare() throws IOException, EncodingFormatException, DataFormatException {
		defaulttimeZone = TimeZone.getDefault();
    	//decode in local CET time zone
		TimeZone.setDefault(TimeZone.getTimeZone("CET"));
		
		algorithmOID = Constants.DSA_SHA1;
		keySize = 1024;
		
		Security.addProvider(new BouncyCastleProvider());

		try {
			keyPair  = TestUtils.generateDSAKeys(keySize);
		} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
			e.printStackTrace();
		}

        assert(keyPair != null);
        
    	//get original ticket
        byte[] content = UperEncoder.bytesFromHexString(getEncodingV2Hex());
        // decode to get the ticket
        Decoder decoder = new Decoder(content);
        ticket = decoder.getUicTicket();
        StaticFrame frame = decoder.getStaticFrame();
        securityProvider = frame.getSecurityProvider();
	}
	
	
	/**
	 * clean up
	 */
	@After public void resetTimeZone() {
		TimeZone.setDefault(defaulttimeZone);
	}
	
    @Test
    public void testDecoder() throws Exception {

        Encoder encoder = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_CLASSIC, 1, 2);
        encoder.signLevel1(securityProvider, keyPair.getPrivate(), algorithmOID, "1");
        
        byte[] encoded =  encoder.encode();
        
        assert(encoded != null);
        
        //TODO check ticket in detail
        
    }
    
	private static String getEncodingV2Hex() {
		
		 return "2355543032313038303030303032782e" + 
				"2fe184a1d85e89e9338b298ec61aeba2" + 
				"48ce722056ca940a967c8a1d39126e2c" + 
				"628c4fcea91ba35216a0a350f894de5e" + 
				"bd7b8909920fde947feede0e20c43031" + 
			    "3939789c01bc0043ff555f464c455831" + 
				"333031383862b20086e10dc125ea2815" + 
				"110881051c844464d985668e23a00a80" + 
				"000e96c2e4e6e8cadc08aed2d8d90104" + 
				"44d7be0100221ce610ea559b64364c38" + 
				"a82361d1cb5e1e5d32a3d0979bd099c8" + 
				"426b0b7373432b4b6852932baba3634b" + 
				"733b2b715ab34b09d101e18981c181f1" + 
				"424221521291521292a17a3a920a1152" + 
				"5a095282314952b20a49529952826278" + 
				"083001a4c38ae5bb303ace7003800700" + 
				"14b00240400f53757065722053706172" + 
				"7072656973c41e4a03";
	}
	


}