Beispiel #1
0
        public void RebaseUriVisitor_VisitRun_UpdatesBaseUriDictionaryWhenPresent()
        {
            const string srcRoot    = "SRCROOT";
            Uri          srcRootUri = new Uri(@"C:\src\root");

            const string bldRoot    = "BLDROOT";
            Uri          bldRootUri = new Uri(@"C:\bld\root");

            Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output);

            Run oldRun = RandomSarifLogGenerator.GenerateRandomRun(random);
            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor(srcRoot, srcRootUri);

            var oldDictionary = new Dictionary <string, ArtifactLocation>()
            {
                { bldRoot, new ArtifactLocation {
                      Uri = bldRootUri
                  } }
            };

            oldRun.OriginalUriBaseIds = oldDictionary;

            Run newRun = rebaseUriVisitor.VisitRun(oldRun);

            IDictionary <string, ArtifactLocation> baseUriDictionary = newRun.OriginalUriBaseIds;

            baseUriDictionary.Should().ContainKey(srcRoot);
            baseUriDictionary[srcRoot].Uri.Should().BeEquivalentTo(srcRootUri);
            baseUriDictionary.Should().ContainKey(bldRoot);
            baseUriDictionary[bldRoot].Uri.Should().BeEquivalentTo(bldRootUri);
        }
