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