Ejemplo n.º 1
0
        private static void EnsureFileHeaderIsValid(
            int templateCode,
            string fileName,
            ElementDescriptorType elementDescriptorType,
            Stream inputStream)
        {
            var fileFormat = DetectFileFormat(fileName);

            switch (elementDescriptorType)
            {
            case ElementDescriptorType.BitmapImage:
                BitmapImageValidator.ValidateBitmapImageHeader(templateCode, fileFormat, inputStream);
                break;

            case ElementDescriptorType.VectorImage:
                VectorImageValidator.ValidateVectorImageHeader(templateCode, fileFormat, inputStream);
                break;

            case ElementDescriptorType.Article:
                break;

            case ElementDescriptorType.PlainText:
            case ElementDescriptorType.FormattedText:
            case ElementDescriptorType.FasComment:
            case ElementDescriptorType.Link:
            case ElementDescriptorType.Phone:
            case ElementDescriptorType.VideoLink:
            case ElementDescriptorType.Color:
                throw new NotSupportedException($"Not binary element descriptor type {elementDescriptorType}");

            default:
                throw new ArgumentOutOfRangeException(nameof(elementDescriptorType), elementDescriptorType, "Unsupported element descriptor type");
            }
        }
Ejemplo n.º 2
0
        public void TestSvgWithUrlInStyles(string styleContent)
        {
            var          constraints       = new VectorImageElementConstraints();
            const string ExpectedErrorType = nameof(VectorImageElementConstraints.WithoutUrlInStyles);

            void TestAction(int templateCode, Stream stream) =>
            VectorImageValidator.ValidateVectorImage(templateCode, FileFormat.Svg, constraints, stream);

            TestHelpers.MakeBinaryValidationCheck <ImageWithUrlInStylesError>(
                string.Format(SvgTemplate, $"<circle style=\"{styleContent}\" />"),
                TestAction,
                ExpectedErrorType);

            TestHelpers.MakeBinaryValidationCheck <ImageWithUrlInStylesError>(
                string.Format(SvgTemplate, $"<g><rect style=\"{styleContent}\" /></g>"),
                TestAction,
                ExpectedErrorType);

            TestHelpers.MakeBinaryValidationCheck <ImageWithUrlInStylesError>(
                $"<svg style=\"{styleContent}\" />",
                TestAction,
                ExpectedErrorType);

            TestHelpers.MakeBinaryValidationCheck <ImageWithUrlInStylesError>(
                string.Format(SvgTemplate, $"<style>/* <![CDATA[ */{styleContent}/* ]]> */</style>"),
                TestAction,
                ExpectedErrorType);
        }
Ejemplo n.º 3
0
        private static void EnsureFileHeaderIsValid(
            int templateCode,
            ElementDescriptorType elementDescriptorType,
            IElementConstraints elementConstraints,
            Stream inputStream,
            IUploadedFileMetadata uploadedFileMetadata)
        {
            var fileFormat = DetectFileFormat(uploadedFileMetadata.FileName);

            switch (elementDescriptorType)
            {
            case ElementDescriptorType.BitmapImage:
                BitmapImageValidator.ValidateBitmapImageHeader(templateCode, (BitmapImageElementConstraints)elementConstraints, fileFormat, inputStream);
                break;

            case ElementDescriptorType.CompositeBitmapImage:
                if (uploadedFileMetadata.FileType == FileType.SizeSpecificBitmapImage)
                {
                    var imageMetadata = (UploadedImageMetadata)uploadedFileMetadata;
                    BitmapImageValidator.ValidateSizeSpecificBitmapImageHeader(
                        templateCode,
                        (CompositeBitmapImageElementConstraints)elementConstraints,
                        fileFormat,
                        inputStream,
                        imageMetadata.Size);
                }
                else
                {
                    BitmapImageValidator.ValidateCompositeBitmapImageOriginalHeader(
                        templateCode,
                        (CompositeBitmapImageElementConstraints)elementConstraints,
                        fileFormat,
                        inputStream);
                }

                break;

            case ElementDescriptorType.VectorImage:
                VectorImageValidator.ValidateVectorImageHeader(templateCode, fileFormat, inputStream);
                break;

            case ElementDescriptorType.Article:
                break;

            case ElementDescriptorType.PlainText:
            case ElementDescriptorType.FormattedText:
            case ElementDescriptorType.FasComment:
            case ElementDescriptorType.Link:
            case ElementDescriptorType.Phone:
            case ElementDescriptorType.VideoLink:
            case ElementDescriptorType.Color:
                throw new NotSupportedException($"Not binary element descriptor type {elementDescriptorType}");

            default:
                throw new ArgumentOutOfRangeException(nameof(elementDescriptorType), elementDescriptorType, "Unsupported element descriptor type");
            }
        }
