Example #1
0
        public void UseCase_Normal_Test()
        {
            RuleProcessor processor = new RuleProcessor(@"rules\valid");

            processor.AddRules(@"rules\custom", "my rules");

            string testString = "strcpy(dest,src);";

            // strcpy test
            Match match = processor.IsMatch(testString, 0, "cpp");

            Assert.IsTrue(match.Success, "strcpy should be flagged");
            Assert.AreEqual(0, match.Location, "strcpy invalid index");
            Assert.AreEqual(16, match.Length, "strcpy invalid length ");
            Assert.AreEqual("DS185832", match.Rule.Id, "strcpy invalid rule");

            // Fix it test
            Assert.AreNotEqual(match.Rule.Fixes.Length, 0, "strcpy invalid Fixes");
            CodeFix fix       = match.Rule.Fixes[0];
            string  fixedCode = RuleProcessor.Fix(testString, fix);

            Assert.AreEqual("strcpy_s(dest, <size of dest>, src);", fixedCode, "strcpy invalid code fix");
            Assert.IsTrue(fix.Name.Contains("Change to strcpy_s"), "strcpy wrong fix name");

            // TODO test
            testString = "//TODO: fix this later";
            match      = processor.IsMatch(testString, 0, "csharp");
            Assert.IsTrue(match.Success, "todo should be flagged");
            Assert.AreEqual(2, match.Location, "todo invalid index");
            Assert.AreEqual(4, match.Length, "todo invalid length ");
            Assert.AreEqual("DS176209", match.Rule.Id, "todo invalid rule");
            Assert.AreEqual(0, match.Rule.Fixes.Length, "todo invalid Fixes");
            Assert.AreEqual("my rules", match.Rule.Tag, "todo invalid tag");
        }
Example #2
0
        public void UseCase_Normal_Test()
        {
            RuleProcessor processor = new RuleProcessor(LoadRules(true));

            string lang       = Language.FromFileName("testfilename.cpp");
            string testString = "strcpy(dest,src);";

            // strcpy test
            Issue[] issues = processor.Analyze(testString, lang);
            Assert.AreEqual(1, issues.Length, "strcpy should be flagged");
            Assert.AreEqual(0, issues[0].Boundary.Index, "strcpy invalid index");
            Assert.AreEqual(16, issues[0].Boundary.Length, "strcpy invalid length ");
            Assert.AreEqual(1, issues[0].StartLocation.Line, "strcpy invalid start location line ");
            Assert.AreEqual(1, issues[0].StartLocation.Column, "strcpy invalid start location column ");
            Assert.AreEqual(1, issues[0].EndLocation.Line, "strcpy invalid end location line ");
            Assert.AreEqual(16, issues[0].EndLocation.Column, "strcpy invalid end location column ");
            Assert.AreEqual("DS185832", issues[0].Rule.Id, "strcpy invalid rule");

            // Fix it test
            Assert.AreNotEqual(issues[0].Rule.Fixes.Length, 0, "strcpy invalid Fixes");
            CodeFix fix       = issues[0].Rule.Fixes[0];
            string  fixedCode = RuleProcessor.Fix(testString, fix);

            Assert.AreEqual("strcpy_s(dest, <size of dest>, src);", fixedCode, "strcpy invalid code fix");
            Assert.IsTrue(fix.Name.Contains("Change to strcpy_s"), "strcpy wrong fix name");
        }
        public FixSuggestedAction(ITrackingSpan span, CodeFix fix)
        {
            _fix      = fix;
            _span     = span;
            _snapshot = span.TextBuffer.CurrentSnapshot;
            string code = span.GetText(_snapshot);

            _fixedCode = RuleProcessor.Fix(code, _fix);
            _display   = (string.IsNullOrEmpty(_fix.Name)) ? _fixedCode : _fix.Name;
        }
