public void Artifact_FileIsLocked() { string filePath = Path.GetTempFileName(); Uri uri = new Uri(filePath); try { // Place an exclusive read lock on file, so that FileData cannot access its contents. // This raises an IOException, which is swallowed by FileData.Create using (var exclusiveAccessReader = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None)) { Artifact fileData = Artifact.Create(uri, OptionallyEmittedData.TextFiles); fileData.Location.Should().Be(null); fileData.MimeType.Should().Be(MimeType.Binary); fileData.Hashes.Should().BeNull(); fileData.Contents.Should().BeNull(); } } finally { if (File.Exists(filePath)) { File.Delete(filePath); } } }
public void Artifact_PersistTextFileContentsBigEndianUnicode() { Encoding encoding = Encoding.BigEndianUnicode; string filePath = Path.GetTempFileName() + ".cs"; string textValue = "अचम्भा"; byte[] fileContents = encoding.GetBytes(textValue); Uri uri = new Uri(filePath); try { File.WriteAllBytes(filePath, fileContents); Artifact fileData = Artifact.Create(uri, OptionallyEmittedData.TextFiles, mimeType: null, encoding: encoding); fileData.Location.Should().Be(null); fileData.MimeType.Should().Be(MimeType.CSharp); fileData.Hashes.Should().BeNull(); string encodedFileContents = encoding.GetString(fileContents); fileData.Contents.Text.Should().Be(encodedFileContents); } finally { if (File.Exists(filePath)) { File.Delete(filePath); } } }
public void Artifact_SerializeMultipleFileRoles() { Artifact fileData = Artifact.Create(new Uri("file:///example.cs"), OptionallyEmittedData.None); fileData.Roles = ArtifactRoles.ResponseFile | ArtifactRoles.ResultFile; string actual = JsonConvert.SerializeObject(fileData); actual.Should().Be("{\"roles\":[\"responseFile\",\"resultFile\"],\"mimeType\":\"text/x-csharp\"}"); }
public void Artifact_SerializeSingleFileRole() { Artifact fileData = Artifact.Create(new Uri("file:///example.cs"), OptionallyEmittedData.None); fileData.Roles = ArtifactRoles.AnalysisTarget; string result = JsonConvert.SerializeObject(fileData); result.Should().Be("{\"roles\":[\"analysisTarget\"],\"mimeType\":\"text/x-csharp\"}"); }
public void Artifact_FileDoesNotExist() { // If a file does not exist, and we request file contents // persistence, the logger will not raise an exception string filePath = Path.GetTempFileName(); Uri uri = new Uri(filePath); Artifact fileData = Artifact.Create(uri, OptionallyEmittedData.TextFiles); fileData.Location.Should().Be(null); fileData.MimeType.Should().Be(MimeType.Binary); fileData.Hashes.Should().BeNull(); fileData.Contents.Should().BeNull(); }
public void Artifact_PersistBinaryAndTextFileContents( string fileExtension, OptionallyEmittedData dataToInsert, bool shouldBePersisted) { string filePath = Path.GetTempFileName() + fileExtension; string fileContents = Guid.NewGuid().ToString(); Uri uri = new Uri(filePath); try { File.WriteAllText(filePath, fileContents); Artifact fileData = Artifact.Create(uri, dataToInsert); fileData.Location.Should().BeNull(); if (dataToInsert.HasFlag(OptionallyEmittedData.Hashes)) { fileData.Hashes.Should().NotBeNull(); } else { fileData.Hashes.Should().BeNull(); } string encodedFileContents = Convert.ToBase64String(File.ReadAllBytes(filePath)); if (shouldBePersisted) { fileData.Contents.Binary.Should().Be(encodedFileContents); fileData.Contents.Text.Should().BeNull(); } else { fileData.Contents.Should().BeNull(); } } finally { if (File.Exists(filePath)) { File.Delete(filePath); } } }
private static void RunUnauthorizedAccessTextForFile(bool isTextFile) { string extension = isTextFile ? ".cs" : ".dll"; string filePath = Path.GetFullPath(Guid.NewGuid().ToString()) + extension; Uri uri = new Uri(filePath); IFileSystem fileSystem = SetUnauthorizedAccessExceptionMock(); Artifact fileData = Artifact.Create( uri, OptionallyEmittedData.TextFiles, mimeType: null, encoding: null, fileSystem: fileSystem); // We pass none here as the occurrence of UnauthorizedAccessException // should result in non-population of any file contents. Validate(fileData, OptionallyEmittedData.None); }
public void Artifact_ComputeHashes() { string filePath = Path.GetTempFileName(); string fileContents = Guid.NewGuid().ToString(); Uri uri = new Uri(filePath); try { File.WriteAllText(filePath, fileContents); Artifact fileData = Artifact.Create(uri, OptionallyEmittedData.Hashes); fileData.Location.Should().Be(null); HashData hashes = HashUtilities.ComputeHashes(filePath); fileData.MimeType.Should().Be(MimeType.Binary); fileData.Contents.Should().BeNull(); fileData.Hashes.Count.Should().Be(3); foreach (string algorithm in fileData.Hashes.Keys) { switch (algorithm) { case "md5": { fileData.Hashes[algorithm].Should().Be(hashes.MD5); break; } case "sha-1": { fileData.Hashes[algorithm].Should().Be(hashes.Sha1); break; } case "sha-256": { fileData.Hashes[algorithm].Should().Be(hashes.Sha256); break; } default: { true.Should().BeFalse(); break; /* unexpected algorithm kind */ } } } } finally { if (File.Exists(filePath)) { File.Delete(filePath); } } }
public int GetFileIndex( ArtifactLocation fileLocation, bool addToFilesTableIfNotPresent = true, OptionallyEmittedData dataToInsert = OptionallyEmittedData.None, Encoding encoding = null, HashData hashData = null) { if (fileLocation == null) { throw new ArgumentNullException(nameof(fileLocation)); } if (this.Artifacts == null || this.Artifacts.Count == 0) { if (!addToFilesTableIfNotPresent) { return(-1); } } if (_fileToIndexMap == null) { InitializeFileToIndexMap(); } if (fileLocation.Uri == null) { // We only have a file index, so just return it. return(fileLocation.Index); } // Strictly speaking, some elements that may contribute to a files table // key are case sensitive, e.g., everything but the schema and protocol of a // web URI. We don't have a proper comparer implementation that can handle // all cases. For now, we cover the Windows happy path, which assumes that // most URIs in log files are file paths (which are case-insensitive) // // Tracking item for an improved comparer: // https://github.com/Microsoft/sarif-sdk/issues/973 // When we perform a files table look-up, only the uri and uriBaseId // are relevant; these properties together comprise the unique identity // of the file object. The file index, of course, does not relate to the // file identity. We consciously exclude the properties bag as well. // We will normalize the input fileLocation.Uri to make URIs more consistent // throughout the emitted log. fileLocation.Uri = new Uri(UriHelper.MakeValidUri(fileLocation.Uri.OriginalString), UriKind.RelativeOrAbsolute); var filesTableKey = new ArtifactLocation { Uri = fileLocation.Uri, UriBaseId = fileLocation.UriBaseId }; if (!_fileToIndexMap.TryGetValue(filesTableKey, out int fileIndex)) { if (addToFilesTableIfNotPresent) { this.Artifacts = this.Artifacts ?? new List <Artifact>(); fileIndex = this.Artifacts.Count; var fileData = Artifact.Create( filesTableKey.Uri, dataToInsert, hashData: hashData, encoding: null); // Copy ArtifactLocation to ensure changes to Result copy don't affect new Run.Artifacts copy fileData.Location = new ArtifactLocation(fileLocation); this.Artifacts.Add(fileData); _fileToIndexMap[filesTableKey] = fileIndex; } else { // We did not find the item. The call was not configured to add the entry. // Return the default value that indicates the item isn't present. fileIndex = -1; } } fileLocation.Index = fileIndex; return(fileIndex); }
public void Artifact_Create_NullUri() { Action action = () => { Artifact.Create(null, OptionallyEmittedData.None); }; action.Should().Throw <ArgumentNullException>(); }