예제 #1
0
        public async Task StartupAnalyzer_MvcOptionsAnalysis_MultipleMiddleware()
        {
            // Arrange
            var source = Read("MvcOptions_UseMvcWithOtherMiddleware");

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var optionsAnalysis = Assert.Single(Analyses.OfType <OptionsAnalysis>());

            Assert.False(OptionsFacts.IsEndpointRoutingExplicitlyDisabled(optionsAnalysis));

            var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());

            Assert.Collection(
                middlewareAnalysis.Middleware,
                item => Assert.Equal("UseStaticFiles", item.UseMethod.Name),
                item => Assert.Equal("UseMiddleware", item.UseMethod.Name),
                item => Assert.Equal("UseMvc", item.UseMethod.Name),
                item => Assert.Equal("UseRouting", item.UseMethod.Name),
                item => Assert.Equal("UseEndpoints", item.UseMethod.Name));

            Assert.Collection(
                diagnostics,
                diagnostic =>
            {
                Assert.Same(StartupAnalyzer.Diagnostics.UnsupportedUseMvcWithEndpointRouting, diagnostic.Descriptor);
                AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location);
            });
        }
예제 #2
0
        public async Task StartupAnalyzer_MvcOptionsAnalysis_MultipleUseMvc()
        {
            // Arrange
            var source = Read("MvcOptions_UseMvcMultiple");

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var optionsAnalysis = Assert.Single(Analyses.OfType <OptionsAnalysis>());

            Assert.False(OptionsFacts.IsEndpointRoutingExplicitlyDisabled(optionsAnalysis));

            Assert.Collection(
                diagnostics,
                diagnostic =>
            {
                Assert.Same(StartupAnalyzer.Diagnostics.UnsupportedUseMvcWithEndpointRouting, diagnostic.Descriptor);
                AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM1"], diagnostic.Location);
            },
                diagnostic =>
            {
                Assert.Same(StartupAnalyzer.Diagnostics.UnsupportedUseMvcWithEndpointRouting, diagnostic.Descriptor);
                AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM2"], diagnostic.Location);
            },
                diagnostic =>
            {
                Assert.Same(StartupAnalyzer.Diagnostics.UnsupportedUseMvcWithEndpointRouting, diagnostic.Descriptor);
                AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM3"], diagnostic.Location);
            });
        }
예제 #3
0
        public async Task StartupAnalyzer_MvcOptionsAnalysis_MultipleMiddleware()
        {
            // Arrange
            var source = ReadSource("MvcOptions_UseMvcWithOtherMiddleware");

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var mvcOptionsAnalysis = Assert.Single(Analyses.OfType <MvcOptionsAnalysis>());

            Assert.Null(mvcOptionsAnalysis.EndpointRoutingEnabled);

            var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());

            Assert.Collection(
                middlewareAnalysis.Middleware,
                item => Assert.Equal("UseAuthorization", item.UseMethod.Name),
                item => Assert.Equal("UseMiddleware", item.UseMethod.Name),
                item => Assert.Equal("UseMvc", item.UseMethod.Name),
                item => Assert.Equal("UseRouting", item.UseMethod.Name),
                item => Assert.Equal("UseEndpoints", item.UseMethod.Name));

            Assert.Collection(
                diagnostics,
                diagnostic =>
            {
                Assert.Same(StartupAnalzyer.UnsupportedUseMvcWithEndpointRouting, diagnostic.Descriptor);
                AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location);
            });
        }
예제 #4
0
        public async Task StartupAnalyzer_MvcOptionsAnalysis_FindsEndpointRoutingEnabled(string sourceFileName, string mvcMiddlewareName)
        {
            // Arrange
            var source = ReadSource(sourceFileName);

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var mvcOptionsAnalysis = Assert.Single(Analyses.OfType <MvcOptionsAnalysis>());

            Assert.Null(mvcOptionsAnalysis.EndpointRoutingEnabled);

            var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());
            var middleware         = Assert.Single(middlewareAnalysis.Middleware);

            Assert.Equal(mvcMiddlewareName, middleware.UseMethod.Name);

            Assert.Collection(
                diagnostics,
                diagnostic =>
            {
                Assert.Same(StartupAnalzyer.UnsupportedUseMvcWithEndpointRouting, diagnostic.Descriptor);
                AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location);
            });
        }
