コード例 #1
0
        public static void TestLambda(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
using System;
class Program
{
	public static void Foo(int x)
	{
		Console.WriteLine(x);
	}	

    public static void Main()
    {
        Func<int, int> lambda = x => { var i = x + 1; Foo(i); return i * x;  }; 
        var result = lambda(2);     
    }
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Main", true), callgraph));
                // This should be reachable
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Foo", true), callgraph));
            },
                                     strategy);
        }
コード例 #2
0
        public static void TestVirtualCallViaSuperClass(AnalysisStrategyKind strategy)
        {
            #region source code
            var source =
                @"
class SuperClass
{
    public virtual void M() { }
}

class SubClass : SuperClass
{
    public override void M() { }
}

class MainClass
{    
    static void Main(string[] argv)
	{
        SuperClass oSub = new SubClass();
        oSub.M(); // SubClass.M() should be reachable.
    } 
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("SubClass", "M"), callgraph));
            },
                                     strategy);
        }
コード例 #3
0
        public static void TestExtensionMethodCall(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
using System;

static class Extension
{
	public static void Foo(this int x)
	{
		Console.WriteLine(x);
	}
}

class Program
{
    public static void Main()
	{
		var num = 5;
		num.Foo();
	}
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Main", true), callgraph));
                // This should be reachable
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Extension", "Foo", true), callgraph));
            },
                                     strategy);
        }
コード例 #4
0
        public static void TestCallViaInterface(AnalysisStrategyKind strategy)
        {
            #region source code
            var source =
                @"
interface Interface
{
	void M(int x);
}

class SubClass : Interface
{
	public void M(int x) { }
}

class MainClass
{    
	static void Main(string[] argv)
	{
		var oSub = new SubClass();
		oSub.M(4); // SubClass.M() should be reachable.
	} 
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("SubClass", "M"), callgraph));
                Assert.IsTrue(s.IsCalled(new MethodDescriptor("MainClass", "Main", true), new MethodDescriptor("SubClass", "M"), callgraph));
            },
                                     strategy);
        }
コード例 #5
0
        public static void TestArrowMethodBody(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
using System;
class Program
{
	public static void Foo(int x)
	{
		Console.WriteLine(x);
	}	

    public static void Main() => Foo(5);
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Main", true), callgraph));
                // This should be reachable
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Foo", true), callgraph));
            },
                                     strategy);
        }
コード例 #6
0
        public static void LongGeneratedTestAsync2(AnalysisStrategyKind strategy)
        {
            var source = BasicTestsSources.Test["LongGeneratedTest2"];

            TestUtils.AnalyzeExample(source, (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "Main", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "N0", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "N1", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "N2", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "N3", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "N4", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "N5", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "N6", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "N7", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "N8", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "N9", true), callgraph));

                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N8", true), new MethodDescriptor("C", "N4", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N8", true), new MethodDescriptor("C", "N8", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N8", true), new MethodDescriptor("C", "N7", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N8", true), new MethodDescriptor("C", "N0", true)));

                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N5", true), new MethodDescriptor("C", "N6", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N5", true), new MethodDescriptor("C", "N2", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N5", true), new MethodDescriptor("C", "N3", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N5", true), new MethodDescriptor("C", "N7", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N5", true), new MethodDescriptor("C", "N4", true)));

                Assert.IsTrue(callgraph.GetNodes().Count() == 101);
                var edgeCount = callgraph.GetEdges().Count();
                //s.Callgraph.Save(@"c:\temp\test.dot");
                Assert.IsTrue(edgeCount == 589, string.Format("Found {0} edges", edgeCount));
            }, strategy);
        }
