changeset 1193:a197230b7219

PcapSessionAnalyzerHook.onLoopEnd .onDataEnd, PcapHttpListener.onSessionClosed bugfix, PcapDnsListener bugfix
author Devel 2
date Fri, 19 Jun 2020 09:29:24 +0200
parents b66616cb2543
children 50ea8e0c97fe
files stress-tester/src/main/java/com/passus/st/emitter/SessionInfo.java stress-tester/src/main/java/com/passus/st/source/BaseSessionAnalyzerListener.java stress-tester/src/main/java/com/passus/st/source/PcapDnsListener.java stress-tester/src/main/java/com/passus/st/source/PcapHttpListener.java stress-tester/src/main/java/com/passus/st/source/PcapHttpSessionAnalyzerHook.java stress-tester/src/main/java/com/passus/st/source/PcapSessionAnalyzerHook.java stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java stress-tester/src/test/java/com/passus/st/source/PcapSessionEventSourceTest.java stress-tester/src/test/resources/pcap/http/http_req_rst.pcap
diffstat 9 files changed, 100 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/emitter/SessionInfo.java	Thu Jun 18 18:12:01 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/SessionInfo.java	Fri Jun 19 09:29:24 2020 +0200
@@ -4,6 +4,7 @@
 import com.passus.net.IpAddress;
 import com.passus.net.SocketAddress;
 import com.passus.net.session.Session;
+import com.passus.net.session.SessionContext;
 import org.apache.commons.lang3.StringUtils;
 
 import java.text.ParseException;
@@ -203,4 +204,11 @@
         }
     }
 
+    public static SessionInfo create(SessionContext context, int protocolId) {
+        return new SessionInfo(
+                context.getSrcIpAddr(), context.getSrcPort(),
+                context.getDstIpAddr(), context.getDstPort(),
+                context.getProtocol(), protocolId, context.getId()
+        );
+    }
 }
--- a/stress-tester/src/main/java/com/passus/st/source/BaseSessionAnalyzerListener.java	Thu Jun 18 18:12:01 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/source/BaseSessionAnalyzerListener.java	Fri Jun 19 09:29:24 2020 +0200
@@ -33,14 +33,10 @@
         this.protocolId = protocolId;
     }
 
-    protected void firePayloadEvent(Object req, Object resp, SessionContext context, long timestamp) {
-        SessionInfo info = new SessionInfo(
-                context.getSrcIpAddr(), context.getSrcPort(),
-                context.getDstIpAddr(), context.getDstPort(),
-                context.getProtocol(), protocolId, context.getId());
-        info.setSourceName(sourceName);
+    protected void firePayloadEvent(Object req, Object resp, SessionInfo session, long timestamp) {
+        session.setSourceName(sourceName);
 
-        Event event = new SessionPayloadEvent(info, req, resp, protocolId, sourceName);
+        Event event = new SessionPayloadEvent(session, req, resp, protocolId, sourceName);
         event.setTimestamp(timestamp);
         eventHandler.handle(event);
         if (collectMetric) {
@@ -48,6 +44,14 @@
         }
     }
 
+    protected void firePayloadEvent(Object req, Object resp, SessionContext context, long timestamp) {
+        SessionInfo session = new SessionInfo(
+                context.getSrcIpAddr(), context.getSrcPort(),
+                context.getDstIpAddr(), context.getDstPort(),
+                context.getProtocol(), protocolId, context.getId());
+        firePayloadEvent(req, resp, session, timestamp);
+    }
+
     protected void onSessionClosed(SessionContext context, int status, long timestamp) {
 
     }
--- a/stress-tester/src/main/java/com/passus/st/source/PcapDnsListener.java	Thu Jun 18 18:12:01 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapDnsListener.java	Fri Jun 19 09:29:24 2020 +0200
@@ -32,7 +32,7 @@
 
     @Override
     protected void onSessionClosed(SessionContext context, int status, long timestamp) {
-        Dns req = lastRequests.get(context.getKey());
+        Dns req = lastRequests.remove(context.getKey());
         if (req != null) {
             firePayloadEvent(req, null, context, timestamp);
         }
--- a/stress-tester/src/main/java/com/passus/st/source/PcapHttpListener.java	Thu Jun 18 18:12:01 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapHttpListener.java	Fri Jun 19 09:29:24 2020 +0200
@@ -3,8 +3,8 @@
 import com.passus.net.http.HttpMessage;
 import com.passus.net.http.HttpRequest;
 import com.passus.net.session.SessionContext;
-import com.passus.net.session.SessionKey;
 import com.passus.st.client.EventHandler;
+import com.passus.st.emitter.SessionInfo;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -13,7 +13,7 @@
 
 public class PcapHttpListener extends BaseSessionAnalyzerListener<HttpMessage> {
 
-    private final Map<SessionKey, HttpRequest> lastRequests;
+    private final Map<SessionInfo, HttpRequest> lastRequests;
 
     public PcapHttpListener(String sourceName, EventHandler eventHandler,
                             boolean collectMetric, PcapSessionEventSourceMetric metric, int maxSessionNum) {
@@ -22,21 +22,34 @@
     }
 
     @Override
-    public void onMessageReceived(SessionContext context, HttpMessage message, long timestamp) {
-        if (message.isRequest()) {
-            lastRequests.put(context.getKey(), (HttpRequest) message);
+    public void onMessageReceived(SessionContext context, HttpMessage msg, long timestamp) {
+        SessionInfo session = SessionInfo.create(context, protocolId);
+        if (msg.isRequest()) {
+            HttpRequest lastRequest = lastRequests.put(session, (HttpRequest) msg);
+            if (lastRequest != null) {
+                firePayloadEvent(lastRequest, null, session, timestamp);
+            }
         } else {
-            HttpRequest req = lastRequests.remove(context.getKey());
-            firePayloadEvent(req, message, context, timestamp);
+            HttpRequest req = lastRequests.remove(session);
+            firePayloadEvent(req, msg, session, timestamp);
         }
     }
 
     @Override
     protected void onSessionClosed(SessionContext context, int status, long timestamp) {
-        HttpRequest req = lastRequests.get(context.getKey());
+        SessionInfo session = SessionInfo.create(context, protocolId);
+        HttpRequest req = lastRequests.remove(session);
         if (req != null) {
-            firePayloadEvent(req, null, context, timestamp);
+            firePayloadEvent(req, null, session, timestamp);
         }
     }
 
+    public void onLoopEnd(long timestamp) {
+        if (!lastRequests.isEmpty()) {
+            lastRequests.forEach((session, req) -> {
+                firePayloadEvent(req, null, session, timestamp);
+            });
+            lastRequests.clear();
+        }
+    }
 }
\ No newline at end of file
--- a/stress-tester/src/main/java/com/passus/st/source/PcapHttpSessionAnalyzerHook.java	Thu Jun 18 18:12:01 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapHttpSessionAnalyzerHook.java	Fri Jun 19 09:29:24 2020 +0200
@@ -33,4 +33,19 @@
         tcpProc.removeAnalyzer(analyzer);
     }
 
+    @Override
+    public void onLoopEnd(SessionAnalyzer analyzer, PcapSessionAnalyzerHookContext context) {
+        PcapHttpListener listener = (PcapHttpListener) analyzer.getListener();
+        if (listener != null) {
+            listener.onLoopEnd(System.currentTimeMillis());
+        }
+    }
+
+    @Override
+    public void onDataEnd(SessionAnalyzer analyzer, PcapSessionAnalyzerHookContext context) {
+        PcapHttpListener listener = (PcapHttpListener) analyzer.getListener();
+        if (listener != null) {
+            listener.onLoopEnd(System.currentTimeMillis());
+        }
+    }
 }
--- a/stress-tester/src/main/java/com/passus/st/source/PcapSessionAnalyzerHook.java	Thu Jun 18 18:12:01 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapSessionAnalyzerHook.java	Fri Jun 19 09:29:24 2020 +0200
@@ -31,4 +31,12 @@
     }
 
     protected abstract void doDetach(SessionAnalyzer analyzer, PcapSessionAnalyzerHookContext context);
+
+    public void onLoopEnd(SessionAnalyzer analyzer, PcapSessionAnalyzerHookContext context) {
+
+    }
+
+    public void onDataEnd(SessionAnalyzer analyzer, PcapSessionAnalyzerHookContext context) {
+
+    }
 }
