Null-check PagedBufferedStringWriter

This commit is contained in:
ryanbrandenburg 2016-03-29 13:21:09 -07:00
parent f638c051fa
commit 906ac728c7
4 changed files with 73 additions and 3 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
@ -34,6 +35,7 @@ namespace MvcSandbox
{
var host = new WebHostBuilder()
.UseDefaultHostingConfiguration(args)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISPlatformHandlerUrl()
.UseKestrel()
.UseStartup<Startup>()

View File

@ -18,6 +18,9 @@
"Microsoft.Extensions.Logging.Console": "1.0.0-*",
"Microsoft.NETCore.Platforms": "1.0.1-*"
},
"content": [
"Views"
],
"tools": {
"dotnet-razor-tooling": "1.0.0-*"
},

View File

@ -83,11 +83,21 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
public override void Write(char[] buffer)
{
if (buffer == null)
{
return;
}
Write(buffer, 0, buffer.Length);
}
public override void Write(char[] buffer, int index, int count)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
while (count > 0)
{
var page = GetCurrentPage();
@ -110,6 +120,11 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
public override void Write(string value)
{
if (value == null)
{
return;
}
var index = 0;
var count = value.Length;

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
@ -9,11 +10,11 @@ using Xunit;
namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
{
public class PagedBufferedStringWriterTest
public class PagedBufferedTextWriterTest
{
private static readonly char[] Content;
static PagedBufferedStringWriterTest()
static PagedBufferedTextWriterTest()
{
Content = new char[4 * PagedBufferedTextWriter.PageSize];
for (var i = 0; i < Content.Length; i++)
@ -43,6 +44,24 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
Assert.Equal<char>(Content, inner.ToString().ToCharArray());
}
[Fact]
public async Task Write_CharArray_Null()
{
// Arrange
var pool = new TestArrayPool();
var inner = new StringWriter();
var writer = new PagedBufferedTextWriter(pool, inner);
// Act
writer.Write((char[])null);
await writer.FlushAsync();
// Assert
Assert.Empty(inner.ToString());
}
[Fact]
public async Task Write_CharArray()
{
@ -82,6 +101,19 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
Assert.Equal<char>(Content, inner.ToString().ToCharArray());
}
[Fact]
public void Write_CharArray_Bounded_Null()
{
// Arrange
var pool = new TestArrayPool();
var inner = new StringWriter();
var writer = new PagedBufferedTextWriter(pool, inner);
// Act & Assert
Assert.Throws<ArgumentNullException>("buffer", () => writer.Write(null, 0, 0));
}
[Fact]
public async Task Write_CharArray_Bounded()
{
@ -109,6 +141,24 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
Assert.Equal<char>(Content, inner.ToString().ToCharArray());
}
[Fact]
public async Task Write_String_Null()
{
// Arrange
var pool = new TestArrayPool();
var inner = new StringWriter();
var writer = new PagedBufferedTextWriter(pool, inner);
// Act
writer.Write((string)null);
await writer.FlushAsync();
// Assert
Assert.Empty(inner.ToString());
}
[Fact]
public async Task Write_String()
{
@ -158,7 +208,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
Assert.Equal(3, pool.Returned.Count);
}
private class TestArrayPool: ArrayPool<char>
private class TestArrayPool : ArrayPool<char>
{
public IList<char[]> Returned { get; } = new List<char[]>();