Search in sources :

Example 6 with CommandLineArgument

use of org.parosproxy.paros.extension.CommandLineArgument in project zaproxy by zaproxy.

the class CommandLine method getHelp.

public static String getHelp(List<CommandLineArgument[]> cmdList) {
    String zap;
    if (Constant.isWindows()) {
        zap = "zap.bat";
    } else {
        zap = "zap.sh";
    }
    StringBuilder sb = new StringBuilder();
    sb.append(Constant.messages.getString("cmdline.help", zap));
    if (cmdList != null) {
        for (CommandLineArgument[] extArgs : cmdList) {
            for (CommandLineArgument extArg : extArgs) {
                sb.append("\t");
                sb.append(extArg.getHelpMessage()).append("\n");
            }
        }
    }
    return sb.toString();
}
Also used : CommandLineArgument(org.parosproxy.paros.extension.CommandLineArgument)

Example 7 with CommandLineArgument

use of org.parosproxy.paros.extension.CommandLineArgument in project zaproxy by zaproxy.

the class CommandLine method parse.

public void parse(List<CommandLineArgument[]> commandList, Map<String, CommandLineListener> extMap) throws Exception {
    this.commandList = commandList;
    CommandLineArgument lastArg = null;
    boolean found = false;
    int remainingValueCount = 0;
    for (int i = 0; i < args.length; i++) {
        if (args[i] == null) {
            continue;
        }
        found = false;
        for (int j = 0; j < commandList.size() && !found; j++) {
            CommandLineArgument[] extArg = commandList.get(j);
            for (int k = 0; k < extArg.length && !found; k++) {
                if (args[i].compareToIgnoreCase(extArg[k].getName()) == 0) {
                    // check if previous keyword satisfied its required no. of parameters
                    if (remainingValueCount > 0) {
                        throw new Exception("Missing parameters for keyword '" + lastArg.getName() + "'.");
                    }
                    // process this keyword
                    lastArg = extArg[k];
                    lastArg.setEnabled(true);
                    found = true;
                    args[i] = null;
                    remainingValueCount = lastArg.getNumOfArguments();
                }
            }
        }
        // check if current string is a keyword preceded by '-'
        if (args[i] != null && args[i].startsWith("-")) {
            continue;
        }
        // check if there is no more expected param value
        if (lastArg != null && remainingValueCount == 0) {
            continue;
        }
        // check if consume remaining for last matched keywords
        if (!found && lastArg != null) {
            if (lastArg.getPattern() == null || lastArg.getPattern().matcher(args[i]).find()) {
                lastArg.getArguments().add(args[i]);
                if (remainingValueCount > 0) {
                    remainingValueCount--;
                }
                args[i] = null;
            } else {
                throw new Exception(lastArg.getErrorMessage());
            }
        }
    }
    // check if the last keyword satisfied its no. of parameters.
    if (lastArg != null && remainingValueCount > 0) {
        throw new Exception("Missing parameters for keyword '" + lastArg.getName() + "'.");
    }
    // check for supported extensions
    for (int i = 0; i < args.length; i++) {
        if (args[i] == null) {
            continue;
        }
        int dotIndex = args[i].lastIndexOf(".");
        if (dotIndex < 0) {
            // Only support files with extensions
            continue;
        }
        File file = new File(args[i]);
        if (!file.exists() || !file.canRead()) {
            // Not there or cant read .. move on
            continue;
        }
        String ext = args[i].substring(dotIndex + 1);
        CommandLineListener cll = extMap.get(ext);
        if (cll != null) {
            if (cll.handleFile(file)) {
                found = true;
                args[i] = null;
            }
        }
    }
    // check if there is some unknown keywords or parameters
    for (String arg : args) {
        if (arg != null) {
            if (arg.startsWith("-")) {
                throw new Exception(Constant.messages.getString("start.cmdline.badparam", arg));
            } else {
                // Assume they were trying to specify a file
                File f = new File(arg);
                if (!f.exists()) {
                    throw new Exception(Constant.messages.getString("start.cmdline.nofile", arg));
                } else if (!f.canRead()) {
                    throw new Exception(Constant.messages.getString("start.cmdline.noread", arg));
                } else {
                    // We probably dont handle this sort of file
                    throw new Exception(Constant.messages.getString("start.cmdline.badfile", arg));
                }
            }
        }
    }
}
Also used : CommandLineArgument(org.parosproxy.paros.extension.CommandLineArgument) CommandLineListener(org.parosproxy.paros.extension.CommandLineListener) File(java.io.File)

