204c1442119a388c8c6b656a98d84d3d106dc235
[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
6 import java.util.ArrayList;
7
8 import junitx.framework.ArrayAssert;
9
10 import org.junit.Ignore;
11 import org.junit.Test;
12 import org.opendaylight.lispflowmapping.implementation.serializer.MapRegisterSerializer;
13 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
14 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
15 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
16 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
17 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
18 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecordBuilder;
19 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4;
20 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4Builder;
21 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecord;
22 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapnotifymessage.MapNotifyBuilder;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
24
25 public class LispAuthenticationTest extends BaseTestCase {
26
27     @Test
28     public void validate_WrongAuthentication() throws Exception {
29         // LISP(Type = 3 Map-Register, P=1, M=1
30         // Record Counter: 1
31         // Nonce: (something)
32         // Key ID: 0x0001
33         // AuthDataLength: 20 Data:
34         // e8:f5:0b:c5:c5:f2:b0:21:27:a8:21:41:04:f3:46:5a:5a:5b:5c:5d
35         // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
36         // No-Action
37         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
38         // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
39         //
40         MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB " //
41                 + "00 00 00 00 00 00 00 01 00 14 e8 f5 0b c5 c5 f2 " //
42                 + "b0 21 27 a8 21 41 04 f3 46 5a 5a 5b 5c 5d 00 00 " //
43                 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
44                 + "ff 00 00 05 00 01 c0 a8 88 0a"));
45
46         assertFalse(LispAuthenticationUtil.validate(mapRegister, "password"));
47     }
48
49     @Test
50     public void validate__SHA1() throws Exception {
51         // LISP(Type = 3 Map-Register, P=1, M=1
52         // Record Counter: 1
53         // Nonce: (something)
54         // Key ID: 0x0001
55         // AuthDataLength: 20 Data:
56         // b2:dd:1a:25:c0:60:b1:46:e8:dc:6d:a6:ae:2e:92:92:a6:ca:b7:9d
57         // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
58         // No-Action
59         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
60         // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
61         //
62         MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB " //
63                 + "00 00 00 00 00 00 00 01 00 14 2c 61 b9 c9 9a 20 ba d8 f5 40 d3 55 6f 5f 6e 5a b2 0a bf b5 00 00 " //
64                 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
65                 + "ff 00 00 05 00 01 c0 a8 88 0a"));
66
67         assertTrue(LispAuthenticationUtil.validate(mapRegister, "password"));
68         assertFalse(LispAuthenticationUtil.validate(mapRegister, "wrongPassword"));
69     }
70
71     @Test
72     public void validate__SHA256() throws Exception {
73         // LISP(Type = 3 Map-Register, P=1, M=1
74         // Record Counter: 1
75         // Nonce: (something)
76         // Key ID: 0x0002
77         // AuthDataLength: 32 Data:
78         // 70 30 d4 c6 10 44 0d 83 be 4d bf fd a9 8c 57 6d 68 a5 bf 32 11 c9 7b
79         // 58 c4 b9 9f 06 11 23 b9 38
80         // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
81         // No-Action
82         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
83         // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
84         //
85         MapRegister mapRegister = MapRegisterSerializer
86                 .getInstance()
87                 .deserialize(
88                         hexToByteBuffer("38 00 01 01 FF BB " //
89                                 + "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 11 c9 7b 58 c4 b9 9f 06 11 23 b9 38 00 00 " //
90                                 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
91                                 + "ff 00 00 05 00 01 c0 a8 88 0a"));
92
93         assertTrue(LispAuthenticationUtil.validate(mapRegister, "password"));
94         assertFalse(LispAuthenticationUtil.validate(mapRegister, "wrongPassword"));
95     }
96
97     @Test
98     public void validate__NoAuthentication() throws Exception {
99         // LISP(Type = 3 Map-Register, P=1, M=1
100         // Record Counter: 1
101         // Nonce: (something)
102         // Key ID: 0x0000
103         // AuthDataLength: 0:
104         // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
105         // No-Action
106         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
107         // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
108         //
109         MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB " //
110                 + "00 00 00 00 00 00 00 00 00 00 " //
111                 + "00 00 " //
112                 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
113                 + "ff 00 00 05 00 01 c0 a8 88 0a"));
114
115         assertTrue(LispAuthenticationUtil.validate(mapRegister, "password"));
116         assertTrue(LispAuthenticationUtil.validate(mapRegister, "wrongPassword"));
117     }
118
119     // @Test
120     // public void authenticate__MapNotifySHA1() throws Exception {
121     // MapNotify mapNotify = new MapNotify();
122     // mapNotify.addEidToLocator(new EidToLocatorRecord().setPrefix(new
123     // LispIpv4Address(1)));
124     //
125     // mapNotify.addEidToLocator(new EidToLocatorRecord().setPrefix(new
126     // LispIpv4Address(73)));
127     // mapNotify.setNonce(6161616161L);
128     // mapNotify.setKeyId((short) 0x0001);
129     // byte[] wantedAuthenticationData = new byte[] { (byte) 0x66, (byte) 0x69,
130     // (byte) 0x2c, (byte) 0xb8, (byte) 0xb8, (byte) 0x58, (byte) 0x7c,
131     // (byte) 0x8f, (byte) 0x4c, (byte) 0xd4, (byte) 0x8b, (byte) 0x77, (byte)
132     // 0x46, (byte) 0xf0, (byte) 0x6b, (byte) 0x9f, (byte) 0x66,
133     // (byte) 0xd2, (byte) 0xaa, (byte) 0x2c };
134     // ArrayAssert.assertEquals(wantedAuthenticationData,
135     // LispAuthenticationUtil.createAuthenticationData(mapNotify, "password"));
136     //
137     // }
138     //
139     // @Test
140     // public void authenticate__MapNotifySHA256() throws Exception {
141     // MapNotify mapNotify = new MapNotify();
142     // mapNotify.addEidToLocator(new EidToLocatorRecord().setPrefix(new
143     // LispIpv4Address(1)));
144     //
145     // mapNotify.addEidToLocator(new EidToLocatorRecord().setPrefix(new
146     // LispIpv4Address(73)));
147     // mapNotify.setNonce(6161616161L);
148     // mapNotify.setKeyId((short) 0x0002);
149     // byte[] wantedAuthenticationData = new byte[] { (byte) 0x4c, (byte) 0xf1,
150     // (byte) 0x5a, (byte) 0x4c, (byte) 0xdb, (byte) 0x8d, (byte) 0x88,
151     // (byte) 0x47, (byte) 0xf1, (byte) 0x7f, (byte) 0x27, (byte) 0x81, (byte)
152     // 0x1e, (byte) 0xbf, (byte) 0x22, (byte) 0xc7, (byte) 0xe6,
153     // (byte) 0x70, (byte) 0x16, (byte) 0x5e, (byte) 0xa1, (byte) 0x59, (byte)
154     // 0xe4, (byte) 0x06, (byte) 0x3f, (byte) 0xc2, (byte) 0x6a,
155     // (byte) 0x1c, (byte) 0x86, (byte) 0xa5, (byte) 0x8d, (byte) 0x63 };
156     // ArrayAssert.assertEquals(wantedAuthenticationData,
157     // LispAuthenticationUtil.createAuthenticationData(mapNotify, "password"));
158     //
159     // }
160
161     @Test
162     public void authenticate__MapNotifyNoAuthenticationData() throws Exception {
163         MapNotifyBuilder mapNotifyBuilder = new MapNotifyBuilder();
164         mapNotifyBuilder.setKeyId((short) 0x0000);
165         mapNotifyBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
166         EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
167         etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
168         etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress("1.1.1.1")));
169         etlrBuilder.setRecordTtl(55);
170         mapNotifyBuilder.getEidToLocatorRecord().add(etlrBuilder.build());
171         ArrayAssert.assertEquals(new byte[0], LispAuthenticationUtil.createAuthenticationData(mapNotifyBuilder.build(), "password"));
172
173     }
174
175     private Ipv4 asIPAfiAddress(String ip) {
176         return new Ipv4Builder().setIpv4Address(new Ipv4Address(ip)).setAfi((short) AddressFamilyNumberEnum.IP.getIanaCode()).build();
177     }
178 }