changeset 773:5fd3d7d7a21e

vars integration, unit tests
author Devel 1
date Mon, 18 Dec 2017 14:39:16 +0100
parents b2e54c963007
children c3e5932e8411
files stress-tester/src/main/java/com/passus/st/CliHelper.java stress-tester/src/main/java/com/passus/st/Main.java stress-tester/src/main/java/com/passus/st/Main2.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpVarsFilter.java stress-tester/src/test/java/com/passus/st/client/http/filter/HttpVarsFilterTest.java
diffstat 5 files changed, 97 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/CliHelper.java	Mon Dec 18 11:59:04 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/CliHelper.java	Mon Dec 18 14:39:16 2017 +0100
@@ -6,6 +6,7 @@
 import com.passus.config.validation.Errors;
 import static com.passus.config.validation.ErrorsUtils.objectErrorToString;
 import com.passus.config.validation.ObjectError;
+import com.passus.filter.ValueExtractor;
 import com.passus.net.PortRangeSet;
 import com.passus.st.client.http.HttpClient;
 import com.passus.st.client.http.filter.HttpFilter;
@@ -20,10 +21,14 @@
 import com.passus.st.source.EventSource;
 import com.passus.st.source.NcEventSource;
 import com.passus.st.source.PcapSessionEventSource;
+import com.passus.st.vars.VarsConfigurator;
+import com.passus.st.vars.VarsValueExtractorResolver;
 import java.io.File;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 import org.apache.commons.cli.CommandLine;
 import static org.apache.commons.io.FileUtils.openInputStream;
 import org.apache.commons.io.IOUtils;
