提交 6872be24 编写于 作者: A Andy Gocke

Add more tests.

上级 d1ec9ff7
......@@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
using System.Globalization;
namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Emit
{
......@@ -878,5 +879,37 @@ public static void Main()
Assert.Equal(expected, versionData);
}
[Fact]
public void ResourceProviderStreamGivesBadLength()
{
var backingStream = new MemoryStream(new byte[] { 1, 2, 3, 4 });
var stream = new TestStream(
readFunc: backingStream.Read,
length: 6, // Lie about the length (> backingStream.Length)
getPosition: () => backingStream.Position);
var c1 = CreateCompilationWithMscorlib("");
var tmpCulture = CultureInfo.CurrentUICulture;
try
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
var result = c1.Emit(new MemoryStream(), manifestResources:
new[]
{
new ResourceDescription("res", () => stream, false)
});
result.Diagnostics.Verify(
// error CS1566: Error reading resource 'res' -- 'Resource stream ended at 4 bytes, expected 6 bytes.'
Diagnostic(ErrorCode.ERR_CantReadResource).WithArguments("res", "Resource stream ended at 4 bytes, expected 6 bytes.").WithLocation(1, 1));
}
finally
{
Thread.CurrentThread.CurrentUICulture = tmpCulture;
}
}
}
}
......@@ -105,5 +105,18 @@ public void ExceptionMayChangePosition()
Assert.Throws<IOException>(() => stream.TryReadAll(destArray, 0, sourceArray.Length));
Assert.Equal(2, backingStream.Position);
}
[Fact]
public void PrematureEndOfStream()
{
var sourceArray = new byte[] { 1, 2, 3, 4 };
var stream = new MemoryStream(sourceArray);
var destArray = new byte[6];
// Try to read more bytes than exist in the stream
Assert.Equal(4, stream.TryReadAll(destArray, 0, 6));
var expected = new byte[] { 1, 2, 3, 4, 0, 0 };
Assert.Equal(expected, destArray);
}
}
}
......@@ -877,7 +877,24 @@ public void ProperStreamRead()
});
var builder = PooledBlobBuilder.GetInstance(sourceArray.Length);
builder.TryWriteBytes(stream, sourceArray.Length);
Assert.Equal(sourceArray.Length, builder.TryWriteBytes(stream, sourceArray.Length));
Assert.Equal(sourceArray, builder.ToArray());
builder.Free();
}
[Fact]
public void PrematureEndOfStream()
{
var sourceArray = new byte[] { 1, 2, 3, 4 };
var stream = new MemoryStream(sourceArray);
var destArray = new byte[6];
var builder = PooledBlobBuilder.GetInstance(destArray.Length);
// Try to write more bytes than exist in the stream
Assert.Equal(4, builder.TryWriteBytes(stream, 6));
Assert.Equal(sourceArray, builder.ToArray());
builder.Free();
......
......@@ -6,6 +6,7 @@
using System.IO;
using Microsoft.CodeAnalysis;
using Roslyn.Utilities;
using System.Globalization;
namespace Microsoft.Cci
{
......@@ -52,7 +53,7 @@ public void WriteData(BlobBuilder resourceWriter)
if (bytesWritten != count)
{
throw new EndOfStreamException(
string.Format(CodeAnalysisResources.ResourceStreamEndedUnexpectedly, bytesWritten, count));
string.Format(CultureInfo.CurrentUICulture, CodeAnalysisResources.ResourceStreamEndedUnexpectedly, bytesWritten, count));
}
resourceWriter.Align(8);
}
......
......@@ -8,13 +8,19 @@ namespace Roslyn.Test.Utilities
public class TestStream : Stream
{
private readonly bool _canRead, _canSeek, _canWrite;
private Func<byte[], int, int, int> _readFunc;
private readonly Func<byte[], int, int, int> _readFunc;
private readonly long _length;
private readonly Func<long> _getPosition;
private readonly Action<long> _setPosition;
public TestStream(
bool canRead = false,
bool canSeek = false,
bool canWrite = false,
Func<byte[], int, int, int> readFunc = null)
Func<byte[], int, int, int> readFunc = null,
long length = 0,
Func<long> getPosition = null,
Action<long> setPosition = null)
{
_canRead = canRead;
_canSeek = canSeek;
......@@ -22,6 +28,13 @@ public class TestStream : Stream
_readFunc = readFunc != null
? readFunc
: (_1, _2, _3) => { throw new NotImplementedException(); };
_length = length;
_getPosition = getPosition != null
? getPosition
: () => { throw new NotImplementedException(); };
_setPosition = setPosition != null
? setPosition
: (_1) => { throw new NotImplementedException(); };
}
public override bool CanRead => _canRead;
......@@ -30,25 +43,12 @@ public class TestStream : Stream
public override bool CanWrite => _canWrite;
public override long Length
{
get
{
throw new NotSupportedException();
}
}
public override long Length => _length;
public override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
get { return _getPosition(); }
set { _setPosition(value); }
}
public override void Flush()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册