Ejemplo n.º 4
0
        public void TestSvgWithBitmaps(string svgContent)
        {
            var constraints = new VectorImageElementConstraints();

            void TestAction(int templateCode, Stream stream) =>
            VectorImageValidator.ValidateVectorImage(templateCode, FileFormat.Svg, constraints, stream);

            TestHelpers.MakeBinaryValidationCheck <ImageWithBitmapsError>(
                string.Format(SvgTemplate, svgContent),
                TestAction,
                nameof(VectorImageElementConstraints.WithoutBitmaps));
        }
Ejemplo n.º 5
0
        public void TestSvgWithUnclosedPaths(string svgContent)
        {
            var          constraints       = new VectorImageElementConstraints();
            const string ExpectedErrorType = nameof(VectorImageElementConstraints.PathsAreClosed);

            void TestAction(int templateCode, Stream stream) =>
            VectorImageValidator.ValidateVectorImage(templateCode, FileFormat.Svg, constraints, stream);

            TestHelpers.MakeBinaryValidationCheck <ImageWithUnclosedPathsError>(
                string.Format(SvgTemplate, svgContent),
                TestAction,
                ExpectedErrorType);
        }
Ejemplo n.º 6
0
        public void TestSvgWithGradient(string svgContent, int gradientElementsCount = 1)
        {
            var          constraints       = new VectorImageElementConstraints();
            const string ExpectedErrorType = nameof(VectorImageElementConstraints.WithoutGradient);

            void TestAction(int templateCode, Stream stream) =>
            VectorImageValidator.ValidateVectorImage(templateCode, FileFormat.Svg, constraints, stream);

            var error = TestHelpers.MakeBinaryValidationCheck <ImageWithGradientError>(
                string.Format(SvgTemplate, svgContent),
                TestAction,
                ExpectedErrorType);

            Assert.Equal(gradientElementsCount, error.GradientElements.Count);
        }
Ejemplo n.º 7
0
        public void TestSvgWithClosedPaths()
        {
            var constraints = new VectorImageElementConstraints();

            void TestAction(int templateCode, Stream stream) =>
            VectorImageValidator.ValidateVectorImage(templateCode, FileFormat.Svg, constraints, stream);

            TestHelpers.MakeBinaryValidationCheck(string.Format(SvgTemplate, "<path />"), TestAction);
            TestHelpers.MakeBinaryValidationCheck(
                string.Format(SvgTemplate, "<path d=\"M 100 100 L 300 100 L 200 300 z\" fill=\"red\" stroke=\"blue\" stroke-width=\"3\" />"),
                TestAction);

            TestHelpers.MakeBinaryValidationCheck(
                string.Format(SvgTemplate, "<g><path d=\"M 100 100 L 300 100 L 200 300 Z \" /></g>"),
                TestAction);
        }
Ejemplo n.º 8
0
        public void TestPdfValidationCheck()
        {
            void TestAction(int templateCode, Stream stream) =>
            VectorImageValidator.ValidateVectorImageHeader(templateCode, FileFormat.Pdf, stream);

            TestHelpers.MakeBinaryValidationCheck("%PDF-12356", TestAction);

            TestHelpers.MakeBinaryValidationCheck <InvalidImageError>(
                "1%PDF-",
                TestAction,
                nameof(IImageElementConstraints.ValidImage));

            TestHelpers.MakeBinaryValidationCheck <InvalidImageError>(
                string.Empty,
                TestAction,
                nameof(IImageElementConstraints.ValidImage));
        }
        public static void EnsureFileContentIsValid(
            int templateCode,
            ElementDescriptorType elementDescriptorType,
            IElementConstraints elementConstraints,
            Stream inputStream,
            string fileName)
        {
            switch (elementDescriptorType)
            {
            case ElementDescriptorType.BitmapImage:
                BitmapImageValidator.ValidateBitmapImage(templateCode, (BitmapImageElementConstraints)elementConstraints, inputStream);
                break;

            case ElementDescriptorType.VectorImage:
                var fileFormat = DetectFileFormat(fileName);
                VectorImageValidator.ValidateVectorImage(templateCode, fileFormat, (VectorImageElementConstraints)elementConstraints, inputStream);
                break;

            case ElementDescriptorType.Article:
                ArticleValidator.ValidateArticle(templateCode, inputStream);
                break;

            case ElementDescriptorType.CompositeBitmapImage:
                break;

            case ElementDescriptorType.ScalableBitmapImage:
                break;

            case ElementDescriptorType.PlainText:
            case ElementDescriptorType.FormattedText:
            case ElementDescriptorType.FasComment:
            case ElementDescriptorType.Link:
            case ElementDescriptorType.Phone:
            case ElementDescriptorType.VideoLink:
            case ElementDescriptorType.Color:
                throw new NotSupportedException($"Specified element descriptor type '{elementDescriptorType}' is non-binary");

            default:
                throw new ArgumentOutOfRangeException(nameof(elementDescriptorType), elementDescriptorType, "Unsupported element descriptor type");
            }
        }
