Author: nestor
Date: 2007-09-01 14:21:33 -0400 (Sat, 01 Sep 2007)
New Revision: 85175
Added:
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/DontSwallowErrorsCatchingNonspecificExceptionsRule.cs
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/Test/DontSwallowErrorsCatchingNonspecificExceptionsTest.cs
Modified:
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/ChangeLog
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/Makefile.am
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/Test/ChangeLog
Log:
Merging some work of summer of code 2007. Added a new rule for check error
swallowing.
* Makefile.am: Added the sources of new rule and unit tests.
* DontSwallowErrorsCatchingNonspecificExceptionsRule.cs: New. Rule for
check error swallowing.
* Test/DontSwallowErrorsCatchingNonspecificExceptionsTest.cs: New.
Unit tests for DontSwallowErrorsCatchingNonSpecificExceptionsRule.
Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/ChangeLog
2007-09-01 18:19:00 UTC (rev 85174)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/ChangeLog
2007-09-01 18:21:33 UTC (rev 85175)
@@ -1,3 +1,9 @@
+2007-09-01 Nestor Salceda <[EMAIL PROTECTED]>
+
+ * Makefile.am: Added the sources of new rule and unit tests.
+ * DontSwallowErrorsCatchingNonspecificExceptionsRule.cs: New. Rule for
+ check error swallowing.
+
2006-10-15 Sebastien Pouliot <[EMAIL PROTECTED]>
* Gendarme.Rules.Exceptions.xml.in: Update Uri for rules (to generate
Added:
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/DontSwallowErrorsCatchingNonspecificExceptionsRule.cs
===================================================================
---
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/DontSwallowErrorsCatchingNonspecificExceptionsRule.cs
2007-09-01 18:19:00 UTC (rev 85174)
+++
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/DontSwallowErrorsCatchingNonspecificExceptionsRule.cs
2007-09-01 18:21:33 UTC (rev 85175)
@@ -0,0 +1,89 @@
+//
+// DontSwallowErrorsCatchingNonspecificExceptions class
+//
+// Authors:
+// Néstor Salceda <[EMAIL PROTECTED]>
+//
+// (C) 2007 Néstor Salceda
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.Exceptions {
+ public class DontSwallowErrorsCatchingNonspecificExceptions :
IMethodRule {
+
+ //Added System.Object because is the code behind the following
block:
+ //try {
+ // File.Open (foo, bar);
+ //}
+ //catch {
+ //}
+ private string[] forbiddenTypeInCatches = {"System.Exception",
"System.SystemException", "System.Object"};
+
+ private bool IsForbiddenTypeInCatches (string typeName)
+ {
+ foreach (String forbiddenTypeName in
forbiddenTypeInCatches) {
+ if (typeName.Equals (forbiddenTypeName)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private bool ThrowsGeneralException (ExceptionHandler
exceptionHandler)
+ {
+ for (Instruction currentInstruction =
exceptionHandler.HandlerStart; currentInstruction !=
exceptionHandler.HandlerEnd; currentInstruction = currentInstruction.Next) {
+ if (currentInstruction.OpCode.Code ==
Code.Rethrow)
+ return true;
+ }
+ return false;
+ }
+
+ public MessageCollection CheckMethod (MethodDefinition
methodDefinition, Runner runner)
+ {
+ MessageCollection messageCollection = new
MessageCollection ();
+ if (methodDefinition.HasBody) {
+ ExceptionHandlerCollection
exceptionHandlerCollection = methodDefinition.Body.ExceptionHandlers;
+ foreach (ExceptionHandler exceptionHandler in
exceptionHandlerCollection) {
+ if (exceptionHandler.Type ==
ExceptionHandlerType.Catch) {
+ string catchTypeName =
exceptionHandler.CatchType.FullName;
+ if (IsForbiddenTypeInCatches
(catchTypeName)) {
+ if
(!ThrowsGeneralException (exceptionHandler)) {
+ Location
location = new Location (methodDefinition.Name, methodDefinition.Name,
exceptionHandler.HandlerStart.Offset);
+ Message message
= new Message ("Do not swallow errors catching nonspecific exceptions.",
location, MessageType.Error);
+
messageCollection.Add (message);
+ }
+ }
+ }
+ }
+ }
+
+ if (messageCollection.Count == 0)
+ return null;
+ return messageCollection;
+ }
+ }
+}
Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/Makefile.am
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/Makefile.am
2007-09-01 18:19:00 UTC (rev 85174)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/Makefile.am
2007-09-01 18:21:33 UTC (rev 85175)
@@ -13,7 +13,8 @@
ISEHGuardedBlock.cs ISEHHandlerBlock.cs \
SEHHandlerType.cs Impl/ExceptionBlockParser.cs Impl/ExecutionBlock.cs \
Impl/ExecutionPath.cs Impl/ExecutionPathFactory.cs
Impl/SEHCatchBlock.cs \
- Impl/SEHGuardedBlock.cs Impl/SEHHandlerBlock.cs
Impl/SEHHandlerBlockCollection.cs
+ Impl/SEHGuardedBlock.cs Impl/SEHHandlerBlock.cs
Impl/SEHHandlerBlockCollection.cs \
+ DontSwallowErrorsCatchingNonspecificExceptionsRule.cs
exceptions_rules_build_sources = $(addprefix $(srcdir)/,
$(exceptions_rules_sources))
exceptions_rules_build_sources += $(exceptions_rules_generated_sources)
@@ -35,7 +36,7 @@
rm -f $(DESTDIR)$(pkglibdir)/`basename $$ASM`; \
done;
-exceptions_test_sources = TestPatterns.cs
+exceptions_test_sources = TestPatterns.cs
DontSwallowErrorsCatchingNonspecificExceptionsTest.cs
exceptions_test_build_sources = $(addprefix $(srcdir)/Test/,
$(exceptions_test_sources))
Test.Rules.Exceptions.dll: $(exceptions_test_build_sources)
Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/Test/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/Test/ChangeLog
2007-09-01 18:19:00 UTC (rev 85174)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/Test/ChangeLog
2007-09-01 18:21:33 UTC (rev 85175)
@@ -1,3 +1,8 @@
+2007-09-01 Nestor Salceda <[EMAIL PROTECTED]>
+
+ * DontSwallowErrorsCatchingNonspecificExceptionsTest.cs: New. Unit
+ tests for DontSwallowErrorsCatchingNonSpecificExceptionsRule.
+
2006-09-28 Sebastien Pouliot <[EMAIL PROTECTED]>
* Makefile.am: Adjust EXTRA_DIST to include project files in tarballs.
Added:
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/Test/DontSwallowErrorsCatchingNonspecificExceptionsTest.cs
===================================================================
---
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/Test/DontSwallowErrorsCatchingNonspecificExceptionsTest.cs
2007-09-01 18:19:00 UTC (rev 85174)
+++
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/Test/DontSwallowErrorsCatchingNonspecificExceptionsTest.cs
2007-09-01 18:21:33 UTC (rev 85175)
@@ -0,0 +1,377 @@
+//
+// Unit Test for DontSwallowErrorsCatchingNonspecificExceptions Rule
+//
+// Authors:
+// Néstor Salceda <[EMAIL PROTECTED]>
+//
+// (C) 2007 Néstor Salceda
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections;
+using System.Reflection;
+using System.IO;
+
+using Gendarme.Framework;
+using Gendarme.Rules.Exceptions;
+using Mono.Cecil;
+using NUnit.Framework;
+
+namespace Test.Rules.Exceptions {
+
+
+ [TestFixture]
+ public class DontSwallowErrorsCatchingNonspecificExceptionsTest {
+
+ private IMethodRule rule;
+ private AssemblyDefinition assembly;
+ private MethodDefinition method;
+ private TypeDefinition type;
+ private MessageCollection messageCollection;
+
+ [TestFixtureSetUp]
+ public void FixtureSetUp ()
+ {
+ string unit = Assembly.GetExecutingAssembly ().Location;
+ assembly = AssemblyFactory.GetAssembly (unit);
+ rule = new
DontSwallowErrorsCatchingNonspecificExceptions ();
+ type = assembly.MainModule.Types
["Test.Rules.Exceptions.DontSwallowErrorsCatchingNonspecificExceptionsTest"];
+ messageCollection = null;
+ }
+
+ private void CheckMessageType (MessageCollection
messageCollection, MessageType messageType)
+ {
+ IEnumerator enumerator =
messageCollection.GetEnumerator ();
+ if (enumerator.MoveNext ()) {
+ Message message = (Message) enumerator.Current;
+ Assert.AreEqual (message.Type, messageType);
+ }
+ }
+
+ [Test]
+ public void SwallowErrorsCatchingExceptionsEmptyCatchBlockTest
()
+ {
+ method = type.Methods.GetMethod
("SwallowErrorsCatchingExceptionEmptyCatchBlock", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (messageCollection.Count, 1);
+ CheckMessageType (messageCollection, MessageType.Error);
+ }
+
+
+ [Test]
+ public void
SwallowErrorsCatchingExceptionsNoEmptyCatchBlockTest ()
+ {
+ method = type.Methods.GetMethod
("SwallowErrorsCatchingExceptionNoEmptyCatchBlock", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (messageCollection.Count, 1);
+ CheckMessageType (messageCollection, MessageType.Error);
+ }
+
+ [Test]
+ public void
SwallowErrorsCatchingSystemExceptionEmptyCatchBlockTest ()
+ {
+ method = type.Methods.GetMethod
("SwallowErrorsCatchingSystemExceptionEmptyCatchBlock", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (messageCollection.Count, 1);
+ CheckMessageType (messageCollection, MessageType.Error);
+ }
+
+
+ [Test]
+ public void
SwallowErrorsCatchingSystemExceptionNoEmptyCatchBlockTest ()
+ {
+ method = type.Methods.GetMethod
("SwallowErrorsCatchingSystemExceptionNoEmptyCatchBlock", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (messageCollection.Count, 1);
+ CheckMessageType (messageCollection, MessageType.Error);
+ }
+
+ [Test]
+ public void
SwallowErrorsCatchingTypeExceptionEmptyCatchBlockTest ()
+ {
+ method = type.Methods.GetMethod
("SwallowErrorsCatchingTypeExceptionEmptyCatchBlock", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (messageCollection.Count, 1);
+ CheckMessageType (messageCollection, MessageType.Error);
+ }
+
+ [Test]
+ public void
SwallowErrorsCatchingTypeExceptionNoEmptyCatchBlockTest ()
+ {
+ method = type.Methods.GetMethod
("SwallowErrorsCatchingTypeExceptionNoEmptyCatchBlock", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (messageCollection.Count, 1);
+ CheckMessageType (messageCollection, MessageType.Error);
+ }
+
+ [Test]
+ public void SwallowErrorsCatchingAllEmptyCatchBlockTest ()
+ {
+ method = type.Methods.GetMethod
("SwallowErrorsCatchingAllEmptyCatchBlock", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (messageCollection.Count, 1);
+ CheckMessageType (messageCollection, MessageType.Error);
+ }
+
+ [Test]
+ public void SwallowErrorsCatchingAllNoEmptyCatchBlockTest ()
+ {
+ method = type.Methods.GetMethod
("SwallowErrorsCatchingAllNoEmptyCatchBlock", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (messageCollection.Count, 1);
+ CheckMessageType (messageCollection, MessageType.Error);
+ }
+
+ [Test]
+ public void NotSwallowRethrowingExceptionTest ()
+ {
+ method = type.Methods.GetMethod
("NotSwallowRethrowingException", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (messageCollection.Count, 1);
+ CheckMessageType (messageCollection, MessageType.Error);
+ }
+
+
+ [Test]
+ public void NotSwallowRethrowingGeneralExceptionTest ()
+ {
+ method = type.Methods.GetMethod
("NotSwallowRethrowingGeneralException", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNull (messageCollection);
+ }
+
+
+ [Test]
+ public void NotSwallowCatchingSpecificExceptionTest ()
+ {
+ method = type.Methods.GetMethod
("NotSwallowCatchingSpecificException", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNull (messageCollection);
+ }
+
+ [Test]
+ public void NotSwallowThrowingANewExceptionTest ()
+ {
+ method = type.Methods.GetMethod
("NotSwallowThrowingANewException", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (messageCollection.Count, 1);
+ CheckMessageType (messageCollection, MessageType.Error);
+ }
+
+ [Test]
+ public void NotSwallowCatchingAllThrowingANewExceptionTest ()
+ {
+ method = type.Methods.GetMethod
("NotSwallowCatchingAllThrowingANewException", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (messageCollection.Count, 1);
+ CheckMessageType (messageCollection, MessageType.Error);
+ }
+
+ [Test]
+ public void
NotSwallowCatchingTypeExceptionThrowingANewExceptionTest ()
+ {
+ method = type.Methods.GetMethod
("NotSwallowCatchingTypeExceptionThrowingANewException", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (messageCollection.Count, 1);
+ CheckMessageType (messageCollection, MessageType.Error);
+ }
+
+ [Test]
+ public void
NotSwallowCatchingSystemExceptionThrowingANewExceptionTest ()
+ {
+ method = type.Methods.GetMethod
("NotSwallowCatchingSystemExceptionThrowingANewException", Type.EmptyTypes);
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (messageCollection.Count, 1);
+ CheckMessageType (messageCollection, MessageType.Error);
+ }
+
+ //Methods for make the tests
+ public void SwallowErrorsCatchingExceptionEmptyCatchBlock ()
+ {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch (Exception exception) {
+ }
+ }
+
+ public void SwallowErrorsCatchingExceptionNoEmptyCatchBlock ()
+ {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch (Exception exception) {
+ Console.WriteLine (exception.Message);
+ Console.WriteLine (exception);
+ }
+ }
+
+
+ public void SwallowErrorsCatchingSystemExceptionEmptyCatchBlock
()
+ {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch (SystemException exception) {
+ }
+ }
+
+
+
+ public void
SwallowErrorsCatchingSystemExceptionNoEmptyCatchBlock ()
+ {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch (SystemException exception) {
+ Console.WriteLine (exception.Message);
+ Console.WriteLine (exception);
+ }
+ }
+
+ public void SwallowErrorsCatchingTypeExceptionEmptyCatchBlock
()
+ {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch (Exception) {
+ }
+ }
+
+ public void SwallowErrorsCatchingTypeExceptionNoEmptyCatchBlock
()
+ {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch (Exception) {
+ Console.WriteLine ("Has happened an
exception.");
+ }
+ }
+
+ public void SwallowErrorsCatchingAllEmptyCatchBlock ()
+ {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch {
+ }
+ }
+
+ public void SwallowErrorsCatchingAllNoEmptyCatchBlock ()
+ {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch {
+ Console.WriteLine ("Has happened an
exception.");
+ }
+ }
+
+ public void NotSwallowRethrowingGeneralException () {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch (Exception exception) {
+ Console.WriteLine (exception.Message);
+ Console.WriteLine (exception);
+ throw;
+ Console.WriteLine (exception.Message);
+ Console.WriteLine (exception);
+ }
+ }
+
+ public void NotSwallowRethrowingException () {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch (Exception exception) {
+ Console.WriteLine (exception.Message);
+ Console.WriteLine (exception);
+ throw exception;
+ Console.WriteLine (exception.Message);
+ Console.WriteLine (exception);
+ }
+ }
+
+ public void NotSwallowCatchingSpecificException ()
+ {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch (FileNotFoundException exception) {
+ }
+ }
+
+ public void NotSwallowThrowingANewException ()
+ {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch (Exception exception) {
+ throw new SystemException ("Message");
+ }
+ }
+
+ public void NotSwallowCatchingAllThrowingANewException ()
+ {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch {
+ throw new Exception ("Message");
+ }
+ }
+
+ public void
NotSwallowCatchingTypeExceptionThrowingANewException ()
+ {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch (Exception) {
+ throw new Exception ("Message");
+ }
+ }
+
+ public void
NotSwallowCatchingSystemExceptionThrowingANewException ()
+ {
+ try {
+ File.Open ("foo.txt", FileMode.Open);
+ }
+ catch (System.Exception exception) {
+ throw new Exception ("Message");
+ }
+ }
+ }
+}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches