@Test public void testProcessOutputForWindows() { final String windowsOsCmdOutput = "Host Name: hostname\n" + "OS Name: Microsoft Windows 10 Enterprise\n" + "OS Version: 10\n" + "OS Manufacturer: Microsoft Corporation\n" + "OS Configuration: Member Workstation\n" + "OS Build Type: Multiprocessor Free\n" + "Registered Owner: Owner\n" + "Registered Organization: Organisation\n" + "Product ID: 00329-00000-00003-AA434\n" + "Original Install Date: 04/09/2017, 10:12:27\n" + "System Boot Time: 05/12/2017, 09:57:09\n" + "System Manufacturer: Hewlett-Packard\n" + "System Model: HP ZBook 15 G2\n" + "System Type: x64-based PC\n"; OperatingSystemDetails actualOsDetails = osDetectorHelperService.processOutput(new OperatingSystemDetails(), new HashMap<String, String>() {{ put("returnResult", windowsOsCmdOutput); put("returnCode", "0"); }}, "det option"); assertEquals("Microsoft Windows 10 Enterprise", actualOsDetails.getName()); assertEquals("10", actualOsDetails.getVersion()); assertEquals("Windows", actualOsDetails.getFamily()); assertEquals("x64-based PC", actualOsDetails.getArchitecture()); assertEquals(singletonList(windowsOsCmdOutput), actualOsDetails.getCommandsOutput().get("det option")); }
public OperatingSystemDetails processOutput(OperatingSystemDetails operatingSystemDetails, Map<String, String> execute, String detectOption) { String resultString = execute.get(RETURN_RESULT); operatingSystemDetails.addCommandOutput(detectOption, singletonList(resultString)); String lineSeparator = contains(resultString, WINDOWS_LINE_SEPARATOR) ? WINDOWS_LINE_SEPARATOR : contains(resultString, UNIX_LINE_SEPARATOR) ? UNIX_LINE_SEPARATOR : EARLY_MAC_LINE_SEPARATOR; if (isWindows(execute)) { operatingSystemDetails.setFamily(WINDOWS); operatingSystemDetails.setName(strip(cropValue(resultString, WINDOWS_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, WINDOWS_OS_VERSION_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setArchitecture(strip(cropValue(resultString, WINDOWS_OS_ARCHITECTURE_KEY, lineSeparator), QUOTES)); return operatingSystemDetails; } else if (isUnix(execute)){ String osFamily = strip(cropValue(resultString, UNIX_OS_FAMILY_KEY, lineSeparator), QUOTES); operatingSystemDetails.setFamily(osFamily); operatingSystemDetails.setArchitecture(strip(cropValue(resultString, LINUX_OS_ARCHITECTURE_KEY, lineSeparator), QUOTES)); if (equalsIgnoreCase(osFamily, "darwin")) { operatingSystemDetails.setName(strip(cropValue(resultString, MAC_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, MAC_OS_VERSION_KEY, lineSeparator), QUOTES)); } else { operatingSystemDetails.setName(strip(cropValue(resultString, LINUX_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, LINUX_OS_VERSION_KEY, lineSeparator), QUOTES)); } return operatingSystemDetails; } return operatingSystemDetails; }
OsDetectorHelperService { public OperatingSystemDetails processOutput(OperatingSystemDetails operatingSystemDetails, Map<String, String> execute, String detectOption) { String resultString = execute.get(RETURN_RESULT); operatingSystemDetails.addCommandOutput(detectOption, singletonList(resultString)); String lineSeparator = contains(resultString, WINDOWS_LINE_SEPARATOR) ? WINDOWS_LINE_SEPARATOR : contains(resultString, UNIX_LINE_SEPARATOR) ? UNIX_LINE_SEPARATOR : EARLY_MAC_LINE_SEPARATOR; if (isWindows(execute)) { operatingSystemDetails.setFamily(WINDOWS); operatingSystemDetails.setName(strip(cropValue(resultString, WINDOWS_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, WINDOWS_OS_VERSION_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setArchitecture(strip(cropValue(resultString, WINDOWS_OS_ARCHITECTURE_KEY, lineSeparator), QUOTES)); return operatingSystemDetails; } else if (isUnix(execute)){ String osFamily = strip(cropValue(resultString, UNIX_OS_FAMILY_KEY, lineSeparator), QUOTES); operatingSystemDetails.setFamily(osFamily); operatingSystemDetails.setArchitecture(strip(cropValue(resultString, LINUX_OS_ARCHITECTURE_KEY, lineSeparator), QUOTES)); if (equalsIgnoreCase(osFamily, "darwin")) { operatingSystemDetails.setName(strip(cropValue(resultString, MAC_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, MAC_OS_VERSION_KEY, lineSeparator), QUOTES)); } else { operatingSystemDetails.setName(strip(cropValue(resultString, LINUX_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, LINUX_OS_VERSION_KEY, lineSeparator), QUOTES)); } return operatingSystemDetails; } return operatingSystemDetails; } }
OsDetectorHelperService { public OperatingSystemDetails processOutput(OperatingSystemDetails operatingSystemDetails, Map<String, String> execute, String detectOption) { String resultString = execute.get(RETURN_RESULT); operatingSystemDetails.addCommandOutput(detectOption, singletonList(resultString)); String lineSeparator = contains(resultString, WINDOWS_LINE_SEPARATOR) ? WINDOWS_LINE_SEPARATOR : contains(resultString, UNIX_LINE_SEPARATOR) ? UNIX_LINE_SEPARATOR : EARLY_MAC_LINE_SEPARATOR; if (isWindows(execute)) { operatingSystemDetails.setFamily(WINDOWS); operatingSystemDetails.setName(strip(cropValue(resultString, WINDOWS_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, WINDOWS_OS_VERSION_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setArchitecture(strip(cropValue(resultString, WINDOWS_OS_ARCHITECTURE_KEY, lineSeparator), QUOTES)); return operatingSystemDetails; } else if (isUnix(execute)){ String osFamily = strip(cropValue(resultString, UNIX_OS_FAMILY_KEY, lineSeparator), QUOTES); operatingSystemDetails.setFamily(osFamily); operatingSystemDetails.setArchitecture(strip(cropValue(resultString, LINUX_OS_ARCHITECTURE_KEY, lineSeparator), QUOTES)); if (equalsIgnoreCase(osFamily, "darwin")) { operatingSystemDetails.setName(strip(cropValue(resultString, MAC_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, MAC_OS_VERSION_KEY, lineSeparator), QUOTES)); } else { operatingSystemDetails.setName(strip(cropValue(resultString, LINUX_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, LINUX_OS_VERSION_KEY, lineSeparator), QUOTES)); } return operatingSystemDetails; } return operatingSystemDetails; }  }
OsDetectorHelperService { public OperatingSystemDetails processOutput(OperatingSystemDetails operatingSystemDetails, Map<String, String> execute, String detectOption) { String resultString = execute.get(RETURN_RESULT); operatingSystemDetails.addCommandOutput(detectOption, singletonList(resultString)); String lineSeparator = contains(resultString, WINDOWS_LINE_SEPARATOR) ? WINDOWS_LINE_SEPARATOR : contains(resultString, UNIX_LINE_SEPARATOR) ? UNIX_LINE_SEPARATOR : EARLY_MAC_LINE_SEPARATOR; if (isWindows(execute)) { operatingSystemDetails.setFamily(WINDOWS); operatingSystemDetails.setName(strip(cropValue(resultString, WINDOWS_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, WINDOWS_OS_VERSION_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setArchitecture(strip(cropValue(resultString, WINDOWS_OS_ARCHITECTURE_KEY, lineSeparator), QUOTES)); return operatingSystemDetails; } else if (isUnix(execute)){ String osFamily = strip(cropValue(resultString, UNIX_OS_FAMILY_KEY, lineSeparator), QUOTES); operatingSystemDetails.setFamily(osFamily); operatingSystemDetails.setArchitecture(strip(cropValue(resultString, LINUX_OS_ARCHITECTURE_KEY, lineSeparator), QUOTES)); if (equalsIgnoreCase(osFamily, "darwin")) { operatingSystemDetails.setName(strip(cropValue(resultString, MAC_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, MAC_OS_VERSION_KEY, lineSeparator), QUOTES)); } else { operatingSystemDetails.setName(strip(cropValue(resultString, LINUX_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, LINUX_OS_VERSION_KEY, lineSeparator), QUOTES)); } return operatingSystemDetails; } return operatingSystemDetails; }  String formatOsCommandsOutput(Map<String, List<String>> commandsOutput); void validateNmapInputs(OsDetectorInputs osDetectorInputs, NmapOsDetectorService nmapOsDetectorService); String cropValue(String stdOut, String begin, String end); boolean foundOperatingSystem(OperatingSystemDetails os); OperatingSystemDetails processOutput(OperatingSystemDetails operatingSystemDetails, Map<String, String> execute, String detectOption); String resolveOsFamily(String osName); }
OsDetectorHelperService { public OperatingSystemDetails processOutput(OperatingSystemDetails operatingSystemDetails, Map<String, String> execute, String detectOption) { String resultString = execute.get(RETURN_RESULT); operatingSystemDetails.addCommandOutput(detectOption, singletonList(resultString)); String lineSeparator = contains(resultString, WINDOWS_LINE_SEPARATOR) ? WINDOWS_LINE_SEPARATOR : contains(resultString, UNIX_LINE_SEPARATOR) ? UNIX_LINE_SEPARATOR : EARLY_MAC_LINE_SEPARATOR; if (isWindows(execute)) { operatingSystemDetails.setFamily(WINDOWS); operatingSystemDetails.setName(strip(cropValue(resultString, WINDOWS_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, WINDOWS_OS_VERSION_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setArchitecture(strip(cropValue(resultString, WINDOWS_OS_ARCHITECTURE_KEY, lineSeparator), QUOTES)); return operatingSystemDetails; } else if (isUnix(execute)){ String osFamily = strip(cropValue(resultString, UNIX_OS_FAMILY_KEY, lineSeparator), QUOTES); operatingSystemDetails.setFamily(osFamily); operatingSystemDetails.setArchitecture(strip(cropValue(resultString, LINUX_OS_ARCHITECTURE_KEY, lineSeparator), QUOTES)); if (equalsIgnoreCase(osFamily, "darwin")) { operatingSystemDetails.setName(strip(cropValue(resultString, MAC_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, MAC_OS_VERSION_KEY, lineSeparator), QUOTES)); } else { operatingSystemDetails.setName(strip(cropValue(resultString, LINUX_OS_NAME_KEY, lineSeparator), QUOTES)); operatingSystemDetails.setVersion(strip(cropValue(resultString, LINUX_OS_VERSION_KEY, lineSeparator), QUOTES)); } return operatingSystemDetails; } return operatingSystemDetails; }  String formatOsCommandsOutput(Map<String, List<String>> commandsOutput); void validateNmapInputs(OsDetectorInputs osDetectorInputs, NmapOsDetectorService nmapOsDetectorService); String cropValue(String stdOut, String begin, String end); boolean foundOperatingSystem(OperatingSystemDetails os); OperatingSystemDetails processOutput(OperatingSystemDetails operatingSystemDetails, Map<String, String> execute, String detectOption); String resolveOsFamily(String osName);  }