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