Ejemplo n.º 1
0
        public void WritesChunksInTheRightOrder()
        {
            var bytes = new byte[]
            { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
            var pipe = new PipeMock(new[]
            {
                new Chunk {
                    Bytes = new byte[] { 0x33, 0x44, 0x55, 0x66 }, Index = 1
                },
                new Chunk {
                    Bytes = new byte[] { 0x77, 0x88, 0x99 }, Index = 2
                },
                new Chunk {
                    Bytes = new byte[] { 0x00, 0x11, 0x22 }, Index = 0
                },
                new Chunk {
                    Bytes = new byte[] { 0xdd, 0xee, 0xff }, Index = 4
                },
                new Chunk {
                    Bytes = new byte[] { 0xaa, 0xbb, 0xcc }, Index = 3
                }
            });
            var stream = new MemoryStream();
            var writer = new ChunksWriter(pipe, new LoggerMock());

            writer.WriteToStream(stream, new CancellationToken(), 5);

            Assert.Equal(bytes, stream.ToArray());
        }
Ejemplo n.º 2
0
        public void TestMissingBody()
        {
            var pipe   = new PipeMock();
            var reader = new CompressedChunksReader(pipe, 4, new LoggerMock());
            var bytes  = new byte[] { 0x12, 0x34 };
            var stream = new MemoryStream(BitConverter.GetBytes(bytes.Length + 1).Concat(bytes).ToArray());

            Assert.Throws <FileCorruptedException>(() => reader.ReadFromStream(stream, new CancellationToken()));
        }
Ejemplo n.º 3
0
        public void TestCorruptedLengthHeader()
        {
            var pipe   = new PipeMock();
            var reader = new CompressedChunksReader(pipe, 4, new LoggerMock());
            var bytes1 = new byte[] { 0x12, 0x34 };
            var stream = new MemoryStream(bytes1);

            Assert.Throws <FileCorruptedException>(() => reader.ReadFromStream(stream, new CancellationToken()));
        }
Ejemplo n.º 4
0
        public void EmptyStreamProducesNoChunks()
        {
            var pipe   = new PipeMock();
            var reader = new CompressedChunksReader(pipe, 4, new LoggerMock());
            var stream = new MemoryStream();

            reader.ReadFromStream(stream, new CancellationToken());
            Assert.Empty(pipe.Chunks);
        }
Ejemplo n.º 5
0
        public void WritesNoContentForEmptyPipe()
        {
            var pipe   = new PipeMock(new Chunk[0]);
            var stream = new MemoryStream();
            var writer = new ChunksWriter(pipe, new LoggerMock());

            writer.WriteToStream(stream, new CancellationToken(), 0);

            Assert.Equal(0, stream.Length);
        }
Ejemplo n.º 6
0
        public void EmptyStreamProducesNoChunks()
        {
            var inputStream = new MemoryStream();
            var pipe        = new PipeMock();

            var reader = new ChunksReader(pipe, chunkSize: 8, logger: new LoggerMock());

            reader.ReadFromStream(inputStream, new CancellationToken());

            Assert.Empty(pipe.Chunks);
        }
Ejemplo n.º 7
0
        public void TestSingleChunk()
        {
            var pipe   = new PipeMock();
            var reader = new CompressedChunksReader(pipe, 4, new LoggerMock());
            var bytes  = new byte[] { 0x12, 0x34 };
            var stream = new MemoryStream(BitConverter.GetBytes(bytes.Length).Concat(bytes).ToArray());

            reader.ReadFromStream(stream, new CancellationToken());

            Assert.Single(pipe.Chunks);
            Assert.Equal(bytes, pipe.Chunks[0].Bytes);
        }
Ejemplo n.º 8
0
        public void TestIncorrectLengthHeader()
        {
            var pipe   = new PipeMock();
            var reader = new CompressedChunksReader(pipe, 4, new LoggerMock());
            var bytes1 = new byte[] { 0x12, 0x34 };
            var bytes2 = new byte[] { 0x56, 0x78, 0x90, 0xAB, 0xCD };
            var stream = new MemoryStream(
                BitConverter.GetBytes(bytes1.Length).Concat(bytes1)
                .Concat(bytes2)
                .ToArray());

            Assert.Throws <FileCorruptedException>(() => reader.ReadFromStream(stream, new CancellationToken()));
        }
Ejemplo n.º 9
0
        public void SmallFileFitsInOneChunk()
        {
            var bytes       = new byte[] { 0x11, 0x22, 0x33, 0x42 };
            var inputStream = new MemoryStream(bytes);
            var pipe        = new PipeMock();

            var reader = new ChunksReader(pipe, chunkSize: 8, logger: new LoggerMock());

            reader.ReadFromStream(inputStream, new CancellationToken());

            Assert.Single(pipe.Chunks);
            Assert.Equal(bytes, pipe.Chunks[0].Bytes);
        }
Ejemplo n.º 10
0
        public void WritesSingleChunkToStream()
        {
            var bytes = new byte[] { 0x00, 0x11, 0x22 };
            var pipe  = new PipeMock(new[]
            {
                new Chunk {
                    Bytes = bytes
                }
            });
            var stream = new MemoryStream();
            var writer = new ChunksWriter(pipe, new LoggerMock());

            writer.WriteToStream(stream, new CancellationToken(), 1);

            Assert.Equal(bytes, stream.ToArray());
        }
Ejemplo n.º 11
0
        public void BigFileFitsInSeveralChunks()
        {
            var bytes       = new byte[] { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB };
            var inputStream = new MemoryStream(bytes);
            var pipe        = new PipeMock();

            var reader = new ChunksReader(pipe, chunkSize: 8, logger: new LoggerMock());

            reader.ReadFromStream(inputStream, new CancellationToken());

            Assert.Equal(2, pipe.Chunks.Count);
            Assert.Equal(8, pipe.Chunks[0].Bytes.Length);
            Assert.Equal(3, pipe.Chunks[1].Bytes.Length);
            Assert.Equal(1, pipe.Chunks[1].Index);
            Assert.True(pipe.WasClosed);
        }
Ejemplo n.º 12
0
        public void WritesTwoChunksConsecutive()
        {
            var bytes = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
            var pipe  = new PipeMock(new[]
            {
                new Chunk {
                    Bytes = new byte[] { 0x00, 0x11, 0x22 }, Index = 0
                },
                new Chunk {
                    Bytes = new byte[] { 0x33, 0x44, 0x55, 0x66 }, Index = 1
                }
            });
            var stream = new MemoryStream();
            var writer = new ChunksWriter(pipe, new LoggerMock());

            writer.WriteToStream(stream, new CancellationToken(), 2);

            Assert.Equal(bytes, stream.ToArray());
        }
Ejemplo n.º 13
0
        public void TestSeveralChunks()
        {
            var pipe   = new PipeMock();
            var reader = new CompressedChunksReader(pipe, 4, new LoggerMock());
            var bytes1 = new byte[] { 0x12, 0x34 };
            var bytes2 = new byte[] { 0x56, 0x78, 0x90, 0xAB, 0xCD };
            var stream = new MemoryStream(
                BitConverter.GetBytes(bytes1.Length).Concat(bytes1)
                .Concat(BitConverter.GetBytes(bytes2.Length)).Concat(bytes2)
                .ToArray());

            reader.ReadFromStream(stream, new CancellationToken());

            Assert.Equal(2, pipe.Chunks.Count);

            Assert.Equal(bytes1, pipe.Chunks[0].Bytes);
            Assert.Equal(0, pipe.Chunks[0].Index);

            Assert.Equal(bytes2, pipe.Chunks[1].Bytes);
            Assert.Equal(1, pipe.Chunks[1].Index);
        }
Ejemplo n.º 14
0
        public void TestChunksLengthsWriting()
        {
            var bytes = BitConverter.GetBytes(3).Concat(new byte[] { 0x00, 0x11, 0x22 })
                        .Concat(BitConverter.GetBytes(4)).Concat(new byte[] { 0x33, 0x44, 0x55, 0x66 })
                        .ToArray();
            var pipe = new PipeMock(new[]
            {
                new Chunk {
                    Bytes = new byte[] { 0x00, 0x11, 0x22 }, Index = 0
                },
                new Chunk {
                    Bytes = new byte[] { 0x33, 0x44, 0x55, 0x66 }, Index = 1
                }
            });
            var stream = new MemoryStream();
            var writer = new ChunksWriter(pipe, new LoggerMock());

            writer.WriteToStream(stream, new CancellationToken(), 2, writeChunksLengths: true);

            Assert.Equal(bytes, stream.ToArray());
        }
Ejemplo n.º 15
0
        public void TestCorruptedExpectedChunks()
        {
            var bytes = BitConverter.GetBytes(3).Concat(new byte[] { 0x00, 0x11, 0x22 })
                        .Concat(BitConverter.GetBytes(4)).Concat(new byte[] { 0x33, 0x44, 0x55, 0x66 })
                        .ToArray();
            var pipe = new PipeMock(new[]
            {
                new Chunk {
                    Bytes = new byte[] { 0x00, 0x11, 0x22 }, Index = 0
                },
                new Chunk {
                    Bytes = new byte[] { 0x33, 0x44, 0x55, 0x66 }, Index = 1
                }
            });
            var stream = new MemoryStream();
            var writer = new ChunksWriter(pipe, new LoggerMock());

            Assert.Throws <FileCorruptedException>(
                () => writer.WriteToStream(
                    stream,
                    new CancellationToken(),
                    expectedChunksCount: 222,
                    writeChunksLengths: true));
        }