Пример #1
0
        public void ExceptionsPropagate()
        {
            var buffer = new byte[10];

            var stream = new TestStream(readFunc: (_1, _2, _3) => { throw new IOException(); });
            Assert.Throws<IOException>(() => stream.TryReadAll(null, 0, 1));

            stream = new TestStream(readFunc: (buf, offset, count) =>
            {
                if (offset + count > buf.Length)
                {
                    throw new ArgumentException();
                }
                return 0;
            });
            Assert.Equal(0, stream.TryReadAll(buffer, 0, 1));
            Assert.Throws<ArgumentException>(() => stream.TryReadAll(buffer, 0, 100));
        }
Пример #2
0
        public void ExceptionMayChangeOutput()
        {
            var firstRead = true;
            var sourceArray = new byte[] { 1, 2, 3, 4 };

            var stream = new TestStream(readFunc: (buf, offset, count) =>
            {
                if (firstRead)
                {
                    count = count / 2;
                    Array.Copy(sourceArray, 0, buf, offset, count);
                    firstRead = false;
                    return count;
                }
                throw new IOException();
            });

            var destArray = new byte[4];
            var destCopy = destArray.AsImmutable();
            // Note: Buffer is in undefined state after receiving an exception
            Assert.Throws<IOException>(() => stream.TryReadAll(destArray, 0, sourceArray.Length));
            Assert.NotEqual(destArray, destCopy);
        }
Пример #3
0
        public void CallsReadMultipleTimes()
        {
            var firstRead = true;
            var sourceArray = new byte[] { 1, 2, 3, 4 };
            int sourceOffset = 0;

            var stream = new TestStream(readFunc: (buf, offset, count) =>
            {
                if (firstRead)
                {
                    count = count / 2;
                    firstRead = false;
                }
                Array.Copy(sourceArray, sourceOffset, buf, offset, count);
                sourceOffset += count;
                return count;
            });

            var destArray = new byte[4];
            var destCopy = destArray.AsImmutable();
            // Note: Buffer is in undefined state after receiving an exception
            Assert.Equal(sourceArray.Length, stream.TryReadAll(destArray, 0, sourceArray.Length));
            Assert.Equal(sourceArray, destArray);
        }
Пример #4
0
        public void EmitToNonWritableStreams()
        {
            var peStream = new TestStream(canRead: false, canSeek: false, canWrite: false);
            var pdbStream = new TestStream(canRead: false, canSeek: false, canWrite: false);

            var c = CSharpCompilation.Create("a",
                new[] { SyntaxFactory.ParseSyntaxTree("class C { static void Main() {} }") },
                new[] { MscorlibRef });

            Assert.Throws<ArgumentException>(() => c.Emit(peStream));
            Assert.Throws<ArgumentException>(() => c.Emit(new MemoryStream(), pdbStream));
        }
Пример #5
0
        public void EmitToNonSeekableStreams()
        {
            var peStream = new TestStream(canRead: false, canSeek: false, canWrite: true);
            var pdbStream = new TestStream(canRead: false, canSeek: false, canWrite: true);

            var c = CSharpCompilation.Create("a",
                new[] { SyntaxFactory.ParseSyntaxTree("class C { static void Main() {} }") },
                new[] { MscorlibRef });

            var r = c.Emit(peStream, pdbStream);
            r.Diagnostics.Verify();
        }
Пример #6
0
        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("");

            using (new EnsureEnglishUICulture())
            {
                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));
            }
        }
Пример #7
0
        public void ProperStreamRead()
        {
            var firstRead = true;
            var sourceArray = new byte[] { 1, 2, 3, 4 };
            int sourceOffset = 0;

            var stream = new TestStream(readFunc: (buf, offset, count) =>
            {
                if (firstRead)
                {
                    count = count / 2;
                    firstRead = false;
                }
                Array.Copy(sourceArray, sourceOffset, buf, offset, count);
                sourceOffset += count;
                return count;
            });

            var builder = PooledBlobBuilder.GetInstance(sourceArray.Length);
            Assert.Equal(sourceArray.Length, builder.TryWriteBytes(stream, sourceArray.Length));
            Assert.Equal(sourceArray, builder.ToArray());

            builder.Free();
        }
Пример #8
0
        public void SourceLink_Errors()
        {
            string source = @"
using System;

class C
{
    public static void Main()
    {
        Console.WriteLine();
    }
}
";
            var sourceLinkStream = new TestStream(canRead: true, readFunc: (_, __, ___) => { throw new Exception("Error!"); });

            var c = CreateCompilationWithMscorlib(Parse(source, "f:/build/foo.cs"), options: TestOptions.DebugDll);
            var result = c.Emit(new MemoryStream(), new MemoryStream(), options: EmitOptions.Default.WithDebugInformationFormat(DebugInformationFormat.PortablePdb), sourceLinkStream: sourceLinkStream);
            result.Diagnostics.Verify(
                // error CS0041: Unexpected error writing debug information -- 'Error!'
                Diagnostic(ErrorCode.FTL_DebugEmitFailure).WithArguments("Error!").WithLocation(1, 1));
        }
Пример #9
0
        public void ExceptionMayChangePosition()
        {
            var firstRead = true;
            var sourceArray = new byte[] { 1, 2, 3, 4 };
            var backingStream = new MemoryStream(sourceArray);

            var stream = new TestStream(readFunc: (buf, offset, count) =>
            {
                if (firstRead)
                {
                    count = count / 2;
                    backingStream.Read(buf, offset, count);
                    firstRead = false;
                    return count;
                }
                throw new IOException();
            });

            var destArray = new byte[4];
            Assert.Equal(0, backingStream.Position);
            Assert.Throws<IOException>(() => stream.TryReadAll(destArray, 0, sourceArray.Length));
            Assert.Equal(2, backingStream.Position);
        }