Beispiel #2
0
        public void RebaseUriVisitor_VisitPhysicalLocation_DoesNotRebaseAlreadyRebasedUri()
        {
            PhysicalLocation location         = new PhysicalLocation(new Uri(@"C:\bld\src\test.dll"), "BLDROOT", null);
            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor("SRCROOT", new Uri(@"C:\bld\src\"));

            rebaseUriVisitor.VisitPhysicalLocation(location).ShouldBeEquivalentTo(location, "We should not rebase a URI multiple times.");
        }
Beispiel #3
0
        public void RebaseUriVisitor_VisitFileData_PatchesUriAndParentUri()
        {
            Uri      fileUri   = new Uri(@"file://*****:*****@"C:\src\root\blah.zip";
            FileData fileData  = new FileData()
            {
                Uri = fileUri, ParentKey = parentKey
            };
            Run run = new Run()
            {
                Files = new Dictionary <string, FileData>()
                {
                    { fileUri.ToString(), fileData }
                }
            };

            string           srcroot          = "SRCROOT";
            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor(srcroot, new Uri(@"C:\src\root\"));

            rebaseUriVisitor.FixFiles(run);

            run.Files.Should().ContainKey("blah.zip#/stuff.doc");
            var newFileData = run.Files["blah.zip#/stuff.doc"];

            newFileData.Uri.IsAbsoluteUri.Should().BeFalse();
            newFileData.Uri.Should().NotBeSameAs(fileUri);
            newFileData.UriBaseId.Should().Be(srcroot);
            newFileData.ParentKey.Should().NotBeSameAs(parentKey);
        }
Beispiel #4
0
        public void RebaseUriVisitor_VisitFileData_DoesNotPatchUriAndParentWhenNotAppropriate()
        {
            Uri      fileUri   = new Uri(@"file://*****:*****@"C:\src\root\blah.zip";
            FileData fileData  = new FileData()
            {
                Uri = fileUri, ParentKey = parentKey
            };
            Run run = new Run()
            {
                Files = new Dictionary <string, FileData>()
                {
                    { fileUri.ToString(), fileData }
                }
            };

            string           bldroot          = "BLDROOT";
            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor(bldroot, new Uri(@"C:\bld\"));

            rebaseUriVisitor.FixFiles(run);

            run.Files.Should().ContainKey(fileUri.ToString());
            var newFileData = run.Files[fileUri.ToString()];

            newFileData.Uri.Should().BeSameAs(fileUri);
            newFileData.UriBaseId.Should().BeNullOrEmpty();
            newFileData.ParentKey.Should().BeSameAs(parentKey);
        }
Beispiel #5
0
        public void RebaseUriVisitor_VisitPhysicalLocation_RebasesUri_WhenAppropriate(string rootName, string locationUriStr, string baseUriStr, string expectedDifference)
        {
            Uri locationUri           = new Uri(locationUriStr);
            Uri baseUri               = new Uri(baseUriStr);
            PhysicalLocation location = new PhysicalLocation
            {
                ArtifactLocation = new ArtifactLocation
                {
                    Uri = locationUri
                }
            };
            RebaseUriVisitor visitor     = new RebaseUriVisitor(rootName, baseUri);
            PhysicalLocation newLocation = visitor.VisitPhysicalLocation(location);

            if (!string.IsNullOrEmpty(expectedDifference))
            {
                newLocation.ArtifactLocation.UriBaseId.Should().BeEquivalentTo(rootName, because: "we should set the root name for these.");
                newLocation.ArtifactLocation.Uri.Should().BeEquivalentTo(baseUri.MakeRelativeUri(locationUri), because: "the base URI should be relative if the expected difference is there.");
                newLocation.ArtifactLocation.Uri.ToString().Should().BeEquivalentTo(expectedDifference);
            }
            else
            {
                newLocation.Should().BeEquivalentTo(location, "When we have no expected difference, we expect the location to not be changed by the rebase operation.");
            }
        }
Beispiel #6
0
        public void RebaseUriVisitor_VisitRun_ReplacesBaseUriDictionaryWhenIncorrect()
        {
            Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output);

            Run oldRun = RandomSarifLogGenerator.GenerateRandomRun(random);

            oldRun.Properties = new Dictionary <string, SerializedPropertyInfo>();
            SerializedPropertyInfo oldData = new SerializedPropertyInfo("42", false);

            oldRun.Properties.Add(RebaseUriVisitor.BaseUriDictionaryName, oldData);

            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor("SRCROOT", new Uri(@"C:\src\root"));

            Run newRun = rebaseUriVisitor.VisitRun(oldRun);

            newRun.Properties.Should().ContainKey(RebaseUriVisitor.BaseUriDictionaryName);

            Dictionary <string, Uri> baseUriDictionary = RebaseUriVisitor.DeserializePropertyDictionary(newRun.Properties[RebaseUriVisitor.BaseUriDictionaryName]);

            baseUriDictionary.Should().ContainKey("SRCROOT");
            baseUriDictionary["SRCROOT"].ShouldBeEquivalentTo(new Uri(@"C:\src\root"));

            newRun.Properties.Should().ContainKey(RebaseUriVisitor.BaseUriDictionaryName + RebaseUriVisitor.IncorrectlyFormattedDictionarySuffix);
            newRun.Properties[RebaseUriVisitor.BaseUriDictionaryName + RebaseUriVisitor.IncorrectlyFormattedDictionarySuffix].ShouldBeEquivalentTo(oldData);
        }
        public override Run VisitRun(Run node)
        {
            Run newRun;

            // Reset URI mappings for this run.
            _currentUriMappings = new Dictionary <string, Uri>();

            // Try to get the uri mappings dictionary out of the
            if (node.Properties != null && node.Properties.ContainsKey(RebaseUriVisitor.BaseUriDictionaryName))
            {
                // For a given run, we'll reset the Uri Mappings while traversing it.
                if (!RebaseUriVisitor.TryDeserializePropertyDictionary(node.Properties[RebaseUriVisitor.BaseUriDictionaryName], out _currentUriMappings))
                {
                    throw new InvalidOperationException($"Base URI Dictionary incorrectly formatted, we expect a string->uri dictionary in the Run Properties with name {RebaseUriVisitor.BaseUriDictionaryName}");
                }

                // If we don't have a dictionary we won't need to fix the files up.
                if (node.Files != null)
                {
                    FixFiles(node);
                }
            }

            newRun = base.VisitRun(node);

            return(newRun);
        }
Beispiel #8
0
        public void RebaseUriVisitor_VisitRun_UpdatesBaseUriDictionaryWhenPresent()
        {
            const string srcRoot    = "SRCROOT";
            Uri          srcRootUri = new Uri(@"C:\src\root");

            const string bldRoot    = "BLDROOT";
            Uri          bldRootUri = new Uri(@"C:\bld\root");

            Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output);

            Run oldRun = RandomSarifLogGenerator.GenerateRandomRun(random);

            oldRun.Properties = new Dictionary <string, SerializedPropertyInfo>();
            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor(srcRoot, srcRootUri);

            Dictionary <string, Uri> oldDictionary = new Dictionary <string, Uri>()
            {
                { bldRoot, bldRootUri }
            };

            oldRun.Properties.Add(RebaseUriVisitor.BaseUriDictionaryName, RebaseUriVisitor.ReserializePropertyDictionary(oldDictionary));

            Run newRun = rebaseUriVisitor.VisitRun(oldRun);

            newRun.Properties.Should().ContainKey(RebaseUriVisitor.BaseUriDictionaryName);

            Dictionary <string, Uri> baseUriDictionary = RebaseUriVisitor.DeserializePropertyDictionary(newRun.Properties[RebaseUriVisitor.BaseUriDictionaryName]);

            baseUriDictionary.Should().ContainKey(srcRoot);
            baseUriDictionary[srcRoot].ShouldBeEquivalentTo(srcRootUri);
            baseUriDictionary.Should().ContainKey(bldRoot);
            baseUriDictionary[bldRoot].ShouldBeEquivalentTo(bldRootUri);
        }
Beispiel #9
0
        private Run GenerateRunForTest(Dictionary <string, Uri> uriBaseIdMapping)
        {
            Run run = new Run();

            run.Files = new Dictionary <string, FileData>()
            {
                { "src/file1.cs", new FileData()
                  {
                      FileLocation = new FileLocation {
                          Uri = new Uri("src/file1.cs", UriKind.Relative), UriBaseId = "%TEST1%"
                      }, ParentKey = null
                  } },
                { "src/file2.dll", new FileData()
                  {
                      FileLocation = new FileLocation {
                          Uri = new Uri("src/file2.dll", UriKind.Relative), UriBaseId = "%TEST2%"
                      }, ParentKey = null
                  } },
                { "src/archive.zip", new FileData()
                  {
                      FileLocation = new FileLocation {
                          Uri = new Uri("src/archive.zip", UriKind.Relative), UriBaseId = "%TEST1%"
                      }, ParentKey = null
                  } },
                { "src/archive.zip#file3.cs", new FileData()
                  {
                      FileLocation = new FileLocation {
                          Uri = new Uri("src/archive.zip#file3.cs", UriKind.Relative), UriBaseId = "%TEST1%"
                      }, ParentKey = "src/archive.zip"
                  } },
                { "src/archive.zip#archive2.gz", new FileData()
                  {
                      FileLocation = new FileLocation {
                          Uri = new Uri("src/archive.zip#archive2.gz", UriKind.Relative), UriBaseId = "%TEST1%"
                      }, ParentKey = "src/archive.zip"
                  } },
                { "src/archive.zip#archive2.gz/file4.cs", new FileData()
                  {
                      FileLocation = new FileLocation {
                          Uri = new Uri("src/archive.zip#archive2.gz/file4.cs", UriKind.Relative), UriBaseId = "%TEST1%"
                      }, ParentKey = "src/archive.zip#archive2.gz"
                  } },
                { "src/archive.zip#file5.cs", new FileData()
                  {
                      FileLocation = new FileLocation {
                          Uri = new Uri("src/archive.zip#file5.cs", UriKind.Relative), UriBaseId = "%TEST1%"
                      }, ParentKey = "src/archive.zip"
                  } },
            };

            if (uriBaseIdMapping != null)
            {
                run.Properties = new Dictionary <string, SerializedPropertyInfo>();

                run.Properties[RebaseUriVisitor.BaseUriDictionaryName] = RebaseUriVisitor.ReserializePropertyDictionary(uriBaseIdMapping);
            }
            return(run);
        }
Beispiel #10
0
        public void RebaseUriVisitor_VisitFileData_RebasesAllTheThings()
        {
            string comprehensiveSarifPath = Path.Combine(Environment.CurrentDirectory, @"v2\SpecExamples\Comprehensive.sarif");

            string inputText = File.ReadAllText(comprehensiveSarifPath);

            SarifLog sarifLog = PrereleaseCompatibilityTransformer.UpdateToCurrentVersion(inputText, formatting: Formatting.None, out inputText);

            sarifLog.Runs.Count().Should().Be(1);

            var visitor = new RebaseVerifyingVisitor();

            visitor.VisitRun(sarifLog.Runs[0]);

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

            string uriRootText     = "file:///home/buildAgent/";
            string toolsRootBaseId = "TOOLS_ROOT";
            string srcRootBaseId   = "SRCROOT";

            int uriCount = 19;
            int toolsRootUriBaseIdCount = 4;
            int srcRootUriBaseIdCount   = 1;
            int uriBaseIdCount          = toolsRootUriBaseIdCount + srcRootUriBaseIdCount;
            int uriRootTextCount        = 13;

            visitor.FileLocationUriBaseIds.Count.Should().Be(uriCount);
            visitor.FileLocationUriBaseIds.Where(u => u == null).Count().Should().Be(uriCount - uriBaseIdCount);
            visitor.FileLocationUriBaseIds.Where(u => u != null).Count().Should().Be(uriBaseIdCount);
            visitor.FileLocationUriBaseIds.Where(u => u == toolsRootBaseId).Count().Should().Be(toolsRootUriBaseIdCount);
            visitor.FileLocationUriBaseIds.Where(u => u == srcRootBaseId).Count().Should().Be(srcRootUriBaseIdCount);

            visitor.FileLocationUris.Count.Should().Be(uriCount);
            visitor.FileLocationUris.Where(u => u != null && u.StartsWith(uriRootText)).Count().Should().Be(uriRootTextCount);

            string agentRootBaseId = "AGENT_ROOT";

            var rebaseUriVisitor = new RebaseUriVisitor(agentRootBaseId, new Uri(uriRootText));
            Run rebasedRun       = rebaseUriVisitor.VisitRun(sarifLog.Runs[0]);

            outputText = JsonConvert.SerializeObject(sarifLog, Formatting.Indented);

            visitor = new RebaseVerifyingVisitor();
            visitor.VisitRun(rebasedRun);

            visitor.FileLocationUriBaseIds.Count.Should().Be(uriCount);
            visitor.FileLocationUriBaseIds.Where(u => u == null).Count().Should().Be(1);
            visitor.FileLocationUriBaseIds.Where(u => u == toolsRootBaseId).Count().Should().Be(toolsRootUriBaseIdCount);
            visitor.FileLocationUriBaseIds.Where(u => u == srcRootBaseId).Count().Should().Be(srcRootUriBaseIdCount);
            visitor.FileLocationUriBaseIds.Where(u => u == agentRootBaseId).Count().Should().Be(uriRootTextCount);

            visitor.FileLocationUris.Count.Should().Be(uriCount);

            // The AGENT_ROOT originalUriBaseId should _not_ be counted as a file location.
            visitor.FileLocationUris.Where(u => u != null && u.StartsWith(uriRootText)).Count().Should().Be(0);
        }
Beispiel #11
0
        public void RebaseUriVisitor_VisitPhysicalLocation_DoesNothingIfIndexReferenceToRunArtifacts()
        {
            PhysicalLocation location = new PhysicalLocation
            {
                ArtifactLocation = new ArtifactLocation
                {
                    Index = 23
                }
            };
            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor("SRCROOT", new Uri(@"C:\bld\src\"));

            rebaseUriVisitor.VisitPhysicalLocation(location).Should().BeEquivalentTo(location, because: "artifact location does not need to be rebased.");
        }
Beispiel #12
0
        public void RebaseUriVisitor_VisitRun_CorrectlyPatchesFileDictionaryKeys()
        {
            Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output);

            Run oldRun = RandomSarifLogGenerator.GenerateRandomRun(random);

            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor("SRCROOT", new Uri(@"C:\src\"));

            Run newRun = rebaseUriVisitor.VisitRun(oldRun);

            newRun.Properties.Should().ContainKey(RebaseUriVisitor.BaseUriDictionaryName);

            newRun.Files.Keys.Where(k => k.StartsWith(@"C:\src\")).Should().BeEmpty();
        }
Beispiel #13
0
        public void RebaseUriVisitor_VisitPhysicalLocation_DoesNotRebaseAlreadyRebasedUri()
        {
            PhysicalLocation location = new PhysicalLocation
            {
                ArtifactLocation = new ArtifactLocation
                {
                    Uri       = new Uri(@"C:\bld\src\test.dll"),
                    UriBaseId = "BLDROOT"
                }
            };
            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor("SRCROOT", new Uri(@"C:\bld\src\"));

            rebaseUriVisitor.VisitPhysicalLocation(location).Should().BeEquivalentTo(location, because: "we should not rebase a URI multiple times.");
        }
Beispiel #14
0
        public void RebaseUriVisitor_VisitRun_CorrectlyPatchesFileDictionaryKeys()
        {
            Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output);

            Run oldRun = RandomSarifLogGenerator.GenerateRandomRun(random);

            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor("SRCROOT", new Uri(@"C:\src\"));

            Run newRun = rebaseUriVisitor.VisitRun(oldRun);

            newRun.OriginalUriBaseIds.Should().ContainKey("SRCROOT");

            newRun.Artifacts.Where(f => f.Location.Uri.OriginalString.StartsWith(@"C:\src\")).Should().BeEmpty();
        }
Beispiel #15
0
        public void RebaseUriVisitor_VisitRun_DoesNotPatchFileDictionaryKeysWhenNotABaseUri()
        {
            Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output);

            Run oldRun = RandomSarifLogGenerator.GenerateRandomRun(random);

            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor("SRCROOT", new Uri(@"C:\bld\"));

            Run newRun = rebaseUriVisitor.VisitRun(oldRun);

            newRun.OriginalUriBaseIds.Should().ContainKey("SRCROOT");

            // Random sarif log generator uses "C:\src\" as the root.
            newRun.Artifacts.Should().BeEquivalentTo(oldRun.Artifacts);
        }
        public void RebaseUriVisitor_VisitRun_AddsBaseUriDictionaryWhenNotPresent()
        {
            Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output);

            Run oldRun = RandomSarifLogGenerator.GenerateRandomRun(random);

            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor("SRCROOT", new Uri(@"C:\src\root"));

            Run newRun = rebaseUriVisitor.VisitRun(oldRun);

            IDictionary <string, Uri> baseUriDictionary = newRun.OriginalUriBaseIds;

            baseUriDictionary.Should().ContainKey("SRCROOT");
            baseUriDictionary.Should().ContainValue(new Uri(@"C:\src\root"));
        }
Beispiel #17
0
        public void RebaseUriVisitor_VisitRun_AddsBaseUriDictionaryWhenNotPresent()
        {
            Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output);

            Run oldRun = RandomSarifLogGenerator.GenerateRandomRun(random);

            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor("SRCROOT", new Uri(@"C:\src\root"));

            Run newRun = rebaseUriVisitor.VisitRun(oldRun);

            newRun.Properties.Should().ContainKey(RebaseUriVisitor.BaseUriDictionaryName);

            Dictionary <string, Uri> baseUriDictionary = RebaseUriVisitor.DeserializePropertyDictionary(newRun.Properties[RebaseUriVisitor.BaseUriDictionaryName]);

            baseUriDictionary.Should().ContainKey("SRCROOT");
            baseUriDictionary.Should().ContainValue(new Uri(@"C:\src\root"));
        }
        public void RebaseUriVisitor_VisitFileData_PatchesUriAndParentUri()
        {
            Uri      fileUri   = new Uri(@"file://*****:*****@"C:\src\root\blah.zip";
            FileData fileData  = new FileData()
            {
                FileLocation = new FileLocation {
                    Uri = fileUri
                }, ParentKey = parentKey
            };
            Run run = new Run()
            {
                Files = new Dictionary <string, FileData>()
                {
                    { fileUri.ToString(), fileData }
                }, Results = new List <Result> {
                    new Result()
                    {
                        Locations = new List <Location> {
                            new Location()
                            {
                                PhysicalLocation = new PhysicalLocation()
                                {
                                    FileLocation = new FileLocation()
                                    {
                                        Uri = fileUri
                                    }
                                }
                            }
                        }
                    }
                }
            };

            string           srcroot          = "SRCROOT";
            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor(srcroot, new Uri(@"C:\src\root\"));

            run = rebaseUriVisitor.VisitRun(run);

            run.Files.Should().ContainKey("#SRCROOT#blah.zip#/stuff.doc");
            var newFileData = run.Files["#SRCROOT#blah.zip#/stuff.doc"];

            run.OriginalUriBaseIds.Should().ContainKey(srcroot);
            run.OriginalUriBaseIds[srcroot].OriginalString.Should().Be(@"C:\src\root\");
        }
Beispiel #19
0
        public void RebaseUriVisitor_VisitFileData_PatchesParentUri()
        {
            Uri rootfileUri  = new Uri(@"file://*****:*****@"/stuff.doc", UriKind.RelativeOrAbsolute);

            Artifact rootFileData = new Artifact()
            {
                Location = new ArtifactLocation {
                    Uri = rootfileUri
                }, ParentIndex = -1
            };
            Artifact childFileData = new Artifact()
            {
                Location = new ArtifactLocation {
                    Uri = childFileUri
                }, ParentIndex = 0
            };
            Run run = new Run
            {
                Artifacts = new List <Artifact>
                {
                    new Artifact {
                        Location = new ArtifactLocation {
                            Uri = rootfileUri, Index = 0
                        }, ParentIndex = -1
                    },
                    new Artifact {
                        Location = new ArtifactLocation {
                            Uri = childFileUri, Index = 1
                        }, ParentIndex = 0
                    },
                    new Artifact {
                        Location = new ArtifactLocation {
                            Uri = childFileUri, Index = 2
                        }, ParentIndex = -1
                    }
                },
                Results = new List <Result> {
                    new Result {
                        Locations = new List <Location> {
                            new Location {
                                PhysicalLocation = new PhysicalLocation()
                                {
                                    ArtifactLocation = new ArtifactLocation()
                                    {
                                        Uri = childFileUri, Index = 1
                                    }
                                }
                            }
                        }
                    }
                }
            };

            string           srcroot          = "SRCROOT";
            Uri              rootUriBaseId    = new Uri(@"C:\src\root\");
            RebaseUriVisitor rebaseUriVisitor = new RebaseUriVisitor(srcroot, rootUriBaseId);

            run = rebaseUriVisitor.VisitRun(run);

            run.Artifacts[0].Location.Uri.Should().Be("blah.zip");
            run.Artifacts[0].Location.UriBaseId.Should().Be("SRCROOT");
            run.OriginalUriBaseIds.Should().ContainKey(srcroot);
            run.OriginalUriBaseIds[srcroot].Uri.Should().Be(@"C:\src\root\");
        }