ScriptValidationService extends AbstractScriptValidationService { @Override public String validate(User user, IFileEntry scriptIEntry, boolean useScriptInSVN, String hostString) { FileEntry scriptEntry = cast(scriptIEntry); try { checkNotNull(scriptEntry, "scriptEntity should be not null"); checkNotEmpty(scriptEntry.getPath(), "scriptEntity path should be provided"); if (!useScriptInSVN) { checkNotEmpty(scriptEntry.getContent(), "scriptEntity content should be provided"); } checkNotNull(user, "user should be provided"); ScriptHandler handler = scriptHandlerFactory.getHandler(scriptEntry); if (config.getControllerProperties().getPropertyBoolean(PROP_CONTROLLER_VALIDATION_SYNTAX_CHECK)) { String result = handler.checkSyntaxErrors(scriptEntry.getPath(), scriptEntry.getContent()); LOGGER.info("Perform Syntax Check by {} for {}", user.getUserId(), scriptEntry.getPath()); if (result != null) { return result; } } File scriptDirectory = config.getHome().getScriptDirectory(user); FileUtils.deleteDirectory(scriptDirectory); Preconditions.checkTrue(scriptDirectory.mkdirs(), "Script directory {} creation is failed."); ProcessingResultPrintStream processingResult = new ProcessingResultPrintStream(new ByteArrayOutputStream()); handler.prepareDist(0L, user, scriptEntry, scriptDirectory, config.getControllerProperties(), processingResult); if (!processingResult.isSuccess()) { return new String(processingResult.getLogByteArray()); } File scriptFile = new File(scriptDirectory, FilenameUtils.getName(scriptEntry.getPath())); if (useScriptInSVN) { fileEntryService.writeContentTo(user, scriptEntry.getPath(), scriptDirectory); } else { FileUtils.writeStringToFile(scriptFile, scriptEntry.getContent(), StringUtils.defaultIfBlank(scriptEntry.getEncoding(), "UTF-8")); } File doValidate = localScriptTestDriveService.doValidate(scriptDirectory, scriptFile, new Condition(), config.isSecurityEnabled(), config.getSecurityLevel(), hostString, getTimeout()); List<String> readLines = FileUtils.readLines(doValidate); StringBuilder output = new StringBuilder(); File homeDirectory = config.getHome().getDirectory(); String absolutePath = homeDirectory.getAbsolutePath(); String realPath = homeDirectory.toPath().toRealPath().toString(); for (String each : readLines) { if (!each.startsWith("*sys-package-mgr")) { each = each .replace(absolutePath, "${NGRINDER_HOME}") .replace(realPath, "${NGRINDER_HOME}"); output.append(each).append("\n"); } } return output.toString(); } catch (Exception e) { throw processException(e); } }  @Override String validate(User user, IFileEntry scriptIEntry, boolean useScriptInSVN, String hostString);  }
@Test(timeout = 30000) public void testInfiniteScriptValidation() throws IOException { String script = IOUtils.toString(new ClassPathResource("/validation/script_loop.py").getInputStream()); FileEntry fileEntry = new FileEntry(); fileEntry.setPath("/script.py"); fileEntry.setContent(script); scriptValidationService.validate(getTestUser(), fileEntry, false, ""); }
@Test public void testNormalScriptValidation() throws IOException { String script = IOUtils.toString(new ClassPathResource("/validation/script_1time.py").getInputStream()); FileEntry fileEntry = new FileEntry(); fileEntry.setPath("/script2.py"); fileEntry.setContent(script); String validateScript = scriptValidationService.validate(getTestUser(), fileEntry, false, ""); assertThat(validateScript, not(containsString("Validation should be performed within"))); assertThat(validateScript.length(), lessThan(10000)); }
@Test public void testScriptValidationWithSvnScript() throws IOException { String script = IOUtils.toString(new ClassPathResource("/validation/script_1time.py").getInputStream()); FileEntry fileEntry = new FileEntry(); fileEntry.setPath("/script2.py"); fileEntry.setContent(script); fileEntryService.save(getTestUser(), fileEntry); fileEntry.setContent(""); String validateScript = scriptValidationService.validate(getTestUser(), fileEntry, true, ""); assertThat(validateScript, not(containsString("Validation should be performed"))); assertThat(validateScript.length(), lessThan(10000)); }