--- a/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Thu Jun 18 18:12:01 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Fri Jun 19 09:29:24 2020 +0200
@@ -497,10 +497,20 @@
             tcpProc.closeAllSessions();
             udpProc.closeAllSessions();
             eventHandler.flush();
+
+            for (SessionAnalyzer analyzer : analyzers) {
+                PcapSessionAnalyzerHook hook = getHook(analyzer);
+                hook.onLoopEnd(analyzer, hookContext);
+            }
         }
 
         public void onDataEnd() {
             eventHandler.flush();
+
+            for (SessionAnalyzer analyzer : analyzers) {
+                PcapSessionAnalyzerHook hook = getHook(analyzer);
+                hook.onDataEnd(analyzer, hookContext);
+            }
         }
 
         public void handle(Ip ip) {
--- a/stress-tester/src/test/java/com/passus/st/source/PcapSessionEventSourceTest.java	Thu Jun 18 18:12:01 2020 +0200
+++ b/stress-tester/src/test/java/com/passus/st/source/PcapSessionEventSourceTest.java	Fri Jun 19 09:29:24 2020 +0200
@@ -83,6 +83,31 @@
     }
 
     @Test(enabled = true)
+    public void testProcessHttpOnlyRequest() throws Exception {
+        File pcapFile = ResourceUtils.getFile("pcap/http/http_req_rst.pcap");
+        PcapSessionEventSource src = new PcapSessionEventSource();
+        src.setName("pcapSource");
+        src.setLoops(1);
+        src.setPcapFile(pcapFile.getAbsolutePath());
+
+        ArrayListEventHandler handler = new ArrayListEventHandler();
+        src.setHandler(handler);
+
+        src.start();
+        waitForSource(src);
+        src.stop();
+
+        SessionPayloadEvent payloadEvent = (SessionPayloadEvent) handler.findFirst(SessionPayloadEvent.TYPE);
+
+        assertTrue(payloadEvent.getRequest() instanceof HttpRequest);
+        assertTrue(payloadEvent.getResponse() == null);
+
+        assertTrue(handler.get(handler.size() - 3) instanceof SessionStatusEvent);
+        assertTrue(handler.get(handler.size() - 2) instanceof DataLoopEnd);
+        assertTrue(handler.get(handler.size() - 1) instanceof DataEnd);
+    }
+
+    @Test(enabled = true)
     public void testProcessDnsUdp() throws Exception {
         File pcapFile = ResourceUtils.getFile("pcap/dns/dns_A_req_resp.pcap");
         PcapSessionEventSource src = new PcapSessionEventSource();
Binary file stress-tester/src/test/resources/pcap/http/http_req_rst.pcap has changed