@@ -152,13 +157,13 @@
 
     public List<HttpFilter> readHttpFilters(CommandLine cl) throws Exception {
         if (cl.hasOption("ff")) {
-            File filtersFile = new File(cl.getOptionValue("ff"));
-            Configuration config = YamlConfigurationReader.readFromFile(filtersFile);
+            File file = new File(cl.getOptionValue("ff"));
+            Configuration cfg = YamlConfigurationReader.readFromFile(file);
             HttpFiltersConfigurator configurator = new HttpFiltersConfigurator();
             Errors errors = new Errors();
-            configurator.configure(config, errors, null);
+            configurator.configure(cfg, errors, null);
             if (errors.getErrorCount() != 0) {
-                printError("Error in file '" + filtersFile.getAbsolutePath() + "'.", false);
+                printError("Error in file '" + file.getAbsolutePath() + "'.", false);
                 for (ObjectError error : errors.getAllErrors()) {
                     printError("\t" + objectErrorToString(error), false);
                 }
@@ -171,14 +176,33 @@
         return null;
     }
 
+    /**
+     * Implicitly adds VarsFilter at the end.
+     *
+     * @param cl
+     * @return
+     * @throws Exception
+     */
     public List<HttpFilter> readHttpClientFilters(CommandLine cl) throws Exception {
         List<HttpFilter> filters = readHttpFilters(cl);
         if (filters == null) {
             filters = new ArrayList<>();
         }
 
-        filters.add(new HttpVarsFilter());
+        Map<String, ValueExtractor> globalVars = readGlobalVars(cl);
+        VarsValueExtractorResolver resolver = new VarsValueExtractorResolver(globalVars);
+        HttpVarsFilter httpVarsFilter = new HttpVarsFilter(resolver);
+
+        filters.add(httpVarsFilter);
         return filters;
     }
 
+    public Map<String, ValueExtractor> readGlobalVars(CommandLine cl) throws Exception {
+        if (cl.hasOption("vf")) {
+            File file = new File(cl.getOptionValue("vf"));
+            Configuration cfg = YamlConfigurationReader.readFromFile(file);
+            return VarsConfigurator.getGlobalVars(cfg);
+        }
+        return Collections.emptyMap();
+    }
 }
--- a/stress-tester/src/main/java/com/passus/st/Main.java	Mon Dec 18 11:59:04 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/Main.java	Mon Dec 18 14:39:16 2017 +0100
@@ -7,10 +7,10 @@
 import com.passus.st.client.MemoryEventsCache;
 import com.passus.st.client.http.DumperHttpClientListener;
 import com.passus.st.client.http.HttpClient;
-import com.passus.st.client.http.ReporterRemoteDestination;
 import com.passus.st.client.http.HttpSourceNameAwareClientWorkerDispatcher;
 import com.passus.st.client.http.ReporterDestination;
 import com.passus.st.client.http.ReporterFileDestination;
+import com.passus.st.client.http.ReporterRemoteDestination;
 import com.passus.st.client.http.SummaryHttpClientListener;
 import com.passus.st.client.http.WriterHttpClientListener;
 import com.passus.st.client.http.filter.HttpFilter;
@@ -23,6 +23,7 @@
 import com.passus.st.reporter.ReporterClient;
 import com.passus.st.reporter.trx.SocketReporterClient;
 import com.passus.st.source.EventSource;
+import static com.passus.st.utils.CliUtils.option;
 import com.passus.st.utils.PeriodFormatter;
 import java.io.File;
 import java.io.FileOutputStream;
@@ -44,7 +45,6 @@
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.io.IOUtils;
-import static com.passus.st.utils.CliUtils.option;
 
 /**
  *
--- a/stress-tester/src/main/java/com/passus/st/Main2.java	Mon Dec 18 11:59:04 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/Main2.java	Mon Dec 18 14:39:16 2017 +0100
@@ -8,10 +8,10 @@
 import com.passus.st.client.MemoryEventsCache;
 import com.passus.st.client.http.DumperHttpClientListener;
 import com.passus.st.client.http.HttpClient;
-import com.passus.st.client.http.ReporterRemoteDestination;
 import com.passus.st.client.http.HttpSourceNameAwareClientWorkerDispatcher;
 import com.passus.st.client.http.ReporterDestination;
 import com.passus.st.client.http.ReporterFileDestination;
+import com.passus.st.client.http.ReporterRemoteDestination;
 import com.passus.st.client.http.SummaryHttpClientListener;
 import com.passus.st.client.http.WriterHttpClientListener;
 import com.passus.st.client.http.filter.HttpFilter;
@@ -24,6 +24,7 @@
 import com.passus.st.reporter.ReporterClient;
 import com.passus.st.reporter.trx.SocketReporterClient;
 import com.passus.st.source.PcapSessionEventSource;
+import static com.passus.st.utils.CliUtils.option;
 import com.passus.st.utils.PeriodFormatter;
 import java.io.File;
 import java.io.FileOutputStream;
@@ -45,7 +46,6 @@
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.io.IOUtils;
-import static com.passus.st.utils.CliUtils.option;
 
 /**
  *
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpVarsFilter.java	Mon Dec 18 11:59:04 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpVarsFilter.java	Mon Dec 18 14:39:16 2017 +0100
@@ -2,13 +2,13 @@
 
 import com.passus.commons.Assert;
 import com.passus.data.ByteBuff;
+import com.passus.data.ByteBuffDataSource;
 import com.passus.data.HeapByteBuff;
+import com.passus.filter.config.ValueExtractorResolver;
 import com.passus.net.http.HttpMessageHelper;
 import com.passus.net.http.HttpRequest;
 import com.passus.net.http.HttpResponse;
 import com.passus.st.client.http.HttpFlowContext;
-import com.passus.st.client.http.filter.HttpFilter;
-import com.passus.st.client.http.filter.HttpMessageWrapper;
 import com.passus.st.vars.Var;
 import com.passus.st.vars.VarsCompiler;
 import com.passus.st.vars.VarsExecutor;
@@ -25,8 +25,6 @@
 
     private static final Logger LOGGER = LogManager.getLogger(HttpVarsFilter.class);
 
-    private final HttpMessageHelper helper = HttpMessageHelper.get();
-
     private final VarsCompiler compiler;
 
     private final VarsExecutor executor;
@@ -36,6 +34,12 @@
         executor = new VarsExecutor();
     }
 
+    public HttpVarsFilter(ValueExtractorResolver resolver) {
+        Assert.notNull(resolver, "resolver");
+        compiler = new VarsCompiler(resolver);
+        executor = new VarsExecutor();
+    }
+
     public HttpVarsFilter(VarsCompiler compiler, VarsExecutor executor) {
         Assert.notNull(compiler, "compiler");
         Assert.notNull(executor, "executor");
@@ -53,13 +57,15 @@
         //Przetwarzamy tylko requesty, response'a nie ma sensu
         if (req != null) {
             try {
-                helper.decodeContent(req, true);
+                HttpMessageHelper.get().decodeContent(req, true);
                 ByteBuff buffer = req.getContentByteBuff();
                 if (buffer != null) {
                     List<Var> vars = compiler.search(buffer);
                     if (vars != null && !vars.isEmpty()) {
                         ByteBuff result = new HeapByteBuff(buffer.readableBytes());
                         executor.execute(buffer, result, vars, new HttpMessageWrapper(req, resp, context));
+                        req.setContent(new ByteBuffDataSource(result));
+                        HttpMessageHelper.get().setContentLength(req, result.readableBytes());
                     }
                 }
             } catch (IOException e) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/client/http/filter/HttpVarsFilterTest.java	Mon Dec 18 14:39:16 2017 +0100
@@ -0,0 +1,53 @@
+package com.passus.st.client.http.filter;
+
+import com.passus.data.ByteString;
+import com.passus.data.DataSourceUtils;
+import com.passus.filter.UnmutableValueExtractor;
+import com.passus.filter.ValueExtractor;
+import com.passus.net.http.HttpDataPart;
+import com.passus.net.http.HttpMessageHelper;
+import com.passus.net.http.HttpRequest;
+import com.passus.st.utils.TestHttpUtils;
+import com.passus.st.vars.VarsValueExtractorResolver;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class HttpVarsFilterTest {
+
+    private static final Charset UTF_8 = Charset.forName("UTF-8");
+
+    private final ValueExtractor value = new UnmutableValueExtractor("value1");
+    private final Map<String, ValueExtractor> vars = Collections.singletonMap("var1", value);
+    private final VarsValueExtractorResolver resolver = new VarsValueExtractorResolver(vars);
+
+    @Test
+    public void testPostUrlEncoded() throws IOException {
+        String reqs = "POST /login_check HTTP/1.1\r\n"
+                + "Host: 172.16.60.23\r\n"
+                + "Content-Type: application/x-www-form-urlencoded\r\n"
+                + "Content-Length: 23\r\n"
+                + "\r\n"
+                + "user=admin&pass=${var1}";
+        HttpRequest request = TestHttpUtils.request(reqs);
+
+        HttpVarsFilter filter = new HttpVarsFilter(resolver);
+
+        filter.filterOutbound(request, null, null);
+        
+        String content = request.getContentByteString().toString();
+        int contentLength = (int) HttpMessageHelper.get().getContentLength(request);
+        assertEquals("user=admin&pass=value1", content);
+        assertEquals(content.length(), contentLength);
+    }
+
+
+}