예제 #1
0
        public void FromBytes_Large()
        {
            var bytes    = Encoding.Unicode.GetBytes(LargeSource);
            var checksum = SourceText.CalculateChecksum(bytes, 0, bytes.Length, SourceHashAlgorithm.Sha256);
            var text     = EmbeddedText.FromBytes("pathToLarge", new ArraySegment <byte>(bytes, 0, bytes.Length), SourceHashAlgorithm.Sha256);

            Assert.Equal("pathToLarge", text.FilePath);
            Assert.Equal(SourceHashAlgorithm.Sha256, text.ChecksumAlgorithm);
            AssertEx.Equal(checksum, text.Checksum);
            AssertEx.Equal(BitConverter.GetBytes(bytes.Length), text.Blob.Take(4));
            AssertEx.Equal(bytes, Decompress(text.Blob.Skip(4)));
        }
예제 #2
0
        public void FromBytes_Small()
        {
            var bytes    = Encoding.UTF8.GetBytes(SmallSource);
            var checksum = SourceText.CalculateChecksum(bytes, 0, bytes.Length, SourceHashAlgorithm.Sha1);
            var text     = EmbeddedText.FromBytes("pathToSmall", new ArraySegment <byte>(bytes, 0, bytes.Length));

            Assert.Equal("pathToSmall", text.FilePath);
            Assert.Equal(SourceHashAlgorithm.Sha1, text.ChecksumAlgorithm);
            AssertEx.Equal(checksum, text.Checksum);
            AssertEx.Equal(new byte[] { 0, 0, 0, 0 }, text.Blob.Take(4));
            AssertEx.Equal(bytes, text.Blob.Skip(4));
        }
예제 #3
0
        public void FromBytes_Empty()
        {
            var text = EmbeddedText.FromBytes(
                "pathToEmpty",
                new ArraySegment <byte>(new byte[0], 0, 0),
                SourceHashAlgorithm.Sha1
                );

            Assert.Equal("pathToEmpty", text.FilePath);
            Assert.Equal(SourceHashAlgorithm.Sha1, text.ChecksumAlgorithm);
            AssertEx.Equal(
                SourceText.CalculateChecksum(new byte[0], 0, 0, SourceHashAlgorithm.Sha1),
                text.Checksum
                );
            AssertEx.Equal(new byte[] { 0, 0, 0, 0 }, text.Blob);
        }
예제 #4
0
        public static EmbeddedText FromBytes(string filePath, ArraySegment <byte> bytes, SourceHashAlgorithm checksumAlgorithm = SourceHashAlgorithm.Sha1)
        {
            ValidateFilePath(filePath);

            if (bytes.Array == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }

            SourceText.ValidateChecksumAlgorithm(checksumAlgorithm);

            return(new EmbeddedText(
                       filePath,
                       SourceText.CalculateChecksum(bytes.Array, bytes.Offset, bytes.Count, checksumAlgorithm),
                       checksumAlgorithm,
                       CreateBlob(bytes)));
        }
예제 #5
0
        public void FromStream_Empty()
        {
            var text = EmbeddedText.FromStream(
                "pathToEmpty",
                new MemoryStream(new byte[0]),
                SourceHashAlgorithm.Sha1
                );
            var checksum = SourceText.CalculateChecksum(
                new byte[0],
                0,
                0,
                SourceHashAlgorithm.Sha1
                );

            Assert.Equal("pathToEmpty", text.FilePath);
            Assert.Equal(SourceHashAlgorithm.Sha1, text.ChecksumAlgorithm);
            AssertEx.Equal(checksum, text.Checksum);
            AssertEx.Equal(new byte[] { 0, 0, 0, 0 }, text.Blob);
        }
예제 #6
0
        public void FromSource_Precomputed()
        {
            byte[] bytes = Encoding.ASCII.GetBytes(LargeSource);
            bytes[0] = 0xFF; // invalid ASCII, should be reflected in checksum, blob.

            foreach (bool useStream in new[] { true, false })
            {
                var source = useStream ?
                             SourceText.From(new MemoryStream(bytes), Encoding.ASCII, SourceHashAlgorithm.Sha1, canBeEmbedded: true) :
                             SourceText.From(bytes, bytes.Length, Encoding.ASCII, SourceHashAlgorithm.Sha1, canBeEmbedded: true);

                var text = EmbeddedText.FromSource("pathToPrecomputed", source);
                Assert.Equal("pathToPrecomputed", text.FilePath);
                Assert.Equal(SourceHashAlgorithm.Sha1, text.ChecksumAlgorithm);
                AssertEx.Equal(SourceText.CalculateChecksum(bytes, 0, bytes.Length, SourceHashAlgorithm.Sha1), source.GetChecksum());
                AssertEx.Equal(source.GetChecksum(), text.Checksum);
                AssertEx.Equal(BitConverter.GetBytes(bytes.Length), text.Blob.Take(4));
                AssertEx.Equal(bytes, Decompress(text.Blob.Skip(4)));
            }
        }
예제 #7
0
        public void FromSource_Empty()
        {
            var source = SourceText.From(
                "",
                new UTF8Encoding(encoderShouldEmitUTF8Identifier: false),
                SourceHashAlgorithm.Sha1
                );
            var text     = EmbeddedText.FromSource("pathToEmpty", source);
            var checksum = SourceText.CalculateChecksum(
                new byte[0],
                0,
                0,
                SourceHashAlgorithm.Sha1
                );

            Assert.Equal("pathToEmpty", text.FilePath);
            Assert.Equal(SourceHashAlgorithm.Sha1, text.ChecksumAlgorithm);
            AssertEx.Equal(checksum, text.Checksum);
            AssertEx.Equal(new byte[] { 0, 0, 0, 0 }, text.Blob);
        }
예제 #8
0
        public static EmbeddedText FromStream(string filePath, Stream stream, SourceHashAlgorithm checksumAlgorithm = SourceHashAlgorithm.Sha1)
        {
            ValidateFilePath(filePath);

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (!stream.CanRead || !stream.CanSeek)
            {
                throw new ArgumentException(CodeAnalysisResources.StreamMustSupportReadAndSeek, nameof(stream));
            }

            SourceText.ValidateChecksumAlgorithm(checksumAlgorithm);

            return(new EmbeddedText(
                       filePath,
                       SourceText.CalculateChecksum(stream, checksumAlgorithm),
                       checksumAlgorithm,
                       CreateBlob(stream)));
        }