예제 #5
0
    public async Task StartupAnalyzer_UseAuthorizationConfiguredBeforeUseRoutingChained_ReportsDiagnostics()
    {
        // This one asserts a false negative for https://github.com/dotnet/aspnetcore/issues/15203.
        // We don't correctly identify chained calls, this test verifies the behavior.
        // Arrange
        var source = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseFileServer()
    .UseAuthorization()
    .UseRouting()
    .UseEndpoints(r => {});
app.Run();";

        // Act
        await VerifyAnalyzerAsync(source, DiagnosticResult.EmptyDiagnosticResults);

        // Assert
        var middlewareAnalysis = Analyses.OfType <MiddlewareAnalysis>().First();

        Assert.NotEmpty(middlewareAnalysis.Middleware);
    }
예제 #6
0
        public async Task StartupAnalyzer_MvcOptionsAnalysis_MultipleUseMvc()
        {
            // Arrange
            var source = ReadSource("MvcOptions_UseMvcMultiple");

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var mvcOptionsAnalysis = Assert.Single(Analyses.OfType <MvcOptionsAnalysis>());

            Assert.Null(mvcOptionsAnalysis.EndpointRoutingEnabled);

            Assert.Collection(
                diagnostics,
                diagnostic =>
            {
                Assert.Same(StartupAnalzyer.UnsupportedUseMvcWithEndpointRouting, diagnostic.Descriptor);
                AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM1"], diagnostic.Location);
            },
                diagnostic =>
            {
                Assert.Same(StartupAnalzyer.UnsupportedUseMvcWithEndpointRouting, diagnostic.Descriptor);
                AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM2"], diagnostic.Location);
            },
                diagnostic =>
            {
                Assert.Same(StartupAnalzyer.UnsupportedUseMvcWithEndpointRouting, diagnostic.Descriptor);
                AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM3"], diagnostic.Location);
            });
        }
예제 #7
0
        public async Task StartupAnalyzer_WorksWithOtherMethodsInProgram()
        {
            // Arrange
            var source = TestSource.Read(@"using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        builder.Services.AddMvc();
        var app = builder.Build();
        app.UseStaticFiles();
        app.UseMiddleware<AuthorizationMiddleware>();
        /*MM*/app.UseMvc();
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
        });
        app.Run();
    }

    private static void MethodA()
    {
    }

    private static void MethodB()
    {
    }
}");

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var optionsAnalysis = Assert.Single(Analyses.OfType <OptionsAnalysis>());

            Assert.False(OptionsFacts.IsEndpointRoutingExplicitlyDisabled(optionsAnalysis));

            var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());

            Assert.Collection(
                middlewareAnalysis.Middleware,
                item => Assert.Equal("UseStaticFiles", item.UseMethod.Name),
                item => Assert.Equal("UseMiddleware", item.UseMethod.Name),
                item => Assert.Equal("UseMvc", item.UseMethod.Name),
                item => Assert.Equal("UseRouting", item.UseMethod.Name),
                item => Assert.Equal("UseEndpoints", item.UseMethod.Name));

            Assert.Collection(
                diagnostics,
                diagnostic =>
            {
                Assert.Same(StartupAnalyzer.Diagnostics.UnsupportedUseMvcWithEndpointRouting, diagnostic.Descriptor);
                AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location);
                Assert.Contains("inside 'Main", diagnostic.GetMessage());
            });
        }
예제 #8
0
    public async Task StartupAnalyzer_UseAuthorizationConfiguredAfterUseEndpoints_ReportsDiagnostics()
    {
        // Arrange
        var source = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseRouting();
app.UseEndpoints(r => { });
{|#0:app.UseAuthorization()|};
app.Run();";

        var diagnosticResult = new DiagnosticResult(StartupAnalyzer.Diagnostics.IncorrectlyConfiguredAuthorizationMiddleware)
                               .WithLocation(0);

        // Act
        await VerifyAnalyzerAsync(source, diagnosticResult);

        // Assert
        var middlewareAnalysis = Analyses.OfType <MiddlewareAnalysis>().First();

        Assert.NotEmpty(middlewareAnalysis.Middleware);
    }
예제 #9
0
    public async Task StartupAnalyzer_UseAuthorizationConfiguredAfterUseEndpoints_ReportsDiagnostics()
    {
        // Arrange
        var source           = @"
using Microsoft.AspNetCore.Builder;

namespace Microsoft.AspNetCore.Analyzers.TestFiles.StartupAnalyzerTest
{
    public class UseAuthAfterUseEndpoints
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseRouting();
            app.UseEndpoints(r => { });
            {|#0:app.UseAuthorization()|};
        }
    }
}
";
        var diagnosticResult = new DiagnosticResult(StartupAnalyzer.Diagnostics.IncorrectlyConfiguredAuthorizationMiddleware)
                               .WithLocation(0);

        // Act
        await VerifyAnalyzerAsync(source, diagnosticResult);

        // Assert
        var middlewareAnalysis = Analyses.OfType <MiddlewareAnalysis>().First();

        Assert.NotEmpty(middlewareAnalysis.Middleware);
    }
