Exemple #1
0
        /// <summary>
        /// Adds (if not already present) p to either fplanDB or fexceptionThrowingPlanDB,
        /// by executing the plan to determine if it throws exceptions.
        /// </summary>
        /// <param name="v"></param>
        public void AddMaybeExecutingIfNeeded(Plan p, StatsManager stats)
        {
            //foreach (string s in p.Codestring)
            //    Console.WriteLine(s);


            if (builderPlans.Containsplan(p))
            {
                redundantAdds++;
                stats.CreatedNew(CreationResult.Redundant);
            }
            else if (exceptionPlans.Containsplan(p))
            {
                redundantAdds++;
                stats.CreatedNew(CreationResult.Redundant);
            }
            else
            {
                addCounter++;
                if (addCounter % 1000 == 0)
                {
                    Console.Write(".");
                    PrintPercentageExecuted();
                }

                stats.CreatedNew(CreationResult.New);
                ResultTuple execResult;
                TextWriter  writer = new StringWriter();
                Exception   exceptionThrown;
                bool        contractViolated;

                this.testFileWriter.WriteTest(p);

                if (config.executionmode == ExecutionMode.DontExecute)
                {
                    builderPlans.AddPlan(p);
                    stats.ExecutionResult("normal");
                }
                else
                {
                    Util.Assert(config.executionmode == ExecutionMode.Reflection);

                    TextWriter executionLog = new StreamWriter(config.executionLog);
                    executionLog.WriteLine("LASTPLANID:" + p.uniqueId);

                    long startTime = 0;
                    Timer.QueryPerformanceCounter(ref startTime);

                    bool execSucceeded = p.Execute(out execResult,
                                                   executionLog, writer, out exceptionThrown,
                                                   out contractViolated, this.config.forbidnull,
                                                   config.monkey);

                    long endTime = 0;
                    Timer.QueryPerformanceCounter(ref endTime);
                    TimeTracking.timeSpentExecutingTestedCode += (endTime - startTime);

                    executionLog.Close();

                    /*
                     * New: Now the execution of plan might fail (recursively) if any of the output tuple
                     * objects violate the contract for ToString(), HashCode(), Equals(o)
                     */
                    if (!execSucceeded)
                    {
                        stats.ExecutionResult(exceptionThrown == null ? "other" : exceptionThrown.GetType().FullName);

                        // TODO This should alway be true...
                        if (exceptionThrown != null)
                        {
                            p.exceptionThrown = exceptionThrown;
                            this.testFileWriter.Move(p, exceptionThrown);

                            if (exceptionThrown is AccessViolationException)
                            {
                                Console.WriteLine("SECOND-CHANCE ACCESS VIOLATION EXCEPTION.");
                                System.Environment.Exit(1);
                            }
                        }


                        string exceptionMessage = writer.ToString();

                        Util.Assert(p.exceptionThrown != null || contractViolated);

                        if (config.monkey)
                        {
                            builderPlans.AddPlan(p);
                            //exceptionPlans.AddPlan(p); //new: also add it to exceptions for monkey
                        }
                        else if (exceptionThrown != null)
                        {
                            exceptionPlans.AddPlan(p);
                        }
                    }
                    else
                    {
                        stats.ExecutionResult("normal");

                        if (config.outputnormalinputs)
                        {
                            this.testFileWriter.MoveNormalTermination(p);
                        }
                        else
                        {
                            this.testFileWriter.Remove(p);
                        }

                        // If forbidNull, then make inactive any result tuple elements that are null.
                        if (this.config.forbidnull)
                        {
                            Util.Assert(p.NumTupleElements == execResult.tuple.Length);
                            for (int i = 0; i < p.NumTupleElements; i++)
                            {
                                if (execResult.tuple[i] == null)
                                {
                                    p.SetActiveTupleElement(i, false);
                                }
                            }
                            //Util.Assert(!allNull); What is the motivation behind this assertion?
                        }


                        //only allow the receivers to be arguments to future methods
                        if (config.forbidparamobj)
                        {
                            Util.Assert(p.NumTupleElements == execResult.tuple.Length);
                            for (int i = 1; i < p.NumTupleElements; i++)
                            {
                                p.SetActiveTupleElement(i, false);
                            }
                        }

                        builderPlans.AddPlan(p, execResult);
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Adds (if not already present) p to either fplanDB or fexceptionThrowingPlanDB,
        /// by executing the plan to determine if it throws exceptions.
        /// </summary>
        /// <param name="v"></param>
        public void AddMaybeExecutingIfNeeded(Plan plan, StatsManager stats)
        {
            if (builderPlans.Containsplan(plan))
            {
                redundantAdds++;
                stats.CreatedNew(CreationResult.Redundant);
            }
            else if (exceptionPlans.Containsplan(plan))
            {
                redundantAdds++;
                stats.CreatedNew(CreationResult.Redundant);
            }
            else
            {
                addCounter++;
                if (addCounter % 1000 == 0)
                {
                    Console.Write(".");
                }

                stats.CreatedNew(CreationResult.New);
                ResultTuple execResult;

                TextWriter writer = new StringWriter();
                ////[email protected] changes for debuglog
                //TextWriter writer
                //    = new StreamWriter("..\\..\\TestRandoopBare\\1ab1qkwm.jbd\\debug.log");

                Exception exceptionThrown;
                bool      contractViolated;
                bool      preconditionViolated;

                testFileWriter.WriteTest(plan);

                if (config.executionmode == ExecutionMode.DontExecute)
                {
                    builderPlans.AddPlan(plan);
                    stats.ExecutionResult("normal");
                }
                else
                {
                    Util.Assert(config.executionmode == ExecutionMode.Reflection);

                    //TextWriter executionLog = new StreamWriter(config.executionLog);
                    TextWriter executionLog = new StreamWriter(config.executionLog + addCounter.ToString() + ".log"); //[email protected] changes
                    executionLog.WriteLine("LASTPLANID:" + plan.UniqueId);

                    long startTime = 0;
                    Timer.QueryPerformanceCounter(ref startTime);

                    bool execSucceeded = plan.Execute(out execResult,
                                                      executionLog, writer, out preconditionViolated, out exceptionThrown,
                                                      out contractViolated, config.forbidnull,
                                                      config.useRandoopContracts);

                    long endTime = 0;
                    Timer.QueryPerformanceCounter(ref endTime);
                    TimeTracking.timeSpentExecutingTestedCode += (endTime - startTime);

                    executionLog.Close();

                    if (!execSucceeded)
                    {
                        if (preconditionViolated)
                        {
                            stats.ExecutionResult("precondition violated");
                            testFileWriter.Remove(plan);
                        }

                        if (exceptionThrown != null)
                        {
                            stats.ExecutionResult(exceptionThrown.GetType().FullName);
                            plan.exceptionThrown = exceptionThrown;
                            testFileWriter.Move(plan, exceptionThrown);

                            if (exceptionThrown is AccessViolationException)
                            {
                                Logger.Error("SECOND-CHANCE ACCESS VIOLATION EXCEPTION.");
                                Environment.Exit(1);
                            }
                        }


                        //string exceptionMessage = writer.ToString(); //no use? [email protected] comments out

                        Util.Assert(plan.exceptionThrown != null || contractViolated || preconditionViolated);

                        if (config.monkey)
                        {
                            builderPlans.AddPlan(plan);
                        }
                        else if (exceptionThrown != null)
                        {
                            exceptionPlans.AddPlan(plan);
                        }
                    }
                    else
                    {
                        stats.ExecutionResult("normal");

                        if (config.outputnormalinputs)
                        {
                            testFileWriter.MoveNormalTermination(plan);
                        }
                        else
                        {
                            testFileWriter.Remove(plan);
                        }

                        // If forbidNull, then make inactive any result tuple elements that are null.
                        if (config.forbidnull)
                        {
                            Util.Assert(plan.NumTupleElements == execResult.tuple.Length);
                            for (int i = 0; i < plan.NumTupleElements; i++)
                            {
                                if (execResult.tuple[i] == null)
                                {
                                    plan.SetActiveTupleElement(i, false);
                                }
                            }
                            //Util.Assert(!allNull); What is the motivation behind this assertion?
                        }


                        //only allow the receivers to be arguments to future methods
                        if (config.forbidparamobj)
                        {
                            Util.Assert(plan.NumTupleElements == execResult.tuple.Length);
                            for (int i = 1; i < plan.NumTupleElements; i++)
                            {
                                plan.SetActiveTupleElement(i, false);
                            }
                        }

                        builderPlans.AddPlan(plan, execResult);
                    }
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Main randoop entrypoint.
        /// </summary>
        static void Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("Randoop.NET: an API fuzzer for .Net. Version {0} (compiled {1}).",
                              Common.Enviroment.RandoopVersion, Common.Enviroment.RandoopCompileDate);

            if (args.Length == 0 || IsHelpCommand(args[0]))
            {
                Console.Error.WriteLine(HelpScreen.Usagestring());
                System.Environment.Exit(1);
            }

            if (ContainsHelp(args))
            {
                Console.WriteLine(HelpScreen.Usagestring());
                System.Environment.Exit(0);
            }

            if (args[0].Equals("/about"))
            {
                WriteAboutMessageToConsole();
                System.Environment.Exit(0);
            }

            if (args[0].Equals("minimize"))
            {
                string[] args2 = new string[args.Length - 1];
                Array.Copy(args, 1, args2, 0, args.Length - 1);
                TestCaseUtils.Minimize(TestCaseUtils.CollectFilesEndingWith(".cs", args2));
                System.Environment.Exit(0);
            }

            if (args[0].Equals("reduce"))
            {
                string[] args2 = new string[args.Length - 1];
                Array.Copy(args, 1, args2, 0, args.Length - 1);
                Collection <FileInfo> oldTests = TestCaseUtils.CollectFilesEndingWith(".cs", args2);
                Console.WriteLine("Number of original tests: " + oldTests.Count);
                Collection <FileInfo> newTests = TestCaseUtils.Reduce(oldTests);
                Console.WriteLine("Number of reduced tests: " + newTests.Count);
                Dictionary <TestCase.ExceptionDescription, Collection <FileInfo> > classifiedTests =
                    TestCaseUtils.ClassifyTestsByMessage(newTests);
                PrintStats(classifiedTests);
                StringBuilder b = new StringBuilder();
                foreach (string s in args)
                {
                    b.Append(" " + s);
                }
                string htmlFileName = "reduced" + Path.GetRandomFileName() + ".html";
                HtmlSummary.CreateIndexHtml(classifiedTests, htmlFileName, b.ToString());
                OpenExplorer(htmlFileName);
                System.Environment.Exit(0);
            }

            if (args[0].Equals("reproduce"))
            {
                string[] args2 = new string[args.Length - 1];
                Array.Copy(args, 1, args2, 0, args.Length - 1);
                TestCaseUtils.ReproduceBehavior(TestCaseUtils.CollectFilesEndingWith(".cs", args2));
                System.Environment.Exit(0);
            }

            if (args[0].StartsWith("stats:"))
            {
                string statsResultsFileName = args[0].Substring("stats:".Length);

                string[] args2 = new string[args.Length - 1];
                Array.Copy(args, 1, args2, 0, args.Length - 1);

                StatsManager.ComputeStats(statsResultsFileName,
                                          TestCaseUtils.CollectFilesEndingWith(".stats.txt", args2));
                System.Environment.Exit(0);
            }

            GenerateTests(args);
        }
Exemple #4
0
        /// <summary>
        /// Main randoop entrypoint.
        /// </summary>
        static void Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("Randoop.NET: an API fuzzer for .Net. Version {0} (compiled {1}).",
                              Common.Enviroment.RandoopVersion, Common.Enviroment.RandoopCompileDate);

            if (args.Length == 0 || IsHelpCommand(args[0]))
            {
                Console.Error.WriteLine(HelpScreen.Usagestring());
                System.Environment.Exit(1);
            }

            if (ContainsHelp(args))
            {
                Console.WriteLine(HelpScreen.Usagestring());
                System.Environment.Exit(0);
            }

            if (args[0].Equals("/about"))
            {
                WriteAboutMessageToConsole();
                System.Environment.Exit(0);
            }

            //[email protected] adds "RandoopMappedCalls"
            if (args[0].Equals("methodtransformer"))
            {
                string   mapfile = args[1];
                string[] args2   = new string[args.Length - 2];
                Array.Copy(args, 2, args2, 0, args.Length - 2);
                Dictionary <string, string> methodmapper = new Dictionary <string, string>();

                try
                {
                    Instrument.ParseMapFile(mapfile, ref methodmapper); //parse a file that defines the mapping between target method and replacement method

                    foreach (string asm in args2)
                    {
                        int       mapcnt      = methodmapper.Count;
                        string [] origmethods = new string [mapcnt];
                        methodmapper.Keys.CopyTo(origmethods, 0);
                        foreach (string src in origmethods)
                        {
                            Instrument.MethodInstrument(asm, src, methodmapper[src]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    System.Environment.Exit(-1);
                }

                System.Environment.Exit(0);
            }
            //[email protected] adds "RandoopMappedCalls"

            if (args[0].Equals("minimize"))
            {
                string[] args2 = new string[args.Length - 1];
                Array.Copy(args, 1, args2, 0, args.Length - 1);
                TestCaseUtils.Minimize(TestCaseUtils.CollectFilesEndingWith(".cs", args2));
                System.Environment.Exit(0);
            }

            if (args[0].Equals("reduce"))
            {
                string[] args2 = new string[args.Length - 1];
                Array.Copy(args, 1, args2, 0, args.Length - 1);
                Collection <FileInfo> oldTests = TestCaseUtils.CollectFilesEndingWith(".cs", args2);
                Console.WriteLine("Number of original tests: " + oldTests.Count);
                Collection <FileInfo> newTests = TestCaseUtils.Reduce(oldTests);
                Console.WriteLine("Number of reduced tests: " + newTests.Count);
                //Dictionary<TestCase.ExceptionDescription, Collection<FileInfo>> classifiedTests =
                //    TestCaseUtils.ClassifyTestsByMessage(newTests);
                //PrintStats(classifiedTests);
                //StringBuilder b = new StringBuilder();
                //foreach (string s in args) b.Append(" " + s);
                //string htmlFileName = "reduced" + Path.GetRandomFileName() + ".html";
                ////HtmlSummary.CreateIndexHtml(classifiedTests, htmlFileName, b.ToString());
                //HtmlSummary.CreateIndexHtml2(classifiedTests, htmlFileName, b.ToString());
                //OpenExplorer(htmlFileName);
                System.Environment.Exit(0);
            }

            if (args[0].Equals("reduce2")) //[email protected] adds for sequence-based reduction
            {
                string[] args2 = new string[args.Length - 1];
                Array.Copy(args, 1, args2, 0, args.Length - 1);
                Collection <FileInfo> oldTests = TestCaseUtils.CollectFilesEndingWith(".cs", args2);
                Console.WriteLine("Number of original tests: " + oldTests.Count);
                Collection <FileInfo> newTests = TestCaseUtils.Reduce2(oldTests);
                Console.WriteLine("Number of reduced tests: " + newTests.Count);
                System.Environment.Exit(0);
            }

            if (args[0].Equals("reproduce"))
            {
                string[] args2 = new string[args.Length - 1];
                Array.Copy(args, 1, args2, 0, args.Length - 1);
                TestCaseUtils.ReproduceBehavior(TestCaseUtils.CollectFilesEndingWith(".cs", args2));
                System.Environment.Exit(0);
            }

            if (args[0].StartsWith("stats:"))
            {
                string statsResultsFileName = args[0].Substring("stats:".Length);

                string[] args2 = new string[args.Length - 1];
                Array.Copy(args, 1, args2, 0, args.Length - 1);

                StatsManager.ComputeStats(statsResultsFileName,
                                          TestCaseUtils.CollectFilesEndingWith(".stats.txt", args2));
                System.Environment.Exit(0);
            }

            GenerateTests(args);
        }