Merge "Moving authentication to southbound"
[lispflowmapping.git] / mappingservice / southbound / src / test / java / org / opendaylight / lispflowmapping / southbound / lisp / LispXtrSouthboundHandlerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, 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.southbound.lisp;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import com.google.common.collect.Lists;
14 import io.netty.buffer.Unpooled;
15 import io.netty.channel.ChannelHandlerContext;
16 import io.netty.channel.socket.DatagramPacket;
17 import java.net.InetSocketAddress;
18 import java.nio.ByteBuffer;
19 import java.util.Arrays;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
28 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
29 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4BinaryAfi;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address
34         .address.Ipv4BinaryBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRloc;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRlocBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.SourceEidBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequest;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class LispXtrSouthboundHandlerTest {
48
49     @Mock(name = "notificationPublishService") private static NotificationPublishService notificationPublishServiceMock;
50     @InjectMocks private static LispXtrSouthboundHandler handler;
51
52     private static final String IPV4_STRING_1 =      "1.2.3.4";
53     private static final String IPV4_STRING_2 =      "127.0.0.1";
54     private static final String IPV4_STRING_PREFIX = "/32";
55
56     private static final long NONCE = 4435248268955932168L;
57     private static final int HEADER_LENGTH = 74;
58     private static final int LISP_MAP_REQUEST_PACKET_LENGTH = 32;
59     private static final int LISP_MAP_REPLY_PACKET_LENGTH = 40;
60     private static final int PORT = 9999;
61
62     /**
63      * SRC: 127.0.0.1:58560 to 127.0.0.1:4342
64      * LISP(Type = 8 - Encapsulated)
65      * IP: 192.168.136.10 -> 153.16.254.1
66      * UDP: 56756
67      * LISP Type = Map-Request (1)
68      * ITR-RLOC count: 0
69      * Record Count: 1
70      * Nonce: 0x3d8d2acd39c8d608
71      * Source EID AFI: 1
72      * Source EID 1.2.3.4
73      * ITR-RLOC AFI=1 Address=192.168.136.10
74      * Record 1: 127.0.0.1/32
75      */
76     private static final String MAP_REQUEST_PACKET_STRING =
77             "0000   00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " +
78             "0010   00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 " +
79             "0020   00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 " +
80             "0030   00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 " +
81             "0040   fe 01 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d " +
82             "0050   2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 7f 00 " +
83             "0060   00 02 00 20 00 01 7f 00 00 01 ac 4a 06 7d";
84
85     private static final String MAP_REQUEST_PACKET_STRING_MALFORMED =
86             "0000   00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " +
87             "0010   00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 " +
88             "0020   00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 " +
89             "0030   00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 " +
90             "0040   fe 01 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d " +
91             "0050   2a cd 39 c8 d6 08 00 01 01 02 03 04 00 00 00 00 " +
92             "0060   00 00 00 20 00 01 7f 00 00 01 ac 4a 06 7d";
93
94     /**
95      * SRC: 127.0.0.1:58560 to 127.0.0.1:4342
96      * LISP(Type = 8 - Encapsulated)
97      * IP: 192.168.136.10 -> 153.16.254.1
98      * UDP: 56756
99      * LISP Type = Map-Reply (2)
100      * Record Count: 1
101      * Nonce: 0x3d8d2acd39c8d608
102      * Source EID AFI: 1
103      * Source EID Mask Length: 32
104      * Source EID 1.2.3.4
105      * Record TTL: 32-bit Max value
106      * Locator Count: 1
107      * Locator Record 1: 127.0.0.1/32
108      */
109     private static final String MAP_REPLY_PACKET_STRING =
110             "0000   00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " +
111             "0010   00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 " +
112             "0020   00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 " +
113             "0030   00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 " +
114             "0040   fe 01 dd b4 10 f6 00 24 ef 3a 28 00 00 01 3d 8d " +
115             "0050   2a cd 39 c8 d6 08 ff ff ff ff 01 20 10 00 00 00 " +
116             "0060   00 01 01 02 03 04 00 00 00 00 00 00 00 01 fe fe " +
117             "0070   fe fe 0d e3 70 40";
118
119     /**
120      * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request.
121      *
122      * @throws InterruptedException
123      */
124     @Test
125     public void handlePacketTest_withMapRequest() throws InterruptedException {
126         final ArgumentCaptor<XtrRequestMapping> captor = ArgumentCaptor.forClass(XtrRequestMapping.class);
127
128         // expected result
129         final MapRequest expectedRequest = getDefaultMapRequestBuilder().build();
130
131         handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING, HEADER_LENGTH,
132                 LISP_MAP_REQUEST_PACKET_LENGTH));
133         Mockito.verify(notificationPublishServiceMock).putNotification(captor.capture());
134
135         assertEquals(expectedRequest, captor.getValue().getMapRequest());
136     }
137
138     /**
139      * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request, null NotificationPublishService.
140      *
141      * @throws InterruptedException
142      */
143     @Test
144     public void handlePacketTest_withMapRequest_withNullNotifPublishService() throws InterruptedException {
145         final LispXtrSouthboundHandler handler = new LispXtrSouthboundHandler();
146         handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING, HEADER_LENGTH,
147                 LISP_MAP_REQUEST_PACKET_LENGTH));
148         Mockito.verifyZeroInteractions(notificationPublishServiceMock);
149     }
150
151     /**
152      * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request, no Itr Rlocs.
153      *
154      * @throws InterruptedException
155      */
156     @Test(expected = LispMalformedPacketException.class)
157     public void handlePacketTest__withMapRequest_withNoItrRloc() throws InterruptedException {
158         handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING_MALFORMED, HEADER_LENGTH,
159                 LISP_MAP_REQUEST_PACKET_LENGTH));
160     }
161
162     /**
163      * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Reply.
164      *
165      * @throws InterruptedException
166      */
167     @Test
168     public void handlePacketTest_withMapReply() throws InterruptedException {
169         ArgumentCaptor<XtrReplyMapping> captor = ArgumentCaptor.forClass(XtrReplyMapping.class);
170         handler.handlePacket(extractLispPacket(MAP_REPLY_PACKET_STRING, HEADER_LENGTH,
171                 LISP_MAP_REPLY_PACKET_LENGTH));
172
173         Mockito.verify(notificationPublishServiceMock).putNotification(captor.capture());
174         assertNotNull(captor.getValue().getMapReply());
175     }
176
177     /**
178      * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Reply over channelRead0 method.
179      *
180      * @throws Exception
181      */
182     @Test
183     public void handlePacketTest_withMapReply_withNullNotifPublishService() throws Exception {
184         final LispXtrSouthboundHandler handler = new LispXtrSouthboundHandler();
185         handler.channelRead0(Mockito.mock(ChannelHandlerContext.class),
186                 extractLispPacket(MAP_REPLY_PACKET_STRING, HEADER_LENGTH, LISP_MAP_REPLY_PACKET_LENGTH));
187
188         Mockito.verifyZeroInteractions(notificationPublishServiceMock);
189     }
190
191     /**
192      * Tests {@link LispXtrSouthboundHandler#channelReadComplete} method.
193      *
194      * @throws Exception
195      */
196     @Test
197     public void channelReadCompleteTest() throws Exception {
198         ChannelHandlerContext ctxMock = Mockito.mock(ChannelHandlerContext.class);
199         handler.channelReadComplete(ctxMock);
200
201         Mockito.verify(ctxMock).flush();
202     }
203
204     /**
205      * Following test is executed for coverage-increase purpose only.
206      */
207     @Test
208     public void otherTest() throws Exception {
209
210         // This map-notification packet is not valid! Don't use it anywhere else.
211         String mapNotificationPacket =
212                 "0000   00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " +
213                 "0010   00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 " +
214                 "0020   00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 " +
215                 "0030   00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 " +
216                 "0040   fe 01 dd b4 10 f6 00 24 ef 3a 40 00 00 01 3d 8d " +
217                 "0050   2a cd 39 c8 d6 08 ff ff ff ff 01 20 10 00 00 00 " +
218                 "0060   00 01 01 02 03 04 00 00 00 00 00 00 00 01 fe fe " +
219                 "0070   fe fe 0d e3 70 40";
220
221         handler.exceptionCaught(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Throwable.class));
222         handler.setNotificationProvider(notificationPublishServiceMock);
223         handler.handlePacket(extractLispPacket(mapNotificationPacket, HEADER_LENGTH, LISP_MAP_REPLY_PACKET_LENGTH));
224     }
225
226     private static DatagramPacket extractLispPacket(String packetString, int headerLength, int lispPacketLength) {
227         final String[] tokens = packetString.split("\\s+");
228         ByteBuffer buffer = ByteBuffer.allocate(tokens.length);
229
230         for (String token : tokens) {
231             if (token.length() == 2) {
232                 buffer.put((byte) Integer.parseInt(token, 16));
233             }
234         }
235
236         byte[] result = Arrays.copyOfRange(buffer.array(),
237                 headerLength, headerLength + lispPacketLength);
238         final InetSocketAddress address = new InetSocketAddress(PORT);
239         return new DatagramPacket(Unpooled.copiedBuffer(result), address);
240     }
241
242     private static MapRequestBuilder getDefaultMapRequestBuilder() {
243         final ItrRloc itrRloc = new ItrRlocBuilder()
244                 .setRloc(new RlocBuilder()
245                         .setAddressType(Ipv4BinaryAfi.class)
246                         .setAddress(new Ipv4BinaryBuilder()
247                                 .setIpv4Binary(new Ipv4AddressBinary(new byte[]{127, 0, 0, 2})).build())
248                         .build())
249                 .build();
250
251         final EidItem eidItem = new EidItemBuilder()
252                 .setEid(LispAddressUtil.toEid(new Ipv4Prefix(IPV4_STRING_2 + IPV4_STRING_PREFIX), null)).build();
253
254         return new MapRequestBuilder()
255                 .setItrRloc(Lists.newArrayList(itrRloc))
256                 .setEidItem(Lists.newArrayList(eidItem))
257                 .setNonce(NONCE)
258                 .setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.asIpv4Eid(IPV4_STRING_1)).build())
259                 .setAuthoritative(false)
260                 .setMapDataPresent(false)
261                 .setPitr(false)
262                 .setProbe(false)
263                 .setSmr(false)
264                 .setSmrInvoked(false);
265     }
266 }