PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/Test/Mono.Cecil.Tests/SecurityDeclarationTests.cs

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