Author: spouliot
Date: 2007-12-26 22:21:41 -0500 (Wed, 26 Dec 2007)
New Revision: 91912

Added:
   
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/FeatureRequiresRootPrivilegeOnUnixRule.cs
Modified:
   trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/ChangeLog
   
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Gendarme.Rules.Portability.mdp
   
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Gendarme.Rules.Portability.xml.in
   trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Makefile.am
   trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/NewLineLiteralRule.cs
Log:
2007-12-26  Sebastien Pouliot  <[EMAIL PROTECTED]>

        * FeatureRequiresRootPrivilegeOnUnixRule.cs: New. Rule that check
        for the use of features that requires to be root under Linux.
        * NewLineLiteralRule.cs: Update to use latest best-practice since
        it's a likely starting point for new rules.
        * Gendarme.Rules.Portability.xml.in: Add new rule definition.
        * Gendarme.Rules.Portability.mdp: Add new rule to the MonoDevelop 
        project file.
        * Makefile.am: Add new rule/tests to the build.



Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/ChangeLog     
2007-12-27 03:18:37 UTC (rev 91911)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/ChangeLog     
2007-12-27 03:21:41 UTC (rev 91912)
@@ -1,3 +1,14 @@
+2007-12-26  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * FeatureRequiresRootPrivilegeOnUnixRule.cs: New. Rule that check
+       for the use of features that requires to be root under Linux.
+       * NewLineLiteralRule.cs: Update to use latest best-practice since
+       it's a likely starting point for new rules.
+       * Gendarme.Rules.Portability.xml.in: Add new rule definition.
+       * Gendarme.Rules.Portability.mdp: Add new rule to the MonoDevelop 
+       project file.
+       * Makefile.am: Add new rule/tests to the build.
+
 2007-10-07  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * Gendarme.Rules.Portability.mdp: Update project file.

Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/FeatureRequiresRootPrivilegeOnUnixRule.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/FeatureRequiresRootPrivilegeOnUnixRule.cs
     2007-12-27 03:18:37 UTC (rev 91911)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/FeatureRequiresRootPrivilegeOnUnixRule.cs
     2007-12-27 03:21:41 UTC (rev 91912)
@@ -0,0 +1,100 @@
+//
+// Gendarme.Rules.Portability.FeatureRequiresRootPrivilegeOnUnixRule
+//
+// Authors:
+//     Andreas Noever <[EMAIL PROTECTED]>
+//
+//  (C) 2007 Andreas Noever
+//
+// 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.Diagnostics;
+
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.Portability {
+
+       public class FeatureRequiresRootPrivilegeOnUnixRule : IMethodRule {
+
+               public MessageCollection CheckMethod (MethodDefinition method, 
Runner runner)
+               {
+                       if (!method.HasBody)
+                               return runner.RuleSuccess;
+
+                       string fullname = method.DeclaringType.FullName;
+                       MessageCollection results = null;
+
+                       foreach (Instruction ins in method.Body.Instructions) {
+
+                               //Check for usage of 
System.Diagnostics.Process.set_PriorityClass
+                               switch (ins.OpCode.Name) {
+                               case "call":
+                               case "calli":
+                               case "callvirt":
+                                       MethodReference calledMethod = 
(MethodReference) ins.Operand;
+                                       if (calledMethod.Name != 
"set_PriorityClass")
+                                               break;
+                                       if (calledMethod.DeclaringType.FullName 
!= "System.Diagnostics.Process")
+                                               break;
+
+                                       Instruction prev = ins.Previous; 
//check stack
+                                       if (prev.OpCode == OpCodes.Ldc_I4_S)
+                                               if ((ProcessPriorityClass) 
(sbyte) prev.Operand == ProcessPriorityClass.Normal)
+                                                       break;
+
+                                       if (results == null)
+                                               results = new MessageCollection 
();
+
+                                       Location loc = new Location (fullname, 
method.Name, ins.Offset);
+                                       Message msg = new Message ("Setting 
Process.PriorityClass to something else than ProcessPriorityClass.Normal 
requires root privileges.", loc, MessageType.Warning);
+                                       results.Add (msg);
+                                       break;
+                               default:
+                                       break;
+                               }
+
+                               switch (ins.OpCode.Name) {
+                               case "newobj": //new Ping ()
+                               case "call": //MyPing () : base () ! (automatic 
parent constructor call)
+                               case "calli":
+                               case "callvirt":
+                                       MethodReference calledMethod = 
(MethodReference) ins.Operand;
+                                       if (calledMethod.DeclaringType.FullName 
!= "System.Net.NetworkInformation.Ping")
+                                               break;
+
+                                       if (results == null)
+                                               results = new MessageCollection 
();
+
+                                       Location loc = new Location (fullname, 
method.Name, ins.Offset);
+                                       Message msg = new Message ("Usage of 
System.Net.NetworkInformation.Ping requires root privileges.", loc, 
MessageType.Warning);
+                                       results.Add (msg);
+                                       break;
+                               }
+                       }
+
+                       return results;
+               }
+       }
+}

Modified: 
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Gendarme.Rules.Portability.mdp
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Gendarme.Rules.Portability.mdp
        2007-12-27 03:18:37 UTC (rev 91911)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Gendarme.Rules.Portability.mdp
        2007-12-27 03:21:41 UTC (rev 91912)
@@ -14,7 +14,8 @@
     </Configuration>
   </Configurations>
   <Contents>
-    <File name="./NewLineLiteralRule.cs" subtype="Code" buildaction="Compile" 
/>
+    <File name="NewLineLiteralRule.cs" subtype="Code" buildaction="Compile" />
+    <File name="FeatureRequiresRootPrivilegeOnUnixRule.cs" subtype="Code" 
buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Project" localcopy="True" refto="framework" />

