changeset 861:74a25a7af9e3

mapper rule toString
author Devel 1
date Wed, 24 Jan 2018 10:54:34 +0100
parents 1c3bc5ba2772
children c189e87bfaaa
files stress-tester/src/main/java/com/passus/st/emitter/PortIgnoringSocketAddressProvider.java stress-tester/src/main/java/com/passus/st/emitter/PortSpecificSocketAddressProvider.java stress-tester/src/main/java/com/passus/st/emitter/RuleBasedSessionMapper.java stress-tester/src/main/java/com/passus/st/emitter/SocketAddressProviderBase.java stress-tester/src/test/java/com/passus/st/emitter/RuleBasedSessionMapperTest.java stress-tester/src/test/java/com/passus/st/emitter/SocketAddressProviderBaseTest.java
diffstat 6 files changed, 133 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/emitter/PortIgnoringSocketAddressProvider.java	Wed Jan 24 10:46:04 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/emitter/PortIgnoringSocketAddressProvider.java	Wed Jan 24 10:54:34 2018 +0100
@@ -43,4 +43,9 @@
         return 0;
     }
 
+    @Override
+    public String toString() {
+        return addressesToRanges(addresses);
+    }
+
 }
--- a/stress-tester/src/main/java/com/passus/st/emitter/PortSpecificSocketAddressProvider.java	Wed Jan 24 10:46:04 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/emitter/PortSpecificSocketAddressProvider.java	Wed Jan 24 10:54:34 2018 +0100
@@ -51,4 +51,9 @@
         return size;
     }
 
+    @Override
+    public String toString() {
+        return addressesToRanges(addresses) + ":" + portsToRanges(ports);
+    }
+
 }
--- a/stress-tester/src/main/java/com/passus/st/emitter/RuleBasedSessionMapper.java	Wed Jan 24 10:46:04 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/emitter/RuleBasedSessionMapper.java	Wed Jan 24 10:54:34 2018 +0100
@@ -220,6 +220,25 @@
         public boolean match(SessionInfo info) {
             return matcher.match(info.getDstIp(), info.getDstPort());
         }
+
+        @Override
+        public String toString() {
+            StringBuilder sb = new StringBuilder();
+            if (matcher == SimpleSocketMatcher.ALL_MATCH && remoteAddress == null) {
+
+            } else {
+                sb.append(matcher).append(" -> ").append(remoteAddress);
+            }
+
+            if (bindAddressProvider != SocketAddressProviderBase.ANY_SOCKET_PROVIDER) {
+                if (sb.length() > 0) {
+                    sb.append(' ');
+                }
+                sb.append("bind ").append(bindAddressProvider);
+            }
+            return sb.toString();
+        }
+
     }
 
     public static final class RuleBasedSessionMapperNodeDefCreator implements NodeDefinitionCreator {
--- a/stress-tester/src/main/java/com/passus/st/emitter/SocketAddressProviderBase.java	Wed Jan 24 10:46:04 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/emitter/SocketAddressProviderBase.java	Wed Jan 24 10:54:34 2018 +0100
@@ -111,4 +111,51 @@
         }
     }
 
+    static String portsToRanges(int... ports) {
+        StringBuilder sb = new StringBuilder();
+        int i = 0;
+        while (i < ports.length) {
+            int begin = ports[i++];
+            int end = begin;
+            while (i < ports.length && ports[i] == end + 1) {
+                ++i;
+                ++end;
+            }
+            if (sb.length() > 0) {
+                sb.append(", ");
+            }
+            sb.append(begin);
+            if (begin != end) {
+                sb.append('-');
+                sb.append(end);
+            }
+        }
+        return sb.toString();
+    }
+
+    static String addressesToRanges(IpAddress... addresses) {
+        StringBuilder sb = new StringBuilder();
+        int i = 0;
+        while (i < addresses.length) {
+            Ip4Address beginAddr = (Ip4Address) addresses[i++];
+            Ip4Address endAddr = beginAddr;
+            int begin = beginAddr.getIntValue();
+            int end = begin;
+            while (i < addresses.length && ((Ip4Address) addresses[i]).getIntValue() == end + 1) {
+                endAddr = (Ip4Address) addresses[i];
+                ++i;
+                ++end;
+            }
+
+            if (sb.length() > 0) {
+                sb.append(", ");
+            }
+            sb.append(beginAddr);
+            if (begin != end) {
+                sb.append('-');
+                sb.append(endAddr);
+            }
+        }
+        return sb.toString();
+    }
 }
--- a/stress-tester/src/test/java/com/passus/st/emitter/RuleBasedSessionMapperTest.java	Wed Jan 24 10:46:04 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/emitter/RuleBasedSessionMapperTest.java	Wed Jan 24 10:54:34 2018 +0100
@@ -3,6 +3,7 @@
 import com.passus.net.HostResolver;
 import com.passus.net.Ip4Address;
 import com.passus.net.SocketAddress;
+import com.passus.st.emitter.RuleBasedSessionMapper.Rule;
 import com.passus.st.emitter.SessionMapper.ConnectionParams;
 import java.util.Arrays;
 import java.util.List;
@@ -51,6 +52,39 @@
     }
 
     @Test
