public void ThrowsGuardException_ForLongFluentCall() { // https://github.com/dotnet/roslyn/issues/9795 var code = string.Join(".", Enumerable.Repeat("M()", 1000)); Assert.Throws <RoslynGuardException>( () => CSharpRoslynGuard.Validate(code) ); }
public void Allows_ReasonableFluentCall() { var code = @" X.M().M().M().M().M().M().M() "; // Assert.DoesNotThrow CSharpRoslynGuard.Validate(code); }
private ResultViewModel Run(string code) { var totalStopwatch = Stopwatch.StartNew(); var compilationStopwatch = new Stopwatch(); var rewriteStopwatch = new Stopwatch(); var executionStopwatch = new Stopwatch(); ResultViewModel resultModel(string?output) => new ResultViewModel( output, compilationStopwatch.Elapsed, rewriteStopwatch.Elapsed, executionStopwatch.Elapsed, totalStopwatch.Elapsed ); try { compilationStopwatch.Start(); CSharpRoslynGuard.Validate(code); var compilation = CSharpCompilation.Create( "_", new[] { CSharpSyntaxTree.ParseText(code) }, MetadataReferences, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary) ); using (var assemblyStream = MemoryStreamManager.GetStream()) using (var rewrittenStream = MemoryStreamManager.GetStream()) { var compilationResult = compilation.Emit(assemblyStream); compilationStopwatch.Stop(); if (!compilationResult.Success) { return(resultModel(string.Join("\r\n", compilationResult.Diagnostics))); } assemblyStream.Seek(0, SeekOrigin.Begin); rewriteStopwatch.Start(); var guardToken = AssemblyGuard.Rewrite(assemblyStream, rewrittenStream); rewriteStopwatch.Stop(); var currentSetup = AppDomain.CurrentDomain.SetupInformation; using (var context = AppDomainContext.Create(new AppDomainSetup { ApplicationBase = currentSetup.ApplicationBase, PrivateBinPath = currentSetup.PrivateBinPath })) { context.LoadAssembly(LoadMethod.LoadFrom, Assembly.GetExecutingAssembly().GetAssemblyFile().FullName); executionStopwatch.Start(); var result = RemoteFunc.Invoke(context.Domain, rewrittenStream, guardToken, RemoteRun); executionStopwatch.Stop(); return(resultModel(result?.ToString())); } } } catch (Exception ex) { return(resultModel(ex.ToString())); } }
public void Allows_ReasonableNestingLevel() { var code = @"public class C { class N { void M() { int MN() { return X.x(() => 5); } } } }"; // Assert.DoesNotThrow CSharpRoslynGuard.Validate(code); }
public void ThrowsGuardException_ForLargeNestingLevel(string prefix, string open, string close) { // https://github.com/dotnet/roslyn/issues/20062 // found by Valery Sarkisov(@VSarkisov) var builder = new StringBuilder(); for (var i = 0; i < 500; i++) { builder.Append(prefix).Append(i).Append(open); } for (var i = 0; i < 500; i++) { builder.Append(close); } Assert.Throws <RoslynGuardException>( () => CSharpRoslynGuard.Validate(builder.ToString()) ); }