protected override IExecutionResult <TestResult> ExecuteAgainstTestsInput( IExecutionContext <TestsInputModel> executionContext) { executionContext.SanitizeContent(); var result = new ExecutionResult <TestResult>(); var userSubmissionContent = executionContext.FileContent; this.ExtractFilesInWorkingDirectory(userSubmissionContent, this.WorkingDirectory); var csProjFilePath = this.GetCsProjFilePath(); var compilerPath = this.GetCompilerPathFunc(executionContext.CompilerType); var compilerResult = this.Compile( executionContext.CompilerType, compilerPath, executionContext.AdditionalCompilerArguments, csProjFilePath); result.IsCompiledSuccessfully = compilerResult.IsCompiledSuccessfully; if (!result.IsCompiledSuccessfully) { result.CompilerComment = compilerResult.CompilerComment; return(result); } var executor = new RestrictedProcessExecutor(this.BaseTimeUsed, this.BaseMemoryUsed); var checker = executionContext.Input.GetChecker(); var arguments = new string[] { compilerResult.OutputFile, AdditionalExecutionArguments }; foreach (var test in executionContext.Input.Tests) { var processExecutionResult = executor.Execute( compilerPath, test.Input, executionContext.TimeLimit, executionContext.MemoryLimit, arguments, this.WorkingDirectory); var testResult = this.ExecuteAndCheckTest( test, processExecutionResult, checker, processExecutionResult.ReceivedOutput); result.Results.Add(testResult); } return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var solution = executionContext.FileContent; var result = new ExecutionResult(); var compileResult = this.ExecuteCompiling(executionContext, this.getCompilerPathFunc, result); if (!compileResult.IsCompiledSuccessfully) { return(result); } var outputAssemblyPath = this.PreprocessAndCompileTestRunner(executionContext, compileResult.OutputFile); IExecutor executor = new RestrictedProcessExecutor(); var processExecutionResult = executor.Execute(outputAssemblyPath, string.Empty, executionContext.TimeLimit, executionContext.MemoryLimit); var workingDirectory = compileResult.OutputFile; if (Directory.Exists(workingDirectory)) { Directory.Delete(workingDirectory, true); } this.ProcessTests(processExecutionResult, executionContext, result); return(result); }
private static void StartRestrictedProcess(string applicationPath, string textToWrite, int timeLimit, int memoryLimit) { var restrictedProcessExecutor = new RestrictedProcessExecutor(); Console.WriteLine("-------------------- Starting RestrictedProcess... --------------------"); var result = restrictedProcessExecutor.Execute(applicationPath, textToWrite, timeLimit, memoryLimit); Console.WriteLine("------------ Process output: {0} symbols. First 2048 symbols: ------------ ", result.ReceivedOutput.Length); Console.WriteLine(result.ReceivedOutput.Substring(0, Math.Min(2048, result.ReceivedOutput.Length))); if (!string.IsNullOrWhiteSpace(result.ErrorOutput)) { Console.WriteLine( "------------ Process error: {0} symbols. First 2048 symbols: ------------ ", result.ErrorOutput.Length); Console.WriteLine(result.ErrorOutput.Substring(0, Math.Min(2048, result.ErrorOutput.Length))); } Console.WriteLine("------------ Process info ------------ "); Console.WriteLine(ProcessInfoFormat, "Type:", result.Type); Console.WriteLine(ProcessInfoFormat, "ExitCode:", string.Format("{0} ({1})", result.ExitCode, new Win32Exception(result.ExitCode).Message)); Console.WriteLine(ProcessInfoFormat, "Total time:", result.TimeWorked); Console.WriteLine(ProcessInfoFormat, "PrivilegedProcessorTime:", result.PrivilegedProcessorTime); Console.WriteLine(ProcessInfoFormat, "UserProcessorTime:", result.UserProcessorTime); Console.WriteLine(ProcessInfoFormat, "Memory:", string.Format("{0:0.00}MB", result.MemoryUsed / 1024.0 / 1024.0)); Console.WriteLine(new string('-', 79)); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var result = new ExecutionResult(); var userSubmissionContent = executionContext.FileContent; var submissionFilePath = $"{this.WorkingDirectory}\\{ZippedSubmissionName}"; File.WriteAllBytes(submissionFilePath, userSubmissionContent); FileHelpers.UnzipFile(submissionFilePath, this.WorkingDirectory); File.Delete(submissionFilePath); var csProjFilePath = FileHelpers.FindFirstFileMatchingPattern( this.WorkingDirectory, CsProjFileSearchPattern); this.ExtractTestNames(executionContext.Tests); var project = new Project(csProjFilePath); this.CorrectProjectReferences(executionContext.Tests, project); project.Save(csProjFilePath); project.ProjectCollection.UnloadAllProjects(); result.IsCompiledSuccessfully = true; var executor = new RestrictedProcessExecutor(); var checker = Checker.CreateChecker( executionContext.CheckerAssemblyName, executionContext.CheckerTypeName, executionContext.CheckerParameter); result = this.RunUnitTests(executionContext, executor, checker, result, csProjFilePath); return(result); }
protected override IExecutionResult <TestResult> ExecuteAgainstTestsInput( IExecutionContext <TestsInputModel> executionContext) { var result = new ExecutionResult <TestResult>(); var userSubmissionContent = executionContext.FileContent; this.ExtractFilesInWorkingDirectory(userSubmissionContent, this.WorkingDirectory); var csProjFilePath = this.GetCsProjFilePath(); var project = new Project(csProjFilePath); this.SaveSetupFixture(project.DirectoryPath); this.CorrectProjectReferences(project); var executor = new RestrictedProcessExecutor(this.BaseTimeUsed, this.BaseMemoryUsed); result = this.RunUnitTests( this.NUnitConsoleRunnerPath, executionContext, executor, executionContext.Input.GetChecker(), result, csProjFilePath, AdditionalExecutionArguments); return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var result = new ExecutionResult { IsCompiledSuccessfully = true }; this.CreateSubmissionFile(executionContext); this.ProgramEntryPath = FileHelpers.FindFileMatchingPattern(this.WorkingDirectory, EntryFileName); var codeToExecute = this.PreprocessJsSubmission( this.JsCodeTemplate, executionContext, this.ProgramEntryPath); var codeSavePath = FileHelpers.SaveStringToTempFile(this.WorkingDirectory, codeToExecute); var executor = new RestrictedProcessExecutor(); var checker = Checker.CreateChecker( executionContext.CheckerAssemblyName, executionContext.CheckerTypeName, executionContext.CheckerParameter); result.TestResults = this.ProcessTests(executionContext, executor, checker, codeSavePath); File.Delete(codeSavePath); return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var result = new ExecutionResult(); // In NodeJS there is no compilation result.IsCompiledSuccessfully = true; // Preprocess the user submission var codeToExecute = this.PreprocessJsSubmission( this.JsCodeTemplate, executionContext); // Save the preprocessed submission which is ready for execution var codeSavePath = FileHelpers.SaveStringToTempFile(this.WorkingDirectory, codeToExecute); // Process the submission and check each test var executor = new RestrictedProcessExecutor(); var checker = Checker.CreateChecker( executionContext.CheckerAssemblyName, executionContext.CheckerTypeName, executionContext.CheckerParameter); result.TestResults = this.ProcessTests(executionContext, executor, checker, codeSavePath); // Clean up File.Delete(codeSavePath); return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var result = new ExecutionResult(); // setting the IsCompiledSuccessfully variable to true as in the NodeJS // execution strategy there is no compilation result.IsCompiledSuccessfully = true; // Preprocess the user submission var codeToExecute = this.PreprocessJsSubmission(this.JsCodeTemplate, executionContext.Code); // Save the preprocessed submission which is ready for execution var codeSavePath = FileHelpers.SaveStringToTempFile(codeToExecute); // Process the submission and check each test IExecutor executor = new RestrictedProcessExecutor(); IChecker checker = Checker.CreateChecker(executionContext.CheckerAssemblyName, executionContext.CheckerTypeName, executionContext.CheckerParameter); result.TestResults = new List <TestResult>(); foreach (var test in executionContext.Tests) { var processExecutionResult = executor.Execute(this.nodeJsExecutablePath, test.Input, executionContext.TimeLimit, executionContext.MemoryLimit, new[] { codeSavePath }); var testResult = this.ExecuteAndCheckTest(test, processExecutionResult, checker, processExecutionResult.ReceivedOutput); result.TestResults.Add(testResult); } // Clean up File.Delete(codeSavePath); return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { Directory.CreateDirectory(this.NUnitLiteConsoleAppDirectory); Directory.CreateDirectory(this.UserProjectDirectory); var result = new ExecutionResult(); var userSubmission = executionContext.FileContent; this.ExtractFilesInWorkingDirectory(userSubmission, this.UserProjectDirectory); this.ExtractTestNames(executionContext.Tests); this.SaveTestFiles(executionContext.Tests, this.NUnitLiteConsoleAppDirectory); this.SaveSetupFixture(this.NUnitLiteConsoleAppDirectory); var userCsProjPaths = FileHelpers.FindAllFilesMatchingPattern( this.UserProjectDirectory, CsProjFileSearchPattern); var nunitLiteConsoleAppCsProjPath = this.CreateNunitLiteConsoleApp( NUnitLiteConsoleAppProgramTemplate, this.nunitLiteConsoleAppCsProjTemplate, this.NUnitLiteConsoleAppDirectory, userCsProjPaths); var compilerPath = this.GetCompilerPathFunc(executionContext.CompilerType); var compilerResult = this.Compile( executionContext.CompilerType, compilerPath, executionContext.AdditionalCompilerArguments, nunitLiteConsoleAppCsProjPath); result.IsCompiledSuccessfully = compilerResult.IsCompiledSuccessfully; if (!result.IsCompiledSuccessfully) { result.CompilerComment = compilerResult.CompilerComment; return(result); } // Delete tests before execution so the user can't access them FileHelpers.DeleteFiles(this.TestPaths.ToArray()); var executor = new RestrictedProcessExecutor(); var checker = Checker.CreateChecker( executionContext.CheckerAssemblyName, executionContext.CheckerTypeName, executionContext.CheckerParameter); result = this.RunUnitTests( compilerPath, executionContext, executor, checker, result, compilerResult.OutputFile, AdditionalExecutionArguments); return(result); }
protected override IExecutionResult <TestResult> ExecuteAgainstTestsInput( IExecutionContext <TestsInputModel> executionContext) { // In NodeJS there is no compilation var result = new ExecutionResult <TestResult>() { IsCompiledSuccessfully = true }; var executor = new RestrictedProcessExecutor(this.BaseTimeUsed, this.BaseMemoryUsed); // Preprocess the user submission var codeToExecute = this.PreprocessJsSubmission( this.JsCodeTemplate, executionContext); // Save the preprocessed submission which is ready for execution var codeSavePath = FileHelpers.SaveStringToTempFile(this.WorkingDirectory, codeToExecute); // Process the submission and check each test result.Results = this.ProcessTests( executionContext, executor, executionContext.Input.GetChecker(), codeSavePath); // Clean up File.Delete(codeSavePath); return(result); }
protected override IExecutionResult <TestResult> ExecuteAgainstTestsInput( IExecutionContext <TestsInputModel> executionContext) { var result = new ExecutionResult <TestResult> { IsCompiledSuccessfully = true }; this.CreateSubmissionFile(executionContext); this.ProgramEntryPath = FileHelpers.FindFileMatchingPattern(this.WorkingDirectory, EntryFileName); var codeToExecute = this.PreprocessJsSubmission( this.JsCodeTemplate, executionContext, this.ProgramEntryPath); var codeSavePath = FileHelpers.SaveStringToTempFile(this.WorkingDirectory, codeToExecute); var executor = new RestrictedProcessExecutor(this.BaseTimeUsed, this.BaseMemoryUsed); result.Results = this.ProcessTests( executionContext, executor, executionContext.Input.GetChecker(), codeSavePath); File.Delete(codeSavePath); return(result); }
protected override IExecutionResult <TestResult> ExecuteAgainstTestsInput( IExecutionContext <TestsInputModel> executionContext) { var result = new ExecutionResult <TestResult> { IsCompiledSuccessfully = true }; // Copy and unzip the file (save file to WorkingDirectory) this.CreateSubmissionFile(executionContext); this.ProgramEntryPath = FileHelpers.FindFileMatchingPattern(this.WorkingDirectory, AppJsFileName); // Replace the placeholders in the JS Template with the real values var codeToExecute = this.PreprocessJsSubmission( this.JsCodeTemplate, executionContext, this.ProgramEntryPath); // Save code to file var codeSavePath = FileHelpers.SaveStringToTempFile(this.WorkingDirectory, codeToExecute); // Create a Restricted Process Executor var executor = new RestrictedProcessExecutor(this.BaseTimeUsed, this.BaseMemoryUsed); // Process tests result.Results = this.ProcessTests( executionContext, executor, executionContext.Input.GetChecker(), codeSavePath); // Clean up File.Delete(codeSavePath); return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var result = new ExecutionResult(); var userSubmissionContent = executionContext.FileContent; this.ExtractFilesInWorkingDirectory(userSubmissionContent, this.WorkingDirectory); var csProjFilePath = this.GetCsProjFilePath(); var project = new Project(csProjFilePath); this.SaveSetupFixture(project.DirectoryPath); this.CorrectProjectReferences(project); project.Save(csProjFilePath); project.ProjectCollection.UnloadAllProjects(); var executor = new RestrictedProcessExecutor(); var checker = Checker.CreateChecker( executionContext.CheckerAssemblyName, executionContext.CheckerTypeName, executionContext.CheckerParameter); result = this.RunUnitTests( this.NUnitConsoleRunnerPath, executionContext, executor, checker, result, csProjFilePath, AdditionalExecutionArguments); return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { IExecutor executor = new RestrictedProcessExecutor(); var result = this.CompileExecuteAndCheck(executionContext, this.GetCompilerPathFunc, executor, false); return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var result = new ExecutionResult(); // PHP code is not compiled result.IsCompiledSuccessfully = true; var codeSavePath = FileHelpers.SaveStringToTempFile(this.WorkingDirectory, executionContext.Code); // Process the submission and check each test var executor = new RestrictedProcessExecutor(); var checker = Checker.CreateChecker(executionContext.CheckerAssemblyName, executionContext.CheckerTypeName, executionContext.CheckerParameter); result.TestResults = new List <TestResult>(); foreach (var test in executionContext.Tests) { var processExecutionResult = executor.Execute( this.phpCgiExecutablePath, string.Empty, // Input data is passed as the last execution argument executionContext.TimeLimit, executionContext.MemoryLimit, new[] { FileToExecuteOption, codeSavePath, string.Format("\"{0}\"", test.Input) }); var testResult = this.ExecuteAndCheckTest(test, processExecutionResult, checker, processExecutionResult.ReceivedOutput); result.TestResults.Add(testResult); } // Clean up File.Delete(codeSavePath); return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var result = new ExecutionResult(); // Python code is not compiled result.IsCompiledSuccessfully = true; var codeSavePath = FileHelpers.SaveStringToTempFile(executionContext.Code); // Process the submission and check each test var executor = new RestrictedProcessExecutor(); var checker = Checker.CreateChecker(executionContext.CheckerAssemblyName, executionContext.CheckerTypeName, executionContext.CheckerParameter); result.TestResults = new List <TestResult>(); foreach (var test in executionContext.Tests) { var processExecutionResult = executor.Execute( this.pythonExecutablePath, test.Input, executionContext.TimeLimit, executionContext.MemoryLimit, new[] { PythonIsolatedModeArgument, PythonOptimizeAndDiscardDocstringsArgument, codeSavePath }); var testResult = this.ExecuteAndCheckTest(test, processExecutionResult, checker, processExecutionResult.ReceivedOutput); result.TestResults.Add(testResult); } // Clean up File.Delete(codeSavePath); return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var result = new ExecutionResult(); var userSubmissionContent = executionContext.FileContent; var submissionFilePath = $"{this.WorkingDirectory}\\{ZippedSubmissionName}"; File.WriteAllBytes(submissionFilePath, userSubmissionContent); FileHelpers.RemoveFilesFromZip(submissionFilePath, RemoveMacFolderPattern); FileHelpers.UnzipFile(submissionFilePath, this.WorkingDirectory); File.Delete(submissionFilePath); var csProjFilePath = FileHelpers.FindFileMatchingPattern( this.WorkingDirectory, CsProjFileSearchPattern, this.IsDotNetCoreFile, f => new FileInfo(f).Length); var compilerPath = this.getCompilerPathFunc(executionContext.CompilerType); var compileResult = this.Compile( executionContext.CompilerType, compilerPath, executionContext.AdditionalCompilerArguments, csProjFilePath); if (!compileResult.IsCompiledSuccessfully) { return(result); } result.IsCompiledSuccessfully = compileResult.IsCompiledSuccessfully; var outputAssemblyPath = this.PreprocessAndCompileTestRunner( executionContext, Path.GetDirectoryName(compileResult.OutputFile)); IExecutor executor = new RestrictedProcessExecutor(); var processExecutionResult = executor.Execute( outputAssemblyPath, string.Empty, executionContext.TimeLimit, executionContext.MemoryLimit); if (!string.IsNullOrWhiteSpace(processExecutionResult.ErrorOutput)) { throw new ArgumentException(processExecutionResult.ErrorOutput); } var workingDirectory = compileResult.OutputFile; if (Directory.Exists(workingDirectory)) { Directory.Delete(workingDirectory, true); } this.ProcessTests(processExecutionResult, executionContext, result); return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var result = new ExecutionResult(); var userSubmissionContent = executionContext.FileContent; this.ExtractFilesInWorkingDirectory(userSubmissionContent, this.WorkingDirectory); var csProjFilePath = this.GetCsProjFilePath(); var compilerPath = this.GetCompilerPathFunc(executionContext.CompilerType); var compilerResult = this.Compile( executionContext.CompilerType, compilerPath, executionContext.AdditionalCompilerArguments, csProjFilePath); result.IsCompiledSuccessfully = compilerResult.IsCompiledSuccessfully; if (!result.IsCompiledSuccessfully) { result.CompilerComment = compilerResult.CompilerComment; return(result); } var executor = new RestrictedProcessExecutor(); var checker = Checker.CreateChecker( executionContext.CheckerAssemblyName, executionContext.CheckerTypeName, executionContext.CheckerParameter); var arguments = new string[] { compilerResult.OutputFile, AdditionalExecutionArguments }; foreach (var test in executionContext.Tests) { var processExecutionResult = executor.Execute( compilerPath, test.Input, executionContext.TimeLimit, executionContext.MemoryLimit, arguments, this.WorkingDirectory); var testResult = this.ExecuteAndCheckTest( test, processExecutionResult, checker, processExecutionResult.ReceivedOutput); result.TestResults.Add(testResult); } return(result); }
public void RestrictedProcessShouldNotBlockWhenEnterEndlessLoop() { var exePath = this.CreateExe("EndlessLoop.exe", EndlessLoopSourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 50, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.TimeLimit); }
public void RestrictedProcessShouldStopProgramAfterTimeIsEnded() { var exePath = this.CreateExe("TimeLimit.exe", TimeLimitSourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 100, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.TimeLimit); }
protected override IExecutionResult <TestResult> ExecuteAgainstTestsInput( IExecutionContext <TestsInputModel> executionContext) { executionContext.SanitizeContent(); Directory.CreateDirectory(this.NUnitLiteConsoleAppDirectory); Directory.CreateDirectory(this.UserProjectDirectory); var result = new ExecutionResult <TestResult>(); var userSubmission = executionContext.FileContent; this.ExtractFilesInWorkingDirectory(userSubmission, this.UserProjectDirectory); this.ExtractTestNames(executionContext.Input.Tests); this.SaveTestFiles(executionContext.Input.Tests, this.NUnitLiteConsoleAppDirectory); this.SaveSetupFixture(this.NUnitLiteConsoleAppDirectory); var userCsProjPaths = FileHelpers.FindAllFilesMatchingPattern( this.UserProjectDirectory, CsProjFileSearchPattern); var nUnitLiteConsoleApp = this.CreateNUnitLiteConsoleApp(userCsProjPaths); var compilerPath = this.GetCompilerPathFunc(executionContext.CompilerType); var compilerResult = this.Compile( executionContext.CompilerType, compilerPath, executionContext.AdditionalCompilerArguments, nUnitLiteConsoleApp.csProjPath); result.IsCompiledSuccessfully = compilerResult.IsCompiledSuccessfully; if (!result.IsCompiledSuccessfully) { result.CompilerComment = compilerResult.CompilerComment; return(result); } // Delete tests before execution so the user can't access them FileHelpers.DeleteFiles(this.TestPaths.ToArray()); var executor = new RestrictedProcessExecutor(this.BaseTimeUsed, this.BaseMemoryUsed); result = this.RunUnitTests( compilerPath, executionContext, executor, executionContext.Input.GetChecker(), result, compilerResult.OutputFile, AdditionalExecutionArguments); return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var result = new ExecutionResult(); // PHP code is not compiled result.IsCompiledSuccessfully = true; string submissionPath = $@"{this.WorkingDirectory}\\{ZippedSubmissionName}{GlobalConstants.ZipFileExtension}"; File.WriteAllBytes(submissionPath, executionContext.FileContent); FileHelpers.UnzipFile(submissionPath, this.WorkingDirectory); File.Delete(submissionPath); string applicationEntryPointPath = FileHelpers.FindFileMatchingPattern(this.WorkingDirectory, ApplicationEntryPoint); if (string.IsNullOrEmpty(applicationEntryPointPath)) { throw new ArgumentException($"{ApplicationEntryPoint} not found in submission folder!"); } this.RequireSuperGlobalsTemplateInUserCode(applicationEntryPointPath); var checker = Checker.CreateChecker( executionContext.CheckerAssemblyName, executionContext.CheckerTypeName, executionContext.CheckerParameter); result.TestResults = new List <TestResult>(); var executor = new RestrictedProcessExecutor(); foreach (var test in executionContext.Tests) { File.WriteAllText(this.SuperGlobalsTemplatePath, test.Input); var processExecutionResult = executor.Execute( this.phpCliExecutablePath, string.Empty, executionContext.TimeLimit, executionContext.MemoryLimit, new[] { applicationEntryPointPath }); var testResult = this.ExecuteAndCheckTest( test, processExecutionResult, checker, processExecutionResult.ReceivedOutput); result.TestResults.Add(testResult); } return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var result = new ExecutionResult(); var submissionDestination = $@"{this.WorkingDirectory}\{SubmissionName}"; File.WriteAllBytes(submissionDestination, executionContext.FileContent); FileHelpers.RemoveFilesFromZip(submissionDestination, RemoveMacFolderPattern); if (!string.IsNullOrEmpty(executionContext.TaskSkeletonAsString)) { var pathsOfHeadersAndCppFiles = this.ExtractTaskSkeleton(executionContext.TaskSkeletonAsString); FileHelpers.AddFilesToZipArchive(submissionDestination, string.Empty, pathsOfHeadersAndCppFiles.ToArray()); } var compilerPath = this.getCompilerPathFunc(executionContext.CompilerType); var compilationResult = this.Compile( executionContext.CompilerType, compilerPath, executionContext.AdditionalCompilerArguments, submissionDestination); if (!compilationResult.IsCompiledSuccessfully) { return(result); } result.IsCompiledSuccessfully = true; var executor = new RestrictedProcessExecutor(); var checker = Checker.CreateChecker( executionContext.CheckerAssemblyName, executionContext.CheckerTypeName, executionContext.CheckerParameter); foreach (var test in executionContext.Tests) { var processExecutionResult = executor.Execute( compilationResult.OutputFile, test.Input, executionContext.TimeLimit, executionContext.MemoryLimit); var testResults = this.ExecuteAndCheckTest( test, processExecutionResult, checker, processExecutionResult.ReceivedOutput); result.TestResults.Add(testResults); } return(result); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var result = new ExecutionResult(); var databaseName = this.MySqlHelperStrategy.GetDatabaseName(); // PHP code is not compiled result.IsCompiledSuccessfully = true; string submissionPath = $@"{this.WorkingDirectory}\\{ZippedSubmissionName}{GlobalConstants.ZipFileExtension}"; File.WriteAllBytes(submissionPath, executionContext.FileContent); FileHelpers.UnzipFile(submissionPath, this.WorkingDirectory); File.Delete(submissionPath); this.ReplaceDatabaseConfigurationFile(databaseName); var applicationEntryPointPath = this.AddTestRunnerTemplateToApplicationEntryPoint(); this.RequireSuperGlobalsTemplateInUserCode(applicationEntryPointPath); var checker = Checker.CreateChecker( executionContext.CheckerAssemblyName, executionContext.CheckerTypeName, executionContext.CheckerParameter); result.TestResults = new List <TestResult>(); var executor = new RestrictedProcessExecutor(); foreach (var test in executionContext.Tests) { var dbConnection = this.MySqlHelperStrategy.GetOpenConnection(databaseName); dbConnection.Close(); File.WriteAllText(this.SuperGlobalsTemplatePath, test.Input); var processExecutionResult = executor.Execute( this.phpCliExecutablePath, string.Empty, executionContext.TimeLimit, executionContext.MemoryLimit, new[] { applicationEntryPointPath }); var testResult = this.ExecuteAndCheckTest( test, processExecutionResult, checker, processExecutionResult.ReceivedOutput); result.TestResults.Add(testResult); this.MySqlHelperStrategy.DropDatabase(databaseName); } return(result); }
public void RestrictedProcessShouldWorkWithCyrillic() { var exePath = this.CreateExe("RestrictedProcessShouldWorkWithCyrillic.exe", ReadInputAndThenOutputSourceCode); const string InputData = "Николай\n"; var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, InputData, 2000, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.AreEqual(InputData.Trim(), result.ReceivedOutput.Trim()); }
public void RestrictedProcessShouldNotBeAbleToReadClipboard() { Clipboard.SetText("clipboard test"); var exePath = this.CreateExe("ReadClipboard.exe", ReadClipboardSourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 1500, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.RunTimeError, "No exception is thrown!"); }
public override ExecutionResult Execute(ExecutionContext executionContext) { var result = new ExecutionResult(); var userSubmissionContent = executionContext.FileContent; this.ExtractFilesInWorkingDirectory(userSubmissionContent, this.WorkingDirectory); var csProjFilePath = this.GetCsProjFilePath(); this.ExtractTestNames(executionContext.Tests); var project = new Project(csProjFilePath); var compileDirectory = project.DirectoryPath; this.SaveTestFiles(executionContext.Tests, compileDirectory); this.SaveSetupFixture(compileDirectory); this.CorrectProjectReferences(executionContext.Tests, project); var compilerPath = this.GetCompilerPathFunc(executionContext.CompilerType); var compilerResult = this.Compile( executionContext.CompilerType, compilerPath, executionContext.AdditionalCompilerArguments, csProjFilePath); result.IsCompiledSuccessfully = compilerResult.IsCompiledSuccessfully; result.CompilerComment = compilerResult.CompilerComment; if (!compilerResult.IsCompiledSuccessfully) { return(result); } // Delete tests before execution so the user can't access them FileHelpers.DeleteFiles(this.TestPaths.ToArray()); var executor = new RestrictedProcessExecutor(); var checker = Checker.CreateChecker( executionContext.CheckerAssemblyName, executionContext.CheckerTypeName, executionContext.CheckerParameter); result = this.RunUnitTests( this.NUnitConsoleRunnerPath, executionContext, executor, checker, result, compilerResult.OutputFile, AdditionalExecutionArguments); return(result); }
public void RestrictedProcessShouldSendInputDataToProcess() { var exePath = this.CreateExe("InputOutput.exe", ReadInputAndThenOutputSourceCode); const string InputData = "SomeInputData!!@#$%^&*(\n"; var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, InputData, 2000, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.AreEqual(InputData.Trim(), result.ReceivedOutput.Trim()); }
public void RestrictedProcessShouldNotBeAbleToWriteToClipboard() { var exePath = this.CreateExe("WriteToClipboard.exe", WriteToClipboardSourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 1500, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.RunTimeError, "No exception is thrown!"); Assert.AreNotEqual("i did it", Clipboard.GetText()); }
public void RestrictedProcessShouldStandardErrorContentShouldContainExceptions() { var exePath = this.CreateExe("ThrowException.exe", ThrowExceptionSourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 500, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.RunTimeError, "No exception is thrown!"); Assert.IsTrue(result.ErrorOutput.Contains("Exception message!")); }
public void RestrictedProcessShouldReturnCorrectAmountOfUsedMemory() { var exePath = this.CreateExe("Consuming50MbOfMemory.exe", Consuming50MbOfMemorySourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 5000, 100 * 1024 * 1024); Console.WriteLine(result.MemoryUsed); Assert.IsNotNull(result); Assert.IsTrue(result.MemoryUsed > 50 * 1024 * 1024); }
public void RestrictedProcessShouldNotBeAbleToStartProcess() { var notepadsBefore = Process.GetProcessesByName("notepad.exe").Count(); var exePath = this.CreateExe("StartProcess.exe", StartNotepadProcessSourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 1500, 32 * 1024 * 1024); var notepadsAfter = Process.GetProcessesByName("notepad.exe").Count(); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.RunTimeError, "No exception is thrown!"); Assert.AreEqual(notepadsBefore, notepadsAfter); }
public void RestrictedProcessShouldNotBlockWhenEnterEndlessLoop() { const string EndlessLoopSourceCode = @"using System; class Program { public static void Main() { while(true) { } } }"; var exePath = this.CreateExe("RestrictedProcessShouldNotBlockWhenEnterEndlessLoop.exe", EndlessLoopSourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 50, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.TimeLimit); }
public void RestrictedProcessShouldStopProgramAfterTimeIsEnded() { const string TimeLimitSourceCode = @"using System; using System.Threading; class Program { public static void Main() { Thread.Sleep(150); } }"; var exePath = this.CreateExe("RestrictedProcessShouldStopProgramAfterTimeIsEnded.exe", TimeLimitSourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 100, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.TimeLimit); }
public void RestrictedProcessShouldNotBeAbleToCreateFiles() { const string CreateFileSourceCode = @"using System; using System.IO; class Program { public static void Main() { File.OpenWrite(""test.txt""); } }"; var exePath = this.CreateExe("RestrictedProcessShouldNotBeAbleToCreateFiles.exe", CreateFileSourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 1000, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.RunTimeError, "No exception is thrown!"); }
public void RestrictedProcessShouldNotBeAbleToWriteToClipboard() { const string WriteToClipboardSourceCode = @"using System; using System.Windows.Forms; class Program { public static void Main() { Clipboard.SetText(""i did it""); } }"; var exePath = this.CreateExe("RestrictedProcessShouldNotBeAbleToWriteToClipboard.exe", WriteToClipboardSourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 1500, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.RunTimeError, "No exception is thrown!"); Assert.AreNotEqual("i did it", Clipboard.GetText()); }
public void RestrictedProcessShouldOutputProperLengthForCyrillicText() { const string ReadInputAndThenOutputTheLengthSourceCode = @"using System; class Program { public static void Main() { var line = Console.ReadLine(); Console.WriteLine(line.Length); } }"; var exePath = this.CreateExe("RestrictedProcessShouldOutputProperLengthForCyrillicText.exe", ReadInputAndThenOutputTheLengthSourceCode); const string InputData = "Николай\n"; var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, InputData, 2000, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.AreEqual("7", result.ReceivedOutput.Trim()); }
public void RestrictedProcessShouldNotBeAbleToStartProcess() { const string StartNotepadProcessSourceCode = @"using System; using System.Diagnostics; class Program { public static void Main() { Process.Start(string.Format(""{0}\\notepad.exe"", Environment.SystemDirectory)); } }"; var notepadsBefore = Process.GetProcessesByName("notepad.exe").Count(); var exePath = this.CreateExe("RestrictedProcessShouldNotBeAbleToStartProcess.exe", StartNotepadProcessSourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 1500, 32 * 1024 * 1024); var notepadsAfter = Process.GetProcessesByName("notepad.exe").Count(); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.RunTimeError, "No exception is thrown!"); Assert.AreEqual(notepadsBefore, notepadsAfter); }
public void RestrictedProcessShouldNotBeAbleToReadClipboard() { const string ReadClipboardSourceCode = @"using System; using System.Windows.Forms; class Program { public static void Main() { if (string.IsNullOrEmpty(Clipboard.GetText())) { throw new Exception(""Clipboard empty!""); } } }"; Clipboard.SetText("clipboard test"); var exePath = this.CreateExe("RestrictedProcessShouldNotBeAbleToReadClipboard.exe", ReadClipboardSourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 1500, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.RunTimeError, "No exception is thrown!"); }
public void RestrictedProcessStandardErrorContentShouldContainExceptions() { const string ThrowExceptionSourceCode = @"using System; using System.Windows.Forms; class Program { public static void Main() { throw new Exception(""Exception message!""); } }"; var exePath = this.CreateExe("RestrictedProcessShouldStandardErrorContentShouldContainExceptions.exe", ThrowExceptionSourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 500, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.RunTimeError, "No exception is thrown!"); Assert.IsTrue(result.ErrorOutput.Contains("Exception message!")); }
public void RestrictedProcessShouldReturnMemoryLimitWhenNeeded() { var exePath = this.CreateExe("Consuming50MbOfMemory.exe", Consuming50MbOfMemorySourceCode); var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, string.Empty, 5000, 30 * 1024 * 1024); Console.WriteLine(result.MemoryUsed); Assert.IsNotNull(result); Assert.IsTrue(result.Type == ProcessExecutionResultType.MemoryLimit); }
public void RestrictedProcessShouldReceiveCyrillicText() { const string ReadInputAndThenCheckTheTextToContainCyrillicLettersSourceCode = @"using System; class Program { public static void Main() { var line = Console.ReadLine(); Console.WriteLine((line.Contains(""а"") || line.Contains(""е""))); } }"; var exePath = this.CreateExe("RestrictedProcessShouldReceiveCyrillicText.exe", ReadInputAndThenCheckTheTextToContainCyrillicLettersSourceCode); const string InputData = "абвгдежзийклмнопрстуфхцчшщъьюя\n"; var process = new RestrictedProcessExecutor(); var result = process.Execute(exePath, InputData, 2000, 32 * 1024 * 1024); Assert.IsNotNull(result); Assert.AreEqual("True", result.ReceivedOutput.Trim()); }