Authentication to southbound
[lispflowmapping.git] / mappingservice / southbound / src / test / java / org / opendaylight / lispflowmapping / southbound / lisp / LispSouthboundServiceTest.java
index 3db37c21ea34d0819e836e1c4cddbb3a64f9ffce..bcf7c42d61773320dd4e214947bd8403f80b9928 100644 (file)
@@ -13,7 +13,6 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
-import static org.opendaylight.lispflowmapping.southbound.lisp.MapRegisterCacheTestUtil.authenticationData;
 import static org.opendaylight.lispflowmapping.southbound.lisp.MapRegisterCacheTestUtil.data1;
 import static org.opendaylight.lispflowmapping.southbound.lisp.MapRegisterCacheTestUtil.data2;
 import static org.opendaylight.lispflowmapping.southbound.lisp.MapRegisterCacheTestUtil.data3;
@@ -36,11 +35,14 @@ import junitx.framework.ArrayAssert;
 import org.apache.commons.lang3.ArrayUtils;
 import org.jmock.api.Invocation;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.mockito.AdditionalMatchers;
 import org.mockito.InOrder;
+import org.mockito.Matchers;
 import org.mockito.Mockito;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
@@ -49,11 +51,13 @@ import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
 import org.opendaylight.lispflowmapping.lisp.util.MapNotifyBuilderHelper;
 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
+import org.opendaylight.lispflowmapping.mapcache.SimpleMapCache;
 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.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.binary.address.types.rev160504.Ipv4PrefixBinaryAfi;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv6PrefixBinaryAfi;
