File: CustomAttributeDataTest.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (318 lines) | stat: -rw-r--r-- 12,881 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//
// System.Reflection.CustomAttributeData Test Cases
//
// Authors:
//  Zoltan Varga (vargaz@gmail.com)
//
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
// Copyright (C) 2004-2008 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
// "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 NUnit.Framework;
using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace MonoTests.System.Reflection
{
	enum Levels { one, two, three }

	class Attr : Attribute {
		public Attr (byte[] arr) {
		}
	}

	[AttributeUsage (AttributeTargets.Method)]
	class TestAttrWithObjectCtorParam : Attribute {
		object o;
		public TestAttrWithObjectCtorParam (object o) => this.o = o;
	}

	[AttributeUsage (AttributeTargets.Method)]
	class TestAttrWithEnumCtorParam : Attribute {
		Levels level;
		public TestAttrWithEnumCtorParam (Levels level) => this.level = level;
	}

	[AttributeUsage (AttributeTargets.Method)]
	class TestAttrWithObjectArrayCtorParam : Attribute {
		object[] o;
		public TestAttrWithObjectArrayCtorParam (object[] o) => this.o = o;
	}

	[AttributeUsage (AttributeTargets.Method)]
	class TestAttrWithObjectArrayCtorParamAndParamsKeyword : Attribute {
		object[] o;
		public TestAttrWithObjectArrayCtorParamAndParamsKeyword (params object[] o) => this.o = o;
	}

	[AttributeUsage (AttributeTargets.Method)]
	class TestAttrWithObjectArrayCtorParams : Attribute {
		object[] o1;
		object[] o2;
		public TestAttrWithObjectArrayCtorParams (object[] o1, params object[] o2) {
			this.o1 = o1;
			this.o2 = o2;
		}
	}

	[TestFixture]
	public class CustomAttributeDataTest
	{
		[DllImport ("libc")]
		public static extern int readlink (string path, byte[] buffer, int buflen);

		[MarshalAs (UnmanagedType.LPStr)]
		[NonSerialized]
		public string fieldDecoratedWithPseudoCustomAttributes = "test";

		[Attr (new byte [] { 1, 2 })]
		public void MethodWithAttr () {
		}

		[TestAttrWithObjectCtorParam (Levels.two)]
		public void MethodDecoratedWithAttribute1 ()
		{
		}

		[TestAttrWithEnumCtorParam (Levels.two)]
		public void MethodDecoratedWithAttribute2 ()
		{
		}

		[TestAttrWithObjectArrayCtorParam (new object[] { Levels.one, Levels.two})]
		public void MethodDecoratedWithAttribute3 ()
		{
		}		

		[TestAttrWithObjectArrayCtorParamAndParamsKeyword (Levels.one, Levels.two)]
		public void MethodDecoratedWithAttribute4 ()
		{
		}

		[TestAttrWithObjectArrayCtorParams (new object[] { Levels.one, Levels.two}, Levels.three, Levels.two)]
		public void MethodDecoratedWithAttribute5 ()
		{
		}

		public void MethodWithParamDecoratedWithPseudoCustomAttributes ([Optional, In, Out, MarshalAs (UnmanagedType.LPStr)] String s)
		{
		}

		[return: MarshalAs (UnmanagedType.LPStr)]
		public string MethodWithReturnValueDecoratedWithMarshalAs ()
		{
			return "test";
		}

		[Test]
		public void Arrays () {
			IList<CustomAttributeData> cdata = CustomAttributeData.GetCustomAttributes (typeof (CustomAttributeDataTest).GetMethod ("MethodWithAttr"));
			Assert.AreEqual (1, cdata.Count);
			CustomAttributeTypedArgument arg = cdata [0].ConstructorArguments [0];
			Assert.IsTrue (typeof (IList<CustomAttributeTypedArgument>).IsAssignableFrom (arg.Value.GetType ()));
			IList<CustomAttributeTypedArgument> arr = (IList<CustomAttributeTypedArgument>)arg.Value;
			Assert.AreEqual (2, arr.Count);
			Assert.AreEqual (typeof (byte), arr [0].ArgumentType);
			Assert.AreEqual (1, arr [0].Value);
			Assert.AreEqual (typeof (byte), arr [1].ArgumentType);
			Assert.AreEqual (2, arr [1].Value);
		}

		[Test]
		// https://github.com/mono/mono/issues/10951
		public void ObjectArrays () 
		{
			CheckObjectArrayParam (nameof (MethodDecoratedWithAttribute3));
			CheckObjectArrayParam (nameof (MethodDecoratedWithAttribute4));
		}

		private void CheckObjectArrayParam (string methodName)
		{
			IList<CustomAttributeData> cdata = CustomAttributeData.GetCustomAttributes (typeof (CustomAttributeDataTest).GetMethod (methodName));
			Assert.AreEqual (1, cdata.Count, $"{methodName}#0");
			
			CustomAttributeTypedArgument arg = cdata [0].ConstructorArguments [0];
			Assert.IsTrue (typeof (IList<CustomAttributeTypedArgument>).IsAssignableFrom (arg.Value.GetType ()), $"{methodName}#1");
			
			IList<CustomAttributeTypedArgument> arr = (IList<CustomAttributeTypedArgument>)arg.Value;
			Assert.AreEqual (2, arr.Count, $"{methodName}#2");
			
			Assert.AreEqual (typeof (Levels), arr [0].ArgumentType, $"{methodName}#3");
			Assert.IsTrue (arr [0].ArgumentType.GetTypeInfo().IsEnum, $"{methodName}#4");
			Assert.AreEqual (0, arr [0].Value, $"{methodName}#5");
			Assert.AreEqual (typeof (int), arr [0].Value.GetType (), $"{methodName}#6");
			
			Assert.AreEqual (typeof (Levels), arr [1].ArgumentType, $"{methodName}#7");
			Assert.IsTrue (arr [1].ArgumentType.GetTypeInfo().IsEnum, $"{methodName}#8");
			Assert.AreEqual (1, arr [1].Value, $"{methodName}#9");
			Assert.AreEqual (typeof (int), arr [1].Value.GetType (), $"{methodName}#10");
		}

		[Test]
		// https://github.com/mono/mono/issues/10951
		public void CheckObjectArrayParams ()
		{
			string methodName = nameof (MethodDecoratedWithAttribute5);
			IList<CustomAttributeData> cdata = CustomAttributeData.GetCustomAttributes (typeof (CustomAttributeDataTest).GetMethod (methodName));
			Assert.AreEqual (1, cdata.Count, $"{methodName}#0");
			Assert.AreEqual (2, cdata [0].ConstructorArguments.Count, $"{methodName}#00");

			CustomAttributeTypedArgument arg = cdata [0].ConstructorArguments [0];
			Assert.IsTrue (typeof (IList<CustomAttributeTypedArgument>).IsAssignableFrom (arg.Value.GetType ()), $"{methodName}#1");
			
			IList<CustomAttributeTypedArgument> arr = (IList<CustomAttributeTypedArgument>)arg.Value;
			Assert.AreEqual (2, arr.Count, $"{methodName}#2");
			
			Assert.AreEqual (typeof (Levels), arr [0].ArgumentType, $"{methodName}#3");
			Assert.IsTrue (arr [0].ArgumentType.GetTypeInfo().IsEnum, $"{methodName}#4");
			Assert.AreEqual (0, arr [0].Value, $"{methodName}#5");
			Assert.AreEqual (typeof (int), arr [0].Value.GetType (), $"{methodName}#6");
			
			Assert.AreEqual (typeof (Levels), arr [1].ArgumentType, $"{methodName}#7");
			Assert.IsTrue (arr [1].ArgumentType.GetTypeInfo().IsEnum, $"{methodName}#8");
			Assert.AreEqual (1, arr [1].Value, $"{methodName}#9");
			Assert.AreEqual (typeof (int), arr [1].Value.GetType (), $"{methodName}#10");

			arg = cdata [0].ConstructorArguments [1];
			Assert.IsTrue (typeof (IList<CustomAttributeTypedArgument>).IsAssignableFrom (arg.Value.GetType ()), $"{methodName}#11");
			
			arr = (IList<CustomAttributeTypedArgument>)arg.Value;
			Assert.AreEqual (2, arr.Count, $"{methodName}#12");
			
			Assert.AreEqual (typeof (Levels), arr [0].ArgumentType, $"{methodName}#13");
			Assert.IsTrue (arr [0].ArgumentType.GetTypeInfo().IsEnum, $"{methodName}#14");
			Assert.AreEqual (2, arr [0].Value, $"{methodName}#15");
			Assert.AreEqual (typeof (int), arr [0].Value.GetType (), $"{methodName}#16");
			
			Assert.AreEqual (typeof (Levels), arr [1].ArgumentType, $"{methodName}#17");
			Assert.IsTrue (arr [1].ArgumentType.GetTypeInfo().IsEnum, $"{methodName}#18");
			Assert.AreEqual (1, arr [1].Value, $"{methodName}#19");
			Assert.AreEqual (typeof (int), arr [1].Value.GetType (), $"{methodName}#20");			
		}		
        
		[Test]
		public void ParameterIncludesPseudoCustomAttributesData ()
		{
			var methodInfo = typeof (CustomAttributeDataTest).GetMethod ("MethodWithParamDecoratedWithPseudoCustomAttributes");
			var paramInfo = methodInfo.GetParameters () [0];
			var customAttributesData = CustomAttributeData.GetCustomAttributes (paramInfo);

			Assert.AreEqual (4, customAttributesData.Count);

			var inAttributeData = customAttributesData [0];
			var outAttributeData = customAttributesData [1];
			var optionalAttributeData = customAttributesData [2];			
			var marshalAsAttributeData = customAttributesData [3];

			var marshalAsAttributeCtorArg = marshalAsAttributeData.ConstructorArguments [0];

			Assert.AreEqual (typeof (InAttribute), inAttributeData.AttributeType);
			Assert.AreEqual (typeof (OutAttribute), outAttributeData.AttributeType);
			Assert.AreEqual (typeof (OptionalAttribute), optionalAttributeData.AttributeType);

			Assert.AreEqual (typeof (MarshalAsAttribute), marshalAsAttributeData.AttributeType);
			Assert.AreEqual (typeof (UnmanagedType), marshalAsAttributeCtorArg.ArgumentType);
			Assert.AreEqual ((int)UnmanagedType.LPStr, marshalAsAttributeCtorArg.Value);
		}

		[Test]
		public void FieldIncludesPseudoCustomAttributesData ()
		{
			var fieldInfo = typeof (CustomAttributeDataTest).GetField ("fieldDecoratedWithPseudoCustomAttributes");
			var customAttributesData = CustomAttributeData.GetCustomAttributes (fieldInfo);

			Assert.AreEqual (2, customAttributesData.Count);

			var nonSerializedAttributeData = customAttributesData [0];
			var marshalAsAttributeData = customAttributesData [1];
			var marshalAsAttributeDataCtorArg = marshalAsAttributeData.ConstructorArguments [0];

			Assert.AreEqual (typeof (NonSerializedAttribute), nonSerializedAttributeData.AttributeType);
			Assert.AreEqual (typeof (MarshalAsAttribute), marshalAsAttributeData.AttributeType);
			Assert.AreEqual (typeof (UnmanagedType), marshalAsAttributeDataCtorArg.ArgumentType);
			Assert.AreEqual ((int)UnmanagedType.LPStr, marshalAsAttributeDataCtorArg.Value);
		}

		[Test]
		public void MethodIncludesMarshalAsAttributeData ()
		{
			var methodInfo = typeof (CustomAttributeDataTest).GetMethod ("MethodWithReturnValueDecoratedWithMarshalAs");
			var paramInfo = (ParameterInfo)methodInfo.ReturnTypeCustomAttributes;
			var customAttributesData = CustomAttributeData.GetCustomAttributes (paramInfo);
			var marshalAsAttributeData = customAttributesData [0];
			var ctorArg = marshalAsAttributeData.ConstructorArguments [0];

			Assert.AreEqual (1, customAttributesData.Count);
			Assert.AreEqual (typeof (MarshalAsAttribute), marshalAsAttributeData.AttributeType);
			Assert.AreEqual (typeof (UnmanagedType), ctorArg.ArgumentType);
			Assert.AreEqual ((int)UnmanagedType.LPStr, ctorArg.Value);
		}

		[Test]
		// https://github.com/mono/mono/issues/10544
		public void MethodIncludesDllImportAttributeData ()
		{
			var mi = typeof (CustomAttributeDataTest).FindMembers (MemberTypes.Method, BindingFlags.Static | BindingFlags.Public, (m, criteria) => m.Name == "readlink", null);
			var data = ((MethodInfo)(mi[0])).CustomAttributes;
			
			Assert.AreEqual (2, data.Count ());

			Assert.AreEqual (typeof (PreserveSigAttribute), data.First ().AttributeType);

			var dllImportAttributeData = data.Last ();
			var ctorArg = dllImportAttributeData.ConstructorArguments [0];

			Assert.AreEqual (typeof (DllImportAttribute), dllImportAttributeData.AttributeType);
			Assert.AreEqual ("libc", ctorArg.Value);
		}

		[Test]
		// https://github.com/mono/mono/issues/10555
		public void CustomAttributeCtor_TakesEnumArg ()
		{
			var method = GetMethod (nameof (MethodDecoratedWithAttribute1));
			var data = method.CustomAttributes;
			var ctorArg = data.First ().ConstructorArguments [0];

			Assert.AreEqual (typeof (Levels), ctorArg.ArgumentType);
			Assert.AreEqual (1, ctorArg.Value);
			Assert.AreEqual (typeof (int), ctorArg.Value.GetType ());

			method = GetMethod (nameof (MethodDecoratedWithAttribute2));
			data = method.CustomAttributes;
			ctorArg = data.First ().ConstructorArguments [0];

			Assert.AreEqual (typeof (Levels), ctorArg.ArgumentType);
			Assert.AreEqual (1, ctorArg.Value);
			Assert.AreEqual (typeof (int), ctorArg.Value.GetType ());			
		}

		private MethodInfo GetMethod (string methodName)
		{
			var mi = typeof (CustomAttributeDataTest).FindMembers (MemberTypes.Method, BindingFlags.Instance | BindingFlags.Public, (m, criteria) => m.Name == methodName, null);
			return (MethodInfo)(mi [0]);
		}
	}
}