Пример #1
0
        public void SarifFixer_ShouldNotChange_SpansMultipleLines()
        {
            // Arrange
            string testSarif = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""shortMessage"": ""Test shortMessage. 
It features ""quoted text""."",
      ""properties"": {
        ""severity"": ""Info"",
        ""helpLink"": ""https://github.com/SonarSource/sonar-msbuild-runner"",
      }
    }
  ]
}";

            // Act
            string fixedSarif;
            bool   returnStringIsValid = RoslynV1SarifFixer.FixRoslynV1Sarif(testSarif, out fixedSarif);

            // Assert
            Assert.IsFalse(returnStringIsValid);
            Assert.AreEqual(testSarif, fixedSarif);
        }
        public void SarifFixer_ShouldChange_EscapeQuotes()
        {
            // Arrange
            var logger  = new TestLogger();
            var testDir = TestUtils.CreateTestSpecificFolder(TestContext);

            var testSarifString = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""shortMessage"": ""Test shortMessage. It features ""quoted text""."",
      ""properties"": {
        ""severity"": ""Info"",
        ""helpLink"": ""https://github.com/SonarSource/sonar-msbuild-runner"",
      }
    }
  ]
}";
            var testSarifPath   = Path.Combine(testDir, "testSarif.json");

            File.WriteAllText(testSarifPath, testSarifString);
            var originalWriteTime = new FileInfo(testSarifPath).LastWriteTime;

            // Act
            var returnedSarifPath = new RoslynV1SarifFixer(logger).LoadAndFixFile(testSarifPath, RoslynV1SarifFixer.CSharpLanguage);

            // Assert
            // fixable -> no change to file, file path in return value, file contents as expected
            AssertFileUnchanged(testSarifPath, originalWriteTime);
            returnedSarifPath.Should().NotBeNull();

            var returnedSarifString = File.ReadAllText(returnedSarifPath);

            returnedSarifString.Should().Be(@"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""shortMessage"": ""Test shortMessage. It features \""quoted text\""."",
      ""properties"": {
        ""severity"": ""Info"",
        ""helpLink"": ""https://github.com/SonarSource/sonar-msbuild-runner"",
      }
    }
  ]
}");
        }
Пример #3
0
        public void SarifFixer_ShouldChange_EscapeBackslashes()
        {
            // Arrange
            string testSarif = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\agent\_work\2\s\MyTestProj\Program.cs"",
}
          ]
        }
      ],
    }
  ]
}";

            // Act
            string fixedSarif;
            bool   returnStringIsValid = RoslynV1SarifFixer.FixRoslynV1Sarif(testSarif, out fixedSarif);

            // Assert
            Assert.IsTrue(returnStringIsValid);
            Assert.AreEqual(@"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\\agent\\_work\\2\\s\\MyTestProj\\Program.cs"",
}
          ]
        }
      ],
    }
  ]
}", fixedSarif);
        }
Пример #4
0
        public void SarifCompilerVersionCheck_FailureNoToolInfo()
        {
            // Arrange
            string testSarif = @"{ }";

            // Act
            bool valid = RoslynV1SarifFixer.IsSarifFromRoslynV1(testSarif);

            // Assert
            Assert.IsFalse(valid, "Expecting the compiler version check to return false for 'not Roslyn 1.0'");
        }
