internal Artifact CreateFileData(FileDataVersionOne v1FileData, string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            Artifact fileData = null;

            if (v1FileData != null)
            {
                string parentKey   = v1FileData.ParentKey;
                int    parentIndex = parentKey == null
                    ? -1
                    : _v1FileKeytoV2IndexMap[parentKey];

                fileData = new Artifact
                {
                    Hashes      = v1FileData.Hashes?.Select(CreateHash).ToDictionary(p => p.Key, p => p.Value),
                    Length      = v1FileData.Length,
                    MimeType    = v1FileData.MimeType,
                    Offset      = v1FileData.Offset,
                    ParentIndex = parentIndex,
                    Properties  = v1FileData.Properties
                };

                fileData.Location           = ArtifactLocation.CreateFromFilesDictionaryKey(key, parentKey);
                fileData.Location.UriBaseId = v1FileData.UriBaseId;

                if (v1FileData.Contents != null)
                {
                    fileData.Contents = new ArtifactContent();

                    if (MimeType.IsTextualMimeType(v1FileData.MimeType))
                    {
                        fileData.Contents.Text = SarifUtilities.DecodeBase64String(v1FileData.Contents);
                    }
                    else
                    {
                        fileData.Contents.Binary = v1FileData.Contents;
                    }
                }
            }

            return(fileData);
        }
        public void ArtifactLocation_CreateFromFilesDictionaryKey()
        {
            var sb = new StringBuilder();

            var testCases = new Tuple <string, string, ArtifactLocation>[]
            {
                new Tuple <string, string, ArtifactLocation>
                    ("file.txt", null, new ArtifactLocation {
                    Uri = new Uri("file.txt", UriKind.Relative)
                }),
                new Tuple <string, string, ArtifactLocation>
                    (@"c:\file.txt", null, new ArtifactLocation {
                    Uri = new Uri("file:///c:/file.txt", UriKind.Absolute)
                }),
                new Tuple <string, string, ArtifactLocation> // this test normalizes the uri to contain three slases
                    ("file://c:/file.txt", null, new ArtifactLocation {
                    Uri = new Uri("file:///c:/file.txt", UriKind.Absolute)
                }),
                new Tuple <string, string, ArtifactLocation>
                    ("file:///c:/file.txt", null, new ArtifactLocation {
                    Uri = new Uri("file:///c:/file.txt", UriKind.Absolute)
                }),
                new Tuple <string, string, ArtifactLocation>
                    ("file://c:/archive.zip#file.txt", null, new ArtifactLocation {
                    Uri = new Uri("file.txt", UriKind.Relative)
                }),
                new Tuple <string, string, ArtifactLocation>
                    ("#SRC#file.txt", null, new ArtifactLocation {
                    Uri = new Uri("file.txt", UriKind.Relative), UriBaseId = "SRC"
                }),
                new Tuple <string, string, ArtifactLocation>
                    ("#SRC#archive.zip#archive_two.zip/file.txt", "#SRC#archive.zip#archive_two.zip/", new ArtifactLocation {
                    Uri = new Uri("file.txt", UriKind.Relative)
                }),
            };

            foreach (Tuple <string, string, ArtifactLocation> testCase in testCases)
            {
                var fileLocation = ArtifactLocation.CreateFromFilesDictionaryKey(key: testCase.Item1, parentKey: testCase.Item2);
                if (!fileLocation.ValueEquals(testCase.Item3))
                {
                    sb.AppendLine(string.Format("Unexpected file location conversion for key: '{0}', parentKey: '{1}'.", testCase.Item1, testCase.Item2));
                }
            }
            sb.Length.Should().Be(0, because: "all file locations conversions should succeed but the following cases failed." + Environment.NewLine + sb.ToString());
        }
        public override ArtifactLocation VisitArtifactLocation(ArtifactLocation node)
        {
            if (_fileLocationKeyToIndexMap != null)
            {
                string key = node.Uri.OriginalString;

                string uriBaseId = node.UriBaseId;
                if (!string.IsNullOrEmpty(uriBaseId))
                {
                    key = "#" + uriBaseId + "#" + key;
                }

                if (_fileLocationKeyToIndexMap.TryGetValue(key, out int index))
                {
                    var fileLocation = ArtifactLocation.CreateFromFilesDictionaryKey(key);
                    node.Uri       = new Uri(UriHelper.MakeValidUri(fileLocation.Uri.OriginalString), UriKind.RelativeOrAbsolute);
                    node.UriBaseId = fileLocation.UriBaseId;
                    node.Index     = index;
                }
            }

            return(base.VisitArtifactLocation(node));
        }