public void ValidatesUriConversion()
        {
            // First, we start with builders that only populate required properties that are backed by primitives.
            IDictionary <Type, DefaultObjectPopulatingVisitor.PrimitiveValueBuilder> propertyValueBuilders =
                DefaultObjectPopulatingVisitor.GetBuildersForRequiredPrimitives();

            // This test injects a URI into every URI-based property in the format that is a file path with a space.
            // This URI won't be properly percent-encoded unless our UriConverter was invoked during seriallization.
            // This test therefore ensures that all URI's in the format are properly associated with that converter.
            propertyValueBuilders[typeof(Uri)] = (isRequired) => { return(new Uri(@"c:\path with a space\file.txt")); };

            ValidateDefaultDocument(propertyValueBuilders);
        }
Beispiel #2
0
        public void DefaultValuesDoNotSerialize()
        {
            Func <SarifLog, SarifLog> callback =
                (sarifLog) =>
            {
                var visitor = new OverridePrimitiveArraysPopulatingVisitor();
                return(visitor.VisitSarifLog(sarifLog));
            };

            ValidateDefaultDocument(
                propertyValueBuilders: DefaultObjectPopulatingVisitor.GetBuildersForRequiredPrimitives(),
                postPopulationCallback: callback);
        }
Beispiel #3
0
        private void ValidateDefaultDocument(IDictionary <Type, DefaultObjectPopulatingVisitor.PrimitiveValueBuilder> propertyValueBuilders, Func <SarifLog, SarifLog> postPopulationCallback = null)
        {
            var visitor = new DefaultObjectPopulatingVisitor(_schema, propertyValueBuilders);

            var sarifLog = new SarifLog();

            visitor.Visit(sarifLog);

            sarifLog.Version = SarifVersion.Current;

            sarifLog.InlineExternalProperties[0].Version = SarifVersion.Current;

            if (postPopulationCallback != null)
            {
                sarifLog = postPopulationCallback(sarifLog);
            }

            string toValidate = JsonConvert.SerializeObject(sarifLog, Formatting.Indented);

            var validator = new Validator(_schema);

            // Guid here simply creates a verifiably non-existent file name. This name is only
            // used in validation reporting, there's no code that attempts to access the file.
            Result[] errors = validator.Validate(toValidate, Guid.NewGuid().ToString() + ".sarif");

            var sb = new StringBuilder();

            if (errors.Length > 0)
            {
                sb.AppendLine(FailureReason(errors));
            }

            sb.Length.Should().Be(0, sb.ToString());

            SarifLog clonedLog = sarifLog.DeepClone();

            clonedLog.ValueEquals(sarifLog).Should().BeTrue();

            JObject jObjectSarifLog = JObject.Parse(toValidate);

            // The following validations ensure NotYetAutoGenerated fields work as expected.

            // see .src/sarif/NotYetAutoGenerated/ReportingConfiguration.cs (Rank property)
            // see .src/sarif/NotYetAutoGenerated/Result.cs (Rank property)
            bool isRankSerialized = RecursivePropertySearch(jObjectSarifLog, "rank");

            isRankSerialized.Should().BeFalse();

            // TODO: why does this line of code provoke a stack overflow exception?
            // clonedLog.Should().BeEquivalentTo(sarifLog);
        }
Beispiel #4
0
        public void ValidateAllTheThings()
        {
            // First, we start with builders that only populate required properties that are backed by primitives.
            IDictionary <Type, DefaultObjectPopulatingVisitor.PrimitiveValueBuilder> propertyValueBuilders =
                DefaultObjectPopulatingVisitor.GetBuildersForAllPrimitives();

            Func <SarifLog, SarifLog> callback =
                (sarifLog) =>
            {
                var visitor = new OverridePrimitiveArraysPopulatingVisitor();
                return(visitor.VisitSarifLog(sarifLog));
            };

            ValidateDefaultDocument(propertyValueBuilders, callback);
        }
        public void ValidateAllTheThings()
        {
            // First, we start with builders that only populate required properties that are backed by primitives.
            IDictionary <Type, DefaultObjectPopulatingVisitor.PrimitiveValueBuilder> propertyValueBuilders =
                DefaultObjectPopulatingVisitor.GetBuildersForAllPrimitives();

            Func <SarifLog, SarifLog> callback =
                (sarifLog) =>
            {
                sarifLog.Runs[0].Tool.Driver.DottedQuadFileVersion            = "1.0.1.2";
                sarifLog.Runs[0].Conversion.Tool.Driver.DottedQuadFileVersion = "2.7.1500.12";
                return(sarifLog);
            };

            ValidateDefaultDocument(propertyValueBuilders, callback);
        }
        private void ValidateDefaultDocument(IDictionary <Type, DefaultObjectPopulatingVisitor.PrimitiveValueBuilder> propertyValueBuilders, Func <SarifLog, SarifLog> postPopulationCallback = null)
        {
            var visitor = new DefaultObjectPopulatingVisitor(_schema, propertyValueBuilders);

            var sarifLog = new SarifLog();

            visitor.Visit(sarifLog);

            sarifLog.Version = SarifVersion.Current;

            sarifLog.InlineExternalProperties[0].Version = SarifVersion.Current;

            if (postPopulationCallback != null)
            {
                sarifLog = postPopulationCallback(sarifLog);
            }

            string toValidate = JsonConvert.SerializeObject(sarifLog);

            var validator = new Validator(_schema);

            // Guid here simply creates a verifiably non-existent file name. This name is only
            // used in validation reporting, there's no code that attempts to access the file.
            Result[] errors = validator.Validate(toValidate, Guid.NewGuid().ToString() + ".sarif");

            var sb = new StringBuilder();

            if (errors.Length > 0)
            {
                sb.AppendLine(FailureReason(errors));
            }

            sb.Length.Should().Be(0, sb.ToString());

            SarifLog clonedLog = sarifLog.DeepClone();

            clonedLog.ValueEquals(sarifLog).Should().BeTrue();

            // TODO: why does this line of code provoke a stack overflow exception?
            // clonedLog.Should().BeEquivalentTo(sarifLog);
        }
 public void DefaultValuesDoNotSerialize()
 {
     ValidateDefaultDocument(propertyValueBuilders: DefaultObjectPopulatingVisitor.GetBuildersForRequiredPrimitives());
 }