import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.opendaylight.lispflowmapping.southbound.lisp.MapRegisterCacheTestUtil.DATA1;
import static org.opendaylight.lispflowmapping.southbound.lisp.MapRegisterCacheTestUtil.DATA2;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.AdditionalMatchers;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.InOrder;
+import org.mockito.Mock;
import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
import org.opendaylight.lispflowmapping.southbound.LispSouthboundPlugin;
import org.opendaylight.lispflowmapping.southbound.lisp.cache.MapRegisterCache;
import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
-import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
-import org.opendaylight.mdsal.binding.api.DataBroker;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4BinaryAfi;
import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping._record.list.MappingRecordItemBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
-import org.opendaylight.yangtools.yang.binding.Notification;
import org.opendaylight.yangtools.yang.common.Uint16;
import org.opendaylight.yangtools.yang.common.Uint32;
-public class LispSouthboundHandlerTest extends BaseTestCase {
+@RunWith(MockitoJUnitRunner.class)
+public class LispSouthboundHandlerTest {
private LispSouthboundHandler testedLispService;
private byte[] mapRequestPacket;
private MapReplyBuilder mapReplyBuilder;
private MappingRecordBuilder mappingRecordBuilder;
private MapRegisterCache mapRegisterCache;
+ @Mock
private LispSouthboundPlugin mockLispSouthboundPlugin;
- private LispSouthboundPlugin contextMockLispSouthboundPlugin;
private static final long CACHE_RECORD_TIMEOUT = 90000;
private static AuthKeyDb akdb;
Mockito.when(akdl.authKeysForEidsUnchanged(Mockito.anyMap(), Mockito.anyLong())).thenReturn(true);
}
- @Override
+
@Before
public void before() throws Exception {
- super.before();
- mockLispSouthboundPlugin = Mockito.mock(LispSouthboundPlugin.class);
- contextMockLispSouthboundPlugin = context.mock(LispSouthboundPlugin.class);
Mockito.when(mockLispSouthboundPlugin.isMapRegisterCacheEnabled()).thenReturn(true);
Mockito.when(mockLispSouthboundPlugin.getMapRegisterCacheTimeout()).thenReturn(CACHE_RECORD_TIMEOUT);
mapRegisterCache = new MapRegisterCache();
Mockito.when(mockLispSouthboundPlugin.getMapRegisterCache()).thenReturn(mapRegisterCache);
- Mockito.when(mockLispSouthboundPlugin.getDataBroker()).thenReturn(Mockito.mock(DataBroker.class));
Mockito.when(mockLispSouthboundPlugin.getAkdb()).thenReturn(akdb);
Mockito.when(mockLispSouthboundPlugin.getAuthenticationKeyDataListener()).thenReturn(akdl);
ConcurrentLispSouthboundStats lispSouthboundStats = new ConcurrentLispSouthboundStats();
mappingRecordBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("10.0.20.200/32"));
mapReplyBuilder.setNonce(0x3d8d2acd39c8d608L);
- stubHandleRequest();
-
byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
assertEquals(28, result.length);
public void mapReply_q_VerifyBasicIPv6() throws Exception {
mappingRecordBuilder.setEid(LispAddressUtil.asIpv6PrefixEid("0:0:0:0:0:0:0:1/128"));
- stubHandleRequest();
-
byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
assertEquals(40, result.length);
mappingRecordBuilder.getLocatorRecord().add(
new LocatorRecordBuilder().setRloc(LispAddressUtil.asIpv6Rloc("0:0:0:0:0:0:0:2")).build());
- stubHandleRequest();
-
byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
assertEquals(64, result.length);
@Ignore
@Test
public void mapReply__UseEncapsulatedUdpPort() throws Exception {
- stubHandleRequest();
-
assertEquals(LispMessage.PORT_NUM, handleMapRequestPacket(mapRequestPacket).recipient().getPort());
}
mappingRecordBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("10.0.20.200/32"));
mappingRecordBuilder.getLocatorRecord().add(
new LocatorRecordBuilder().setRouted(false).setRloc(LispAddressUtil.asIpv4Rloc("4.3.2.1")).build());
- stubHandleRequest();
byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
assertEquals(0x00, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
mappingRecordBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("10.0.20.200/32"));
mappingRecordBuilder.getLocatorRecord().add(
new LocatorRecordBuilder().setRouted(true).setRloc(LispAddressUtil.asIpv4Rloc("4.3.2.1")).build());
- stubHandleRequest();
byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
assertEquals(40, result.length);
mappingRecordBuilder.getLocatorRecord().add(
new LocatorRecordBuilder().setRouted(true).setRloc(LispAddressUtil.asIpv6Rloc("0:0:0:0:0:0:0:1"))
.build());
- stubHandleRequest();
byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
assertEquals(64, result.length);
Mockito.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(Mockito.any(RequestMapping.class));
}
- private void stubHandleRequest() {
- try {
- allowing(contextMockLispSouthboundPlugin).sendNotificationIfPossible(wany(Notification.class));
- } catch (InterruptedException e) {
- LOG.debug("Interrupted", e);
- }
- }
-
private byte[] handleMapRequestAsByteArray(byte[] inPacket) {
handleMapRequestPacket(inPacket);
return lastMapReplyPacket().content().array();
return null;
}
- private byte[] extractWSUdpByteArray(String wiresharkHex) {
+ private static byte[] extractWSUdpByteArray(String wiresharkHex) {
final int headerLen = 42;
byte[] res = new byte[1000];
String[] split = wiresharkHex.split(" ");
return Arrays.copyOf(res, counter - headerLen);
}
+ private static ByteBuffer hexToByteBuffer(String hex) {
+ String[] hexBytes = hex.split(" ");
+ ByteBuffer bb = ByteBuffer.allocate(hexBytes.length);
+ for (String hexByte : hexBytes) {
+ bb.put((byte) Integer.parseInt(hexByte, 16));
+ }
+ bb.clear();
+ return bb;
+ }
+
+ private static void assertHexEquals(byte expected, byte actual) {
+ assertEquals(String.format("0x%02X", expected), String.format("0x%02X", actual));
+ }
+
@Test(expected = LispMalformedPacketException.class)
public void mapRequest__NoIpItrRloc() throws Exception {
mapRequestPacket = hexToByteBuffer("10 00 "