1+ package com .akamai .edgeauth ;
2+
3+ import org .junit .Assert ;
4+ import org .junit .Rule ;
5+ import org .junit .Test ;
6+ import org .junit .rules .ExpectedException ;
7+
8+ import java .time .Instant ;
9+ import java .util .regex .Pattern ;
10+
11+ // Test content taken from https://github.com/mobilerider/EdgeAuth-Token-Golang/blob/master/edgeauth_test.go
12+ public class EdgeAuthTest {
13+
14+ private static final String sampleKey = "52a152a152a152a152a152a152a1" ;
15+ private static final String samplePath = "/this/is/a/test" ;
16+
17+ @ Test
18+ public void testGenerateAclToken () throws EdgeAuthException {
19+ EdgeAuth ea = new EdgeAuthBuilder ().algorithm ("SHA256" ).key (sampleKey ).startTime (Instant .now ().toEpochMilli ()/1000 ).windowSeconds (300 ).build ();
20+ String token = ea .generateACLToken (samplePath );
21+
22+ String [] fields = token .split (String .valueOf (ea .getFieldDelimiter ()));
23+ Assert .assertEquals ("ACL token should consists of 4 parts: " + token , 4 , fields .length );
24+
25+ String expectedStart = "st=" + ea .getStartTime ();
26+ Assert .assertEquals ("Start field does not match" , expectedStart , fields [0 ]);
27+
28+ String expectedExpire = "exp=" + (ea .getStartTime () + ea .getWindowSeconds ());
29+ Assert .assertEquals ("Expire field does not match" , expectedExpire , fields [1 ]);
30+
31+ String expectedAcl = "acl=.+" ;
32+ Assert .assertTrue ("ACL field does not match: " + fields [2 ], Pattern .matches (expectedAcl , fields [2 ]));
33+
34+ String expectedHmac = "hmac=[a-f0-9]{64}" ;
35+ Assert .assertTrue ("Hmac field does not match: " + fields [3 ], Pattern .matches (expectedHmac , fields [3 ]));
36+ }
37+
38+ @ Test
39+ public void testGenerateUrlToken () throws EdgeAuthException {
40+ EdgeAuth ea = new EdgeAuthBuilder ().algorithm ("SHA256" ).key (sampleKey ).startTime (Instant .now ().toEpochMilli ()/1000 ).windowSeconds (300 ).build ();
41+ String token = ea .generateURLToken (samplePath );
42+
43+ String [] fields = token .split (String .valueOf (ea .getFieldDelimiter ()));
44+ Assert .assertEquals ("URL token should consists of 3 parts: " + token , 3 , fields .length );
45+
46+ String expectedStart = "st=" + ea .getStartTime ();
47+ Assert .assertEquals ("Start field does not match" , expectedStart , fields [0 ]);
48+
49+ String expectedExpire = "exp=" + (ea .getStartTime () + ea .getWindowSeconds ());
50+ Assert .assertEquals ("Expire field does not match" , expectedExpire , fields [1 ]);
51+
52+ String expectedHmac = "hmac=[a-f0-9]{64}" ;
53+ Assert .assertTrue ("Hmac field does not match: " + fields [2 ], Pattern .matches (expectedHmac , fields [2 ]));
54+ }
55+
56+ @ Rule
57+ public ExpectedException exceptionRule = ExpectedException .none ();
58+
59+ @ Test
60+ public void testGenerateTokenWithInvalidStartAndEndDate () throws EdgeAuthException {
61+ exceptionRule .expect (EdgeAuthException .class );
62+ exceptionRule .expectMessage ("Token will have already expired." );
63+
64+ long startTimeSeconds = Instant .now ().toEpochMilli () / 1000 ;
65+ EdgeAuth ea = new EdgeAuthBuilder ().algorithm ("SHA256" ).key (sampleKey )
66+ .startTime (startTimeSeconds + 300 )
67+ .endTime (startTimeSeconds ).build ();
68+ ea .generateURLToken (samplePath );
69+
70+ }
71+
72+ }
0 commit comments