コード例 #7
0
        /// <summary>
        /// IMPORTANT: OnDemandSolvers need an OnDemand Dispatcher
        /// We cannot use the SyncronousDistacther because it doesn't look for the
        /// methods when they are not available. This only works with the entire solution
        /// analysis!!!!
        /// </summary>
        public CallGraph <MethodDescriptor, LocationDescriptor> Analyze(AnalysisStrategyKind strategyKind = AnalysisStrategyKind.NONE)
        {
            if (strategyKind == AnalysisStrategyKind.NONE)
            {
                strategyKind = StringToAnalysisStrategy(ConfigurationManager.AppSettings["Strategy"]);
            }

            switch (strategyKind)
            {
            case AnalysisStrategyKind.ONDEMAND_ASYNC:
            {
                this.AnalyzeOnDemandAsync().Wait();
                var callgraph = this.GenerateCallGraphAsync().Result;
                return(callgraph);
            }

            case AnalysisStrategyKind.ONDEMAND_ORLEANS:
            {
                this.AnalyzeOnDemandOrleans().Wait();
                var callgraph = this.GenerateCallGraphAsync().Result;
                return(callgraph);
            }

            default:
            {
                throw new ArgumentException("Unknown value for Solver " + ConfigurationManager.AppSettings["Solver"]);
            }
            }
        }
コード例 #8
0
        public async Task AnalyzeAsync(AnalysisStrategyKind strategyKind = AnalysisStrategyKind.NONE)
        {
            if (strategyKind == AnalysisStrategyKind.NONE)
            {
                strategyKind = StringToAnalysisStrategy(ConfigurationManager.AppSettings["Strategy"]);
            }

            switch (strategyKind)
            {
            case AnalysisStrategyKind.ONDEMAND_ASYNC:
            {
                await this.AnalyzeOnDemandAsync();

                break;
            }

            case AnalysisStrategyKind.ONDEMAND_ORLEANS:
            {
                await this.AnalyzeOnDemandOrleans();

                break;
            }

            default:
            {
                throw new ArgumentException("Unknown value for Solver " + ConfigurationManager.AppSettings["Solver"]);
            }
            }
        }
コード例 #9
0
        public static void TestStaticMethodCall(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
using System;

static class StaticClass
{
	private static field;

	public static void Foo(int x)
	{
		Console.WriteLine(x);
	}

	public static int Field
	{
		get { return field; }
		set { field = value; }
	}
}

class Program
{
	private static void Foo()
	{
		Console.Clear();
	}

	private static void Bar()
	{
		Console.ReadKey();
	}

    public static void Main()
	{
		Foo();
		Program.Bar();
		StaticClass.Foo(3);
		var a = StaticClass.Field;
		StaticClass.Field = 4;
	}
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Main", true), callgraph));
                // This should be reachable
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Foo", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Bar", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("StaticClass", "Foo", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("StaticClass", "get_Field", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("StaticClass", "set_Field", true), callgraph));
            },
                                     strategy);
        }
コード例 #10
0
        public static void TestSimpleCall(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
using System;
public class D:C
{
    public override void m2(C b)
    {
    }
}
public class C 
{
    int f = 0;
    C g;
    public C m1(C a)
    {
         f = 0;
         g = this;
         this.m2(a);
         m2(g);
         return a;
    }
    public virtual void m2(C b)
    {
        Console.WriteLine(f);
    }
}
class Program
{
    public static void Main()
    {
        C d = new D();
        C c;
        c = new C();
        C h = d.m1(d);
        h.m2(c);
        d.Equals(c);
    }
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "m1"), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("D", "m2"), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor(new TypeDescriptor("System", "Object", "mscorlib"), "Equals", false), callgraph));
                Assert.IsFalse(s.IsReachable(new MethodDescriptor("C", "m2"), callgraph));
                Assert.IsTrue(s.IsCaller(new MethodDescriptor("C", "m1"), new MethodDescriptor("D", "m2"), callgraph));

                // Assert.IsTrue(CallGraphQueryInterface.GetInvocationCountAsync(result.Strategy, new MethodDescriptor("C", "m1")).Result == 2);
                // I don't know why I started numbering by 1
                //var callees = CallGraphQueryInterface.GetCalleesAsync(result.Strategy, new MethodDescriptor("C", "m1"), 1).Result;
                //Assert.IsTrue(callees.Contains(new MethodDescriptor("D", "m2")));
            },
                                     strategy);
        }
