Search in sources :

Example 1 with CommandLineListener

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

the class CommandLineUnitTest method shouldAcceptFileArgumentIfHasSupportedFileExtension.

@Test
void shouldAcceptFileArgumentIfHasSupportedFileExtension(@TempDir Path folder) throws Exception {
    // Given
    String fileExtension = "test";
    File testFile = Files.createFile(folder.resolve("aaa." + fileExtension)).toFile();
    Map<String, CommandLineListener> supportedExtensions = new HashMap<>();
    supportedExtensions.put(fileExtension, new AcceptAllFilesCommandLineListener());
    cmdLine = new CommandLine(new String[] { testFile.toString() });
    // When / Then = Accepted file argument
    assertDoesNotThrow(() -> cmdLine.parse(NO_EXTENSIONS_CUSTOM_ARGUMENTS, supportedExtensions));
}
Also used : CommandLineListener(org.parosproxy.paros.extension.CommandLineListener) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 2 with CommandLineListener

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

the class CommandLineUnitTest method shouldNotAcceptFileArgumentIfRejectedBySupportedFileExtension.

/*@Test TODO
    public void shouldAcceptFileArgumentIfHasSupportedFileExtension() throws Exception {
        // Given
        String fileExtension = "test";
        folder.create();
        File testFile = folder.newFile("aaa." + fileExtension);
        Map<String, CommandLineListener> supportedExtensions = new HashMap<>();
        supportedExtensions.put(fileExtension, new AcceptAllFilesCommandLineListener());
        cmdLine = new CommandLine(new String[] { testFile.toString() });
        // When
        cmdLine.parse(NO_EXTENSIONS_CUSTOM_ARGUMENTS, supportedExtensions);
        // Then = Accepted file argument
    }
    */
@Test(expected = Exception.class)
public void shouldNotAcceptFileArgumentIfRejectedBySupportedFileExtension() throws Exception {
    // Given
    String fileExtension = "test";
    File testFile = folder.newFile("aaa." + fileExtension);
    Map<String, CommandLineListener> supportedExtensions = new HashMap<>();
    supportedExtensions.put(fileExtension, new RejectAllFilesCommandLineListener());
    cmdLine = new CommandLine(new String[] { testFile.toString() });
    // When
    cmdLine.parse(NO_EXTENSIONS_CUSTOM_ARGUMENTS, supportedExtensions);
// Then = Exception.class
}
Also used : HashMap(java.util.HashMap) CommandLineListener(org.parosproxy.paros.extension.CommandLineListener) Matchers.anyString(org.mockito.Matchers.anyString) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with CommandLineListener

use of org.parosproxy.paros.extension.CommandLineListener 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 4 with CommandLineListener

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

the class CommandLineUnitTest method shouldNotAcceptFileArgumentIfRejectedBySupportedFileExtension.

@Test
void shouldNotAcceptFileArgumentIfRejectedBySupportedFileExtension(@TempDir Path folder) throws Exception {
    // Given
    String fileExtension = "test";
    File testFile = Files.createFile(folder.resolve("aaa." + fileExtension)).toFile();
    Map<String, CommandLineListener> supportedExtensions = new HashMap<>();
    supportedExtensions.put(fileExtension, new RejectAllFilesCommandLineListener());
    cmdLine = new CommandLine(new String[] { testFile.toString() });
    // When / Then
    assertThrows(Exception.class, () -> cmdLine.parse(NO_EXTENSIONS_CUSTOM_ARGUMENTS, supportedExtensions));
}
Also used : CommandLineListener(org.parosproxy.paros.extension.CommandLineListener) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

File (java.io.File)4 CommandLineListener (org.parosproxy.paros.extension.CommandLineListener)4 HashMap (java.util.HashMap)3 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Test (org.junit.jupiter.api.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 CommandLineArgument (org.parosproxy.paros.extension.CommandLineArgument)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1