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