コード例 #11
0
        public static void TestAddMethodSimpleCall(AnalysisStrategyKind strategy)
        {
            #region original source code
            var source = @"
using System;

class Program
{
    public static void Main()
    {
    }
}";
            #endregion

            #region modified source code

            var newSource = @"
using System;

class Program
{
	public static void NewMethod(int p)
	{
		Console.WriteLine(p);
	}

    public static void Main()
    {
		NewMethod(5);
    }
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Main", true), callgraph));
            },
                                     (s) =>
            {
                File.WriteAllText(TestConstants.DocumentPath, newSource);
                var modifiedDocuments = new string[] { TestConstants.DocumentPath };

                s.ApplyModificationsAsync(modifiedDocuments).Wait();

                File.Delete(TestConstants.DocumentPath);

                //s.AddMethodAsync(new MethodDescriptor("Program", "NewMethod", true), newSource).Wait();
                //s.UpdateMethodAsync(new MethodDescriptor("Program", "Main", true), newSource).Wait();
            },
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Main", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "NewMethod", true), callgraph));
            },
                                     strategy);
        }
コード例 #12
0
        public static void TestNamedParameters(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
using System;

class C
{
	protected int value;

	public virtual int GetValue()
	{
		return 0;
	}

	public C(int v)
	{
		this.value = v;
	}
}

class D : C
{
	public D(int v) : base(v)
	{
	}
	public override int GetValue()
	{
		return this.value;
	}
}

class Program
{
	public static int Foo(C a, C x = null, C y = null)
	{
		return y.GetValue();
	}	

    public static void Main()
    {
        Foo(new C(0), y: new D(30));     
    }
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Main", true), callgraph));
                // This should be reachable
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Foo", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("D", "GetValue"), callgraph));
            },
                                     strategy);
        }
コード例 #13
0
        private static void TestSyntheticSolution(string zipFileName,
                                                  int correctNodeCount, int correctEdgeCount,
                                                  AnalysisStrategyKind strategy)
        {
            Assert.IsTrue(File.Exists(zipFileName));
            var destinationDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            Assert.IsTrue(!Directory.Exists(destinationDirectory));
            Directory.CreateDirectory(destinationDirectory);
            ZipFile.ExtractToDirectory(zipFileName, destinationDirectory);
            Assert.IsTrue(Directory.Exists(destinationDirectory));
            var solutionPath = Path.Combine(destinationDirectory, TestConstants.SolutionPath);

            Assert.IsTrue(File.Exists(solutionPath));

            TestUtils.AnalyzeSolution(solutionPath, (s, callgraph) =>
            {
                //var reachable = s.GetReachableMethods(callgraph);
                //Assert.IsTrue(reachable.Contains(new MethodDescriptor("C", "Main", true)));
                //Assert.IsTrue(reachable.Contains(new MethodDescriptor("C", "N0", true)));
                //Assert.IsTrue(reachable.Contains(new MethodDescriptor("C", "N1", true)));
                //Assert.IsTrue(reachable.Contains(new MethodDescriptor("C", "N2", true)));
                //Assert.IsTrue(reachable.Contains(new MethodDescriptor("C", "N3", true)));
                //Assert.IsTrue(reachable.Contains(new MethodDescriptor("C", "N4", true)));
                //Assert.IsTrue(reachable.Contains(new MethodDescriptor("C", "N5", true)));
                //Assert.IsTrue(reachable.Contains(new MethodDescriptor("C", "N6", true)));
                //Assert.IsTrue(reachable.Contains(new MethodDescriptor("C", "N7", true)));
                //Assert.IsTrue(reachable.Contains(new MethodDescriptor("C", "N8", true)));
                //Assert.IsTrue(reachable.Contains(new MethodDescriptor("C", "N9", true)));

                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N8", true), new MethodDescriptor("C", "N4", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N8", true), new MethodDescriptor("C", "N8", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N8", true), new MethodDescriptor("C", "N7", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N8", true), new MethodDescriptor("C", "N0", true)));

                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N5", true), new MethodDescriptor("C", "N6", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N5", true), new MethodDescriptor("C", "N2", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N5", true), new MethodDescriptor("C", "N3", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N5", true), new MethodDescriptor("C", "N7", true)));
                //Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "N5", true), new MethodDescriptor("C", "N4", true)));

                var nodeCount = callgraph.GetNodes().Count();
                Assert.IsTrue(nodeCount == correctNodeCount,
                              string.Format("Found {0} nodes instead of {1}",
                                            nodeCount, correctNodeCount));
                var edgeCount = callgraph.GetEdges().Count();

                Assert.IsTrue(edgeCount == correctEdgeCount,
                              string.Format("Found {0} edges instead of {1}",
                                            edgeCount, correctEdgeCount));
            }, strategy);
        }
