Esempio n. 1
0
        public void SarifLogger_WritesRunProperties()
        {
            string propertyName         = "numberValue";
            double propertyValue        = 3.14;
            string logicalId            = nameof(logicalId) + ":" + Guid.NewGuid().ToString();
            string baselineInstanceGuid = nameof(baselineInstanceGuid) + ":" + Guid.NewGuid().ToString();
            string automationLogicalId  = nameof(automationLogicalId) + ":" + Guid.NewGuid().ToString();
            string architecture         = nameof(architecture) + ":" + "x86";
            var    conversion           = new Conversion()
            {
                Tool = DefaultTool
            };
            var utcNow                = DateTime.UtcNow;
            var versionControlUri     = new Uri("https://www.github.com/contoso/contoso");
            var versionControlDetails = new VersionControlDetails()
            {
                Uri = versionControlUri, Timestamp = DateTime.UtcNow
            };
            string originalUriBaseIdKey   = "testBase";
            Uri    originalUriBaseIdValue = new Uri("https://sourceserver.contoso.com");
            var    originalUriBaseIds     = new Dictionary <string, Uri>()
            {
                { originalUriBaseIdKey, originalUriBaseIdValue }
            };
            string defaultFileEncoding = "UTF7";
            string richMessageMimeType = "sarif-markdown";
            string redactionToken      = "[MY_REDACTION_TOKEN]";

            var sb = new StringBuilder();

            var run = new Run();

            using (var textWriter = new StringWriter(sb))
            {
                run.SetProperty(propertyName, propertyValue);

                run.LogicalId                = logicalId;
                run.BaselineInstanceGuid     = baselineInstanceGuid;
                run.AutomationLogicalId      = automationLogicalId;
                run.Architecture             = architecture;
                run.Conversion               = conversion;
                run.VersionControlProvenance = new[] { versionControlDetails };
                run.OriginalUriBaseIds       = originalUriBaseIds;
                run.DefaultFileEncoding      = defaultFileEncoding;
                run.RichMessageMimeType      = richMessageMimeType;
                run.RedactionToken           = redactionToken;

                using (var sarifLogger = new SarifLogger(
                           textWriter,
                           run: run,
                           invocationPropertiesToLog: null))
                {
                }
            }

            string output   = sb.ToString();
            var    sarifLog = JsonConvert.DeserializeObject <SarifLog>(output);

            run = sarifLog.Runs[0];

            run.GetProperty <double>(propertyName).Should().Be(propertyValue);
            run.LogicalId.Should().Be(logicalId);
            run.BaselineInstanceGuid.Should().Be(baselineInstanceGuid);
            run.AutomationLogicalId.Should().Be(automationLogicalId);
            run.Architecture.Should().Be(architecture);
            run.Conversion.Tool.Should().BeEquivalentTo(DefaultTool);
            //run.VersionControlProvenance[0].Timestamp.Should().BeEquivalentTo(utcNow);
            run.VersionControlProvenance[0].Uri.Should().BeEquivalentTo(versionControlUri);
            run.OriginalUriBaseIds[originalUriBaseIdKey].Should().Be(originalUriBaseIdValue);
            run.DefaultFileEncoding.Should().Be(defaultFileEncoding);
            run.RichMessageMimeType.Should().Be(richMessageMimeType);
            run.RedactionToken.Should().Be(redactionToken);
        }