예제 #10
0
    public async Task StartupAnalyzer_MultipleUseAuthorization_ReportsNoDiagnostics()
    {
        // Arrange
        var source = @"
using Microsoft.AspNetCore.Builder;

namespace Microsoft.AspNetCore.Analyzers.TestFiles.StartupAnalyzerTest
{
    public class UseAuthFallbackPolicy
    {
        public void Configure(IApplicationBuilder app)
        {
            // This sort of setup would be useful if the user wants to use Auth for non-endpoint content to be handled using the Fallback policy, while
            // using the second instance for regular endpoint routing based auth. We do not want to produce a warning in this case.
            app.UseAuthorization();
            app.UseStaticFiles();

            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(r => { });
        }
    }
}";

        // Act
        await VerifyAnalyzerAsync(source, DiagnosticResult.EmptyDiagnosticResults);

        // Assert
        var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());

        Assert.NotEmpty(middlewareAnalysis.Middleware);
    }
예제 #11
0
    public async Task StartupAnalyzer_UseAuthorizationConfiguredBeforeUseRoutingChained_ReportsDiagnostics()
    {
        // This one asserts a false negative for https://github.com/dotnet/aspnetcore/issues/15203.
        // We don't correctly identify chained calls, this test verifies the behavior.
        // Arrange
        var source = @"
using Microsoft.AspNetCore.Builder;

namespace Microsoft.AspNetCore.Analyzers.TestFiles.StartupAnalyzerTest
{
    public class UseAuthBeforeUseRoutingChained
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseFileServer()
               .UseAuthorization()
               .UseRouting()
               .UseEndpoints(r => { });
        }
    }
}";

        // Act
        await VerifyAnalyzerAsync(source, DiagnosticResult.EmptyDiagnosticResults);

        // Assert
        var middlewareAnalysis = Analyses.OfType <MiddlewareAnalysis>().First();

        Assert.NotEmpty(middlewareAnalysis.Middleware);
    }
예제 #12
0
    public async Task StartupAnalyzer_UseAuthorizationInvokedMultipleTimesInEndpointRoutingBlock_ReportsNoDiagnostics()
    {
        // Arrange
        var source = @"
using Microsoft.AspNetCore.Builder;

namespace Microsoft.AspNetCore.Analyzers.TestFiles.StartupAnalyzerTest
{
    public class UseAuthMultipleTimes
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseRouting();
            app.UseAuthorization();
            app.UseAuthorization();
            app.UseEndpoints(r => { });
        }
    }
}";

        // Act
        await VerifyAnalyzerAsync(source, DiagnosticResult.EmptyDiagnosticResults);

        // Assert
        var middlewareAnalysis = Analyses.OfType <MiddlewareAnalysis>().First();

        Assert.NotEmpty(middlewareAnalysis.Middleware);
    }
예제 #13
0
    public async Task StartupAnalyzer_UseAuthorizationConfiguredAsAChain_ReportsNoDiagnostics()
    {
        // Regression test for https://github.com/dotnet/aspnetcore/issues/15203
        // Arrange
        var source = @"
using Microsoft.AspNetCore.Builder;

namespace Microsoft.AspNetCore.Analyzers.TestFiles.StartupAnalyzerTest {
    public class UseAuthConfiguredCorrectlyChained
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseRouting()
               .UseAuthorization()
               .UseEndpoints(r => { });
        }
    }
}";

        // Act
        await VerifyAnalyzerAsync(source, DiagnosticResult.EmptyDiagnosticResults);

        // Assert
        var middlewareAnalysis = Analyses.OfType <MiddlewareAnalysis>().First();

        Assert.NotEmpty(middlewareAnalysis.Middleware);
    }
