public void GetSubmissionContentWhenInvalidSubmissionIdShouldThrowException()
        {
            var contest = this.CreateAndSaveContest("sample Name", this.ActiveContestNoPasswordOptions, this.ActiveContestNoPasswordOptions);
            var problem = new Problem();
            contest.Problems.Add(problem);

            var submissionType = new SubmissionType();
            contest.SubmissionTypes.Add(submissionType);

            var participant = new Participant(contest.Id, this.FakeUserProfile.Id, this.IsCompete);
            contest.Participants.Add(participant);
            var submission = new Submission
            {
                ContentAsString = "test content"
            };

            participant.Submissions.Add(submission);
            this.EmptyOjsData.SaveChanges();

            try
            {
                var result = this.CompeteController.GetSubmissionContent(-1);
                Assert.Fail("Expected an exception when an invalid submission id is provided.");
            }
            catch (HttpException ex)
            {
                Assert.AreEqual((int)HttpStatusCode.NotFound, ex.GetHttpCode());
            }
        }
        public void ReadSubmissionResultsWhenParticipantHasSubmissionsShouldReturnNumberOfSubmissions()
        {
            var contest = this.CreateAndSaveContest("contest", this.ActiveContestNoPasswordOptions, this.ActiveContestNoPasswordOptions);

            var problem = new Problem();
            contest.Problems.Add(problem);

            var submissionType = new SubmissionType();
            contest.SubmissionTypes.Add(submissionType);

            var participant = new Participant(contest.Id, this.FakeUserProfile.Id, this.IsCompete);
            contest.Participants.Add(participant);

            for (int i = 0; i < 10; i++)
            {
                var submission = new Submission
                {
                    ContentAsString = "Test submission " + i,
                    SubmissionType = submissionType,
                    Problem = problem
                };

                participant.Submissions.Add(submission);
            }

            this.EmptyOjsData.SaveChanges();

            var result = this.CompeteController
                .ReadSubmissionResults(new DataSourceRequest(), problem.Id, this.IsCompete) as JsonResult;

            var responseData = result.Data as DataSourceResult;
            Assert.AreEqual(participant.Submissions.Count, responseData.Total);
        }
        public void GetSubmissionContentWhenSubmissionNotMadeByTheParticipantShouldThrowException()
        {
            var contest = this.CreateAndSaveContest("sample Name", this.ActiveContestNoPasswordOptions, this.ActiveContestNoPasswordOptions);
            var problem = new Problem();
            contest.Problems.Add(problem);

            var submissionType = new SubmissionType();
            contest.SubmissionTypes.Add(submissionType);

            var participant = new Participant(contest.Id, this.FakeUserProfile.Id, this.IsCompete);

            var anotherUser = new UserProfile
            {
                UserName = "******",
                Email = "*****@*****.**"
            };

            this.EmptyOjsData.Users.Add(anotherUser);
            var anotherParticipant = new Participant(contest.Id, anotherUser.Id, this.IsCompete);

            contest.Participants.Add(participant);
            contest.Participants.Add(anotherParticipant);

            var submission = new Submission
            {
                ContentAsString = "test content"
            };

            anotherParticipant.Submissions.Add(submission);
            this.EmptyOjsData.SaveChanges();

            try
            {
                var result = this.CompeteController.GetSubmissionContent(submission.Id);
                Assert.Fail("Expected an exception when trying to download a submission that was not made by the participant that requested it.");
            }
            catch (HttpException ex)
            {
                Assert.AreEqual((int)HttpStatusCode.Forbidden, ex.GetHttpCode());
            }
        }
        public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb)
        {
            var tests = context.Tests.Select(x => new { x.Id, x.ProblemId, x.IsTrialTest, x.OrderBy }).ToList();

            var count = oldDb.Submissions.Count();
            const int ElementsByIteration = 500;
            var iterations = Math.Ceiling((decimal)count / ElementsByIteration);
            for (int i = 0; i < iterations; i++)
            {
                GC.Collect();
                var newDb = new OjsDbContext();
                newDb.Configuration.AutoDetectChangesEnabled = false;
                newDb.Configuration.ValidateOnSaveEnabled = false;
                var csharpSubmissionType = newDb.SubmissionTypes.FirstOrDefault(x => x.Name == "C# code");
                var cplusPlusSubmissionType = newDb.SubmissionTypes.FirstOrDefault(x => x.Name == "C++ code");
                var javaScriptSubmissionType =
                    newDb.SubmissionTypes.FirstOrDefault(x => x.Name == "JavaScript code (NodeJS)");

                oldDb = new TelerikContestSystemEntities();
                var dataSource =
                    oldDb.Submissions.AsNoTracking()
                        .Where(x => x.Id != 127774)
                        .OrderBy(x => x.Id)
                        .Skip(i * ElementsByIteration)
                        .Take(ElementsByIteration);

                foreach (var oldSubmission in dataSource)
                {
                    var problem = newDb.Problems.FirstOrDefault(x => x.OldId == oldSubmission.Task);
                    var participant = newDb.Participants.FirstOrDefault(x => x.OldId == oldSubmission.Participant);
                    var submission = new Submission
                                         {
                                             Content = oldSubmission.File.ToText().Compress(),
                                             PreserveCreatedOn = true,
                                             CreatedOn = oldSubmission.SubmittedOn,
                                             Problem = problem,
                                             Participant = participant,
                                             Points = oldSubmission.Points,
                                             Processed = true,
                                             Processing = false,
                                         };

                    switch (oldSubmission.Language)
                    {
                        case "C# Code":
                            submission.SubmissionType = csharpSubmissionType;
                            break;
                        case "C++ Code":
                            submission.SubmissionType = cplusPlusSubmissionType;
                            break;
                        case "JavaScript Code":
                            submission.SubmissionType = javaScriptSubmissionType;
                            break;
                        case "C + + 觐�":
                            submission.SubmissionType = cplusPlusSubmissionType;
                            break;
                        default:
                            submission.SubmissionType = csharpSubmissionType;
                            break;
                    }

                    var reportFragments = oldSubmission.FullReport.Split(
                        new[] { this.contentHeader },
                            StringSplitOptions.RemoveEmptyEntries);

                    if (string.IsNullOrWhiteSpace(oldSubmission.FullReport) || !reportFragments.Any())
                    {
                        continue;
                    }

                    if (reportFragments.Count() == 1)
                    {
                        // Some kind of exception (e.g. "not enough disk space")
                        var errorFragments = reportFragments[0].Split(
                            new[] { this.contentFooter },
                            StringSplitOptions.RemoveEmptyEntries);
                        submission.IsCompiledSuccessfully = false;
                        submission.CompilerComment = errorFragments[0];
                    }
                    else if (!reportFragments[1].Trim().StartsWith("Compilation successfull!!!"))
                    {
                        submission.IsCompiledSuccessfully = false;
                        var compilerParts = reportFragments[0].Split(
                            new[] { "\r\n" },
                            3,
                            StringSplitOptions.RemoveEmptyEntries);

                        submission.CompilerComment = compilerParts.Count() > 2 ? compilerParts[2].Trim(' ', '\n', '\r', '\t', '-', '=') : null;
                    }
                    else
                    {
                        submission.IsCompiledSuccessfully = true;
                        var compilerParts = reportFragments[0].Split(
                            new[] { "\r\n" },
                            3,
                            StringSplitOptions.RemoveEmptyEntries);
                        submission.CompilerComment = compilerParts.Count() > 2 ? compilerParts[2].Trim(' ', '\n', '\r', '\t', '-', '=') : null;

                        for (int j = 2; j < reportFragments.Length - 1; j++)
                        {
                            var testRunText = reportFragments[j].Trim();
                            var testRunTextParts = testRunText.Split(new[] { this.contentFooter }, StringSplitOptions.None);
                            var testRunTitle = testRunTextParts[0].Trim();
                            var testRunDescription = testRunTextParts[1].Trim() + Environment.NewLine;
                            var isZeroTest = testRunTitle.StartsWith("Zero");

                            var testOrderAsString = testRunTitle.GetStringBetween("�", " ");

                            var testOrder = int.Parse(testOrderAsString);

                            var test =
                                tests.FirstOrDefault(
                                    x =>
                                    x.ProblemId == problem.Id && x.IsTrialTest == isZeroTest && x.OrderBy == testOrder);

                            if (test == null)
                            {
                                continue;
                            }

                            var testRun = new TestRun
                                              {
                                                  MemoryUsed = 0,
                                                  TimeUsed = 0,
                                                  Submission = submission,
                                                  TestId = test.Id,
                                              };

                            if (testRunDescription.StartsWith("Answer correct!!!"))
                            {
                                testRun.ResultType = TestRunResultType.CorrectAnswer;
                                testRun.ExecutionComment = null;
                                testRun.CheckerComment = null;

                                var timeUsedAsString =
                                    testRunDescription.GetStringBetween("Time used (in milliseconds): ", "Memory used");
                                if (timeUsedAsString != null)
                                {
                                    double timeUsed;
                                    if (double.TryParse(timeUsedAsString.Replace(",", ".").Trim(), out timeUsed))
                                    {
                                        testRun.TimeUsed = (int)timeUsed;
                                    }
                                }

                                var memoryUsedAsString = testRunDescription.GetStringBetween(
                                    "Memory used (in bytes): ",
                                    Environment.NewLine);
                                if (memoryUsedAsString != null)
                                {
                                    testRun.MemoryUsed = int.Parse(memoryUsedAsString.Trim());
                                }
                            }
                            else if (testRunDescription.StartsWith("Answer incorrect!"))
                            {
                                testRun.ResultType = TestRunResultType.WrongAnswer;
                                testRun.ExecutionComment = null;
                                testRun.CheckerComment = testRunDescription.GetStringBetween(
                                    "Answer incorrect!",
                                    "Time used"); // Won't work with non-zero tests (will return null)
                                if (testRun.CheckerComment != null)
                                {
                                    testRun.CheckerComment = testRun.CheckerComment.Trim();
                                    if (string.IsNullOrWhiteSpace(testRun.CheckerComment))
                                    {
                                        testRun.CheckerComment = null;
                                    }
                                }

                                var timeUsedAsString =
                                    testRunDescription.GetStringBetween("Time used (in milliseconds): ", "Memory used");
                                if (timeUsedAsString != null)
                                {
                                    double timeUsed;
                                    if (double.TryParse(timeUsedAsString.Replace(",", ".").Trim(), out timeUsed))
                                    {
                                        testRun.TimeUsed = (int)timeUsed;
                                    }
                                }

                                var memoryUsedAsString = testRunDescription.GetStringBetween(
                                    "Memory used (in bytes): ",
                                    Environment.NewLine);
                                if (memoryUsedAsString != null)
                                {
                                    testRun.MemoryUsed = int.Parse(memoryUsedAsString.Trim());
                                }
                            }
                            else if (testRunDescription.StartsWith("Runtime error:"))
                            {
                                testRun.ResultType = TestRunResultType.RunTimeError;
                                testRun.ExecutionComment = testRunDescription.Replace("Runtime error:", string.Empty).Trim();
                                testRun.CheckerComment = null;
                                testRun.TimeUsed = 0;
                                testRun.MemoryUsed = 0;
                            }
                            else if (testRunDescription.StartsWith("Time limit!"))
                            {
                                testRun.ResultType = TestRunResultType.TimeLimit;
                                testRun.ExecutionComment = null;
                                testRun.CheckerComment = null;
                                testRun.TimeUsed = problem.TimeLimit;
                                testRun.MemoryUsed = 0;
                            }
                            else
                            {
                                testRun.ResultType = TestRunResultType.RunTimeError;
                                testRun.ExecutionComment = testRunDescription.Trim();
                                testRun.CheckerComment = null;
                                testRun.TimeUsed = 0;
                                testRun.MemoryUsed = 0;
                            }

                            newDb.TestRuns.Add(testRun);
                        }
                    }

                    newDb.Submissions.Add(submission);
                }

                newDb.SaveChanges();
                Console.Write(".");
            }
        }
        private void ProcessSubmission(Submission submission)
        {
            // TODO: Check for N+1 queries problem
            this.logger.InfoFormat("Work on submission №{0} started.", submission.Id);

            var executionStrategy = this.CreateExecutionStrategy(submission.SubmissionType.ExecutionStrategyType);
            var context = new ExecutionContext
            {
                SubmissionId = submission.Id,
                AdditionalCompilerArguments = submission.SubmissionType.AdditionalCompilerArguments,
                CheckerAssemblyName = submission.Problem.Checker.DllFile,
                CheckerParameter = submission.Problem.Checker.Parameter,
                CheckerTypeName = submission.Problem.Checker.ClassName,
                FileContent = submission.Content,
                AllowedFileExtensions = submission.SubmissionType.AllowedFileExtensions,
                CompilerType = submission.SubmissionType.CompilerType,
                MemoryLimit = submission.Problem.MemoryLimit,
                TimeLimit = submission.Problem.TimeLimit,
            };

            context.Tests = submission.Problem.Tests.ToList().Select(x => new TestContext
            {
                Id = x.Id,
                Input = x.InputDataAsString,
                Output = x.OutputDataAsString,
                IsTrialTest = x.IsTrialTest
            });

            ExecutionResult executionResult;
            try
            {
                executionResult = executionStrategy.Execute(context);
            }
            catch (Exception exception)
            {
                this.logger.ErrorFormat("executionStrategy.Execute on submission №{0} has thrown an exception: {1}", submission.Id, exception);
                submission.ProcessingComment = string.Format("Exception in executionStrategy.Execute: {0}", exception.Message);
                return;
            }

            submission.IsCompiledSuccessfully = executionResult.IsCompiledSuccessfully;
            submission.CompilerComment = executionResult.CompilerComment;

            if (!executionResult.IsCompiledSuccessfully)
            {
                return;
            }

            foreach (var testResult in executionResult.TestResults)
            {
                var testRun = new TestRun
                {
                    CheckerComment = testResult.CheckerDetails.Comment,
                    ExpectedOutputFragment = testResult.CheckerDetails.ExpectedOutputFragment,
                    UserOutputFragment = testResult.CheckerDetails.UserOutputFragment,
                    ExecutionComment = testResult.ExecutionComment,
                    MemoryUsed = testResult.MemoryUsed,
                    ResultType = testResult.ResultType,
                    TestId = testResult.Id,
                    TimeUsed = testResult.TimeUsed,
                };
                submission.TestRuns.Add(testRun);
            }

            this.logger.InfoFormat("Work on submission №{0} ended.", submission.Id);
        }
 private void CalculatePointsForSubmission(Submission submission)
 {
     // Internal joke: submission.Points = new Random().Next(0, submission.Problem.MaximumPoints + 1) + Weather.Instance.Today("Sofia").IsCloudy ? 10 : 0;
     if (submission.Problem.Tests.Count == 0 || submission.TestsWithoutTrialTestsCount == 0)
     {
         submission.Points = 0;
     }
     else
     {
         submission.Points = (submission.CorrectTestRunsWithoutTrialTestsCount * submission.Problem.MaximumPoints) / submission.TestsWithoutTrialTestsCount;
     }
 }
 private void RetestSubmission(int submissionId)
 {
     var submission = new Submission { Id = submissionId, Processed = false, Processing = false };
     this.Data.Context.Submissions.Attach(submission);
     var entry = this.Data.Context.Entry(submission);
     entry.Property(pr => pr.Processed).IsModified = true;
     entry.Property(pr => pr.Processing).IsModified = true;
 }