Пример #5
0
        public void SarifFixer_ShouldNotChange_Unfixable()
        {
            // Arrange
            TestLogger logger  = new TestLogger();
            string     testDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            string testSarifString = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    { 

}}}}}}}}}}}}}}}}}}}}}}}}}

      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\\agent\\_work\\2\\s\\MyTestProj\\Program.cs"",
}
          ]
        }
      ],
      ""shortMessage"": ""Test shortMessage. It features \""quoted text\""."",
      ""properties"": {
        ""severity"": ""Info"",
        ""helpLink"": ""https://github.com/SonarSource/sonar-msbuild-runner"",
      }
    }
  ]
}";
            string testSarifPath   = Path.Combine(testDir, "testSarif.json");

            File.WriteAllText(testSarifPath, testSarifString);
            DateTime originalWriteTime = new FileInfo(testSarifPath).LastWriteTime;

            // Act
            string returnedSarifPath = new RoslynV1SarifFixer().LoadAndFixFile(testSarifPath, RoslynV1SarifFixer.CSharpLanguage, logger);

            // Assert
            // unfixable -> no change to file, null return
            AssertFileUnchanged(testSarifPath, originalWriteTime);
            Assert.IsNull(returnedSarifPath);
        }
Пример #6
0
        public void SarifCompilerVersionCheck_FailureInvalidToolInfo()
        {
            // Arrange
            string testSarif = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""foo""
  }
        }";

            // Act
            bool valid = RoslynV1SarifFixer.IsSarifFromRoslynV1(testSarif);

            // Assert
            Assert.IsFalse(valid, "Expecting the compiler version check to return false for 'not Roslyn 1.0'");
        }
Пример #7
0
        public void SarifCompilerVersionCheck_HasImproperEscaping()
        {
            // Arrange
            string testSarif = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Foo\Bar"",
    ""productVersion"": ""1.1.0"",
    ""fileVersion"": ""1.0.0""
  }
        }";

            // Act
            bool valid = RoslynV1SarifFixer.IsSarifFromRoslynV1(testSarif);

            // Assert
            Assert.IsFalse(valid, "Expecting the compiler version check to return false for 'not Roslyn 1.0'");
        }
Пример #8
0
        public void SarifCompilerVersionCheck_IsNotFromRoslynV1()
        {
            // Arrange
            string testSarif = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.1.0"",
    ""fileVersion"": ""1.0.0""
  }
        }";

            // Act
            bool valid = RoslynV1SarifFixer.IsSarifFromRoslynV1(testSarif);

            // Assert
            Assert.IsFalse(valid, "Expecting the compiler version check to return false for 'not Roslyn 1.0'");
        }
Пример #9
0
        public void SarifFixer_ShouldNotChange_Unfixable()
        {
            // Arrange
            string testSarif = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    { 

}}}}}}}}}}}}}}}}}}}}}}}}}

      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\\agent\\_work\\2\\s\\MyTestProj\\Program.cs"",
}
          ]
        }
      ],
      ""shortMessage"": ""Test shortMessage. It features \""quoted text\""."",
      ""properties"": {
        ""severity"": ""Info"",
        ""helpLink"": ""https://github.com/SonarSource/sonar-msbuild-runner"",
      }
    }
  ]
}";

            // Act
            string fixedSarif;
            bool   returnStringIsValid = RoslynV1SarifFixer.FixRoslynV1Sarif(testSarif, out fixedSarif);

            // Assert
            Assert.IsFalse(returnStringIsValid);
            Assert.AreEqual(testSarif, fixedSarif);
        }
Пример #10
0
        public void SarifFixer_ShouldNotFixInvalid()
        {
            // Arrange
            TestLogger logger  = new TestLogger();
            string     testDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            string testSarifString = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\agent\_work\2\s\MyTestProj\Program.cs"",
}
          ]
        }
      ],
    }
  ]
}";
            string testSarifPath   = Path.Combine(testDir, "testSarif.json");

            File.WriteAllText(testSarifPath, testSarifString);
            DateTime originalWriteTime = new FileInfo(testSarifPath).LastWriteTime;

            // Act
            string returnedSarifPath = new RoslynV1SarifFixer().LoadAndFixFile(testSarifPath, RoslynV1SarifFixer.VBNetLanguage, logger);

            Assert.IsNull(returnedSarifPath);
        }
        public void SarifFixer_ShouldNotFixInvalid()
        {
            // Arrange
            var logger  = new TestLogger();
            var testDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext);

            var testSarifString = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\agent\_work\2\s\MyTestProj\Program.cs"",
}
          ]
        }
      ],
    }
  ]
}";
            var testSarifPath   = Path.Combine(testDir, "testSarif.json");

            File.WriteAllText(testSarifPath, testSarifString);

            // Act
            var returnedSarifPath = new RoslynV1SarifFixer(logger).LoadAndFixFile(testSarifPath, RoslynV1SarifFixer.VBNetLanguage);

            returnedSarifPath.Should().BeNull();
        }