Esempio n. 2
0
        public void SarifLogger_WritesRunProperties()
        {
            string propertyName         = "numberValue";
            double propertyValue        = 3.14;
            string logicalId            = nameof(logicalId) + ":" + Guid.NewGuid().ToString();
            string baselineInstanceGuid = nameof(baselineInstanceGuid) + ":" + Guid.NewGuid().ToString();
            string runInstanceGuid      = Guid.NewGuid().ToString();
            string automationLogicalId  = nameof(automationLogicalId) + ":" + Guid.NewGuid().ToString();
            string runInstanceId        = automationLogicalId + "/" + runInstanceGuid;
            string architecture         = nameof(architecture) + ":" + "x86";
            var    conversion           = new Conversion()
            {
                Tool = DefaultTool
            };
            var utcNow                = DateTime.UtcNow;
            var versionControlUri     = new Uri("https://www.github.com/contoso/contoso");
            var versionControlDetails = new VersionControlDetails()
            {
                RepositoryUri = versionControlUri, AsOfTimeUtc = DateTime.UtcNow
            };
            string originalUriBaseIdKey   = "testBase";
            Uri    originalUriBaseIdValue = new Uri("https://sourceserver.contoso.com");
            var    originalUriBaseIds     = new Dictionary <string, ArtifactLocation>()
            {
                { originalUriBaseIdKey, new ArtifactLocation {
                      Uri = originalUriBaseIdValue
                  } }
            };
            string        defaultEncoding = "UTF7";
            List <string> redactionTokens = new List <string> {
                "[MY_REDACTION_TOKEN]"
            };


            var sb = new StringBuilder();

            var run = new Run();

            using (var textWriter = new StringWriter(sb))
            {
                run.SetProperty(propertyName, propertyValue);

                run.AutomationDetails = new RunAutomationDetails
                {
                    Id   = runInstanceId,
                    Guid = runInstanceGuid,
                };

                run.BaselineGuid             = baselineInstanceGuid;
                run.Conversion               = conversion;
                run.VersionControlProvenance = new[] { versionControlDetails };
                run.OriginalUriBaseIds       = originalUriBaseIds;
                run.DefaultEncoding          = defaultEncoding;
                run.RedactionTokens          = redactionTokens;

                using (var sarifLogger = new SarifLogger(
                           textWriter,
                           run: run,
                           invocationPropertiesToLog: null))
                {
                }
            }

            string output   = sb.ToString();
            var    sarifLog = JsonConvert.DeserializeObject <SarifLog>(output);

            run = sarifLog.Runs[0];

            run.GetProperty <double>(propertyName).Should().Be(propertyValue);
            run.AutomationDetails.Guid.Should().Be(runInstanceGuid);
            run.BaselineGuid.Should().Be(baselineInstanceGuid);
            run.AutomationDetails.Id.Should().Be(runInstanceId);
            run.Conversion.Tool.Should().BeEquivalentTo(DefaultTool);
            run.VersionControlProvenance[0].RepositoryUri.Should().BeEquivalentTo(versionControlUri);
            run.OriginalUriBaseIds[originalUriBaseIdKey].Uri.Should().Be(originalUriBaseIdValue);
            run.DefaultEncoding.Should().Be(defaultEncoding);
            run.RedactionTokens[0].Should().Be(redactionTokens[0]);
        }
        public void PropertyBagConverter_RoundTrip()
        {
            string intPropertyName  = nameof(intPropertyName);
            int    intPropertyValue = 42;

            string stringPropertyName  = nameof(stringPropertyName);
            string stringPropertyValue = "'\"\\'";

            string normalStringPropertyName  = "source.language";
            string normalStringPropertyValue = "csharp";

            string longPropertyName  = "hugeFileSizeBytes";
            long   longPropertyValue = (long)10 * int.MaxValue;

            string   dateTimePropertyName  = nameof(dateTimePropertyName);
            DateTime dateTimePropertyValue = new DateTime(2019, 9, 27, 13, 52, 0);

            var run = new Run
            {
                Tool = new Tool
                {
                    Driver = new ToolComponent
                    {
                        Name = "CodeScanner"
                    }
                }
            };

            run.SetProperty(intPropertyName, 42);
            run.SetProperty(stringPropertyName, stringPropertyValue);
            run.SetProperty(normalStringPropertyName, normalStringPropertyValue);
            run.SetProperty(longPropertyName, longPropertyValue);
            run.SetProperty(dateTimePropertyName, dateTimePropertyValue);

            var originalLog = new SarifLog
            {
                Runs = new List <Run>
                {
                    run
                }
            };

            var settings = new JsonSerializerSettings {
                Formatting = Formatting.Indented
            };
            string originalLogContents = JsonConvert.SerializeObject(originalLog, settings);

            SarifLog deserializedLog = JsonConvert.DeserializeObject <SarifLog>(originalLogContents);

            run = deserializedLog.Runs[0];

            int integerProperty = run.GetProperty <int>(intPropertyName);

            integerProperty.Should().Be(intPropertyValue);
            run.GetSerializedPropertyValue(intPropertyName).Should().Be(intPropertyValue.ToString());

            string stringProperty = run.GetProperty <string>(stringPropertyName);

            stringProperty.Should().Be(stringPropertyValue);
            run.GetSerializedPropertyValue(stringPropertyName).Should().Be("\"'\\\"\\\\'\"");

            run.GetProperty <string>(normalStringPropertyName).Should().Be(normalStringPropertyValue);

            run.GetProperty <long>(longPropertyName).Should().Be(longPropertyValue);

            run.GetProperty <DateTime>(dateTimePropertyName).Should().Be(dateTimePropertyValue);

            // In addition to checking the individual properties (which is nice for explicitness),
            // we redundantly check that the entire SarifLog objects match.
            // THIS ASSERTION IS COMMENTED OUT BECAUSE OF https://github.com/microsoft/sarif-sdk/issues/1689,
            // "Round-tripped log comparison fails if property bag contains DateTime."
            //originalLog.ValueEquals(deserializedLog).Should().BeTrue();

            string reserializedLogContents = JsonConvert.SerializeObject(deserializedLog, settings);

            reserializedLogContents.Should().Be(originalLogContents);
        }