public void CompressFiles()
        {
            var files = Directory.EnumerateFiles(@"..\..\..\Data");

            foreach (var f in files)
            {
                var source           = File.ReadAllBytes(f);
                var compressedData   = Snappy.Compress(source);
                var decompressedData = Snappy.Decompress(compressedData, 0, compressedData.Length);
                Assert.True(decompressedData.SequenceEqual(source));
            }
        }
        public void CompressRandomData()
        {
            var r   = new Random();
            var rng = new RNGCryptoServiceProvider();

            for (int i = 0; i < 10000; i++)
            {
                var data = new byte[r.Next(UInt16.MaxValue)];
                rng.GetNonZeroBytes(data);
                var compressedData   = Snappy.Compress(data);
                var decompressedData = Snappy.Decompress(compressedData, 0, compressedData.Length);
                Assert.True(decompressedData.SequenceEqual(data));
            }
        }
Beispiel #3
0
        public void BadData_InsufficentOutputBuffer_ThrowsArgumentException()
        {
            var input = new byte[100000];

            Array.Fill(input, (byte)'A');

            var compressed       = new byte[Snappy.GetMaxCompressedLength(input.Length)];
            var compressedLength = Snappy.Compress(input, compressed);

            Assert.Throws <ArgumentException>(() =>
            {
                var output = new byte[100];
                Snappy.Decompress(compressed.AsSpan(0, compressedLength), output);
            });
        }
Beispiel #4
0
        public void BadData_FromFile_ThrowsInvalidDataException(string filename)
        {
            using var resource =
                      typeof(SnappyTests).Assembly.GetManifestResourceStream($"Snappier.Tests.TestData.{filename}");
            Assert.NotNull(resource);

            var input = new byte[resource.Length];

            resource.Read(input);

            Assert.Throws <InvalidDataException>(() =>
            {
                var length = Snappy.GetUncompressedLength(input);
                Assert.InRange(length, 0, 1 << 20);

                var output = new byte[length];
                Snappy.Decompress(input, output);
            });
        }
Beispiel #5
0
        public void BadData_LongLength_ThrowsInvalidDataException()
        {
            var input = new byte[1000];

            Array.Fill(input, (byte)'A');

            var compressed       = new byte[Snappy.GetMaxCompressedLength(input.Length)];
            var compressedLength = Snappy.Compress(input, compressed);
            var compressedSpan   = compressed.AsSpan(0, compressedLength);

            // Set the length header to 16383
            compressedSpan[0] = 255;
            compressedSpan[1] = 127;

            Assert.Throws <InvalidDataException>(() =>
            {
                var output = new byte[1000];
                Snappy.Decompress(compressed, output);
            });
        }
Beispiel #6
0
        public void BadData_SimpleCorruption_ThrowsInvalidDataException()
        {
            var input = Encoding.UTF8.GetBytes("making sure we don't crash with corrupted input");

            var compressed       = new byte[Snappy.GetMaxCompressedLength(input.Length)];
            var compressedLength = Snappy.Compress(input, compressed);
            var compressedSpan   = compressed.AsSpan(0, compressedLength);

            // corrupt the data a bit
            compressedSpan[1]--;
            compressedSpan[3]++;

            Assert.Throws <InvalidDataException>(() =>
            {
                var length = Snappy.GetUncompressedLength(compressed.AsSpan(0, compressedLength));
                Assert.InRange(length, 0, 1 << 20);

                var output = new byte[length];
                Snappy.Decompress(compressed.AsSpan(0, compressedLength), output);
            });
        }
Beispiel #7
0
        public void CompressAndDecompressFile(string filename)
        {
            using var resource =
                      typeof(SnappyTests).Assembly.GetManifestResourceStream($"Snappier.Tests.TestData.{filename}");
            Assert.NotNull(resource);

            var input = new byte[resource.Length];

            resource.Read(input);

            var compressed       = new byte[Snappy.GetMaxCompressedLength(input.Length)];
            var compressedLength = Snappy.Compress(input, compressed);

            var compressedSpan = compressed.AsSpan(0, compressedLength);

            var output       = new byte[Snappy.GetUncompressedLength(compressedSpan)];
            var outputLength = Snappy.Decompress(compressedSpan, output);

            Assert.Equal(input.Length, outputLength);
            Assert.Equal(input, output);
        }