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