Example 8 with CommandLineArgument

use of org.parosproxy.paros.extension.CommandLineArgument in project zaproxy by zaproxy.

the class CommandLineUnitTest method claWithPattern.

@Test
void claWithPattern() throws Exception {
    cmdLine = new CommandLine(new String[] { "-script", "aaa", "bbb", "ccc" });
    Vector<CommandLineArgument[]> customArguments = new Vector<>();
    customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-script", -1, ".*", null, null) });
    cmdLine.parse(customArguments, NO_SUPPORTED_FILE_EXTENSIONS);
    assertTrue(customArguments.get(0)[0].isEnabled());
    assertThat(customArguments.get(0)[0].getArguments().size(), is(equalTo(3)));
}
Also used : CommandLineArgument(org.parosproxy.paros.extension.CommandLineArgument) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Vector(java.util.Vector) Test(org.junit.jupiter.api.Test)

Example 9 with CommandLineArgument

use of org.parosproxy.paros.extension.CommandLineArgument in project zaproxy by zaproxy.

the class CommandLineUnitTest method claWithArgs.

@Test
void claWithArgs() throws Exception {
    cmdLine = new CommandLine(new String[] { "-a", "aaa", "-b", "bbb", "BBB" });
    Vector<CommandLineArgument[]> customArguments = new Vector<>();
    customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-a", 1, null, null, null) });
    customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-b", 2, null, null, null) });
    customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-c", 3, null, null, null) });
    cmdLine.parse(customArguments, NO_SUPPORTED_FILE_EXTENSIONS);
    assertTrue(customArguments.get(0)[0].isEnabled());
    assertThat(customArguments.get(0)[0].getArguments(), hasSize(1));
    assertThat(customArguments.get(0)[0].getArguments(), hasItem("aaa"));
    assertFalse(customArguments.get(0)[0].getArguments().contains("bbb"));
    assertTrue(customArguments.get(1)[0].isEnabled());
    assertThat(customArguments.get(1)[0].getArguments(), hasSize(2));
    assertFalse(customArguments.get(1)[0].getArguments().contains("aaa"));
    assertThat(customArguments.get(1)[0].getArguments(), hasItem("bbb"));
    assertThat(customArguments.get(1)[0].getArguments(), hasItem("BBB"));
    assertFalse(customArguments.get(2)[0].isEnabled());
}
Also used : CommandLineArgument(org.parosproxy.paros.extension.CommandLineArgument) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Vector(java.util.Vector) Test(org.junit.jupiter.api.Test)

Example 10 with CommandLineArgument

use of org.parosproxy.paros.extension.CommandLineArgument in project zaproxy by zaproxy.

the class CommandLineUnitTest method shouldGetNullValueFromNonBuiltInArgument.

@Test
void shouldGetNullValueFromNonBuiltInArgument() throws Exception {
    // Given
    String argName = "-arg";
    Vector<CommandLineArgument[]> supportedArguments = new Vector<>();
    supportedArguments.add(new CommandLineArgument[] { new CommandLineArgument(argName, 1) });
    cmdLine = new CommandLine(new String[] { argName, "value" });
    // When
    cmdLine.parse(supportedArguments, NO_SUPPORTED_FILE_EXTENSIONS);
    // Then
    assertThat(cmdLine.getArgument(argName), is(equalTo(null)));
}
Also used : CommandLineArgument(org.parosproxy.paros.extension.CommandLineArgument) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Vector(java.util.Vector) Test(org.junit.jupiter.api.Test)

Aggregations

CommandLineArgument (org.parosproxy.paros.extension.CommandLineArgument)10 Vector (java.util.Vector)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Test (org.junit.jupiter.api.Test)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 File (java.io.File)1 CommandLineListener (org.parosproxy.paros.extension.CommandLineListener)1