Пример #12
0
        public void IsJsonValid_FalseHasUnescapedSlashes()
        {
            // Arrange
            string testSarif = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\agent\_work\2\s\MyTestProj\Program.cs"",
}
          ]
        }
      ],
      ""shortMessage"": ""Test shortMessage. It features \""quoted text\""."",
      ""properties"": {
        ""severity"": ""Info"",
        ""helpLink"": ""https://github.com/SonarSource/sonar-msbuild-runner"",
      }
    }
  ]
}";

            // Act
            bool isValid = RoslynV1SarifFixer.IsValidJson(testSarif);

            // Assert
            Assert.IsFalse(isValid);
        }
        public void SarifFixer_VBNet()
        {
            // Arrange
            var logger  = new TestLogger();
            var testDir = TestUtils.CreateTestSpecificFolder(TestContext);

            var testSarifString = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual Basic Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\agent\_work\2\s\MyTestProj\Program.cs"",
}
          ]
        }
      ],
    }
  ]
}";
            var testSarifPath   = Path.Combine(testDir, "testSarif.json");

            File.WriteAllText(testSarifPath, testSarifString);
            var originalWriteTime = new FileInfo(testSarifPath).LastWriteTime;

            // Act
            var returnedSarifPath = new RoslynV1SarifFixer(logger).LoadAndFixFile(testSarifPath, RoslynV1SarifFixer.VBNetLanguage);

            // Assert
            // fixable -> no change to file, file path in return value, file contents as expected
            AssertFileUnchanged(testSarifPath, originalWriteTime);
            returnedSarifPath.Should().NotBeNull();

            var returnedSarifString = File.ReadAllText(returnedSarifPath);

            returnedSarifString.Should().Be(@"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual Basic Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\\agent\\_work\\2\\s\\MyTestProj\\Program.cs"",
}
          ]
        }
      ],
    }
  ]
}");
        }