Example #4
0
        public void UseCase_Normal_Test()
        {
            Ruleset rules = Ruleset.FromDirectory(@"rules\valid", null);

            rules.AddDirectory(@"rules\custom", "my rules");

            RuleProcessor processor = new RuleProcessor(rules);

            string lang       = Language.FromFileName("testfilename.cpp");
            string testString = "strcpy(dest,src);";

            // strcpy test
            Issue[] issues = processor.Analyze(testString, lang);
            Assert.AreEqual(1, issues.Length, "strcpy should be flagged");
            Assert.AreEqual(0, issues[0].Index, "strcpy invalid index");
            Assert.AreEqual(16, issues[0].Length, "strcpy invalid length ");
            Assert.AreEqual("DS185832", issues[0].Rule.Id, "strcpy invalid rule");

            // Fix it test
            Assert.AreNotEqual(issues[0].Rule.Fixes.Length, 0, "strcpy invalid Fixes");
            CodeFix fix       = issues[0].Rule.Fixes[0];
            string  fixedCode = RuleProcessor.Fix(testString, fix);

            Assert.AreEqual("strcpy_s(dest, <size of dest>, src);", fixedCode, "strcpy invalid code fix");
            Assert.IsTrue(fix.Name.Contains("Change to strcpy_s"), "strcpy wrong fix name");

            // QUICKFIX test
            processor.SeverityLevel |= Severity.ManualReview;
            testString = "//QUICKFIX: fix this later";
            issues     = processor.Analyze(testString, "csharp");
            Assert.AreEqual(1, issues.Length, "QUICKFIX should be flagged");
            Assert.AreEqual(2, issues[0].Index, "QUICKFIX invalid index");
            Assert.AreEqual(8, issues[0].Length, "QUICKFIX invalid length ");
            Assert.AreEqual("DS276209", issues[0].Rule.Id, "QUICKFIX invalid rule");
            Assert.AreEqual(0, issues[0].Rule.Fixes.Length, "QUICKFIX invalid Fixes");
            Assert.AreEqual("my rules", issues[0].Rule.RuntimeTag, "QUICKFIX invalid tag");

            // Same issue twice test
            testString = "MD5 hash = MD5.Create();";
            issues     = processor.Analyze(testString, "csharp");
            Assert.AreEqual(2, issues.Length, "Same issue should be twice on line");
            Assert.AreEqual(issues[0].Rule, issues[1].Rule, "Same issues should have sames rule IDs");

            // Overlaping issues
            testString = "            MD5 hash = new MD5CryptoServiceProvider();";
            issues     = processor.Analyze(testString, "csharp");
            Assert.AreEqual(2, issues.Length, "Overlaping issue count doesn't add up");

            // Override test
            testString = "strncat(dest, \"this is also bad\", strlen(dest))";
            issues     = processor.Analyze(testString, new string[] { "c", "cpp" });
            Assert.AreEqual(2, issues.Length, "Override test failed");
        }
Example #5
0
        private List <Fix> GetFixits(IssueRecord issue)
        {
            List <Fix> fixes = new List <Fix>();

            if (issue.Issue.Rule.Fixes != null)
            {
                foreach (CodeFix fix in issue.Issue.Rule.Fixes)
                {
                    List <Replacement> replacements = new List <Replacement>();
                    replacements.Add(new Replacement(new Region()
                    {
                        CharOffset = issue.Issue.Boundary.Index,
                        CharLength = issue.Issue.Boundary.Length,
                    }, new ArtifactContent()
                    {
                        Text = RuleProcessor.Fix(issue.TextSample, fix)
                    }, null));

                    var path    = Path.GetFullPath(issue.Filename);
                    var changes = new ArtifactChange[]
                    {
                        new ArtifactChange(
                            new ArtifactLocation()
                        {
                            Uri = new Uri(path)
                        },
                            replacements,
                            null)
                    };

                    fixes.Add(new Fix()
                    {
                        ArtifactChanges = changes,
                        Description     = new Message()
                        {
                            Text = issue.Issue.Rule.Description
                        }
                    });
                }
            }
            return(fixes);
        }
Example #6
0
        private List <Fix> GetFixits(IssueRecord issue)
        {
            List <Fix> fixes = new List <Fix>();

            if (issue.Issue.Rule.Fixes != null)
            {
                foreach (CodeFix fix in issue.Issue.Rule.Fixes)
                {
                    List <Replacement> replacements = new List <Replacement>();
                    replacements.Add(new Replacement(issue.Issue.Boundary.Index,
                                                     issue.Issue.Boundary.Length,
                                                     RuleProcessor.Fix(issue.TextSample, fix)
                                                     ));

                    List <FileChange> changes = new List <FileChange>();
                    changes.Add(new FileChange(new Uri(issue.Filename), null, replacements));

                    fixes.Add(new Fix(fix.Name, changes));
                }
            }
            return(fixes);
        }