@@ -78,6 +82,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.ma
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
 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.mapreplymessage.MapReplyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkeyBuilder;
 import org.opendaylight.yangtools.yang.binding.Notification;
 
 public class LispSouthboundServiceTest extends BaseTestCase {
@@ -96,6 +101,7 @@ public class LispSouthboundServiceTest extends BaseTestCase {
     private LispSouthboundPlugin mockLispSouthboundPlugin;
     private static final long CACHE_RECORD_TIMEOUT = 90000;
 
+    private static SimpleMapCache smc;
 
     private interface MapReplyIpv4SingleLocatorPos {
         int RECORD_COUNT = 3;
@@ -116,15 +122,41 @@ public class LispSouthboundServiceTest extends BaseTestCase {
         int LOCATOR = MapReplyIpv4SingleLocatorPos.LOCATOR + FIRST_LOCATOR_IPV4_LENGTH;
     }
 
+    @BeforeClass
+    public static void initTests() {
+        smc = Mockito.mock(SimpleMapCache.class);
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asIpv4PrefixBinaryEid("10.10.10.10/8"))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asIpv6PrefixBinaryEid
+                ("2610:d0:ffff:192:0:0:0:1/128"))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asIpv4PrefixBinaryEid("153.16.254.1/32"))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asIpv4PrefixBinaryEid("125.124.123.122/8", new
+                InstanceIdType(21L)))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asMacEid("0a:0b:0c:0d:0e:0f"))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asIpv6PrefixBinaryEid
+                ("f0f:f0f:f0f:f0f:f0f:f0f:f0f:f0f/8"))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asIpv4PrefixBinaryEid("172.1.1.2/32"))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+    }
+
     @Override
     @Before
     public void before() throws Exception {
         super.before();
         // mapResolver = context.mock(IMapResolver.class);
         // mapServer = context.mock(IMapServer.class);
-        mapRegisterCache = new MapRegisterCache();
         mockLispSouthboundPlugin = mock(LispSouthboundPlugin.class);
-        testedLispService = new LispSouthboundHandler(mockLispSouthboundPlugin, mapRegisterCache);
+        testedLispService = new LispSouthboundHandler(mockLispSouthboundPlugin);
+        mapRegisterCache = new MapRegisterCache();
+        testedLispService.setMapRegisterCache(mapRegisterCache);
+        testedLispService.setDataBroker(Mockito.mock(DataBroker.class));
+        testedLispService.setSimpleMapCache(smc);
+        testedLispService.init();
         nps = context.mock(NotificationPublishService.class);
         testedLispService.setNotificationProvider(nps);
         lispNotificationSaver = new ValueSaverAction<Notification>();
@@ -413,6 +445,10 @@ public class LispSouthboundServiceTest extends BaseTestCase {
     }
 
     void mapRegisterInvocationForCacheTest(byte[] eidPrefixAfi, byte[] eidPrefix) {
+        mapRegisterInvocationForCacheTest(eidPrefixAfi, eidPrefix, MapRegisterCacheTestUtil.authenticationData);
+    }
+
+    void mapRegisterInvocationForCacheTest(byte[] eidPrefixAfi, byte[] eidPrefix, byte[] authenticationData) {
         final byte[] mapRegisterMessage = MapRegisterCacheTestUtil.joinArrays(data1, nonce, keyId,
                 authenticationData, data2, eidPrefixAfi, eidPrefix, data3, xTRId, siteId);
         handlePacket(mapRegisterMessage);
@@ -430,7 +466,8 @@ public class LispSouthboundServiceTest extends BaseTestCase {
         byte[] eidPrefix = new byte[] {
                 0x0a, 0x0a, 0x0a, 0x0a     //ipv4 address
         };
-        cacheTest(eidPrefixAfi, eidPrefix);
+
+        cacheTest(eidPrefixAfi, eidPrefix, MapRegisterCacheTestUtil.authenticationData);
     }
 
     /**
@@ -448,7 +485,17 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 ,0x0f, 0x0f, 0x0f, 0x0f     //ipv6 address
                 ,0x0f, 0x0f, 0x0f, 0x0f     //ipv6 address
         };
-        cacheTest(eidPrefixAfi, eidPrefix);
+
+        byte[] authenticationData = new byte[]{
+                0x00, 0x14
+                ,0x41,(byte)0x83,0x13,0x7C
+                ,0x48,(byte)0xEE,0x75,(byte)0x9A
+                ,0x4,(byte)0x8C,0x46,(byte)0xA6
+                ,0x1B,0x13,(byte)0xC8,0x4D
+                ,(byte)0xA1,0x17,0x53,(byte)0xC3
+        };
+
+        cacheTest(eidPrefixAfi, eidPrefix, authenticationData);
     }
 
     /**
@@ -464,7 +511,15 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 0x0a, 0x0b, 0x0c, 0x0d     //mac address
                 ,0x0e, 0x0f                 //mac address
         };
-        cacheTest(eidPrefixAfi, eidPrefix);
+        byte[] authenticationData = new byte[]{
+                0x00, 0x14
+                ,(byte)0xB2,(byte)0x8E,0x6,(byte)0x9D
+                ,0x61,(byte)0xD8,0xC,0x24
+                ,(byte)0x80,0x61,0x5A,0x20
+                ,0xD,0x50,0x5E,(byte)0xAE
+                ,0x47,(byte)0xF7,(byte)0x86,0x36
+        };
+        cacheTest(eidPrefixAfi, eidPrefix, authenticationData);
     }
 
     /**
@@ -489,7 +544,18 @@ public class LispSouthboundServiceTest extends BaseTestCase {
 
         byte[] eidPrefix = joinArrays(lcafRsvd1, lcafFlags, lcafType, lcafIIDMaskLength, lcafLength, lcafInstanceId,
                 lcafAfi, lcafAddress);
-        cacheTest(eidPrefixAfi, eidPrefix);
+
+        byte[] authenticationData = new byte[]{
+                0x00, 0x14                 //authentication data length
+                ,0x68, 0x1d, (byte) 0x9e, 0x6e    //auth data
+                ,0x5e, 0x32, (byte) 0x88, 0x1a    //auth data
+                ,(byte) 0xae, 0x6b, (byte) 0xe3, 0x40    //auth data
+                ,0x30, (byte) 0x0b, (byte) 0xb6, (byte) 0xa0    //auth data
+                ,0x71, (byte) 0xf4, (byte) 0x8c, 0x5f    //auth data
+        };
+
+
+        cacheTest(eidPrefixAfi, eidPrefix, authenticationData);
     }
 
     /**
@@ -497,14 +563,15 @@ public class LispSouthboundServiceTest extends BaseTestCase {
      * @param eidPrefixAfi
      * @param eidPrefix
      */
-    public void cacheTest(byte[] eidPrefixAfi, byte[] eidPrefix) throws InterruptedException {
+    public void cacheTest(byte[] eidPrefixAfi, byte[] eidPrefix, byte[] authenticationData) throws
+            InterruptedException {
         final MapRegisterCacheKey mapRegisterCacheKey = MapRegisterCacheTestUtil.createMapRegisterCacheKey(eidPrefix);
 
         final NotificationPublishService mockedNotificationProvider = mock(NotificationPublishService.class);
         testedLispService.setNotificationProvider(mockedNotificationProvider);
 
         MapRegisterCacheTestUtil.beforeMapRegisterInvocationValidation(mapRegisterCacheKey, mapRegisterCache);
-        mapRegisterInvocationForCacheTest(eidPrefixAfi, eidPrefix);
+        mapRegisterInvocationForCacheTest(eidPrefixAfi, eidPrefix, authenticationData);
         MapRegisterCacheTestUtil.afterMapRegisterInvocationValidation(mockedNotificationProvider,
                 mapRegisterCacheKey, mapRegisterCache, eidPrefixAfi, eidPrefix);
     }
@@ -528,15 +595,14 @@ public class LispSouthboundServiceTest extends BaseTestCase {
         cacheRecordExpirationTest(false);
     }
 
-    private void cacheRecordExpirationTest(boolean cacheRecordTimeouted) {
-        mapRegisterCache = mock(MapRegisterCache.class);
-        testedLispService = new LispSouthboundHandler(mockLispSouthboundPlugin, mapRegisterCache);
+    private void cacheRecordExpirationTest(boolean cacheRecordTimeouted) throws InterruptedException {
+        mapRegisterCache = Mockito.mock(MapRegisterCache.class);
+        testedLispService.setMapRegisterCache(mapRegisterCache);
+        testedLispService.setNotificationProvider(Mockito.mock(NotificationPublishService.class));
 
         byte[] eidPrefixAfi = new byte[] {0x00, 0x01};
-
         byte[] eidPrefix = new byte[] {0x0a, 0x0a, 0x0a, 0x0a};
 
-
         MapRegisterCacheKeyBuilder cacheKeyBld = new MapRegisterCacheKeyBuilder();
         MapRegisterCacheValueBuilder cacheValueBld = new MapRegisterCacheValueBuilder();
         cacheKeyBld.setXtrId(xTRId);