Esempio n. 1
0
        public async Task AsyncIfLengthMinusPositionPositiveOverflowsBufferSizeShouldStillBePositive(long length, long position)
        {
            var baseStream = new DelegateStream(
                canReadFunc: () => true,
                canSeekFunc: () => true,
                lengthFunc: () => length,
                positionGetFunc: () => position,
                readFunc: (buffer, offset, count) => 0);
            var trackingStream = new CallTrackingStream(baseStream);

            var dest = Stream.Null;
            await trackingStream.CopyToAsync(dest);

            // Note: We can't check how many times ReadAsync was called
            // here, since trackingStream overrides CopyToAsync and forwards
            // to the inner (non-tracking) stream for the implementation

            Assert.Same(dest, trackingStream.CopyToAsyncDestination);
            Assert.InRange(trackingStream.CopyToAsyncBufferSize, 1, int.MaxValue);
            Assert.Equal(CancellationToken.None, trackingStream.CopyToAsyncCancellationToken);
        }
        public async void AsyncIfLengthIsGreaterThanPositionAndDoesNotOverflowEverythingShouldGoNormally(long length, long position)
        {
            const int ReadLimit = 7;

            // Lambda state
            byte[] outerBuffer = null;
            int?   outerOffset = null;
            int?   outerCount  = null;
            int    readsLeft   = ReadLimit;

            var srcBase = new DelegateStream(
                canReadFunc: () => true,
                canSeekFunc: () => true,
                lengthFunc: () => length,
                positionGetFunc: () => position,
                readFunc: (buffer, offset, count) =>
            {
                Assert.NotNull(buffer);
                Assert.InRange(offset, 0, buffer.Length - count);
                Assert.InRange(count, 1, int.MaxValue);

                // CopyTo should always pass in the same buffer/offset/count

                if (outerBuffer != null)
                {
                    Assert.Same(outerBuffer, buffer);
                }
                else
                {
                    outerBuffer = buffer;
                }

                if (outerOffset != null)
                {
                    Assert.Equal(outerOffset, offset);
                }
                else
                {
                    outerOffset = offset;
                }

                if (outerCount != null)
                {
                    Assert.Equal(outerCount, count);
                }
                else
                {
                    outerCount = count;
                }

                return(--readsLeft);    // CopyTo will call Read on this ReadLimit times before stopping
            });

            var src = new CallTrackingStream(srcBase);

            var destBase = new DelegateStream(
                canWriteFunc: () => true,
                writeFunc: (buffer, offset, count) =>
            {
                Assert.Same(outerBuffer, buffer);
                Assert.Equal(outerOffset, offset);
                Assert.Equal(readsLeft, count);
            });

            var dest = new CallTrackingStream(destBase);
            await src.CopyToAsync(dest);

            // Since we override CopyToAsync in CallTrackingStream,
            // src.Read will actually not get called ReadLimit
            // times, src.Inner.Read will. So, we just assert that
            // CopyToAsync was called once for src.

            Assert.Equal(1, src.TimesCalled(nameof(src.CopyToAsync)));
            Assert.Equal(ReadLimit - 1, dest.TimesCalled(nameof(dest.WriteAsync))); // dest.WriteAsync will still get called repeatedly
        }