コード例 #14
0
        public static void TestClassesWithSameFieldName(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
using System;
public class D
{
    public C f;
    public void m2(C b)
    {
        f = b;
    }
}
public class C 
{
    public C f;
    public C m1(C a)
    {
         f = a;
         return a;
    }
    public virtual void m2(C b)
    {
        Console.WriteLine(f);
    }
}
class Program
{

    public static void Main()
    {
        D d = new D();
        C c = new C();
        d.f  = c.m1(c);
        c.f   = c; 
        d.m2(c);

    }
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "m1"), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("D", "m2"), callgraph));
                Assert.IsFalse(s.IsReachable(new MethodDescriptor("C", "m2"), callgraph));
            },
                                     strategy);
        }
コード例 #15
0
        public static void TestProperty(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
using System;
public class D: C 
{
   public override void m2(C b) 
   {}
}
public class C 
{
    public C F { get { return f; } } 
    C f;

    public C m1(C a)
    {
         f = a;
         F.m2(this);
         this.F.m2(a.F);
         return a;
    }
    public virtual void m2(C b)
    {
        Console.WriteLine(f);
    }
}
class Program
{

    public static void Main()
    {
        C d = new D();
        C c = new C();
        C h = c.m1(d);
    }
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "m1"), callgraph));
                // This should be reachable
                //Assert.IsTrue(s.IsReachable(new MethodDescriptor("D", "m2"), callgraph));
                // This is reachable because of the problem with loadnodes
                // Assert.IsFalse(s.IsReachable(new MethodDescriptor("C", "m2")));
            },
                                     strategy);
        }
コード例 #16
0
        public static void AnalyzeSolution(string solutionPath, RunChecks checker, AnalysisStrategyKind strategy = AnalysisStrategyKind.NONE)
        {
            Environment.SetEnvironmentVariable("MyIPAddr", "127.0.0.1");

            var solAnalyzer = SolutionAnalyzer.CreateFromSolution(GrainClient, solutionPath, AnalysisRootKind.MainMethods);
            var callgraph   = solAnalyzer.Analyze(strategy);

            if (strategy == AnalysisStrategyKind.ONDEMAND_ORLEANS)
            {
                var myStatsGrain = StatsHelper.GetStatGrain(GrainClient);
                myStatsGrain.ResetStats().Wait();
            }

            checker(solAnalyzer, callgraph);
        }
コード例 #17
0
        public static void TestForLoopQueuing(AnalysisStrategyKind strategy)
        {
            #region source code
            var source =
                @"class C
{
    int f;
    int inc = 1;

    public int Inc
    {
        get { return inc; }
        set { inc = value; }
    }

    public C()
    {
        f = 5;
    }
    public int Bound()
    {
		testMethod();
        return f;
    }
	private static void testMethod(){}

    static void Main(string[] argv)
    {
        int i = 0;
        for (C c = new C(); i < c.Bound(); i+=c.Inc)
        {
            C d = new C();
        }
    }
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                // Need to include support for properties
                // Assert.IsTrue(s.IsReachable("C", "Inc"));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "Bound"), callgraph));
                Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "Main", true), new MethodDescriptor("C", "Bound"), callgraph));
                Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "Bound"), new MethodDescriptor("C", "testMethod"), callgraph));
            },
                                     strategy);
        }
