@Test public void testNmapDetectionWithTimeoutException() throws InterruptedException, ExecutionException, TimeoutException, IOException { doThrow(new TimeoutException("timeout msg")).when(processExecutor).execute(anyString(), anyInt()); OperatingSystemDetails actualOsDetails = nmapOsDetectorService.detectOs(new OsDetectorInputs.Builder().withNmapTimeout("007").build()); performFailureChecks(actualOsDetails, "The nmap command timed out"); }
@Override public OperatingSystemDetails detectOs(OsDetectorInputs osDetectorInputs) { OperatingSystemDetails operatingSystemDetails = new OperatingSystemDetails(); List<String> nmapCommandOutput = new ArrayList<>(); try { int timeout = parseInt(osDetectorInputs.getNmapTimeout()); ProcessResponseEntity responseEntity = new ProcessExecutor().execute(buildNmapCommand(osDetectorInputs), timeout); if (responseEntity.isTimeout()) { addTimeoutErrorMessage(nmapCommandOutput); } else { String stdOut = responseEntity.getStdout(); String stdErrors = responseEntity.getStderr(); int exitCode = responseEntity.getExitCode(); if (!isEmpty(stdOut)) { nmapCommandOutput.add(stdOut); String osName = osDetectorHelperService.cropValue(stdOut, OS_DETAILS, System.lineSeparator()); operatingSystemDetails.setName(osName); operatingSystemDetails.setFamily(osDetectorHelperService.resolveOsFamily(osName)); } if (!isEmpty(stdErrors)) { nmapCommandOutput.add(stdErrors); } nmapCommandOutput.add("Execution of Nmap command had exit code: " + exitCode); } } catch (IOException e) { nmapCommandOutput.add("Failed to run Nmap command: " + e.getMessage()); } catch (InterruptedException e) { nmapCommandOutput.add("Execution of Nmap command was canceled."); } catch (ExecutionException e) { nmapCommandOutput.add("An exception occurred while running the Nmap command: " + e.getMessage()); } catch (TimeoutException e) { addTimeoutErrorMessage(nmapCommandOutput); } operatingSystemDetails.addCommandOutput(NMAP, nmapCommandOutput); return operatingSystemDetails; }
NmapOsDetectorService implements OperatingSystemDetector { @Override public OperatingSystemDetails detectOs(OsDetectorInputs osDetectorInputs) { OperatingSystemDetails operatingSystemDetails = new OperatingSystemDetails(); List<String> nmapCommandOutput = new ArrayList<>(); try { int timeout = parseInt(osDetectorInputs.getNmapTimeout()); ProcessResponseEntity responseEntity = new ProcessExecutor().execute(buildNmapCommand(osDetectorInputs), timeout); if (responseEntity.isTimeout()) { addTimeoutErrorMessage(nmapCommandOutput); } else { String stdOut = responseEntity.getStdout(); String stdErrors = responseEntity.getStderr(); int exitCode = responseEntity.getExitCode(); if (!isEmpty(stdOut)) { nmapCommandOutput.add(stdOut); String osName = osDetectorHelperService.cropValue(stdOut, OS_DETAILS, System.lineSeparator()); operatingSystemDetails.setName(osName); operatingSystemDetails.setFamily(osDetectorHelperService.resolveOsFamily(osName)); } if (!isEmpty(stdErrors)) { nmapCommandOutput.add(stdErrors); } nmapCommandOutput.add("Execution of Nmap command had exit code: " + exitCode); } } catch (IOException e) { nmapCommandOutput.add("Failed to run Nmap command: " + e.getMessage()); } catch (InterruptedException e) { nmapCommandOutput.add("Execution of Nmap command was canceled."); } catch (ExecutionException e) { nmapCommandOutput.add("An exception occurred while running the Nmap command: " + e.getMessage()); } catch (TimeoutException e) { addTimeoutErrorMessage(nmapCommandOutput); } operatingSystemDetails.addCommandOutput(NMAP, nmapCommandOutput); return operatingSystemDetails; } }
NmapOsDetectorService implements OperatingSystemDetector { @Override public OperatingSystemDetails detectOs(OsDetectorInputs osDetectorInputs) { OperatingSystemDetails operatingSystemDetails = new OperatingSystemDetails(); List<String> nmapCommandOutput = new ArrayList<>(); try { int timeout = parseInt(osDetectorInputs.getNmapTimeout()); ProcessResponseEntity responseEntity = new ProcessExecutor().execute(buildNmapCommand(osDetectorInputs), timeout); if (responseEntity.isTimeout()) { addTimeoutErrorMessage(nmapCommandOutput); } else { String stdOut = responseEntity.getStdout(); String stdErrors = responseEntity.getStderr(); int exitCode = responseEntity.getExitCode(); if (!isEmpty(stdOut)) { nmapCommandOutput.add(stdOut); String osName = osDetectorHelperService.cropValue(stdOut, OS_DETAILS, System.lineSeparator()); operatingSystemDetails.setName(osName); operatingSystemDetails.setFamily(osDetectorHelperService.resolveOsFamily(osName)); } if (!isEmpty(stdErrors)) { nmapCommandOutput.add(stdErrors); } nmapCommandOutput.add("Execution of Nmap command had exit code: " + exitCode); } } catch (IOException e) { nmapCommandOutput.add("Failed to run Nmap command: " + e.getMessage()); } catch (InterruptedException e) { nmapCommandOutput.add("Execution of Nmap command was canceled."); } catch (ExecutionException e) { nmapCommandOutput.add("An exception occurred while running the Nmap command: " + e.getMessage()); } catch (TimeoutException e) { addTimeoutErrorMessage(nmapCommandOutput); } operatingSystemDetails.addCommandOutput(NMAP, nmapCommandOutput); return operatingSystemDetails; } NmapOsDetectorService(OsDetectorHelperService osDetectorHelperService); }
NmapOsDetectorService implements OperatingSystemDetector { @Override public OperatingSystemDetails detectOs(OsDetectorInputs osDetectorInputs) { OperatingSystemDetails operatingSystemDetails = new OperatingSystemDetails(); List<String> nmapCommandOutput = new ArrayList<>(); try { int timeout = parseInt(osDetectorInputs.getNmapTimeout()); ProcessResponseEntity responseEntity = new ProcessExecutor().execute(buildNmapCommand(osDetectorInputs), timeout); if (responseEntity.isTimeout()) { addTimeoutErrorMessage(nmapCommandOutput); } else { String stdOut = responseEntity.getStdout(); String stdErrors = responseEntity.getStderr(); int exitCode = responseEntity.getExitCode(); if (!isEmpty(stdOut)) { nmapCommandOutput.add(stdOut); String osName = osDetectorHelperService.cropValue(stdOut, OS_DETAILS, System.lineSeparator()); operatingSystemDetails.setName(osName); operatingSystemDetails.setFamily(osDetectorHelperService.resolveOsFamily(osName)); } if (!isEmpty(stdErrors)) { nmapCommandOutput.add(stdErrors); } nmapCommandOutput.add("Execution of Nmap command had exit code: " + exitCode); } } catch (IOException e) { nmapCommandOutput.add("Failed to run Nmap command: " + e.getMessage()); } catch (InterruptedException e) { nmapCommandOutput.add("Execution of Nmap command was canceled."); } catch (ExecutionException e) { nmapCommandOutput.add("An exception occurred while running the Nmap command: " + e.getMessage()); } catch (TimeoutException e) { addTimeoutErrorMessage(nmapCommandOutput); } operatingSystemDetails.addCommandOutput(NMAP, nmapCommandOutput); return operatingSystemDetails; } NmapOsDetectorService(OsDetectorHelperService osDetectorHelperService); @Override OperatingSystemDetails detectOs(OsDetectorInputs osDetectorInputs); @NotNull String appendProxyArgument(String nmapArguments, String proxyHost, String proxyPort); }
NmapOsDetectorService implements OperatingSystemDetector { @Override public OperatingSystemDetails detectOs(OsDetectorInputs osDetectorInputs) { OperatingSystemDetails operatingSystemDetails = new OperatingSystemDetails(); List<String> nmapCommandOutput = new ArrayList<>(); try { int timeout = parseInt(osDetectorInputs.getNmapTimeout()); ProcessResponseEntity responseEntity = new ProcessExecutor().execute(buildNmapCommand(osDetectorInputs), timeout); if (responseEntity.isTimeout()) { addTimeoutErrorMessage(nmapCommandOutput); } else { String stdOut = responseEntity.getStdout(); String stdErrors = responseEntity.getStderr(); int exitCode = responseEntity.getExitCode(); if (!isEmpty(stdOut)) { nmapCommandOutput.add(stdOut); String osName = osDetectorHelperService.cropValue(stdOut, OS_DETAILS, System.lineSeparator()); operatingSystemDetails.setName(osName); operatingSystemDetails.setFamily(osDetectorHelperService.resolveOsFamily(osName)); } if (!isEmpty(stdErrors)) { nmapCommandOutput.add(stdErrors); } nmapCommandOutput.add("Execution of Nmap command had exit code: " + exitCode); } } catch (IOException e) { nmapCommandOutput.add("Failed to run Nmap command: " + e.getMessage()); } catch (InterruptedException e) { nmapCommandOutput.add("Execution of Nmap command was canceled."); } catch (ExecutionException e) { nmapCommandOutput.add("An exception occurred while running the Nmap command: " + e.getMessage()); } catch (TimeoutException e) { addTimeoutErrorMessage(nmapCommandOutput); } operatingSystemDetails.addCommandOutput(NMAP, nmapCommandOutput); return operatingSystemDetails; } NmapOsDetectorService(OsDetectorHelperService osDetectorHelperService); @Override OperatingSystemDetails detectOs(OsDetectorInputs osDetectorInputs); @NotNull String appendProxyArgument(String nmapArguments, String proxyHost, String proxyPort);  }