| 1 | using System; |
| 2 | using System.Globalization; |
| 3 | using System.IO; |
| 4 | using System.Linq; |
| 5 | using System.Text; |
| 6 | |
| 7 | using Mono.Cecil; |
| 8 | using Mono.Cecil.Metadata; |
| 9 | using Mono.Cecil.PE; |
| 10 | |
| 11 | using NUnit.Framework; |
| 12 | |
| 13 | namespace Mono.Cecil.Tests { |
| 14 | |
| 15 | [TestFixture] |
| 16 | public class SecurityDeclarationTests : BaseTestFixture { |
| 17 | |
| 18 | [TestModule ("decsec-xml.dll")] |
| 19 | public void XmlSecurityDeclaration (ModuleDefinition module) |
| 20 | { |
| 21 | var type = module.GetType ("SubLibrary"); |
| 22 | |
| 23 | Assert.IsTrue (type.HasSecurityDeclarations); |
| 24 | |
| 25 | Assert.AreEqual (1, type.SecurityDeclarations.Count); |
| 26 | |
| 27 | var declaration = type.SecurityDeclarations [0]; |
| 28 | Assert.AreEqual (SecurityAction.Deny, declaration.Action); |
| 29 | |
| 30 | Assert.AreEqual (1, declaration.SecurityAttributes.Count); |
| 31 | |
| 32 | var attribute = declaration.SecurityAttributes [0]; |
| 33 | |
| 34 | Assert.AreEqual ("System.Security.Permissions.PermissionSetAttribute", attribute.AttributeType.FullName); |
| 35 | |
| 36 | Assert.AreEqual (1, attribute.Properties.Count); |
| 37 | |
| 38 | var named_argument = attribute.Properties [0]; |
| 39 | |
| 40 | Assert.AreEqual ("XML", named_argument.Name); |
| 41 | |
| 42 | var argument = named_argument.Argument; |
| 43 | |
| 44 | Assert.AreEqual ("System.String", argument.Type.FullName); |
| 45 | |
| 46 | const string permission_set = "<PermissionSet class=\"System.Security.PermissionSe" |
| 47 | + "t\"\r\nversion=\"1\">\r\n<IPermission class=\"System.Security.Permis" |
| 48 | + "sions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture" |
| 49 | + "=neutral, PublicKeyToken=b77a5c561934e089\"\r\nversion=\"1\"\r\nFla" |
| 50 | + "gs=\"UnmanagedCode\"/>\r\n</PermissionSet>\r\n"; |
| 51 | |
| 52 | Assert.AreEqual (permission_set, argument.Value); |
| 53 | } |
| 54 | |
| 55 | [TestModule ("decsec1-xml.dll")] |
| 56 | public void XmlNet_1_1SecurityDeclaration (ModuleDefinition module) |
| 57 | { |
| 58 | var type = module.GetType ("SubLibrary"); |
| 59 | |
| 60 | Assert.IsTrue (type.HasSecurityDeclarations); |
| 61 | |
| 62 | Assert.AreEqual (1, type.SecurityDeclarations.Count); |
| 63 | |
| 64 | var declaration = type.SecurityDeclarations [0]; |
| 65 | Assert.AreEqual (SecurityAction.Deny, declaration.Action); |
| 66 | |
| 67 | Assert.AreEqual (1, declaration.SecurityAttributes.Count); |
| 68 | |
| 69 | var attribute = declaration.SecurityAttributes [0]; |
| 70 | |
| 71 | Assert.AreEqual ("System.Security.Permissions.PermissionSetAttribute", attribute.AttributeType.FullName); |
| 72 | |
| 73 | Assert.AreEqual (1, attribute.Properties.Count); |
| 74 | |
| 75 | var named_argument = attribute.Properties [0]; |
| 76 | |
| 77 | Assert.AreEqual ("XML", named_argument.Name); |
| 78 | |
| 79 | var argument = named_argument.Argument; |
| 80 | |
| 81 | Assert.AreEqual ("System.String", argument.Type.FullName); |
| 82 | |
| 83 | const string permission_set = "<PermissionSet class=\"System.Security.PermissionSe" |
| 84 | + "t\"\r\nversion=\"1\">\r\n<IPermission class=\"System.Security.Permis" |
| 85 | + "sions.SecurityPermission, mscorlib, Version=1.0.0.0, Culture" |
| 86 | + "=neutral, PublicKeyToken=b77a5c561934e089\"\r\nversion=\"1\"\r\nFla" |
| 87 | + "gs=\"UnmanagedCode\"/>\r\n</PermissionSet>\r\n"; |
| 88 | |
| 89 | Assert.AreEqual (permission_set, argument.Value); |
| 90 | } |
| 91 | |
| 92 | [Test] |
| 93 | public void DefineSecurityDeclarationByBlob () |
| 94 | { |
| 95 | var file = Path.Combine(Path.GetTempPath(), "SecDecBlob.dll"); |
| 96 | var module = ModuleDefinition.CreateModule ("SecDecBlob.dll", new ModuleParameters { Kind = ModuleKind.Dll, Runtime = TargetRuntime.Net_2_0 }); |
| 97 | |
| 98 | const string permission_set = "<PermissionSet class=\"System.Security.PermissionSe" |
| 99 | + "t\"\r\nversion=\"1\">\r\n<IPermission class=\"System.Security.Permis" |
| 100 | + "sions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture" |
| 101 | + "=neutral, PublicKeyToken=b77a5c561934e089\"\r\nversion=\"1\"\r\nFla" |
| 102 | + "gs=\"UnmanagedCode\"/>\r\n</PermissionSet>\r\n"; |
| 103 | |
| 104 | var declaration = new SecurityDeclaration (SecurityAction.Deny, Encoding.Unicode.GetBytes (permission_set)); |
| 105 | module.Assembly.SecurityDeclarations.Add (declaration); |
| 106 | |
| 107 | module.Write (file); |
| 108 | module = ModuleDefinition.ReadModule (file); |
| 109 | |
| 110 | declaration = module.Assembly.SecurityDeclarations [0]; |
| 111 | Assert.AreEqual (SecurityAction.Deny, declaration.Action); |
| 112 | Assert.AreEqual (1, declaration.SecurityAttributes.Count); |
| 113 | |
| 114 | var attribute = declaration.SecurityAttributes [0]; |
| 115 | Assert.AreEqual ("System.Security.Permissions.PermissionSetAttribute", attribute.AttributeType.FullName); |
| 116 | Assert.AreEqual (1, attribute.Properties.Count); |
| 117 | |
| 118 | var named_argument = attribute.Properties [0]; |
| 119 | Assert.AreEqual ("XML", named_argument.Name); |
| 120 | var argument = named_argument.Argument; |
| 121 | Assert.AreEqual ("System.String", argument.Type.FullName); |
| 122 | Assert.AreEqual (permission_set, argument.Value); |
| 123 | } |
| 124 | |
| 125 | [TestModule ("empty-decsec-att.dll")] |
| 126 | public void SecurityDeclarationWithoutAttributes (ModuleDefinition module) |
| 127 | { |
| 128 | var type = module.GetType ("TestSecurityAction.ModalUITypeEditor"); |
| 129 | var method = type.GetMethod ("GetEditStyle"); |
| 130 | |
| 131 | Assert.IsNotNull (method); |
| 132 | |
| 133 | Assert.AreEqual (1, method.SecurityDeclarations.Count); |
| 134 | |
| 135 | var declaration = method.SecurityDeclarations [0]; |
| 136 | Assert.AreEqual (SecurityAction.LinkDemand, declaration.Action); |
| 137 | Assert.AreEqual (1, declaration.SecurityAttributes.Count); |
| 138 | |
| 139 | var attribute = declaration.SecurityAttributes [0]; |
| 140 | Assert.AreEqual ("System.Security.Permissions.SecurityPermissionAttribute", attribute.AttributeType.FullName); |
| 141 | Assert.AreEqual (0, attribute.Fields.Count); |
| 142 | Assert.AreEqual (0, attribute.Properties.Count); |
| 143 | } |
| 144 | |
| 145 | [TestModule ("decsec-att.dll")] |
| 146 | public void AttributeSecurityDeclaration (ModuleDefinition module) |
| 147 | { |
| 148 | var type = module.GetType ("SubLibrary"); |
| 149 | |
| 150 | Assert.IsTrue (type.HasSecurityDeclarations); |
| 151 | |
| 152 | Assert.AreEqual (1, type.SecurityDeclarations.Count); |
| 153 | |
| 154 | var declaration = type.SecurityDeclarations [0]; |
| 155 | Assert.AreEqual (SecurityAction.Deny, declaration.Action); |
| 156 | |
| 157 | Assert.AreEqual (1, declaration.SecurityAttributes.Count); |
| 158 | |
| 159 | var attribute = declaration.SecurityAttributes [0]; |
| 160 | |
| 161 | Assert.AreEqual ("System.Security.Permissions.SecurityPermissionAttribute", attribute.AttributeType.FullName); |
| 162 | |
| 163 | Assert.AreEqual (1, attribute.Properties.Count); |
| 164 | |
| 165 | var named_argument = attribute.Properties [0]; |
| 166 | |
| 167 | Assert.AreEqual ("UnmanagedCode", named_argument.Name); |
| 168 | |
| 169 | var argument = named_argument.Argument; |
| 170 | |
| 171 | Assert.AreEqual ("System.Boolean", argument.Type.FullName); |
| 172 | |
| 173 | Assert.AreEqual (true, argument.Value); |
| 174 | } |
| 175 | |
| 176 | static void AssertCustomAttributeArgument (string expected, CustomAttributeNamedArgument named_argument) |
| 177 | { |
| 178 | AssertCustomAttributeArgument (expected, named_argument.Argument); |
| 179 | } |
| 180 | |
| 181 | static void AssertCustomAttributeArgument (string expected, CustomAttributeArgument argument) |
| 182 | { |
| 183 | var result = new StringBuilder (); |
| 184 | PrettyPrint (argument, result); |
| 185 | |
| 186 | Assert.AreEqual (expected, result.ToString ()); |
| 187 | } |
| 188 | |
| 189 | static string PrettyPrint (CustomAttribute attribute) |
| 190 | { |
| 191 | var signature = new StringBuilder (); |
| 192 | signature.Append (".ctor ("); |
| 193 | |
| 194 | for (int i = 0; i < attribute.ConstructorArguments.Count; i++) { |
| 195 | if (i > 0) |
| 196 | signature.Append (", "); |
| 197 | |
| 198 | PrettyPrint (attribute.ConstructorArguments [i], signature); |
| 199 | } |
| 200 | |
| 201 | signature.Append (")"); |
| 202 | return signature.ToString (); |
| 203 | } |
| 204 | |
| 205 | static void PrettyPrint (CustomAttributeArgument argument, StringBuilder signature) |
| 206 | { |
| 207 | var value = argument.Value; |
| 208 | |
| 209 | signature.Append ("("); |
| 210 | |
| 211 | PrettyPrint (argument.Type, signature); |
| 212 | |
| 213 | signature.Append (":"); |
| 214 | |
| 215 | PrettyPrintValue (argument.Value, signature); |
| 216 | |
| 217 | signature.Append (")"); |
| 218 | } |
| 219 | |
| 220 | static void PrettyPrintValue (object value, StringBuilder signature) |
| 221 | { |
| 222 | if (value == null) { |
| 223 | signature.Append ("null"); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | var arguments = value as CustomAttributeArgument []; |
| 228 | if (arguments != null) { |
| 229 | signature.Append ("{"); |
| 230 | for (int i = 0; i < arguments.Length; i++) { |
| 231 | if (i > 0) |
| 232 | signature.Append (", "); |
| 233 | |
| 234 | PrettyPrint (arguments [i], signature); |
| 235 | } |
| 236 | signature.Append ("}"); |
| 237 | |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | switch (Type.GetTypeCode (value.GetType ())) { |
| 242 | case TypeCode.String: |
| 243 | signature.AppendFormat ("\"{0}\"", value); |
| 244 | break; |
| 245 | case TypeCode.Char: |
| 246 | signature.AppendFormat ("'{0}'", (char) value); |
| 247 | break; |
| 248 | default: |
| 249 | var formattable = value as IFormattable; |
| 250 | if (formattable != null) { |
| 251 | signature.Append (formattable.ToString (null, CultureInfo.InvariantCulture)); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | if (value is CustomAttributeArgument) { |
| 256 | PrettyPrint ((CustomAttributeArgument) value, signature); |
| 257 | return; |
| 258 | } |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | static void PrettyPrint (TypeReference type, StringBuilder signature) |
| 264 | { |
| 265 | if (type.IsArray) { |
| 266 | ArrayType array = (ArrayType) type; |
| 267 | signature.AppendFormat ("{0}[]", array.ElementType.etype.ToString ()); |
| 268 | } else if (type.etype == ElementType.None) { |
| 269 | signature.Append (type.FullName); |
| 270 | } else |
| 271 | signature.Append (type.etype.ToString ()); |
| 272 | } |
| 273 | |
| 274 | static void AssertArgument<T> (T value, CustomAttributeNamedArgument named_argument) |
| 275 | { |
| 276 | AssertArgument (value, named_argument.Argument); |
| 277 | } |
| 278 | |
| 279 | static void AssertArgument<T> (T value, CustomAttributeArgument argument) |
| 280 | { |
| 281 | AssertArgument (typeof (T).FullName, (object) value, argument); |
| 282 | } |
| 283 | |
| 284 | static void AssertArgument (string type, object value, CustomAttributeArgument argument) |
| 285 | { |
| 286 | Assert.AreEqual (type, argument.Type.FullName); |
| 287 | Assert.AreEqual (value, argument.Value); |
| 288 | } |
| 289 | } |
| 290 | } |