コード例 #18
0
        public async Task AnalyzeSolutionAsync(string solutionPath, AnalysisStrategyKind strategyKind = StrategyKind)
        {
            {
                // Hack! Remove these lines
                //var solutionToTest = @"ConsoleApplication1\ConsoleApplication1.sln";
                var solutionToTest = @"coby\Coby.sln";
                solutionPath = Path.Combine(OrleansController.ROOT_DIR, solutionToTest);
            }

            OrleansController.solutionPath = solutionPath;
            OrleansController.analyzer     = SolutionAnalyzer.CreateFromSolution(GrainClient.Instance, solutionPath);
            await analyzer.AnalyzeAsync(strategyKind);

            // This call is to fill the documentsAssemblyName mapping
            await GetAllFilesAsync();
        }
コード例 #19
0
        // This is not a test method to run directly, use the above methods instead
        public static void TestRealSolution1(AnalysisStrategyKind strategy)
        {
            //var solutionPath = @"buildtools\src\BuildTools.sln";
            var solutionPath = @"codeformatter\src\CodeFormatter.sln";

            //var solutionPath = @"azure-powershell\src\ResourceManager.ForRefactoringOnly.sln";
            solutionPath = TestUtils.GetRealTestSolutionPath(solutionPath);

            TestUtils.AnalyzeSolution(solutionPath,
                                      (s, callgraph) =>
            {
                var reachableMethods = callgraph.GetReachableMethods();
                Debug.WriteLine("Reachable methods: {0}", reachableMethods.Count);
                Assert.IsTrue(reachableMethods.Count > 0);
            },
                                      strategy);
        }
コード例 #20
0
        // This is not a test method to run directly, use the above methods instead
        public static void TestSolution1(AnalysisStrategyKind strategy)
        {
            var solutionPath = @"ConsoleApplication1\base\ConsoleApplication1.sln";

            solutionPath = TestUtils.GetTestSolutionPath(solutionPath);

            TestUtils.AnalyzeSolution(solutionPath,
                                      (s, callgraph) =>
            {
                //callgraph.Save("solution1.dot");
                Assert.IsTrue(s.IsReachable(new MethodDescriptor(new TypeDescriptor("ConsoleApplication1", "Test", "ConsoleApplication1"), "CallBar"), callgraph));                        // ConsoleApplication1
                // Fails is I use only Contains with hascode!
                Assert.IsTrue(s.IsReachable(new MethodDescriptor(new TypeDescriptor("ClassLibrary1", "RemoteClass1", "ClassLibrary1"), "Bar", false), callgraph));                         // ClassLibrary
                Assert.IsTrue(s.IsReachable(new MethodDescriptor(new TypeDescriptor("ConsoleApplication1", "LocalClass2", "ConsoleApplication1"), "Bar"), callgraph));                     // ConsoleApplication1
                Assert.IsTrue(s.IsReachable(new MethodDescriptor(new TypeDescriptor("ConsoleApplication1", "Test", "ConsoleApplication1"), "CallBar"), callgraph));                        // ConsoleApplication1
            },
                                      strategy);
        }
コード例 #21
0
        public static void TestLambdaAsArgument(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
using System;

class Assert
{
	public static void All(IEnumerable<Order> collection, Action<Order> action)
	{
		Console.WriteLine(collection);
	}

	public static void Null(object obj)
	{
		Console.WriteLine(obj);
	}
}

class Order
{
	public object Customer { get; set; }
}

class Program
{
    public static void Main()
	{
		var orders = new List<Order>();
		Assert.All(orders, o => Assert.Null(o.Customer));
	}
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Main", true), callgraph));
                // This should be reachable
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Assert", "All", true), callgraph));
                //Assert.IsTrue(s.IsReachable(new MethodDescriptor("Assert", "Null", true), callgraph));
            },
                                     strategy);
        }