예제 #14
0
    public async Task StartupAnalyzer_ServicesAnalysis_CallBuildServiceProvider()
    {
        // Arrange
        var source = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Analyzers.TestFiles.StartupAnalyzerTest
{
    public class ConfigureServices_BuildServiceProvider
    {
        public void ConfigureServices(IServiceCollection services)
        {
            {|#0:services.BuildServiceProvider()|};
        }

        public void Configure(IApplicationBuilder app)
        {
        }
    }
}";

        var diagnosticResult = new DiagnosticResult(StartupAnalyzer.Diagnostics.BuildServiceProviderShouldNotCalledInConfigureServicesMethod)
                               .WithLocation(0);

        // Act
        await VerifyAnalyzerAsync(source, diagnosticResult);

        // Assert
        var servicesAnalysis = Analyses.OfType <ServicesAnalysis>().First();

        Assert.NotEmpty(servicesAnalysis.Services);
    }
예제 #15
0
    public async Task StartupAnalyzer_MvcOptionsAnalysis_UseMvc_FindsEndpointRoutingDisabled()
    {
        // Arrange
        var source = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMvc(options => options.EnableEndpointRouting = false);
var app = builder.Build();
app.UseMvcWithDefaultRoute();
app.Run();";

        // Act
        await VerifyAnalyzerAsync(source, DiagnosticResult.EmptyDiagnosticResults);

        // Assert
        var optionsAnalysis = Assert.Single(Analyses.OfType <OptionsAnalysis>());

        Assert.True(OptionsFacts.IsEndpointRoutingExplicitlyDisabled(optionsAnalysis));

        var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());
        var middleware         = Assert.Single(middlewareAnalysis.Middleware);

        Assert.Equal("UseMvcWithDefaultRoute", middleware.UseMethod.Name);
    }
예제 #16
0
    public async Task StartupAnalyzer_MvcOptionsAnalysis_MultipleUseMvc()
    {
        // Arrange
        var source            = @"
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Analyzers.TestFiles.StartupAnalyzerTest
{
    public class MvcOptions_UseMvcMultiple
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app)
        {
            {|#0:app.UseMvcWithDefaultRoute()|};

            app.UseStaticFiles();
            app.UseMiddleware<AuthorizationMiddleware>();

            {|#1:app.UseMvc()|};

            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
            });

            {|#2:app.UseMvc()|};
        }
    }
}";
        var diagnosticResults = new[]
        {
            new DiagnosticResult(StartupAnalyzer.Diagnostics.UnsupportedUseMvcWithEndpointRouting)
            .WithLocation(0)
            .WithArguments("UseMvcWithDefaultRoute", "ConfigureServices"),

            new DiagnosticResult(StartupAnalyzer.Diagnostics.UnsupportedUseMvcWithEndpointRouting)
            .WithLocation(1)
            .WithArguments("UseMvc", "ConfigureServices"),

            new DiagnosticResult(StartupAnalyzer.Diagnostics.UnsupportedUseMvcWithEndpointRouting)
            .WithLocation(2)
            .WithArguments("UseMvc", "ConfigureServices"),
        };

        // Act
        await VerifyAnalyzerAsync(source, diagnosticResults);

        // Assert
        var optionsAnalysis = Analyses.OfType <OptionsAnalysis>().First();

        Assert.False(OptionsFacts.IsEndpointRoutingExplicitlyDisabled(optionsAnalysis));
    }
예제 #17
0
    public async Task StartupAnalyzer_WorksWithOtherMethodsInProgram()
    {
        // Arrange
        var source           = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        builder.Services.AddMvc();
        var app = builder.Build();
        app.UseStaticFiles();
        app.UseMiddleware<AuthorizationMiddleware>();
        {|#0:app.UseMvc()|};
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
        });
        app.Run();
    }

    private static void MethodA()
    {
    }

    private static void MethodB()
    {
    }
}";
        var diagnosticResult = new DiagnosticResult(StartupAnalyzer.Diagnostics.UnsupportedUseMvcWithEndpointRouting)
                               .WithLocation(0)
                               .WithArguments("UseMvc", "Main");

        // Act
        await VerifyAnalyzerAsync(source, diagnosticResult);

        // Assert
        var optionsAnalysis = Analyses.OfType <OptionsAnalysis>().First();

        Assert.False(OptionsFacts.IsEndpointRoutingExplicitlyDisabled(optionsAnalysis));

        var middlewareAnalysis = Analyses.OfType <MiddlewareAnalysis>().First();

        Assert.Collection(
            middlewareAnalysis.Middleware,
            item => Assert.Equal("UseStaticFiles", item.UseMethod.Name),
            item => Assert.Equal("UseMiddleware", item.UseMethod.Name),
            item => Assert.Equal("UseMvc", item.UseMethod.Name),
            item => Assert.Equal("UseRouting", item.UseMethod.Name),
            item => Assert.Equal("UseEndpoints", item.UseMethod.Name));
    }
