/IntegrationTests/Parser/TypeParsing/TypeParserTests/StringBuilderInference/StringBuilder_TypeParserTest.cs

http://injectioncop.codeplex.com · C# · 245 lines · 187 code · 45 blank · 13 comment · 0 complexity · f5055edfd152346564476de97900d697 MD5 · raw file

  1. // Copyright 2013 rubicon informationstechnologie gmbh
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System;
  15. using Microsoft.FxCop.Sdk;
  16. using NUnit.Framework;
  17. using Assert = NUnit.Framework.Assert;
  18. namespace InjectionCop.IntegrationTests.Parser.TypeParsing.TypeParserTests.StringBuilderInference
  19. {
  20. [TestFixture]
  21. public class StringBuilder_TypeParserTest: TypeParserTestBase
  22. {
  23. [Test]
  24. public void Parse_SafeStringBuilderInference_NoProblem()
  25. {
  26. Method sample = TestHelper.GetSample<StringBuilderSample>("SafeStringBuilderInference");
  27. _typeParser.Parse (sample);
  28. ProblemCollection result = _typeParser.Problems;
  29. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.False);
  30. }
  31. [Test]
  32. public void Parse_UnsafeStringBuilderInference_ReturnsProblem()
  33. {
  34. Method sample = TestHelper.GetSample<StringBuilderSample>("UnsafeStringBuilderInference");
  35. _typeParser.Parse (sample);
  36. ProblemCollection result = _typeParser.Problems;
  37. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.True);
  38. }
  39. [Test]
  40. public void Parse_SafeStringBuilderReset_NoProblem()
  41. {
  42. Method sample = TestHelper.GetSample<StringBuilderSample>("SafeStringBuilderReset");
  43. _typeParser.Parse (sample);
  44. ProblemCollection result = _typeParser.Problems;
  45. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.False);
  46. }
  47. [Test]
  48. public void Parse_UnsafeStringBuilderReset_ReturnsProblem()
  49. {
  50. Method sample = TestHelper.GetSample<StringBuilderSample>("UnsafeStringBuilderReset");
  51. _typeParser.Parse (sample);
  52. ProblemCollection result = _typeParser.Problems;
  53. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.True);
  54. }
  55. [Test]
  56. public void Parse_SafeStringBuilderAcrossBlocks_NoProblem()
  57. {
  58. Method sample = TestHelper.GetSample<StringBuilderSample>("SafeStringBuilderAcrossBlocks");
  59. _typeParser.Parse (sample);
  60. ProblemCollection result = _typeParser.Problems;
  61. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.False);
  62. }
  63. [Test]
  64. public void Parse_UnsafeStringBuilderAcrossBlocks_ReturnsProblem()
  65. {
  66. Method sample = TestHelper.GetSample<StringBuilderSample>("UnsafeStringBuilderAcrossBlocks");
  67. _typeParser.Parse (sample);
  68. ProblemCollection result = _typeParser.Problems;
  69. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.True);
  70. }
  71. [Test]
  72. public void Parse_AppendOfSafeType_NoProblem()
  73. {
  74. Method sample = TestHelper.GetSample<StringBuilderSample>("BooleanAppend");
  75. _typeParser.Parse (sample);
  76. ProblemCollection result = _typeParser.Problems;
  77. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.False);
  78. }
  79. [Test]
  80. public void Parse_AppendOfUnsafeType_ReturnsProblem()
  81. {
  82. Method sample = TestHelper.GetSample<StringBuilderSample>("DoubleAppend");
  83. _typeParser.Parse (sample);
  84. ProblemCollection result = _typeParser.Problems;
  85. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.True);
  86. }
  87. [Test]
  88. public void Parse_SafeStringAppend_NoProblem()
  89. {
  90. Method sample = TestHelper.GetSample<StringBuilderSample>("SafeStringAppend");
  91. _typeParser.Parse (sample);
  92. ProblemCollection result = _typeParser.Problems;
  93. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.False);
  94. }
  95. [Test]
  96. public void Parse_UnsafeStringAppend_ReturnsProblem()
  97. {
  98. Method sample = TestHelper.GetSample<StringBuilderSample>("UnsafeStringAppend");
  99. _typeParser.Parse (sample);
  100. ProblemCollection result = _typeParser.Problems;
  101. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.True);
  102. }
  103. [Test]
  104. public void Parse_AppendingLiteralDoesNotChangeSafeFragmentType_NoProblem()
  105. {
  106. Method sample = TestHelper.GetSample<StringBuilderSample>("AppendingLiteralDoesNotChangeSafeFragmentType");
  107. _typeParser.Parse (sample);
  108. ProblemCollection result = _typeParser.Problems;
  109. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.False);
  110. }
  111. [Test]
  112. public void Parse_AppendingLiteralDoesNotChangeUnsafeFragmentType_ReturnsProblem()
  113. {
  114. Method sample = TestHelper.GetSample<StringBuilderSample>("AppendingLiteralDoesNotChangeUnsafeFragmentType");
  115. _typeParser.Parse (sample);
  116. ProblemCollection result = _typeParser.Problems;
  117. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.True);
  118. }
  119. [Test]
  120. public void Parse_SafeStringAppendAcrossBlocks_NoProblem()
  121. {
  122. Method sample = TestHelper.GetSample<StringBuilderSample>("SafeStringAppendAcrossBlocks");
  123. _typeParser.Parse (sample);
  124. ProblemCollection result = _typeParser.Problems;
  125. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.False);
  126. }
  127. [Test]
  128. public void Parse_UnsafeStringAppendAcrossBlocks_ReturnsProblem()
  129. {
  130. Method sample = TestHelper.GetSample<StringBuilderSample>("UnsafeStringAppendAcrossBlocks");
  131. _typeParser.Parse (sample);
  132. ProblemCollection result = _typeParser.Problems;
  133. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.True);
  134. }
  135. [Test]
  136. public void Parse_SafeCallsInDifferentBlocks_NoProblem()
  137. {
  138. Method sample = TestHelper.GetSample<StringBuilderSample>("SafeCallsInDifferentBlocks");
  139. _typeParser.Parse (sample);
  140. ProblemCollection result = _typeParser.Problems;
  141. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.False);
  142. }
  143. [Test]
  144. public void Parse_StringBuilderBlockConflict_ReturnsProblem()
  145. {
  146. Method sample = TestHelper.GetSample<StringBuilderSample>("StringBuilderBlockConflict");
  147. _typeParser.Parse (sample);
  148. ProblemCollection result = _typeParser.Problems;
  149. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.True);
  150. }
  151. [Test]
  152. public void Parse_SafeFragmentTypeDefinitionAcrossBlocks_NoProblem()
  153. {
  154. Method sample = TestHelper.GetSample<StringBuilderSample>("SafeFragmentTypeDefinitionAcrossBlocks");
  155. _typeParser.Parse (sample);
  156. ProblemCollection result = _typeParser.Problems;
  157. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.False);
  158. }
  159. [Test]
  160. public void Parse_UnsafeFragmentTypeDefinitionAcrossBlocks_ReturnsProblem ()
  161. {
  162. Method sample = TestHelper.GetSample<StringBuilderSample> ("UnsafeFragmentTypeDefinitionAcrossBlocks");
  163. _typeParser.Parse (sample);
  164. ProblemCollection result = _typeParser.Problems;
  165. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.True);
  166. }
  167. [Test]
  168. public void Parse_UndefinedInitializationSetSafe_NoProblem ()
  169. {
  170. Method sample = TestHelper.GetSample<StringBuilderSample> ("UndefinedInitializationSetSafe");
  171. _typeParser.Parse (sample);
  172. ProblemCollection result = _typeParser.Problems;
  173. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.False);
  174. }
  175. [Test]
  176. public void Parse_UndefinedInitializationSetUnsafe_ReturnsProblem ()
  177. {
  178. Method sample = TestHelper.GetSample<StringBuilderSample> ("UndefinedInitializationSetUnsafe");
  179. _typeParser.Parse (sample);
  180. ProblemCollection result = _typeParser.Problems;
  181. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.True);
  182. }
  183. [Test]
  184. public void Parse_UnsafeAppendChain_ReturnsProblem ()
  185. {
  186. Method sample = TestHelper.GetSample<StringBuilderSample> ("UnsafeAppendChain");
  187. _typeParser.Parse (sample);
  188. ProblemCollection result = _typeParser.Problems;
  189. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.True);
  190. }
  191. [Test]
  192. public void Parse_SafeStringBuilderToString_NoProblem ()
  193. {
  194. Method sample = TestHelper.GetSample<StringBuilderSample> ("SafeStringBuilderToString");
  195. _typeParser.Parse (sample);
  196. ProblemCollection result = _typeParser.Problems;
  197. Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.False);
  198. }
  199. }
  200. }