コード例 #22
0
        public static void TestRecursion(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
using System;
public class C 
{
    public static int r(int n)
    {
         if (n > 0) {
            return r(n-1) + 1;
         }
         else {
            q();
            return 0;   
         }
    }

    public static void q()
    {
        r(0);
    }
}
class Program
{
    public static void Main()
    {
        var x = C.r(100);
    }
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "r", true), callgraph));
                Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "r", true), new MethodDescriptor("C", "r", true), callgraph));
                Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "r", true), new MethodDescriptor("C", "q", true), callgraph));
                Assert.IsTrue(s.IsCalled(new MethodDescriptor("C", "q", true), new MethodDescriptor("C", "r", true), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "q", true), callgraph));
                Assert.IsTrue(s.IsCalled(new MethodDescriptor("Program", "Main", true), new MethodDescriptor("C", "r", true), callgraph));
            },
                                     strategy);
        }
コード例 #23
0
        public static void TestIf(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
public class D:C
{
    public  override C m1(C a)
    {
        return a;
    }
}
public class C 
{
    public virtual C m1(C a)
    {
         return this;
    }
}
class Program
{

    public static void Main()
    {
        C d = new D();
        C c = new C();
        C h; 
        if(d!=c) 
            h = c;
        else
            h = d; 
        C j = h.m1(c);
    }
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "m1"), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("D", "m1"), callgraph));
            },
                                     strategy);
        }
コード例 #24
0
        public static void TestFieldLoadInCallee(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
public class D
{
    public void m2()
    {}
}
public class C 
{    
    public D f;
    public virtual C m1()
    {
         var b = this.f;
         b.m2();
         return this;
    }
}
class Program
{

    public static void Main()
    {
        C c = new C();
        D d = new D();
        c.f = d;
        c.m1();
    }
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                callgraph.Save("cg.dot");
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "m1"), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("D", "m2"), callgraph));
                Assert.IsFalse(s.IsReachable(new MethodDescriptor("C", "m2"), callgraph));
            },
                                     strategy);
        }
コード例 #25
0
        // This is not a test method to run directly, use the above methods instead
        public static void TestSolution1Incremental(AnalysisStrategyKind strategy)
        {
            var solutionPath = @"ConsoleApplication1\base\ConsoleApplication1.sln";

            solutionPath = TestUtils.GetTestSolutionPath(solutionPath);

            var baseFolder     = Path.GetDirectoryName(solutionPath);
            var testRootFolder = Directory.GetParent(baseFolder).FullName;
            var currentFolder  = Path.Combine(testRootFolder, "current");
            var changesFolder  = Path.Combine(testRootFolder, "changes");

            solutionPath = solutionPath.Replace(baseFolder, currentFolder);

            TestUtils.CopyFiles(baseFolder, currentFolder);

            TestUtils.AnalyzeSolution(solutionPath,
                                      (s, callgraph) =>
            {
                //callgraph.Save("solution1.dot");
                Assert.IsTrue(s.IsReachable(new MethodDescriptor(new TypeDescriptor("ConsoleApplication1", "Test", "ConsoleApplication1"), "CallBar"), callgraph));     // ConsoleApplication1
                // Fails is I use only Contains with hascode!
                Assert.IsTrue(s.IsReachable(new MethodDescriptor(new TypeDescriptor("ClassLibrary1", "RemoteClass1", "ClassLibrary1"), "Bar", false), callgraph));      // ClassLibrary
                Assert.IsTrue(s.IsReachable(new MethodDescriptor(new TypeDescriptor("ConsoleApplication1", "LocalClass2", "ConsoleApplication1"), "Bar"), callgraph));  // ConsoleApplication1
                Assert.IsTrue(s.IsReachable(new MethodDescriptor(new TypeDescriptor("ConsoleApplication1", "Test", "ConsoleApplication1"), "CallBar"), callgraph));     // ConsoleApplication1
                Assert.IsTrue(s.IsReachable(new MethodDescriptor(new TypeDescriptor("ConsoleApplication1", "Test3", "ConsoleApplication1"), "DoTest"), callgraph));     // ConsoleApplication1
                Assert.IsTrue(s.IsReachable(new MethodDescriptor(new TypeDescriptor("ConsoleApplication1", "Test", "ConsoleApplication1"), "DoTest"), callgraph));
            },
                                      (s) =>
            {
                var modifications = TestUtils.CopyFiles(changesFolder, currentFolder);

                s.ApplyModificationsAsync(modifications).Wait();
            },
                                      (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor(new TypeDescriptor("ConsoleApplication1", "Test4", "ConsoleApplication1"), "DoTest"), callgraph));

                Assert.IsFalse(s.IsReachable(new MethodDescriptor(new TypeDescriptor("ConsoleApplication1", "Test", "ConsoleApplication1"), "DoTest"), callgraph));
            },
                                      strategy);
        }
