10
10
11
11
namespace AppInspector . Tests . RuleProcessor ;
12
12
13
+ /// <summary>
14
+ /// Tests for properly detecting commented/live code status in the presence of comment markers inside of quoted strings
15
+ /// </summary>
13
16
[ TestClass ]
14
17
public class QuotedStringsTests
15
18
{
@@ -29,6 +32,12 @@ public class QuotedStringsTests
29
32
/*
30
33
contoso.com
31
34
*/ var url = ""https://contoso.com""" ;
35
+ private const string testRubyInterpolatedStrings = @"findMe = ""findMe""
36
+ puts ""Hello, #{findMe}!"" # findMe
37
+ def inspect # :nodoc:
38
+ ""#<#{findMe} #{findMe}>"" #findMe
39
+ end
40
+ " ; // Should find 5 instances, excluding the two true comments
32
41
33
42
private static string detectContosoRule = @"
34
43
[
@@ -54,7 +63,32 @@ public class QuotedStringsTests
54
63
}
55
64
]
56
65
" ;
57
-
66
+
67
+ private static string detectFindMeRule = @"
68
+ [
69
+ {
70
+ ""id"": ""RE000001"",
71
+ ""name"": ""Testing.Rules.Quotes"",
72
+ ""tags"": [
73
+ ""Testing.Rules.Quotes""
74
+ ],
75
+ ""severity"": ""Critical"",
76
+ ""description"": ""Find findMe"",
77
+ ""patterns"": [
78
+ {
79
+ ""pattern"": ""findMe"",
80
+ ""type"": ""regex"",
81
+ ""confidence"": ""High"",
82
+ ""scopes"": [
83
+ ""code""
84
+ ]
85
+ }
86
+ ],
87
+ ""_comment"": """"
88
+ }
89
+ ]
90
+ " ;
91
+
58
92
private readonly ILoggerFactory _loggerFactory =
59
93
new LogOptions { ConsoleVerbosityLevel = LogEventLevel . Verbose } . GetLoggerFactory ( ) ;
60
94
@@ -78,4 +112,24 @@ public void QuotedStrings(string content, int numIssues)
78
112
Assert . AreEqual ( numIssues ,
79
113
ruleProcessor . AnalyzeFile ( content , new FileEntry ( "testfile.cs" , new MemoryStream ( ) ) , info ) . Count ( ) ) ;
80
114
}
115
+
116
+ /// <summary>
117
+ /// Ruby interpolated strings provide an interesting test case because they use the comment character as part of interpolation
118
+ /// the comment marker is one character long, and it may often come right after the quotation mark
119
+ /// </summary>
120
+ /// <param name="content"></param>
121
+ /// <param name="numIssues"></param>
122
+
123
+ [ DataRow ( testRubyInterpolatedStrings , 5 ) ]
124
+ [ DataTestMethod ]
125
+ public void QuotedStringsRuby ( string content , int numIssues )
126
+ {
127
+ RuleSet rules = new ( _loggerFactory ) ;
128
+ rules . AddString ( detectFindMeRule , "findMeRule" ) ;
129
+ Microsoft . ApplicationInspector . RulesEngine . RuleProcessor ruleProcessor =
130
+ new Microsoft . ApplicationInspector . RulesEngine . RuleProcessor ( rules , new RuleProcessorOptions ( ) ) ;
131
+ _languages . FromFileNameOut ( "testfile.rb" , out LanguageInfo info ) ;
132
+ Assert . AreEqual ( numIssues ,
133
+ ruleProcessor . AnalyzeFile ( content , new FileEntry ( "testfile.rb" , new MemoryStream ( ) ) , info ) . Count ( ) ) ;
134
+ }
81
135
}
0 commit comments