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