Ejemplo n.º 10
0
        public void TestSvgValidationCheck()
        {
            var constraints = new VectorImageElementConstraints();

            void TestAction(int templateCode, Stream stream) =>
            VectorImageValidator.ValidateVectorImage(templateCode, FileFormat.Svg, constraints, stream);

            TestHelpers.MakeBinaryValidationCheck(
                "<svg><title /><style></style><metadata /><defs /></svg>",
                TestAction);

            TestHelpers.MakeBinaryValidationCheck <InvalidImageError>(
                "svg",
                TestAction,
                nameof(IImageElementConstraints.ValidImage));

            TestHelpers.MakeBinaryValidationCheck <InvalidImageError>(
                string.Empty,
                TestAction,
                nameof(IImageElementConstraints.ValidImage));
        }
Ejemplo n.º 11
0
        public void TestSvgWithNonRenderedElements(string svgElementName)
        {
            var          constraints       = new VectorImageElementConstraints();
            const string ExpectedErrorType = nameof(VectorImageElementConstraints.WithoutNonRenderedElements);

            void TestAction(int templateCode, Stream stream) =>
            VectorImageValidator.ValidateVectorImage(templateCode, FileFormat.Svg, constraints, stream);

            TestHelpers.MakeBinaryValidationCheck <ImageWithNonRenderedElementsError>(
                string.Format(SvgTemplate, $"<{svgElementName} />"),
                TestAction,
                ExpectedErrorType);

            var error = TestHelpers.MakeBinaryValidationCheck <ImageWithNonRenderedElementsError>(
                string.Format(SvgTemplate, $"<g><path /><{svgElementName} /><{svgElementName} /></g>"),
                TestAction,
                ExpectedErrorType);

            Assert.Single(error.NonRenderedElements);
            Assert.Equal(svgElementName.ToLowerInvariant(), error.NonRenderedElements.First());
        }
Ejemplo n.º 12
0
        private static string EnsureFileContentIsValid(
            int templateCode,
            string fileName,
            ElementDescriptorType elementDescriptorType,
            IElementConstraints elementConstraints,
            Stream inputStream)
        {
            switch (elementDescriptorType)
            {
            case ElementDescriptorType.BitmapImage:
                return(BitmapImageValidator.ValidateBitmapImage(templateCode, (BitmapImageElementConstraints)elementConstraints, inputStream));

            case ElementDescriptorType.VectorImage:
            {
                var fileFormat = DetectFileFormat(fileName);
                VectorImageValidator.ValidateVectorImage(templateCode, fileFormat, (VectorImageElementConstraints)elementConstraints, inputStream);
                return(ContentTypesMap[fileFormat]);
            }

            case ElementDescriptorType.Article:
                ArticleValidator.ValidateArticle(templateCode, inputStream);
                return(ContentTypesMap[FileFormat.Chm]);

            case ElementDescriptorType.PlainText:
            case ElementDescriptorType.FormattedText:
            case ElementDescriptorType.FasComment:
            case ElementDescriptorType.Link:
            case ElementDescriptorType.Phone:
            case ElementDescriptorType.VideoLink:
            case ElementDescriptorType.Color:
                throw new NotSupportedException($"Not binary element descriptor type {elementDescriptorType}");

            default:
                throw new ArgumentOutOfRangeException(nameof(elementDescriptorType), elementDescriptorType, "Unsupported element descriptor type");
            }
        }