Пример #14
0
        public void SarifFixer_ShouldChange_EscapeBackslashes()
        {
            // Arrange
            TestLogger logger  = new TestLogger();
            string     testDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            string testSarifString = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\agent\_work\2\s\MyTestProj\Program.cs"",
}
          ]
        }
      ],
    }
  ]
}";
            string testSarifPath   = Path.Combine(testDir, "testSarif.json");

            File.WriteAllText(testSarifPath, testSarifString);
            DateTime originalWriteTime = new FileInfo(testSarifPath).LastWriteTime;

            // Act
            string returnedSarifPath = new RoslynV1SarifFixer().LoadAndFixFile(testSarifPath, RoslynV1SarifFixer.CSharpLanguage, logger);

            // Assert
            // fixable -> no change to file, file path in return value, file contents as expected
            AssertFileUnchanged(testSarifPath, originalWriteTime);
            Assert.IsNotNull(returnedSarifPath);

            string returnedSarifString = File.ReadAllText(returnedSarifPath);

            Assert.AreEqual(@"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\\agent\\_work\\2\\s\\MyTestProj\\Program.cs"",
}
          ]
        }
      ],
    }
  ]
}", returnedSarifString);
        }
        public void SarifFixer_ShouldChange_EscapeCharsInAllAffectedFields()
        {
            // Arrange
            var logger  = new TestLogger();
            var testDir = TestUtils.CreateTestSpecificFolder(TestContext);

            var testSarifString = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\agent\_work\2\s\MyTestProj\Program.cs"",
}
          ]
        }
      ],
      ""shortMessage"": ""Test shortMessage. It features ""quoted text"" and has \slashes."",
      ""fullMessage"": ""Test fullMessage. It features ""quoted text"" and has \slashes."",
      ""properties"": {
        ""severity"": ""Info"",
        ""title"": ""Test title. It features ""quoted text"" and has \slashes."",
        ""helpLink"": ""https://github.com/SonarSource/sonar-msbuild-runner"",
      }
    }
  ]
}";
            var testSarifPath   = Path.Combine(testDir, "testSarif.json");

            File.WriteAllText(testSarifPath, testSarifString);
            var originalWriteTime = new FileInfo(testSarifPath).LastWriteTime;

            // Act
            var returnedSarifPath = new RoslynV1SarifFixer().LoadAndFixFile(testSarifPath, RoslynV1SarifFixer.CSharpLanguage, logger);

            // Assert
            // fixable -> no change to file, file path in return value, file contents as expected
            AssertFileUnchanged(testSarifPath, originalWriteTime);
            Assert.IsNotNull(returnedSarifPath);

            var returnedSarifString = File.ReadAllText(returnedSarifPath);

            Assert.AreEqual(@"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\\agent\\_work\\2\\s\\MyTestProj\\Program.cs"",
}
          ]
        }
      ],
      ""shortMessage"": ""Test shortMessage. It features \""quoted text\"" and has \\slashes."",
      ""fullMessage"": ""Test fullMessage. It features \""quoted text\"" and has \\slashes."",
      ""properties"": {
        ""severity"": ""Info"",
        ""title"": ""Test title. It features \""quoted text\"" and has \\slashes."",
        ""helpLink"": ""https://github.com/SonarSource/sonar-msbuild-runner"",
      }
    }
  ]
}", returnedSarifString);
        }
Пример #16
0
        public void SarifFixer_ShouldChange_EscapeCharsInAllAffectedFields()
        {
            // Arrange
            string testSarif = @"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\agent\_work\2\s\MyTestProj\Program.cs"",
}
          ]
        }
      ],
      ""shortMessage"": ""Test shortMessage. It features ""quoted text"" and has \slashes."",
      ""fullMessage"": ""Test fullMessage. It features ""quoted text"" and has \slashes."",
      ""properties"": {
        ""severity"": ""Info"",
        ""title"": ""Test title. It features ""quoted text"" and has \slashes."",
        ""helpLink"": ""https://github.com/SonarSource/sonar-msbuild-runner"",
      }
    }
  ]
}";

            // Act
            string fixedSarif;
            bool   changeApplied = RoslynV1SarifFixer.FixRoslynV1Sarif(testSarif, out fixedSarif);

            // Assert
            Assert.IsTrue(changeApplied);
            Assert.AreEqual(@"{
  ""version"": ""0.1"",
  ""toolInfo"": {
                ""toolName"": ""Microsoft (R) Visual C# Compiler"",
    ""productVersion"": ""1.0.0"",
    ""fileVersion"": ""1.0.0""
  },
  ""issues"": [
    {
      ""ruleId"": ""DD001"",
      ""locations"": [
        {
          ""analysisTarget"": [
            {
              ""uri"": ""C:\\agent\\_work\\2\\s\\MyTestProj\\Program.cs"",
}
          ]
        }
      ],
      ""shortMessage"": ""Test shortMessage. It features \""quoted text\"" and has \\slashes."",
      ""fullMessage"": ""Test fullMessage. It features \""quoted text\"" and has \\slashes."",
      ""properties"": {
        ""severity"": ""Info"",
        ""title"": ""Test title. It features \""quoted text\"" and has \\slashes."",
        ""helpLink"": ""https://github.com/SonarSource/sonar-msbuild-runner"",
      }
    }
  ]
}", fixedSarif);
        }