+    public void testParsePrintRule() throws Exception {
+        testParsePrintRule("src_domain.test:* -> dst_domain.test:90",
+                "1.1.1.1:* -> 2.2.2.2:90");// cannot map IP to domain
+
+        testParsePrintRule("1.1.1.1:* -> 2.2.2.2:90");
+        testParsePrintRule("*:80 -> 2.2.2.2:90");
+        testParsePrintRule("*:* -> 2.2.2.2:90");
+        testParsePrintRule("1.1.1.1:80 -> 2.2.2.2:90");
+        testParsePrintRule(" 1.1.1.1:80->2.2.2.2:90 ",
+                "1.1.1.1:80 -> 2.2.2.2:90"); // whitespace
+
+        testParsePrintRule("bind 172.16.40.10");
+        testParsePrintRule("1.1.1.1:80 -> 2.2.2.2:90 bind 172.16.40.10");
+        testParsePrintRule("1.1.1.1:80 -> 2.2.2.2:90 bind 172.16.40.10:*",
+                "1.1.1.1:80 -> 2.2.2.2:90 bind 172.16.40.10:1-65535"); // asterisk -> range
+        testParsePrintRule("1.1.1.1:80 -> 2.2.2.2:90 bind 172.16.40.10:2001-6000");
+        testParsePrintRule("1.1.1.1:80 -> 2.2.2.2:90 bind 172.16.40.10/31:2001-6000",
+                "1.1.1.1:80 -> 2.2.2.2:90 bind 172.16.40.10-172.16.40.11:2001-6000"); // mask -> range
+        testParsePrintRule("1.1.1.1:80 -> 2.2.2.2:90 bind 172.16.40.10, 172.16.40.20:2001-6000");
+        testParsePrintRule("1.1.1.1:80 -> 2.2.2.2:90 bind 172.16.40.10-172.16.40.20:2001-6000");
+    }
+
+    public void testParsePrintRule(String inputRule) throws Exception {
+        testParsePrintRule(inputRule, inputRule);
+    }
+
+    public void testParsePrintRule(String inputRule, String expected) throws Exception {
+        Rule rule = RuleBasedSessionMapper.parseRule(inputRule, testResolver);
+        String outputRule = rule.toString();
+        assertEquals(expected, outputRule);
+    }
+
+    @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);
@@ -59,8 +93,8 @@
         assertMatchRule("1.1.1.1:80 -> 2.2.2.2:90", new SocketAddress("2.2.2.2:90"), SessionMapper.ANY_SOCKET);
         assertMatchRule(" 1.1.1.1:80->2.2.2.2:90 ", new SocketAddress("2.2.2.2:90"), SessionMapper.ANY_SOCKET);
 
+        assertMatchRule("bind 172.16.40.10", null, new SocketAddress("172.16.40.10", 0));
         assertMatchRule("1.1.1.1:80->2.2.2.2:90 bind 172.16.40.10", new SocketAddress("2.2.2.2:90"), new SocketAddress("172.16.40.10", 0));
-        assertMatchRule("bind 172.16.40.10", null, new SocketAddress("172.16.40.10", 0));
         assertMatchRule("1.1.1.1:80->2.2.2.2:90 bind 172.16.40.10:*", new SocketAddress("2.2.2.2:90"), new SocketAddress("172.16.40.10", 1));
         assertMatchRule("1.1.1.1:80->2.2.2.2:90 bind 172.16.40.10:2001-6000", new SocketAddress("2.2.2.2:90"), new SocketAddress("172.16.40.10", 2001));
         assertMatchRule("1.1.1.1:80->2.2.2.2:90 bind 172.16.40.10/31:2001-6000", new SocketAddress("2.2.2.2:90"), new SocketAddress("172.16.40.10", 2001));
--- a/stress-tester/src/test/java/com/passus/st/emitter/SocketAddressProviderBaseTest.java	Wed Jan 24 10:46:04 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/emitter/SocketAddressProviderBaseTest.java	Wed Jan 24 10:54:34 2018 +0100
@@ -2,9 +2,11 @@
 
 import com.passus.net.IpAddress;
 import com.passus.net.SocketAddress;
+import static com.passus.st.emitter.SocketAddressProviderBase.addressesToRanges;
 import static com.passus.st.emitter.SocketAddressProviderBase.create;
 import static com.passus.st.emitter.SocketAddressProviderBase.parseAddresses;
 import static com.passus.st.emitter.SocketAddressProviderBase.parsePorts;
+import static com.passus.st.emitter.SocketAddressProviderBase.portsToRanges;
 import static org.testng.Assert.*;
 import org.testng.annotations.Test;
 
@@ -98,4 +100,24 @@
     static SocketAddress sa(String a, int p) {
         return new SocketAddress(IpAddress.parse(a), p);
     }
+
+    @Test
+    public void testPortsToRanges() {
+        assertEquals("100", portsToRanges(100));
+        assertEquals("100, 102", portsToRanges(100, 102));
+        assertEquals("100-101", portsToRanges(100, 101));
+        assertEquals("100-103", portsToRanges(100, 101, 102, 103));
+        assertEquals("100-101, 105", portsToRanges(100, 101, 105));
+        assertEquals("100, 102-103", portsToRanges(100, 102, 103));
+    }
+
+    @Test
+    public void testAddressesToRanges() {
+        assertEquals("1.1.1.0", addressesToRanges(a("1.1.1.0")));
+        assertEquals("1.1.1.0, 1.1.1.2", addressesToRanges(a("1.1.1.0", "1.1.1.2")));
+        assertEquals("1.1.1.0-1.1.1.1", addressesToRanges(a("1.1.1.0", "1.1.1.1")));
+        assertEquals("1.1.1.0-1.1.1.3", addressesToRanges(a("1.1.1.0", "1.1.1.1", "1.1.1.2", "1.1.1.3")));
+        assertEquals("1.1.1.0-1.1.1.1, 1.1.1.3", addressesToRanges(a("1.1.1.0", "1.1.1.1", "1.1.1.3")));
+        assertEquals("1.1.1.0, 1.1.1.2-1.1.1.3", addressesToRanges(a("1.1.1.0", "1.1.1.2", "1.1.1.3")));
+    }
 }