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