Debian Codegraph @

external/cecil-legacy/Test/Mono.Cecil.Tests/CustomAttributesTests.cs

raw symbols SCIP
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 CustomAttributesTests : BaseTestFixture {
17
18 [TestCSharp ("CustomAttributes.cs")]
19 public void StringArgumentOnType (ModuleDefinition module)
20 {
21 var hamster = module.GetType ("Hamster");
22
23 Assert.IsTrue (hamster.HasCustomAttributes);
24 Assert.AreEqual (1, hamster.CustomAttributes.Count);
25
26 var attribute = hamster.CustomAttributes [0];
27 Assert.AreEqual ("System.Void FooAttribute::.ctor(System.String)",
28 attribute.Constructor.FullName);
29
30 Assert.IsTrue (attribute.HasConstructorArguments);
31 Assert.AreEqual (1, attribute.ConstructorArguments.Count);
32
33 AssertArgument ("bar", attribute.ConstructorArguments [0]);
34 }
35
36 [TestCSharp ("CustomAttributes.cs")]
37 public void NullString (ModuleDefinition module)
38 {
39 var dentist = module.GetType ("Dentist");
40
41 var attribute = GetAttribute (dentist, "Foo");
42 Assert.IsNotNull (attribute);
43
44 AssertArgument<string> (null, attribute.ConstructorArguments [0]);
45 }
46
47 [TestCSharp ("CustomAttributes.cs")]
48 public void Primitives1 (ModuleDefinition module)
49 {
50 var steven = module.GetType ("Steven");
51
52 var attribute = GetAttribute (steven, "Foo");
53 Assert.IsNotNull (attribute);
54
55 AssertArgument<sbyte> (-12, attribute.ConstructorArguments [0]);
56 AssertArgument<byte> (242, attribute.ConstructorArguments [1]);
57 AssertArgument<bool> (true, attribute.ConstructorArguments [2]);
58 AssertArgument<bool> (false, attribute.ConstructorArguments [3]);
59 AssertArgument<ushort> (4242, attribute.ConstructorArguments [4]);
60 AssertArgument<short> (-1983, attribute.ConstructorArguments [5]);
61 AssertArgument<char> ('c', attribute.ConstructorArguments [6]);
62 }
63
64 [TestCSharp ("CustomAttributes.cs")]
65 public void Primitives2 (ModuleDefinition module)
66 {
67 var seagull = module.GetType ("Seagull");
68
69 var attribute = GetAttribute (seagull, "Foo");
70 Assert.IsNotNull (attribute);
71
72 AssertArgument<int> (-100000, attribute.ConstructorArguments [0]);
73 AssertArgument<uint> (200000, attribute.ConstructorArguments [1]);
74 AssertArgument<float> (12.12f, attribute.ConstructorArguments [2]);
75 AssertArgument<long> (long.MaxValue, attribute.ConstructorArguments [3]);
76 AssertArgument<ulong> (ulong.MaxValue, attribute.ConstructorArguments [4]);
77 AssertArgument<double> (64.646464, attribute.ConstructorArguments [5]);
78 }
79
80 [TestCSharp ("CustomAttributes.cs")]
81 public void StringArgumentOnAssembly (ModuleDefinition module)
82 {
83 var assembly = module.Assembly;
84
85 var attribute = GetAttribute (assembly, "Foo");
86 Assert.IsNotNull (attribute);
87
88 AssertArgument ("bingo", attribute.ConstructorArguments [0]);
89 }
90
91 [TestCSharp ("CustomAttributes.cs")]
92 public void CharArray (ModuleDefinition module)
93 {
94 var rifle = module.GetType ("Rifle");
95
96 var attribute = GetAttribute (rifle, "Foo");
97 Assert.IsNotNull (attribute);
98
99 var argument = attribute.ConstructorArguments [0];
100
101 Assert.AreEqual ("System.Char[]", argument.Type.FullName);
102
103 var array = argument.Value as CustomAttributeArgument [];
104 Assert.IsNotNull (array);
105
106 var str = "cecil";
107
108 Assert.AreEqual (array.Length, str.Length);
109
110 for (int i = 0; i < str.Length; i++)
111 AssertArgument (str [i], array [i]);
112 }
113
114 [TestCSharp ("CustomAttributes.cs")]
115 public void BoxedArguments (ModuleDefinition module)
116 {
117 var worm = module.GetType ("Worm");
118
119 var attribute = GetAttribute (worm, "Foo");
120 Assert.IsNotNull (attribute);
121
122 Assert.AreEqual (".ctor ((Object:(String:\"2\")), (Object:(I4:2)))", PrettyPrint (attribute));
123 }
124
125 [TestCSharp ("CustomAttributes.cs")]
126 public void BoxedArraysArguments (ModuleDefinition module)
127 {
128 var sheep = module.GetType ("Sheep");
129
130 var attribute = GetAttribute (sheep, "Foo");
131 Assert.IsNotNull (attribute);
132
133 // [Foo (new object [] { "2", 2, 'c' }, new object [] { new object [] { 1, 2, 3}, null })]
134 AssertCustomAttribute (".ctor ((Object:(Object[]:{(Object:(String:\"2\")), (Object:(I4:2)), (Object:(Char:'c'))})), (Object:(Object[]:{(Object:(Object[]:{(Object:(I4:1)), (Object:(I4:2)), (Object:(I4:3))})), (Object:(String:null))})))", attribute);
135 }
136
137 [TestCSharp ("CustomAttributes.cs")]
138 public void FieldsAndProperties (ModuleDefinition module)
139 {
140 var angola = module.GetType ("Angola");
141
142 var attribute = GetAttribute (angola, "Foo");
143 Assert.IsNotNull (attribute);
144
145 Assert.AreEqual (2, attribute.Fields.Count);
146
147 var argument = attribute.Fields.Where (a => a.Name == "Pan").First ();
148 AssertCustomAttributeArgument ("(Object:(Object[]:{(Object:(I4:1)), (Object:(String:\"2\")), (Object:(Char:'3'))}))", argument);
149
150 argument = attribute.Fields.Where (a => a.Name == "PanPan").First ();
151 AssertCustomAttributeArgument ("(String[]:{(String:\"yo\"), (String:\"yo\")})", argument);
152
153 Assert.AreEqual (2, attribute.Properties.Count);
154
155 argument = attribute.Properties.Where (a => a.Name == "Bang").First ();
156 AssertArgument (42, argument);
157
158 argument = attribute.Properties.Where (a => a.Name == "Fiou").First ();
159 AssertArgument<string> (null, argument);
160 }
161
162 [TestCSharp ("CustomAttributes.cs")]
163 public void BoxedStringField (ModuleDefinition module)
164 {
165 var type = module.GetType ("BoxedStringField");
166
167 var attribute = GetAttribute (type, "Foo");
168 Assert.IsNotNull (attribute);
169
170 Assert.AreEqual (1, attribute.Fields.Count);
171
172 var argument = attribute.Fields.Where (a => a.Name == "Pan").First ();
173 AssertCustomAttributeArgument ("(Object:(String:\"fiouuu\"))", argument);
174 }
175
176 [TestCSharp ("CustomAttributes.cs")]
177 public void TypeDefinitionEnum (ModuleDefinition module)
178 {
179 var zero = module.GetType ("Zero");
180
181 var attribute = GetAttribute (zero, "Foo");
182 Assert.IsNotNull (attribute);
183
184 Assert.AreEqual (1, attribute.ConstructorArguments.Count);
185
186 Assert.AreEqual ((short) 2, attribute.ConstructorArguments [0].Value);
187 Assert.AreEqual ("Bingo", attribute.ConstructorArguments [0].Type.FullName);
188 }
189
190 [TestCSharp ("CustomAttributes.cs")]
191 public void TypeReferenceEnum (ModuleDefinition module)
192 {
193 var ace = module.GetType ("Ace");
194
195 var attribute = GetAttribute (ace, "Foo");
196 Assert.IsNotNull (attribute);
197
198 Assert.AreEqual (1, attribute.ConstructorArguments.Count);
199
200 Assert.AreEqual ((byte) 0x04, attribute.ConstructorArguments [0].Value);
201 Assert.AreEqual ("System.Security.AccessControl.AceFlags", attribute.ConstructorArguments [0].Type.FullName);
202 Assert.AreEqual (module, attribute.ConstructorArguments [0].Type.Module);
203 }
204
205 [TestCSharp ("CustomAttributes.cs")]
206 public void BoxedEnumReference (ModuleDefinition module)
207 {
208 var bzzz = module.GetType ("Bzzz");
209
210 var attribute = GetAttribute (bzzz, "Foo");
211 Assert.IsNotNull (attribute);
212
213 // [Foo (new object [] { Bingo.Fuel, Bingo.Binga }, null, Pan = System.Security.AccessControl.AceFlags.NoPropagateInherit)]
214
215 Assert.AreEqual (2, attribute.ConstructorArguments.Count);
216
217 var argument = attribute.ConstructorArguments [0];
218
219 AssertCustomAttributeArgument ("(Object:(Object[]:{(Object:(Bingo:2)), (Object:(Bingo:4))}))", argument);
220
221 argument = attribute.ConstructorArguments [1];
222
223 AssertCustomAttributeArgument ("(Object:(String:null))", argument);
224
225 argument = attribute.Fields.Where (a => a.Name == "Pan").First ().Argument;
226
227 AssertCustomAttributeArgument ("(Object:(System.Security.AccessControl.AceFlags:4))", argument);
228 }
229
230 [TestCSharp ("CustomAttributes.cs")]
231 public void TypeOfTypeDefinition (ModuleDefinition module)
232 {
233 var typed = module.GetType ("Typed");
234
235 var attribute = GetAttribute (typed, "Foo");
236 Assert.IsNotNull (attribute);
237
238 Assert.AreEqual (1, attribute.ConstructorArguments.Count);
239
240 var argument = attribute.ConstructorArguments [0];
241
242 Assert.AreEqual ("System.Type", argument.Type.FullName);
243
244 var type = argument.Value as TypeDefinition;
245 Assert.IsNotNull (type);
246
247 Assert.AreEqual ("Bingo", type.FullName);
248 }
249
250 [TestCSharp ("CustomAttributes.cs")]
251 public void TypeOfNestedTypeDefinition (ModuleDefinition module)
252 {
253 var typed = module.GetType ("NestedTyped");
254
255 var attribute = GetAttribute (typed, "Foo");
256 Assert.IsNotNull (attribute);
257
258 Assert.AreEqual (1, attribute.ConstructorArguments.Count);
259
260 var argument = attribute.ConstructorArguments [0];
261
262 Assert.AreEqual ("System.Type", argument.Type.FullName);
263
264 var type = argument.Value as TypeDefinition;
265 Assert.IsNotNull (type);
266
267 Assert.AreEqual ("FooAttribute/Token", type.FullName);
268 }
269
270 [TestCSharp ("CustomAttributes.cs")]
271 public void FieldTypeOf (ModuleDefinition module)
272 {
273 var truc = module.GetType ("Truc");
274
275 var attribute = GetAttribute (truc, "Foo");
276 Assert.IsNotNull (attribute);
277
278 var argument = attribute.Fields.Where (a => a.Name == "Chose").First ().Argument;
279
280 Assert.AreEqual ("System.Type", argument.Type.FullName);
281
282 var type = argument.Value as TypeDefinition;
283 Assert.IsNotNull (type);
284
285 Assert.AreEqual ("Typed", type.FullName);
286 }
287
288 [TestCSharp ("CustomAttributes.cs")]
289 public void FieldNullTypeOf (ModuleDefinition module)
290 {
291 var truc = module.GetType ("Machin");
292
293 var attribute = GetAttribute (truc, "Foo");
294 Assert.IsNotNull (attribute);
295
296 var argument = attribute.Fields.Where (a => a.Name == "Chose").First ().Argument;
297
298 Assert.AreEqual ("System.Type", argument.Type.FullName);
299
300 Assert.IsNull (argument.Value);
301 }
302
303 [TestCSharp ("CustomAttributes.cs")]
304 public void OpenGenericTypeOf (ModuleDefinition module)
305 {
306 var open_generic = module.GetType ("OpenGeneric`2");
307 Assert.IsNotNull (open_generic);
308
309 var attribute = GetAttribute (open_generic, "Foo");
310 Assert.IsNotNull (attribute);
311
312 Assert.AreEqual (1, attribute.ConstructorArguments.Count);
313
314 var argument = attribute.ConstructorArguments [0];
315
316 Assert.AreEqual ("System.Type", argument.Type.FullName);
317
318 var type = argument.Value as TypeReference;
319 Assert.IsNotNull (type);
320
321 Assert.AreEqual ("System.Collections.Generic.Dictionary`2", type.FullName);
322 }
323
324 [TestCSharp ("CustomAttributes.cs")]
325 public void ClosedGenericTypeOf (ModuleDefinition module)
326 {
327 var closed_generic = module.GetType ("ClosedGeneric");
328 Assert.IsNotNull (closed_generic);
329
330 var attribute = GetAttribute (closed_generic, "Foo");
331 Assert.IsNotNull (attribute);
332
333 Assert.AreEqual (1, attribute.ConstructorArguments.Count);
334
335 var argument = attribute.ConstructorArguments [0];
336
337 Assert.AreEqual ("System.Type", argument.Type.FullName);
338
339 var type = argument.Value as TypeReference;
340 Assert.IsNotNull (type);
341
342 Assert.AreEqual ("System.Collections.Generic.Dictionary`2<System.String,OpenGeneric`2<Machin,System.Int32>[,]>", type.FullName);
343 }
344
345 [TestIL ("types.il")]
346 public void EmptyBlob (ModuleDefinition module)
347 {
348 var attribute = module.GetType ("CustomAttribute");
349 Assert.AreEqual (1, attribute.CustomAttributes.Count);
350 Assert.AreEqual (0, attribute.CustomAttributes [0].ConstructorArguments.Count);
351 }
352
353 [Test]
354 public void DefineCustomAttributeFromBlob ()
355 {
356 var file = Path.Combine (Path.GetTempPath (), "CaBlob.dll");
357
358 var module = ModuleDefinition.CreateModule ("CaBlob.dll", new ModuleParameters { Kind = ModuleKind.Dll, Runtime = TargetRuntime.Net_2_0 });
359 var assembly_title_ctor = module.Import (typeof (System.Reflection.AssemblyTitleAttribute).GetConstructor (new [] {typeof (string)}));
360
361 Assert.IsNotNull (assembly_title_ctor);
362
363 var buffer = new ByteBuffer ();
364 buffer.WriteUInt16 (1); // ca signature
365
366 var title = Encoding.UTF8.GetBytes ("CaBlob");
367
368 buffer.WriteCompressedUInt32 ((uint) title.Length);
369 buffer.WriteBytes (title);
370
371 buffer.WriteUInt16 (0); // named arguments
372
373 var blob = new byte [buffer.length];
374 Buffer.BlockCopy (buffer.buffer, 0, blob, 0, buffer.length);
375
376 var attribute = new CustomAttribute (assembly_title_ctor, blob);
377 module.Assembly.CustomAttributes.Add (attribute);
378
379 module.Write (file);
380
381 module = ModuleDefinition.ReadModule (file);
382
383 attribute = GetAttribute (module.Assembly, "AssemblyTitle");
384
385 Assert.IsNotNull (attribute);
386 Assert.AreEqual ("CaBlob", (string) attribute.ConstructorArguments [0].Value);
387 }
388
389 static void AssertCustomAttribute (string expected, CustomAttribute attribute)
390 {
391 Assert.AreEqual (expected, PrettyPrint (attribute));
392 }
393
394 static void AssertCustomAttributeArgument (string expected, CustomAttributeNamedArgument named_argument)
395 {
396 AssertCustomAttributeArgument (expected, named_argument.Argument);
397 }
398
399 static void AssertCustomAttributeArgument (string expected, CustomAttributeArgument argument)
400 {
401 var result = new StringBuilder ();
402 PrettyPrint (argument, result);
403
404 Assert.AreEqual (expected, result.ToString ());
405 }
406
407 static string PrettyPrint (CustomAttribute attribute)
408 {
409 var signature = new StringBuilder ();
410 signature.Append (".ctor (");
411
412 for (int i = 0; i < attribute.ConstructorArguments.Count; i++) {
413 if (i > 0)
414 signature.Append (", ");
415
416 PrettyPrint (attribute.ConstructorArguments [i], signature);
417 }
418
419 signature.Append (")");
420 return signature.ToString ();
421 }
422
423 static void PrettyPrint (CustomAttributeArgument argument, StringBuilder signature)
424 {
425 var value = argument.Value;
426
427 signature.Append ("(");
428
429 PrettyPrint (argument.Type, signature);
430
431 signature.Append (":");
432
433 PrettyPrintValue (argument.Value, signature);
434
435 signature.Append (")");
436 }
437
438 static void PrettyPrintValue (object value, StringBuilder signature)
439 {
440 if (value == null) {
441 signature.Append ("null");
442 return;
443 }
444
445 var arguments = value as CustomAttributeArgument [];
446 if (arguments != null) {
447 signature.Append ("{");
448 for (int i = 0; i < arguments.Length; i++) {
449 if (i > 0)
450 signature.Append (", ");
451
452 PrettyPrint (arguments [i], signature);
453 }
454 signature.Append ("}");
455
456 return;
457 }
458
459 switch (Type.GetTypeCode (value.GetType ())) {
460 case TypeCode.String:
461 signature.AppendFormat ("\"{0}\"", value);
462 break;
463 case TypeCode.Char:
464 signature.AppendFormat ("'{0}'", (char) value);
465 break;
466 default:
467 var formattable = value as IFormattable;
468 if (formattable != null) {
469 signature.Append (formattable.ToString (null, CultureInfo.InvariantCulture));
470 return;
471 }
472
473 if (value is CustomAttributeArgument) {
474 PrettyPrint ((CustomAttributeArgument) value, signature);
475 return;
476 }
477 break;
478 }
479 }
480
481 static void PrettyPrint (TypeReference type, StringBuilder signature)
482 {
483 if (type.IsArray) {
484 ArrayType array = (ArrayType) type;
485 signature.AppendFormat ("{0}[]", array.ElementType.etype.ToString ());
486 } else if (type.etype == ElementType.None) {
487 signature.Append (type.FullName);
488 } else
489 signature.Append (type.etype.ToString ());
490 }
491
492 static void AssertArgument<T> (T value, CustomAttributeNamedArgument named_argument)
493 {
494 AssertArgument (value, named_argument.Argument);
495 }
496
497 static void AssertArgument<T> (T value, CustomAttributeArgument argument)
498 {
499 AssertArgument (typeof (T).FullName, (object) value, argument);
500 }
501
502 static void AssertArgument (string type, object value, CustomAttributeArgument argument)
503 {
504 Assert.AreEqual (type, argument.Type.FullName);
505 Assert.AreEqual (value, argument.Value);
506 }
507
508 static CustomAttribute GetAttribute (ICustomAttributeProvider owner, string type)
509 {
510 Assert.IsTrue (owner.HasCustomAttributes);
511
512 foreach (var attribute in owner.CustomAttributes)
513 if (attribute.Constructor.DeclaringType.Name.StartsWith (type))
514 return attribute;
515
516 return null;
517 }
518 }
519 }