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