added authentication to map notify and map reply, and moved the serialization process...
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / authentication / LispAuthenticationTest.java
1 package org.opendaylight.lispflowmapping.implementation.authentication;
2
3 import static junit.framework.Assert.assertTrue;
4 import static org.junit.Assert.assertFalse;
5 import junitx.framework.ArrayAssert;
6
7 import org.apache.tomcat.util.buf.HexUtils;
8 import org.junit.Test;
9 import org.opendaylight.lispflowmapping.implementation.serializer.MapRegisterSerializer;
10 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
11 import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
12 import org.opendaylight.lispflowmapping.type.lisp.MapNotify;
13 import org.opendaylight.lispflowmapping.type.lisp.MapRegister;
14 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
15
16 public class LispAuthenticationTest extends BaseTestCase{
17
18     
19     @Test
20     public void validate_WrongAuthentication() throws Exception {
21             // LISP(Type = 3 Map-Register, P=1, M=1
22             // Record Counter: 1
23             // Nonce: (something)
24             // Key ID: 0x0001
25             // AuthDataLength: 20 Data:
26             // e8:f5:0b:c5:c5:f2:b0:21:27:a8:21:41:04:f3:46:5a:5a:5b:5c:5d
27             // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
28             // No-Action
29             // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
30             // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
31             //
32             MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(
33                             hexToByteBuffer("38 00 01 01 FF BB " //
34                                             + "00 00 00 00 00 00 00 01 00 14 e8 f5 0b c5 c5 f2 " //
35                                             + "b0 21 27 a8 21 41 04 f3 46 5a 5a 5b 5c 5d 00 00 " //
36                                             + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
37                                             + "ff 00 00 05 00 01 c0 a8 88 0a"));
38             
39             assertFalse(LispAuthenticationUtil.validate(mapRegister));
40     }
41     
42     @Test
43     public void validate__SHA1() throws Exception {
44             // LISP(Type = 3 Map-Register, P=1, M=1
45             // Record Counter: 1
46             // Nonce: (something)
47             // Key ID: 0x0001
48             // AuthDataLength: 20 Data:
49             // b2:dd:1a:25:c0:60:b1:46:e8:dc:6d:a6:ae:2e:92:92:a6:ca:b7:9d
50             // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
51             // No-Action
52             // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
53             // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
54             //
55             MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(
56                             hexToByteBuffer("38 00 01 01 FF BB " //
57                                             + "00 00 00 00 00 00 00 01 00 14 2c 61 b9 c9 9a 20 " //
58                                             + "ba d8 f5 40 d3 55 6f 5f 6e 5a b2 0a bf b5 00 00 " //
59                                             + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
60                                             + "ff 00 00 05 00 01 c0 a8 88 0a"));
61
62             assertTrue(LispAuthenticationUtil.validate(mapRegister));
63     }
64
65     @Test
66     public void validate__SHA256() throws Exception {
67             // LISP(Type = 3 Map-Register, P=1, M=1
68             // Record Counter: 1
69             // Nonce: (something)
70             // Key ID: 0x0002
71             // AuthDataLength: 32 Data:
72             // 70 30 d4 c6 10 44 0d 83 be 4d bf fd a9 8c 57 6d 68 a5 bf 32 11 c9 7b 58 c4 b9 9f 06 11 23 b9 38
73             // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
74             // No-Action
75             // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
76             // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
77             //
78             MapRegister mapRegister = MapRegisterSerializer
79                             .getInstance()
80                             .deserialize(
81                                             hexToByteBuffer("38 00 01 01 FF BB " //
82                                                             + "00 00 00 00 00 00 00 02 00 20 70 30 d4 c6 10 44 0d 83 be 4d bf fd a9 8c 57 6d 68 a5 bf 32 " //
83                                                             + "11 c9 7b 58 c4 b9 9f 06 11 23 b9 38 00 00 " //
84                                                             + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
85                                                             + "ff 00 00 05 00 01 c0 a8 88 0a"));
86
87             assertTrue(LispAuthenticationUtil.validate(mapRegister));
88     }
89     
90     @Test
91     public void validate__NoAuthentication() throws Exception {
92             // LISP(Type = 3 Map-Register, P=1, M=1
93             // Record Counter: 1
94             // Nonce: (something)
95             // Key ID: 0x0000
96             // AuthDataLength: 0:
97             // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
98             // No-Action
99             // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
100             // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
101             //
102             MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(
103                             hexToByteBuffer("38 00 01 01 FF BB " //
104                                             + "00 00 00 00 00 00 00 00 00 00 " //
105                                             + "00 00 " //
106                                             + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
107                                             + "ff 00 00 05 00 01 c0 a8 88 0a"));
108
109             assertTrue(LispAuthenticationUtil.validate(mapRegister));
110     }
111     
112     @Test
113     public void authenticate__MapNotifySHA1() throws Exception {
114             MapNotify mapNotify = new MapNotify();
115             mapNotify.addEidToLocator(new EidToLocatorRecord()
116                             .setPrefix(new LispIpv4Address(1)));
117
118             mapNotify.addEidToLocator(new EidToLocatorRecord()
119                             .setPrefix(new LispIpv4Address(73)));
120             mapNotify.setNonce(6161616161L);
121             mapNotify.setKeyId((short) 0x0001);
122             byte[] wantedAuthenticationData = new byte[] { (byte) 0x66, (byte) 0x69,
123                             (byte) 0x2c, (byte) 0xb8, (byte) 0xb8, (byte) 0x58,
124                             (byte) 0x7c, (byte) 0x8f, (byte) 0x4c, (byte) 0xd4,
125                             (byte) 0x8b, (byte) 0x77, (byte) 0x46, (byte) 0xf0,
126                             (byte) 0x6b, (byte) 0x9f, (byte) 0x66, (byte) 0xd2,
127                             (byte) 0xaa, (byte) 0x2c };
128             System.out.println(HexUtils.toHexString(LispAuthenticationUtil.getAuthenticationData(mapNotify)));
129             ArrayAssert.assertEquals(wantedAuthenticationData, LispAuthenticationUtil.getAuthenticationData(mapNotify));
130
131     }
132     
133     @Test
134     public void authenticate__MapNotifySHA256() throws Exception {
135             MapNotify mapNotify = new MapNotify();
136             mapNotify.addEidToLocator(new EidToLocatorRecord()
137                             .setPrefix(new LispIpv4Address(1)));
138
139             mapNotify.addEidToLocator(new EidToLocatorRecord()
140                             .setPrefix(new LispIpv4Address(73)));
141             mapNotify.setNonce(6161616161L);
142             mapNotify.setKeyId((short) 0x0002);
143             byte[] wantedAuthenticationData = new byte[] { (byte) 0x4c, (byte) 0xf1,
144                             (byte) 0x5a, (byte) 0x4c, (byte) 0xdb, (byte) 0x8d,
145                             (byte) 0x88, (byte) 0x47, (byte) 0xf1, (byte) 0x7f,
146                             (byte) 0x27, (byte) 0x81, (byte) 0x1e, (byte) 0xbf,
147                             (byte) 0x22, (byte) 0xc7, (byte) 0xe6, (byte) 0x70,
148                             (byte) 0x16, (byte) 0x5e, (byte) 0xa1, (byte) 0x59,
149                             (byte) 0xe4, (byte) 0x06, (byte) 0x3f, (byte) 0xc2,
150                             (byte) 0x6a, (byte) 0x1c, (byte) 0x86, (byte) 0xa5,
151                             (byte) 0x8d, (byte) 0x63 };
152             System.out.println(HexUtils.toHexString(LispAuthenticationUtil.getAuthenticationData(mapNotify)));
153             ArrayAssert.assertEquals(wantedAuthenticationData, LispAuthenticationUtil.getAuthenticationData(mapNotify));
154
155     }
156
157     @Test
158     public void authenticate__MapNotifyNoAuthenticationData() throws Exception {
159             MapNotify mapNotify = new MapNotify();
160             mapNotify.setKeyId((short)0x0000);
161             mapNotify.addEidToLocator(new EidToLocatorRecord().setPrefix(
162                             new LispIpv4Address(1)).setRecordTtl(55));
163             ArrayAssert.assertEquals(new byte[0], LispAuthenticationUtil.getAuthenticationData(mapNotify));
164
165     }
166
167 }