forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericExceptionTester.cs
More file actions
329 lines (304 loc) · 12.7 KB
/
GenericExceptionTester.cs
File metadata and controls
329 lines (304 loc) · 12.7 KB
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
319
320
321
322
323
324
325
326
327
328
329
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010-2012 FUJIWARA, Yusuke
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion -- License Terms --
using System;
using System.IO;
#if NETFX_CORE
using System.Linq;
#endif
using System.Linq.Expressions;
#if NETFX_CORE
using System.Reflection;
#endif
#if !NETFX_CORE && !SILVERLIGHT
using System.Runtime.Serialization.Formatters.Binary;
#else
using System.Runtime.Serialization;
#endif // !NETFX_CORE && !SILVERLIGHT
using System.Security;
#if !NETFX_CORE && !SILVERLIGHT
using System.Security.Permissions;
using System.Security.Policy;
#endif // !NETFX_CORE && !SILVERLIGHT
#if !MSTEST
using NUnit.Framework;
#else
using TestFixtureAttribute = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
using TestAttribute = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
using TimeoutAttribute = NUnit.Framework.TimeoutAttribute;
using Assert = NUnit.Framework.Assert;
using Is = NUnit.Framework.Is;
#endif
#if UNITY
using MsgPack.Serialization;
#endif // UNITY
namespace MsgPack
{
/// <summary>
/// Test suite for standard exception constructors, properties, and serialization.
/// </summary>
/// <typeparam name="T">Target exception type.</typeparam>
public sealed class GenericExceptionTester<T>
#if !NETFX_CORE && !SILVERLIGHT
: MarshalByRefObject
#endif // !NETFX_CORE && !SILVERLIGHT
where T : Exception
{
private static readonly Type[] _emptyTypes = new Type[ 0 ];
private static readonly Type[] _messageConstructorParameterTypes = new[] { typeof( string ) };
private static readonly Type[] _innerExceptionConstructorParameterTypes = new[] { typeof( string ), typeof( Exception ) };
private readonly Func<T> _defaultConstructor;
private readonly Func<string, T> _messageConstructor;
private readonly Func<string, Exception, T> _innerExceptionConstructor;
private readonly Exception _constructorException;
public T CreateTargetInstance( string message, Exception inner )
{
return this._innerExceptionConstructor( message, inner );
}
/// <summary>
/// Initializes a new instance of the <see cref="GenericExceptionTester<T>"/> class.
/// </summary>
public GenericExceptionTester()
{
try
{
#if UNITY
this._defaultConstructor = () => ReflectionExtensions.CreateInstancePreservingExceptionType<T>( typeof( T ) );
#elif !NETFX_CORE
this._defaultConstructor = Expression.Lambda<Func<T>>( Expression.New( typeof( T ).GetConstructor( _emptyTypes ) ) ).Compile();
#else
this._defaultConstructor = Expression.Lambda<Func<T>>( Expression.New( typeof( T ).GetTypeInfo().DeclaredConstructors.Single( c => c.GetParameters().Length == 0 ) ) ).Compile();
#endif // if UNITY elif !NETFX_CORE
var message = Expression.Parameter( typeof( string ), "message" );
var innerException = Expression.Parameter( typeof( Exception ), "innerException" );
#if !UNITY
this._messageConstructor =
Expression.Lambda<Func<string, T>>(
#if !NETFX_CORE
Expression.New( typeof( T ).GetConstructor( _messageConstructorParameterTypes ), message ),
#else
Expression.New( typeof( T ).GetTypeInfo().DeclaredConstructors.Single( c => c.GetParameters().Select( p => p.ParameterType ).SequenceEqual( _messageConstructorParameterTypes ) ), message ),
#endif // !NETFX_CORE
message
).Compile();
#else
this._messageConstructor = msg => ReflectionExtensions.CreateInstancePreservingExceptionType<T>( typeof( T ), msg );
#endif // !UNITY
#if !UNITY
this._innerExceptionConstructor =
Expression.Lambda<Func<string, Exception, T>>(
#if !NETFX_CORE
Expression.New( typeof( T ).GetConstructor( _innerExceptionConstructorParameterTypes ), message, innerException ),
#else
Expression.New( typeof( T ).GetTypeInfo().DeclaredConstructors.Single( c => c.GetParameters().Select( p => p.ParameterType ).SequenceEqual( _innerExceptionConstructorParameterTypes ) ), message, innerException ),
#endif // !NETFX_CORE
message,
innerException
).Compile();
#else
this._innerExceptionConstructor = ( msg, inner ) => ReflectionExtensions.CreateInstancePreservingExceptionType<T>( typeof( T ), msg, inner );
#endif // !UNITY
}
catch ( Exception ex )
{
this._constructorException = ex;
}
}
/// <summary>
/// Tests the exception.
/// </summary>
public void TestException()
{
if ( this._constructorException != null )
{
Assert.Fail( this._constructorException.ToString() );
}
this.TestDefaultConstructor();
this.TestMessageConstructor_WithMessage_SetToMessage();
this.TestMessageConstructor_WithNull_SetToDefaultMessage();
this.TestInnerExceptionConstructor_WithMessageAndInnerException_SetToMessageAndInnerException();
this.TestInnerExceptionConstructor_Null_SetToDefaultMessageAndNullInnerException();
#if !NETFX_CORE && !WINDOWS_PHONE && !XAMIOS && !XAMDROID && !UNITY
this.TestSerialization();
#endif // if !NETFX_CORE && !WINDOWS_PHONE && !XAMIOS && !XAMDROID && !UNITY
}
private void TestDefaultConstructor()
{
var target = this._defaultConstructor();
Assert.That( target.Message, Is.Not.Null.And.Not.Empty, "Message should not be null nor empty." );
TestToString( target, typeof( T ).Name + "()" );
}
private void TestMessageConstructor_WithMessage_SetToMessage()
{
var message = Guid.NewGuid().ToString();
var target = this._messageConstructor( message );
Assert.That( target.Message, Is.Not.Null.And.StringContaining( message ), "Message should contain message argument." );
TestToString( target, typeof( T ).Name + "()" );
}
private void TestMessageConstructor_WithNull_SetToDefaultMessage()
{
var defaultInstance = this._defaultConstructor();
var target = this._messageConstructor( null );
Assert.That( target.Message, Is.Not.Null.And.StringContaining( defaultInstance.Message ), "Message should equal to default message." );
TestToString( target, typeof( T ).Name + "()" );
}
private void TestInnerExceptionConstructor_WithMessageAndInnerException_SetToMessageAndInnerException()
{
var message = Guid.NewGuid().ToString();
var innerException = new Exception();
var target = this._innerExceptionConstructor( message, innerException );
Assert.That( target.Message, Is.Not.Null.And.StringContaining( message ), "Message should contain message argument." );
Assert.That( target.InnerException, Is.SameAs( innerException ), "InnerException should contain innerException argument." );
TestToString( target, typeof( T ).Name + "()" );
}
private void TestInnerExceptionConstructor_Null_SetToDefaultMessageAndNullInnerException()
{
var defaultInstance = this._defaultConstructor();
var target = this._innerExceptionConstructor( null, null );
Assert.That( target.Message, Is.Not.Null.And.StringContaining( defaultInstance.Message ), "Message should equal to default message." );
Assert.That( target.InnerException, Is.Null, "InnerException should be null." );
TestToString( target, typeof( T ).Name + "()" );
}
private static void TestToString( T target, string ctor )
{
TestToStringCore( MakeStackTrace( target ), ctor );
}
private static T MakeStackTrace( T target )
{
try
{
throw target;
}
catch ( T ex )
{
return ex;
}
}
private static void TestToStringCore( T target, string ctor )
{
Assert.That( target.ToString(), Is.Not.Null.And.Not.Empty.And.StringContaining( target.Message ).And.StringContaining( target.GetType().FullName ).And.StringContaining( target.StackTrace ), "ToString() should contain Message, Type.FullName, and StackTrace ({0}).", ctor );
if ( target.InnerException != null )
{
Assert.That( target.ToString(), Is.Not.Null.And.StringContaining( target.InnerException.Message ), "ToString() should contain InnerException ({0}).", ctor );
}
}
#if !NETFX_CORE && !WINDOWS_PHONE && !XAMIOS && !XAMDROID && !UNITY
private void TestSerialization()
{
Assert.That( typeof( T ), Is.BinarySerializable );
var innerMessage = Guid.NewGuid().ToString();
var message = Guid.NewGuid().ToString();
var target = this._innerExceptionConstructor( message, new Exception( innerMessage ) );
using ( var buffer = new MemoryStream() )
{
var serializer = new BinaryFormatter();
serializer.Serialize( buffer, target );
buffer.Position = 0;
var deserialized = serializer.Deserialize( buffer ) as T;
Assert.That( deserialized, Is.Not.Null );
Assert.That( deserialized.Message, Is.EqualTo( target.Message ) );
Assert.That( deserialized.InnerException, Is.Not.Null.And.TypeOf( typeof( Exception ) ) );
Assert.That( deserialized.InnerException.Message, Is.EqualTo( target.InnerException.Message ) );
}
}
private void TestSerializationOnPartialTrust()
{
var appDomainSetUp = new AppDomainSetup() { ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase };
var evidence = new Evidence();
#if MONO || NETFX_35
#pragma warning disable 0612
// TODO: patching
// currently, Mono does not declare AddHostEvidence
evidence.AddHost( new Zone( SecurityZone.Internet ) );
#pragma warning restore 0612
var permisions = GetDefaultInternetZoneSandbox();
#else
evidence.AddHostEvidence( new Zone( SecurityZone.Internet ) );
var permisions = SecurityManager.GetStandardSandbox( evidence );
#endif
AppDomain workerDomain = AppDomain.CreateDomain( "PartialTrust", evidence, appDomainSetUp, permisions, GetStrongName( this.GetType() ) );
try
{
var innerMessage = Guid.NewGuid().ToString();
var message = Guid.NewGuid().ToString();
workerDomain.SetData( "MsgPack.GenericExceptionTester.InnerMessage", innerMessage );
workerDomain.SetData( "MsgPack.GenericExceptionTester.Message", message );
workerDomain.SetData( "MsgPack.GenericExceptionTester.Proxy", this );
workerDomain.DoCallBack( TestSerializationOnPartialTrustCore );
var target = workerDomain.GetData( "MsgPack.GenericExceptionTester.Target" ) as T;
Assert.That( target, Is.Not.Null );
Assert.That( target.Message, Is.EqualTo( target.Message ) );
Assert.That( target.InnerException, Is.Not.Null.And.TypeOf( typeof( Exception ) ) );
Assert.That( target.InnerException.Message, Is.EqualTo( target.InnerException.Message ) );
}
finally
{
AppDomain.Unload( workerDomain );
}
}
#if MONO || NETFX_35
private static PermissionSet GetDefaultInternetZoneSandbox()
{
var permissions = new PermissionSet( PermissionState.None );
permissions.AddPermission(
new FileDialogPermission(
FileDialogPermissionAccess.Open
)
);
permissions.AddPermission(
new IsolatedStorageFilePermission( PermissionState.None )
{
UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByUser,
UserQuota = 1024000
}
);
permissions.AddPermission(
new SecurityPermission(
SecurityPermissionFlag.Execution
)
);
permissions.AddPermission(
new UIPermission(
UIPermissionWindow.SafeTopLevelWindows,
UIPermissionClipboard.OwnClipboard
)
);
return permissions;
}
#endif // if MONO || NETFX_35
public static void TestSerializationOnPartialTrustCore()
{
var innerMessage = AppDomain.CurrentDomain.GetData( "MsgPack.GenericExceptionTester.InnerMessage" ) as string;
var message = AppDomain.CurrentDomain.GetData( "MsgPack.GenericExceptionTester.Message" ) as string;
var instance = AppDomain.CurrentDomain.GetData( "MsgPack.GenericExceptionTester.Proxy" ) as GenericExceptionTester<T>;
var target = instance.CreateTargetInstance( message, new Exception( innerMessage ) );
Assert.That( target, Is.Not.Null );
Assert.That( target.Message, Is.EqualTo( target.Message ) );
Assert.That( target.InnerException, Is.Not.Null.And.TypeOf( typeof( Exception ) ) );
Assert.That( target.InnerException.Message, Is.EqualTo( target.InnerException.Message ) );
AppDomain.CurrentDomain.SetData( "MsgPack.GenericExceptionTester.Target", target );
}
private static StrongName GetStrongName( Type type )
{
var assemblyName = type.Assembly.GetName();
return new StrongName( new StrongNamePublicKeyBlob( assemblyName.GetPublicKey() ), assemblyName.Name, assemblyName.Version );
}
#endif // if !NETFX_CORE && !WINDOWS_PHONE && !XAMIOS && !XAMDROID && !UNITY
}
}