changeset 1027:1e0034fb17dd

pcap emitter tests in progress
author Devel 1
date Wed, 01 Apr 2020 15:41:14 +0200
parents 1921cac1f89e
children 056fa28115ff
files stress-tester/src/main/java/com/passus/st/emitter/SessionInfo.java stress-tester/src/main/java/com/passus/st/emitter/pcap/UnidirectionalPcapWorker.java stress-tester/src/test/java/com/passus/st/emitter/pcap/UnidirectionalUdpPcapEmitterTest.java
diffstat 3 files changed, 64 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/emitter/SessionInfo.java	Wed Apr 01 11:04:20 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/SessionInfo.java	Wed Apr 01 15:41:14 2020 +0200
@@ -58,6 +58,10 @@
         this(srcIp, srcPort, dstIp, dstPort, DEFAULT_TRANSPORT, UNKNOWN);
     }
 
+    public SessionInfo(String srcIp, int srcPort, String dstIp, int dstPort, int transport) {
+        this(srcIp, srcPort, dstIp, dstPort, transport, UNKNOWN);
+    }
+
     public SessionInfo(String srcIp, int srcPort, String dstIp, int dstPort, int transport, int protocolId) {
         this(IpAddress.parse(srcIp), srcPort, IpAddress.parse(dstIp), dstPort, transport, protocolId);
     }
--- a/stress-tester/src/main/java/com/passus/st/emitter/pcap/UnidirectionalPcapWorker.java	Wed Apr 01 11:04:20 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/pcap/UnidirectionalPcapWorker.java	Wed Apr 01 15:41:14 2020 +0200
@@ -36,6 +36,8 @@
 
 class UnidirectionalPcapWorker extends Thread implements MetricSource {
 
+    protected static final byte[] ZERO_MAC = {0, 0, 0, 0, 0, 0};
+
     protected final Logger LOGGER = LogManager.getLogger(UnidirectionalPcapWorker.class);
 
     boolean working = true;
@@ -202,7 +204,11 @@
                 networkInterface = findInterface(localAddress.getIp());
             }
 
-            localMac = new MACAddress(networkInterface.getHardwareAddress());
+            byte[] localHwAddress = networkInterface.getHardwareAddress();
+            if (localHwAddress == null) {
+                localHwAddress = ZERO_MAC;
+            }
+            localMac = new MACAddress(localHwAddress);
             device = getPcapDeviceName(networkInterface);
         } catch (IOException ex) {
             doCatchException(channelContext, ex);
--- a/stress-tester/src/test/java/com/passus/st/emitter/pcap/UnidirectionalUdpPcapEmitterTest.java	Wed Apr 01 11:04:20 2020 +0200
+++ b/stress-tester/src/test/java/com/passus/st/emitter/pcap/UnidirectionalUdpPcapEmitterTest.java	Wed Apr 01 15:41:14 2020 +0200
@@ -1,7 +1,57 @@
 package com.passus.st.emitter.pcap;
 
-import static org.testng.Assert.*;
+import com.passus.net.session.Session;
+import com.passus.st.AbstractWireMockTest;
+import com.passus.st.client.TestClientHandler;
+import com.passus.st.emitter.AbstractEmitterTest;
+import com.passus.st.emitter.ChannelContext;
+import com.passus.st.emitter.MapBasedMACAddressResolver;
+import com.passus.st.emitter.SessionInfo;
+import com.passus.st.emitter.SessionMapper;
+import org.testng.AssertJUnit;
+import org.testng.annotations.Test;
 
-public class UnidirectionalUdpPcapEmitterTest {
+public class UnidirectionalUdpPcapEmitterTest extends AbstractWireMockTest {
 
-}
\ No newline at end of file
+    public UnidirectionalUdpPcapEmitter createEmitter(SessionMapper mapper) throws Exception {
+
+        MapBasedMACAddressResolver macResolver = new MapBasedMACAddressResolver();
+        macResolver.add("* -> 00:00:00:00:00:00");
+
+        UnidirectionalUdpPcapEmitter emitter = new UnidirectionalUdpPcapEmitter();
+        emitter.setWorkersNum(1);
+        emitter.setMacResolver(macResolver);
+        if (mapper != null) {
+            emitter.setSessionMapper(mapper);
+        }
+        return emitter;
+    }
+
+    @Test
+    public void testConnectAndClose() throws Exception {
+        UnidirectionalUdpPcapEmitter emitter = createEmitter(null);
+        try {
+            emitter.start();
+            SessionInfo info = new SessionInfo("1.1.1.1", 5000, HOST, port(), Session.PROTOCOL_UDP);
+
+            TestClientHandler handler = new TestClientHandler() {
+                @Override
+                protected void doChannelActive(ChannelContext context) throws Exception {
+                    context.close();
+                }
+            };
+
+            emitter.connect(info, handler, 0);
+            AbstractEmitterTest.waitConn(handler, 4);
+
+            AssertJUnit.assertEquals(4, handler.size());
+            AssertJUnit.assertEquals(TestClientHandler.EventType.CHANNEL_REGISTERED, handler.get(0).getType());
+            AssertJUnit.assertEquals(TestClientHandler.EventType.CHANNEL_ACTIVE, handler.get(1).getType());
+            AssertJUnit.assertEquals(TestClientHandler.EventType.CHANNEL_INACTIVE, handler.get(2).getType());
+            AssertJUnit.assertEquals(TestClientHandler.EventType.CHANNEL_UNREGISTERED, handler.get(3).getType());
+        } finally {
+            emitter.stop();
+        }
+    }
+
+}