예제 #18
0
        public async Task StartupAnalyzer_UseAuthorizationConfiguredCorrectly_ReportsNoDiagnostics()
        {
            // Arrange
            var source = GetSource(nameof(TestFiles.StartupAnalyzerTest.UseAuthConfiguredCorrectly));

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());

            Assert.NotEmpty(middlewareAnalysis.Middleware);
            Assert.Empty(diagnostics);
        }
예제 #19
0
        public async Task StartupAnalyzer_MultipleUseAuthorization_ReportsNoDiagnostics()
        {
            // Arrange
            var source = Read(nameof(TestFiles.StartupAnalyzerTest.UseAuthFallbackPolicy));

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());

            Assert.NotEmpty(middlewareAnalysis.Middleware);
            Assert.Empty(diagnostics);
        }
예제 #20
0
        public async Task StartupAnalyzer_UseAuthorizationInvokedMultipleTimesInEndpointRoutingBlock_ReportsNoDiagnostics()
        {
            // Arrange
            var source = Read(nameof(TestFiles.StartupAnalyzerTest.UseAuthMultipleTimes));

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());

            Assert.NotEmpty(middlewareAnalysis.Middleware);
            Assert.Empty(diagnostics);
        }
예제 #21
0
    private async Task VerifyMvcOptionsAnalysis(string source, string mvcMiddlewareName, params DiagnosticResult[] diagnosticResults)
    {
        // Arrange
        await VerifyAnalyzerAsync(source, diagnosticResults);

        // Assert
        var optionsAnalysis = Analyses.OfType <OptionsAnalysis>().First();

        Assert.False(OptionsFacts.IsEndpointRoutingExplicitlyDisabled(optionsAnalysis));

        var middlewareAnalysis = Analyses.OfType <MiddlewareAnalysis>().First();
        var middleware         = Assert.Single(middlewareAnalysis.Middleware);

        Assert.Equal(mvcMiddlewareName, middleware.UseMethod.Name);
    }
예제 #22
0
        public async Task StartupAnalyzer_UseAuthorizationConfiguredAsAChain_ReportsNoDiagnostics()
        {
            // Regression test for https://github.com/dotnet/aspnetcore/issues/15203
            // Arrange
            var source = GetSource(nameof(TestFiles.StartupAnalyzerTest.UseAuthConfiguredCorrectlyChained));

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());

            Assert.NotEmpty(middlewareAnalysis.Middleware);
            Assert.Empty(diagnostics);
        }
예제 #23
0
        public async Task StartupAnalyzer_UseAuthorizationConfiguredBeforeUseRoutingChained_ReportsDiagnostics()
        {
            // This one asserts a false negative for https://github.com/dotnet/aspnetcore/issues/15203.
            // We don't correctly identify chained calls, this test verifies the behavior.
            // Arrange
            var source = GetSource(nameof(TestFiles.StartupAnalyzerTest.UseAuthBeforeUseRoutingChained));

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());

            Assert.NotEmpty(middlewareAnalysis.Middleware);
            Assert.Empty(diagnostics);
        }
예제 #24
0
        public async Task StartupAnalyzer_ServicesAnalysis_CallBuildServiceProvider()
        {
            // Arrange
            var source = Read("ConfigureServices_BuildServiceProvider");

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var servicesAnalysis = Assert.Single(Analyses.OfType <ServicesAnalysis>());

            Assert.NotEmpty(servicesAnalysis.Services);
            Assert.Collection(diagnostics,
                              diagnostic =>
            {
                Assert.Same(StartupAnalyzer.Diagnostics.BuildServiceProviderShouldNotCalledInConfigureServicesMethod, diagnostic.Descriptor);
                AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM1"], diagnostic.Location);
            });
        }
