summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/uic/barcode/test/DynamicContentCoderTest.java
blob: 5355d5d55624d7f0d4fe2ccc5f3a5d03789e58c4 (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
package org.uic.barcode.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.junit.Before;
import org.junit.Test;
import org.uic.barcode.asn1.uper.UperEncoder;
import org.uic.barcode.dynamicContent.api.DynamicContentCoder;
import org.uic.barcode.dynamicContent.api.IUicDynamicContent;
import org.uic.barcode.dynamicContent.api.SimpleUicDynamicContent;
import org.uic.barcode.ticket.EncodingFormatException;
import org.uic.barcode.ticket.api.impl.SimpleExtension;
import org.uic.barcode.ticket.api.impl.SimpleGeoCoordinate;
import org.uic.barcode.ticket.api.spec.IExtension;
import org.uic.barcode.ticket.api.spec.IGeoCoordinate;

public class DynamicContentCoderTest {
	
	IUicDynamicContent content = null; 
	
	@Before public void initialize() {
		
		content = new SimpleUicDynamicContent();		
		
		content.setAppId("appID");
	
		content.setChallengeString("challenge string");
		
		IExtension e = new SimpleExtension();
		e.setId("challenge_extension_id1");
		e.setBinarydata(UperEncoder.bytesFromHexString("82DA"));
		content.setExtension(e);
		
		IExtension e1 = new SimpleExtension();
		e1.setId("challenge_extension_id1");
		e1.setBinarydata(UperEncoder.bytesFromHexString("82DA"));
		content.addDynamicContentResponse(e1);
			
		content.setPhoneIdHash(UperEncoder.bytesFromHexString("83DA"));
		
		content.setPassIdHash(UperEncoder.bytesFromHexString("84DA"));
		
		IGeoCoordinate g = new SimpleGeoCoordinate();
		g.setLongitude( 12345L);
	    g.setLatitude(  56789L);
		content.setGeoCoordinate(g);

		try {
			content.setTimeStamp(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
		} catch (ParseException e2) {
			// 
		}			
		
	}

	@Test public void testDynamicContentEncoding() {
		
		
		byte[] encodedBytes = null;
		try {
			encodedBytes = DynamicContentCoder.encode(content, null);
		} catch (EncodingFormatException e) {
			assert(false);
		}
		
		String encoding = UperEncoder.hexStringFromBytes(encodedBytes);
		
		String expectedEncoding = "7C170F0E12620F9437000230390300DDD504017A20C6D0C2D8D8CADCCECA40E6E8E4D2DCCE2F8F461D9B32EECF96FE5F1D32EEE7A77EEBFA72310282DA05E1A37EECA0507B409C30F3E60509B42F8F461D9B32EECF96FE5F1D32EEE7A77EEBFA72310282DA";
		
		assert(expectedEncoding.equals(encoding));
		
	}
	
	@Test public void testDynamicContentDecoding() {
		
		String encoding = "7C170F0E12620F9437000230390300DDD504017A20C6D0C2D8D8CADCCECA40E6E8E4D2DCCE2F8F461D9B32EECF96FE5F1D32EEE7A77EEBFA72310282DA05E1A37EECA0507B409C30F3E60509B42F8F461D9B32EECF96FE5F1D32EEE7A77EEBFA72310282DA";
		
		IUicDynamicContent content = DynamicContentCoder.decode(UperEncoder.bytesFromHexString(encoding));
		
		
		assert("appID".equals(content.getAppId()));
		
		assert("challenge string".equals(content.getChallengeString()));
		
		IExtension e1 = content.getExtension();
		assert(UperEncoder.hexStringFromBytes(e1.getBinarydata()).equals("82DA"));
		assert(e1.getId().equals("challenge_extension_id1"));
		
		
		assert(content.getChallengeString().equals("challenge string"));
				
		byte[] ce = null;
		for (IExtension e : content.getDynamicContentResponseList()) {
			if (e.getId().equals("challenge_extension_id1")) {
				ce = e.getBinarydata();
			}
		}
		assert(UperEncoder.hexStringFromBytes(ce).equals("82DA"));
		
		assert(UperEncoder.hexStringFromBytes(content.getPhoneIdHash()).equals("83DA"));

		assert(UperEncoder.hexStringFromBytes(content.getPassIdHash()).equals("84DA"));
	
		assert(content.getGeoCoordinate() != null);
		assert(content.getGeoCoordinate().getLongitude() == 12345L);
		assert(content.getGeoCoordinate().getLatitude() == 56789L);			
		
		assert(content.getTimeStamp() != null);
		
		assert(content.getTimeStamp().toString().contains("04 12:30:00 CET"));
		
		
	}
	
	
	
}