Esempio n. 1
0
        public void StreamWrapperShouldSetValidBufferWhenReadCalled(int count, int offset)
        {
            // Arrange
            var inputBuffer = new byte[1024];
            var streamBytes = new byte[count];

            for (int i = 0; i < count; i++)
            {
                streamBytes[i] = (byte)i;
            }

            var streamMock = new Mock <IStream>();

            streamMock
            .Setup(x => x.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <IntPtr>()))
            .Callback <byte[], int, IntPtr>((buffer, countToRead, bytesReadPtr) =>
            {
                Array.Copy(streamBytes, 0, buffer, 0, streamBytes.Length);
                Marshal.WriteInt32(bytesReadPtr, count);
            });

            using (var streamWrapper = new ReadonlyStream(streamMock.Object))
            {
                // Act
                var bytesRead = streamWrapper.Read(inputBuffer, offset, count);

                // Assert
                CollectionAssert.AreEqual(streamBytes, inputBuffer.Skip(offset).Take(count).ToArray());
                Assert.AreEqual(count, bytesRead);
            }
        }
Esempio n. 2
0
        public void StreamWrapper_ShouldThrow_WhenReadCalledWithOutOfRangeArguments(int bufferLength, int offSet, int bytesToRead)
        {
            // Arrange
            var buffer    = new byte[bufferLength];
            var stremMock = new Mock <IStream>();
            ArgumentOutOfRangeException exception = null;

            var streamWrapper = new ReadonlyStream(stremMock.Object);

            // Act
            try
            {
                streamWrapper.Read(buffer, offSet, bytesToRead);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                exception = ex;
            }

            // Assert
            Assert.IsNotNull(exception);
        }