예제 #25
0
    public async Task StartupAnalyzer_MvcOptionsAnalysis_MultipleUseMvc()
    {
        // Arrange
        var source            = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Authorization;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMvc();
var app = builder.Build();
{|#0:app.UseMvcWithDefaultRoute()|};
app.UseStaticFiles();
app.UseMiddleware<AuthorizationMiddleware>();
{|#1:app.UseMvc()|};
app.UseRouting();
app.UseEndpoints(endpoints =>
{
});
{|#2:app.UseMvc()|};
app.Run();";
        var diagnosticResults = new[]
        {
            new DiagnosticResult(StartupAnalyzer.Diagnostics.UnsupportedUseMvcWithEndpointRouting)
            .WithLocation(0)
            .WithArguments("UseMvcWithDefaultRoute", TopLevelMainName),

            new DiagnosticResult(StartupAnalyzer.Diagnostics.UnsupportedUseMvcWithEndpointRouting)
            .WithLocation(1)
            .WithArguments("UseMvc", TopLevelMainName),

            new DiagnosticResult(StartupAnalyzer.Diagnostics.UnsupportedUseMvcWithEndpointRouting)
            .WithLocation(2)
            .WithArguments("UseMvc", TopLevelMainName),
        };

        // Act
        await VerifyAnalyzerAsync(source, diagnosticResults);

        // Assert
        var optionsAnalysis = Analyses.OfType <OptionsAnalysis>().First();

        Assert.False(OptionsFacts.IsEndpointRoutingExplicitlyDisabled(optionsAnalysis));
    }
예제 #26
0
        public async Task StartupAnalyzer_UseAuthorizationConfiguredAfterUseEndpoints_ReportsDiagnostics()
        {
            // Arrange
            var source = Read(nameof(TestFiles.StartupAnalyzerTest.UseAuthAfterUseEndpoints));

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());

            Assert.NotEmpty(middlewareAnalysis.Middleware);
            Assert.Collection(diagnostics,
                              diagnostic =>
            {
                Assert.Same(StartupAnalyzer.Diagnostics.IncorrectlyConfiguredAuthorizationMiddleware, diagnostic.Descriptor);
                AnalyzerAssert.DiagnosticLocation(source.DefaultMarkerLocation, diagnostic.Location);
            });
        }
예제 #27
0
        public async Task StartupAnalyzer_MvcOptionsAnalysis_UseMvc_FindsEndpointRoutingDisabled()
        {
            // Arrange
            var source = ReadSource("MvcOptions_UseMvcWithDefaultRouteAndEndpointRoutingDisabled");

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var mvcOptionsAnalysis = Assert.Single(Analyses.OfType <MvcOptionsAnalysis>());

            Assert.False(mvcOptionsAnalysis.EndpointRoutingEnabled);

            var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());
            var middleware         = Assert.Single(middlewareAnalysis.Middleware);

            Assert.Equal("UseMvcWithDefaultRoute", middleware.UseMethod.Name);

            Assert.Empty(diagnostics);
        }
예제 #28
0
        public async Task StartupAnalyzer_MvcOptionsAnalysis_AddMvcOptions_FindsEndpointRoutingDisabled()
        {
            // Arrange
            var source = Read("MvcOptions_UseMvcWithDefaultRouteAndAddMvcOptionsEndpointRoutingDisabled");

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var optionsAnalysis = Assert.Single(Analyses.OfType <OptionsAnalysis>());

            Assert.True(OptionsFacts.IsEndpointRoutingExplicitlyDisabled(optionsAnalysis));

            var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());
            var middleware         = Assert.Single(middlewareAnalysis.Middleware);

            Assert.Equal("UseMvcWithDefaultRoute", middleware.UseMethod.Name);

            Assert.Empty(diagnostics);
        }
예제 #29
0
    public async Task StartupAnalyzer_AuthNoRouting()
    {
        // Arrange
        var source = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Authorization;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseAuthorization();
app.Run();";

        // Act
        await VerifyAnalyzerAsync(source, DiagnosticResult.EmptyDiagnosticResults);

        // Assert
        var middlewareAnalysis = Analyses.OfType <MiddlewareAnalysis>().First();

        Assert.Single(middlewareAnalysis.Middleware);
    }
예제 #30
0
        public async Task StartupAnalyzer_AuthNoRouting()
        {
            // Arrange
            var source = TestSource.Read(@"using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Authorization;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseAuthorization();
app.Run();");

            // Act
            var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

            // Assert
            var middlewareAnalysis = Assert.Single(Analyses.OfType <MiddlewareAnalysis>());

            Assert.Single(middlewareAnalysis.Middleware);
            Assert.Empty(diagnostics);
        }