/// <summary> /// Create new instance of <see cref="Runner"/> class. /// </summary> /// /// <remarks> /// Empty arguments mean current user logged in. /// </remarks> /// /// <param name="userName"> /// Username used to launch new process. /// </param> /// <param name="password"> /// Password of user to launch new process. /// </param> /// /// <exception cref="ArgumentNullException"> /// If any argument is null. /// </exception> /// <exception cref="Win32Exception"> /// If pair username/passford is not valid. /// </exception> public Runner(string userName, string password) { string baseDirectory = AppDomain.CurrentDomain.BaseDirectory; executerPath = baseDirectory + @"Bin\ExeWrapper.exe"; //validate arguments ProjectHelper.ValidateNotNull(userName, "userName"); ProjectHelper.ValidateNotNull(password, "password"); //create some dummy process to validate username/password pair try { using (Process dummyProcess = new Process()) { dummyProcess.StartInfo.UserName = userName; dummyProcess.StartInfo.Password = GetSecureString(password); dummyProcess.StartInfo.FileName = "cmd"; dummyProcess.StartInfo.UseShellExecute = false; dummyProcess.Start(); dummyProcess.Kill(); } } catch { //re throw any exception throw; } //set arguments this.userName = userName; this.password = password; }
/// <summary> /// Executes provided exe file and returns the result of program using. /// </summary> /// /// <param name="exePath"> /// Path of exe file to run. /// </param> /// /// <param name="program"> /// Execunitg constraints.(like memory limit, time limit) /// </param> /// /// <returns> /// Detailed result of program executing. /// </returns> /// /// <exception cref="ArgumentNullException"> /// If any argument is null. /// </exception> /// <exception cref="FileNotFoundException"> /// If provided path is invalid. /// </exception> /// <exception cref=""> /// /// </exception> public Result Execute(string exePath, Program program) { //validate arguments ProjectHelper.ValidateNotNull(program, "program"); ProjectHelper.ValidateFileExists(exePath, "exePath"); ProjectHelper.ValidateFileExists(executerPath, "exePath"); XmlSerializer serializer = new XmlSerializer(typeof(Program)); //create new process using (Process exeProcess = new Process()) { exeProcess.StartInfo.FileName = executerPath; exeProcess.StartInfo.UserName = userName; exeProcess.StartInfo.Password = GetSecureString(password); exeProcess.StartInfo.RedirectStandardError = true; exeProcess.StartInfo.RedirectStandardInput = true; exeProcess.StartInfo.RedirectStandardOutput = true; exeProcess.StartInfo.UseShellExecute = false; exeProcess.StartInfo.CreateNoWindow = true; //serialize problem using (MemoryStream inputStream = new MemoryStream()) { serializer.Serialize(inputStream, program); //start process exeProcess.Start(); exeProcess.StandardInput.WriteLine(exePath); inputStream.WriteTo(exeProcess.StandardInput.BaseStream); exeProcess.StandardInput.Close(); XmlSerializer deserializer = new XmlSerializer(typeof(Result)); //deserialize output string output = exeProcess.StandardOutput.ReadToEnd(); //terminate process if (!exeProcess.HasExited) { exeProcess.Kill(); } byte[] buffer = new byte[output.Length]; for (int i = 0; i < output.Length; i++) { buffer[i] = (byte)output[i]; } using (MemoryStream outputStream = new MemoryStream()) { outputStream.Write(buffer, 0, buffer.Length); outputStream.Position = 0; Result result = deserializer.Deserialize(outputStream) as Result; return(result); } } } }
/// <summary> /// Creates new instance of <see cref="Tester"/> class. /// </summary> /// /// <param name="settings"> /// Settings of this component. /// </param> public CompilationTester(Settings settings) { ProjectHelper.ValidateNotNull(settings, "settings"); this.Settings = settings; }
/// <summary> /// Executes provided exe file and returns the result of program using. /// </summary> /// /// <param name="exePath"> /// Path of exe file to run. /// </param> /// /// <param name="program"> /// Execunitg constraints.(like memory limit, time limit) /// </param> /// /// <returns> /// Detailed result of program executing. /// </returns> /// /// <exception cref="ArgumentNullException"> /// If any argument is null. /// </exception> /// <exception cref="FileNotFoundException"> /// If provided path is invalid. /// </exception> /// <exception cref=""> /// /// </exception> public static Result ExecuteWin32(string exePath, Program program, string arguments) { //validate arguments ProjectHelper.ValidateNotNull(program, "program"); ProjectHelper.ValidateFileExists(exePath, "exePath"); //Set error mode, for hiding error message boxes. SetErrorMode(0x0001 | 0x0002 | 0x0004 | 0x8000); Result result = new Result(); result.ProgramStatus = Status.Running; //create new process using (Process exeProcess = new Process()) { exeProcess.StartInfo.RedirectStandardError = true; exeProcess.StartInfo.RedirectStandardInput = true; exeProcess.StartInfo.RedirectStandardOutput = true; exeProcess.StartInfo.CreateNoWindow = true; exeProcess.StartInfo.UseShellExecute = false; exeProcess.StartInfo.FileName = exePath; exeProcess.StartInfo.Arguments = arguments; exeProcess.StartInfo.Arguments = exeProcess.StartInfo.Arguments; //start process MemoryCounter memoryCounter = new MemoryCounter(exeProcess, 20); exeProcess.Start(); Thread.Sleep(200); //write input data exeProcess.StandardInput.Write(program.InputTest); exeProcess.StandardInput.Close(); exeProcess.WaitForExit(program.TimeLimit); if (!exeProcess.HasExited) { exeProcess.Kill(); result.ProgramStatus = Status.TimeLimit; } memoryCounter.Stop(); //get program statistic result.Error = exeProcess.StandardError.ReadToEnd(); result.Output = exeProcess.StandardOutput.ReadToEnd(); result.TimeUsed = (int)exeProcess.TotalProcessorTime.TotalMilliseconds; result.MemoryUsed = (int)memoryCounter.Memory / 1024; result.Output = result.Output.Trim(); //set program status if (result.ProgramStatus != Status.TimeLimit) { if (exeProcess.ExitCode != 0) { result.ProgramStatus = Status.Crashed; } else { if (result.MemoryUsed > program.MemoryLimit) { result.ProgramStatus = Status.MemoryLimit; } else { if (result.Output == program.OutputTest) { result.ProgramStatus = Status.Accepted; } else { result.ProgramStatus = Status.WrongAnswer; } } } } } return(result); }