コード例 #26
0
        public static void TestGenericMethod(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
using System;
class Program
{
	public static void error()
	{
	}

	public static void target()
	{
	}

	public static void m1(bool p)
	{
		error();
	}

	public static void m1<T>(T p)
	{
		target();
	}

    public static void Main()
    {
        m1(5);
    }
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "target", true), callgraph));
                Assert.IsFalse(s.IsReachable(new MethodDescriptor("Program", "error", true), callgraph));
            },
                                     strategy);
        }
コード例 #27
0
        public static void TestInterfaceMethodCall(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
using System;

interface IC
{
	void Bar();
}

class C : IC
{
	public void Bar()
	{
		Console.ReadKey();
	}
}

class Program
{
    public static void Main()
	{
		IC c = new C();
		c.Bar();
	}
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Main", true), callgraph));
                // This should be reachable
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "Bar"), callgraph));
                Assert.IsFalse(s.IsReachable(new MethodDescriptor("IC", "Bar"), callgraph));
            },
                                     strategy);
        }
コード例 #28
0
        public static void TestPropertyCall(AnalysisStrategyKind strategy)
        {
            #region source code
            var source = @"
using System;

class C
{
	private field;

	public int Field
	{
		get { return field; }
		set { field = value; }
	}
}

class Program
{
    public static void Main()
	{
		var c = new C();
		var a = c.Field;
		c.Field = 4;
	}
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("Program", "Main", true), callgraph));
                // This should be reachable
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "get_Field"), callgraph));
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("C", "set_Field"), callgraph));
            },
                                     strategy);
        }
コード例 #29
0
        // Test to ensure that interface methods called via interfaces are reachable
        public static void TestCallInterfaceDelegate(AnalysisStrategyKind strategy)
        {
            #region source code
            var source =
                @"
interface HasS
{
	void S();
}

class SubClass : HasS
{
	public void S() { }
	public void Unreachable() { }
}

class MainClass
{
	delegate void DoSomethingDelegate();

	static void Main(string[] argv)
	{
		HasS s = new SubClass();
		DoSomethingDelegate d = s.S;
		d();
	}
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("SubClass", "S"), callgraph));

                Assert.IsFalse(s.IsReachable(new MethodDescriptor("SubClass", "Unreachable"), callgraph));
            },
                                     strategy);
        }
コード例 #30
0
        // Test to ensure that static methods called via delegates are reachable
        public static void TestCallStaticDelegate(AnalysisStrategyKind strategy)
        {
            #region source code
            var source =
                @" 
class M
{
	public static void S() { }
	public static void Unreachable() { }
}

class MainClass
{
	delegate void DoSomethingDelegate();

	static void Test() { }

	static void Main(string[] argv)
	{
		DoSomethingDelegate d = M.S;
		DoSomethingDelegate d2 = Test;
		d();
	}
}";
            #endregion

            TestUtils.AnalyzeExample(source,
                                     (s, callgraph) =>
            {
                // Should not fail but it wrongly computes MainClass.HasS.S
                // This is because it does not understand "s" in "s.S"
                Assert.IsTrue(s.IsReachable(new MethodDescriptor("M", "S", true), callgraph));

                Assert.IsFalse(s.IsReachable(new MethodDescriptor("M", "Unreachable", true), callgraph));
            },
                                     strategy);
        }