Beispiel #1
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);
        }
        // ReSharper disable once SuggestBaseTypeForParameter
        private void VerifyVectorImageConstraints(int templateCode, VectorImageElementConstraints vectorImageConstraints)
        {
            VerifyBinaryConstraints(templateCode, vectorImageConstraints);

            if (vectorImageConstraints.SupportedFileFormats.Any(x => !VectorImageFileFormats.Contains(x)))
            {
                throw new TemplateValidationException(templateCode, TemplateElementValidationError.UnsupportedImageFileFormat);
            }
        }
        public static void EnsureNoBitmaps(int templateCode, XElement rootNode, VectorImageElementConstraints elementConstraints)
        {
            if (!elementConstraints.WithoutBitmaps)
            {
                return;
            }

            if (rootNode.Descendants().Any(e => ImageElementName.Equals(e.Name.LocalName, StringComparison.OrdinalIgnoreCase)))
            {
                throw new InvalidBinaryException(templateCode, new ImageWithBitmapsError());
            }
        }
Beispiel #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));
        }
Beispiel #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);
        }
Beispiel #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);
        }
        public static void EnsureNoGradient(int templateCode, XElement rootNode, VectorImageElementConstraints elementConstraints)
        {
            if (!elementConstraints.WithoutGradient)
            {
                return;
            }

            var gradientElements = new HashSet <string>(rootNode.Descendants()
                                                        .Select(e => e.Name.LocalName.ToLowerInvariant())
                                                        .Where(name => GradientElements.Contains(name)));

            if (gradientElements.Count > 0)
            {
                throw new InvalidBinaryException(templateCode, new ImageWithGradientError(gradientElements));
            }
        }
Beispiel #8
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);
        }
Beispiel #9
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));
        }
Beispiel #10
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());
        }
        public static void EnsureNoUrlInStyles(int templateCode, XElement rootNode, VectorImageElementConstraints elementConstraints)
        {
            if (!elementConstraints.WithoutUrlInStyles)
            {
                return;
            }

            if (rootNode.Attributes().Any(a => IsStyleAttribute(a) && StyleContainsUrl(a.Value)))
            {
                throw new InvalidBinaryException(templateCode, new ImageWithUrlInStylesError());
            }

            if (rootNode.Descendants().Any(e => IsStyleElement(e) && StyleContainsUrl(e.Value)))
            {
                throw new InvalidBinaryException(templateCode, new ImageWithUrlInStylesError());
            }

            if (rootNode.Descendants().Any(e => e.Attributes().Any(a => IsStyleAttribute(a) && StyleContainsUrl(a.Value))))
            {
                throw new InvalidBinaryException(templateCode, new ImageWithUrlInStylesError());
            }
        }
        private static void ValidateSvg(int templateCode, VectorImageElementConstraints elementConstraints, Stream inputStream)
        {
            XDocument xdoc;

            try
            {
                xdoc = XDocument.Load(inputStream);
            }
            catch (XmlException)
            {
                throw new InvalidBinaryException(templateCode, new InvalidImageError());
            }

            if (xdoc.Root == null || !SvgRootName.Equals(xdoc.Root.Name.LocalName, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidBinaryException(templateCode, new InvalidImageError());
            }

            foreach (var validationRule in SvgValidationRules)
            {
                validationRule(templateCode, xdoc.Root, elementConstraints);
            }
        }
        public static void ValidateVectorImage(int templateCode, FileFormat fileFormat, VectorImageElementConstraints elementConstraints, Stream inputStream)
        {
            switch (fileFormat)
            {
            case FileFormat.Svg:
                ValidateSvg(templateCode, elementConstraints, inputStream);
                break;

            case FileFormat.Pdf:
                break;

            case FileFormat.Png:
            case FileFormat.Gif:
            case FileFormat.Bmp:
            case FileFormat.Chm:
            case FileFormat.Jpg:
            case FileFormat.Jpeg:
                throw new NotSupportedException($"Not vector image file format {fileFormat}");

            default:
                throw new ArgumentOutOfRangeException(nameof(fileFormat), fileFormat, "Unsupported file format");
            }
        }
        public static void EnsureNoUnclosedPaths(int templateCode, XElement rootNode, VectorImageElementConstraints elementConstraints)
        {
            if (!elementConstraints.PathsAreClosed)
            {
                return;
            }

            if (rootNode.Descendants().Any(e => IsPathElement(e) && IsUnclosedPath(e)))
            {
                throw new InvalidBinaryException(templateCode, new ImageWithUnclosedPathsError());
            }
        }