Modified: 
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Gendarme.Rules.Portability.xml.in
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Gendarme.Rules.Portability.xml.in
     2007-12-27 03:18:37 UTC (rev 91911)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Gendarme.Rules.Portability.xml.in
     2007-12-27 03:21:41 UTC (rev 91912)
@@ -1,4 +1,10 @@
 <rules>
+       <rule   Name="FeatureRequiresRootPrivilegeOnUnixRule" 
+               
Type="Gendarme.Rules.Portability.FeatureRequiresRootPrivilegeOnUnixRule, 
Gendarme.Rules.Portability, [EMAIL PROTECTED]@, Culture=neutral, 
PublicKeyToken=null"
+               
Uri="http://www.mono-project.com/Gendarme#FeatureRequiresRootPrivilegeOnUnixRule";
 >
+               <problem>The method '{0}' use some literal values for new lines 
(e.g. \r\n) which aren't portable across operating systems.</problem>
+               <solution>Replace literals with Environment.NewLine.</solution>
+       </rule>
        <rule   Name="NewLineLiteralRule" 
                Type="Gendarme.Rules.Portability.NewLineLiteralRule, 
Gendarme.Rules.Portability, [EMAIL PROTECTED]@, Culture=neutral, 
PublicKeyToken=null"
                Uri="http://www.mono-project.com/Gendarme#NewLineLiteralRule"; >

Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Makefile.am
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Makefile.am   
2007-12-27 03:18:37 UTC (rev 91911)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/Makefile.am   
2007-12-27 03:21:41 UTC (rev 91912)
@@ -9,7 +9,7 @@
 
 portability_rules_sources_in = ../../AssemblyInfo.cs.in
 portability_rules_generated_sources = $(portability_rules_sources_in:.in=)
-portability_rules_sources = NewLineLiteralRule.cs
+portability_rules_sources = NewLineLiteralRule.cs 
FeatureRequiresRootPrivilegeOnUnixRule.cs
 
 portability_rules_build_sources = $(addprefix $(srcdir)/, 
$(portability_rules_sources))
 portability_rules_build_sources += $(portability_rules_generated_sources)
@@ -32,7 +32,7 @@
                rm -f $(DESTDIR)$(pkglibdir)/`basename $$ASM`; \
        done;
 
-portability_test_sources = NewLineLiteralTest.cs
+portability_test_sources = NewLineLiteralTest.cs 
FeatureRequiresRootPrivilegeOnUnixTest.cs
 portability_test_build_sources = $(addprefix $(srcdir)/Test/, 
$(portability_test_sources))
 
 Test.Rules.Portability.dll: $(portability_rules_build_sources) 
$(portability_rules_SCRIPTS)
@@ -42,7 +42,7 @@
 test: Test.Rules.Portability.dll
 
 run-test: test
-       MONO_PATH=../../bin/:$(CECIL_PATH):$(MONO_PATH) nunit-console 
Test.Rules.Portability.dll
+       MONO_PATH=../../bin/:$(CECIL_PATH):$(MONO_PATH) nunit-console2 
Test.Rules.Portability.dll
 
 self-test: $(portability_rules_SCRIPTS)
        mono --debug ../../bin/gendarme.exe $(portability_rules_SCRIPTS)

Modified: 
trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/NewLineLiteralRule.cs
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/NewLineLiteralRule.cs 
2007-12-27 03:18:37 UTC (rev 91911)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Portability/NewLineLiteralRule.cs 
2007-12-27 03:21:41 UTC (rev 91912)
@@ -4,7 +4,7 @@
 // Authors:
 //     Sebastien Pouliot <[EMAIL PROTECTED]>
 //
-// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2006-2007 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -27,7 +27,6 @@
 //
 
 using System;
-using System.Collections;
 
 using Mono.Cecil;
 using Mono.Cecil.Cil;
@@ -42,26 +41,34 @@
                public MessageCollection CheckMethod (MethodDefinition method, 
Runner runner)
                {
                        // methods can be empty (e.g. p/invoke declarations)
-                       if ((method.Body == null) || (method.Body.Instructions 
== null))
-                               return null;
+                       if (!method.HasBody)
+                               return runner.RuleSuccess;
 
-                       string fullname = method.DeclaringType.FullName;
-                       MessageCollection results = new MessageCollection ();
+                       // rule applies
+
+                       string fullname = null;
+                       MessageCollection results = null;
                        foreach (Instruction ins in method.Body.Instructions) {
-                               switch (ins.OpCode.Name) {
-                               case "ldstr":
+                               switch (ins.OpCode.Code) {
+                               case Code.Ldstr:
                                        // check the string being referenced by 
the instruction
                                        string s = (ins.Operand as string);
                                        if (s == null)
                                                continue;
 
                                        if (s.IndexOfAny (InvalidChar) >= 0) {
+                                               if (results == null) {
+                                                       results = new 
MessageCollection ();
+                                                       fullname = 
method.DeclaringType.FullName;
+                                               }
+
                                                Location loc = new Location 
(fullname, method.Name, ins.Offset);
                                                // make the invalid char 
visible on output
                                                s = s.Replace ("\n", "\\n");
                                                s = s.Replace ("\r", "\\r");
                                                Message msg = new Message 
(String.Format ("Found string: \"{0}\"", s),
                                                        loc, 
MessageType.Warning);
+
                                                results.Add (msg);
                                        }
                                        break;
@@ -70,8 +77,9 @@
                                }
                        }
 
-                       if (results.Count == 0)
-                               return null;
+                       if (results == null)
+                               return runner.RuleSuccess;
+
                        return results;
                }
        }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to