changeset 639:ed03df7b7bef

RuleBasedSessionMapper - HostResolver support
author Devel 2
date Mon, 06 Nov 2017 10:53:32 +0100
parents 66d541da3dc3
children 513143f046c8
files stress-tester/src/main/java/com/passus/st/emitter/RuleBasedSessionMapper.java stress-tester/src/test/java/com/passus/st/emitter/RuleBasedSessionMapperTest.java
diffstat 2 files changed, 33 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/emitter/RuleBasedSessionMapper.java	Thu Nov 02 15:13:07 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/emitter/RuleBasedSessionMapper.java	Mon Nov 06 10:53:32 2017 +0100
@@ -8,11 +8,13 @@
 import static com.passus.config.schema.ConfigurationSchemaBuilder.valueDef;
 import com.passus.config.schema.NodeDefinition;
 import com.passus.config.schema.NodeDefinitionCreator;
+import com.passus.net.HostResolver;
 import com.passus.net.IpAddress;
 import com.passus.net.PortRange;
 import com.passus.net.SocketAddress;
 import static com.passus.st.emitter.DynamicSessionMapper.DEFAULT_SESSIONS_CAPACITY;
 import com.passus.st.plugin.PluginConstants;
+import java.net.UnknownHostException;
 import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -36,15 +38,21 @@
 
     private final Map<SessionInfo, ConnectionParams> map = new HashMap<>(DEFAULT_SESSIONS_CAPACITY);
 
+    public void addRule(String rule) throws ParseException {
+        addRule(rule, HostResolver.DEFAULT);
+    }
+
     /**
      *
      * @param rule PCAP_REMOTE -> REMOTE [bind LOCAL]
+     * @param resolver
      * @throws ParseException
      */
-    public void addRule(String rule) throws ParseException {
+    public void addRule(String rule, HostResolver resolver) throws ParseException {
         Assert.notEmpty(rule, "rule");
+        Assert.notNull(resolver, "resolver");
         synchronized (this) {
-            rules.add(parseRule(rule));
+            rules.add(parseRule(rule, resolver));
         }
     }
 
@@ -85,7 +93,12 @@
     }
 
     public static Rule parseRule(String rule) throws ParseException {
+        return parseRule(rule, HostResolver.DEFAULT);
+    }
+
+    public static Rule parseRule(String rule, HostResolver resolver) throws ParseException {
         Assert.notEmpty(rule, "rule");
+        Assert.notNull(resolver, "resolver");
         SocketAddressProvider bindAddressProvider = SocketAddressProviderBase.ANY_SOCKET_PROVIDER;
         SocketAddress remoteAddress = null;
         SocketMatcher matcher;
@@ -101,7 +114,7 @@
 
                 IpAddress ip = null;
                 if (!"*".equals(parts[0])) {
-                    ip = IpAddress.parse(parts[0]);
+                    ip = resolver.resolve(parts[0]);
                 }
 
                 int port = -1;
@@ -111,6 +124,8 @@
                 }
 
                 matcher = new SimpleSocketMatcher(ip, port);
+            } catch (UnknownHostException e) {
+                throw new ParseException("Invalid rule. Unknown host '" + e.getMessage() + "'.", 0);
             } catch (Exception e) {
                 throw new ParseException("Invalid rule.", 0);
             }
@@ -120,7 +135,7 @@
             int bindPos = left.indexOf(RULE_BIND_SEPARATOR);
             if (bindPos == -1) {
                 try {
-                    remoteAddress = new SocketAddress(left.trim());
+                    remoteAddress = new SocketAddress(left.trim(), resolver);
                 } catch (Exception e) {
                     throw new ParseException(e.getMessage(), pos);
                 }
@@ -129,7 +144,7 @@
                 bindPos += RULE_BIND_SEPARATOR.length();
 
                 try {
-                    remoteAddress = new SocketAddress(remoteAddrStr.trim());
+                    remoteAddress = new SocketAddress(remoteAddrStr.trim(), resolver);
                 } catch (Exception e) {
                     throw new ParseException(e.getMessage(), pos);
                 }
--- a/stress-tester/src/test/java/com/passus/st/emitter/RuleBasedSessionMapperTest.java	Thu Nov 02 15:13:07 2017 +0100
+++ b/stress-tester/src/test/java/com/passus/st/emitter/RuleBasedSessionMapperTest.java	Mon Nov 06 10:53:32 2017 +0100
@@ -1,5 +1,7 @@
 package com.passus.st.emitter;
 
+import com.passus.net.HostResolver;
+import com.passus.net.Ip4Address;
 import com.passus.net.SocketAddress;
 import com.passus.st.emitter.SessionMapper.ConnectionParams;
 import java.util.Arrays;
@@ -17,6 +19,15 @@
 
     private SessionInfo validSession;
     private SessionInfo invalidSession;
+    private final HostResolver testResolver = (String host) -> {
+        if (host.equals("src_domain.test")) {
+            return new Ip4Address("1.1.1.1");
+        } else if (host.equals("dst_domain.test")) {
+            return new Ip4Address("2.2.2.2");
+        }
+
+        return new Ip4Address(host);
+    };
 
     @BeforeClass
     public void setUp() throws Exception {
@@ -26,9 +37,8 @@
 
     private void assertMatchRule(String rule, SocketAddress remoteAddress, SocketAddress bindAddress) throws Exception {
         RuleBasedSessionMapper mapper = new RuleBasedSessionMapper();
-        mapper.addRule(rule);
+        mapper.addRule(rule, testResolver);
         ConnectionParams params = mapper.map(validSession);
-        assertTrue(rule, params != null);
         assertEquals(rule, remoteAddress, params.getRemoteAddress());
         assertEquals(rule, bindAddress, params.getBindAddress());
     }
@@ -42,6 +52,7 @@
 
     @Test
     public void testAddRule() throws Exception {
+        assertMatchRule("src_domain.test:* -> dst_domain.test:90", new SocketAddress("2.2.2.2:90"), SessionMapper.ANY_SOCKET);
         assertMatchRule("1.1.1.1:* -> 2.2.2.2:90", new SocketAddress("2.2.2.2:90"), SessionMapper.ANY_SOCKET);
         assertMatchRule("*:80 -> 2.2.2.2:90", new SocketAddress("2.2.2.2:90"), SessionMapper.ANY_SOCKET);
         assertMatchRule("*:* -> 2.2.2.2:90", new SocketAddress("2.2.2.2:90"), SessionMapper.ANY_SOCKET);