-
Notifications
You must be signed in to change notification settings - Fork 401
/
ExpectedException.cs
389 lines (318 loc) · 20 KB
/
ExpectedException.cs
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text.Json;
using System.Xml;
using Microsoft.IdentityModel.Tokens;
using Microsoft.IdentityModel.Tokens.Saml;
using Microsoft.IdentityModel.Tokens.Saml2;
namespace Microsoft.IdentityModel.TestUtils
{
/// <summary>
/// When a test case throws an exception, this class helps to determine if the exception is as expected.
/// Really just a helper for wrapping things.
/// </summary>
public class ExpectedException
{
public ExpectedException(Type typeExpected = null, string substringExpected = null, Type innerTypeExpected = null, bool ignoreInnerException = false, Dictionary<string, object> propertiesExpected = null)
{
IgnoreInnerException = ignoreInnerException;
InnerTypeExpected = innerTypeExpected;
SubstringExpected = substringExpected;
TypeExpected = typeExpected;
if (propertiesExpected != null)
PropertiesExpected = propertiesExpected;
}
public static bool DefaultVerbose { get; set; }
public static ExpectedException ArgumentException(string substringExpected = null, Type inner = null)
{
return new ExpectedException(typeof(ArgumentException), substringExpected, inner);
}
public static ExpectedException ArgumentOutOfRangeException(string substringExpected = null, Type inner = null)
{
return new ExpectedException(typeof(ArgumentOutOfRangeException), substringExpected, inner);
}
public static ExpectedException ArgumentNullException(string substringExpected = null, Type inner = null)
{
return new ExpectedException(typeof(ArgumentNullException), substringExpected, inner);
}
public static ExpectedException CryptographicException(string substringExpected = null, Type inner = null, bool ignoreInnerException = false)
{
return new ExpectedException(typeof(CryptographicException), substringExpected, inner, ignoreInnerException);
}
public static ExpectedException SecurityTokenDecryptionFailedException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenDecryptionFailedException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenMalformedTokenException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenMalformedException), substringExpected, innerTypeExpected);
}
public static ExpectedException InvalidOperationException(string substringExpected = null, Type inner = null, string contains = null)
{
return new ExpectedException(typeof(InvalidOperationException), substringExpected, inner);
}
public static ExpectedException IOException(string substringExpected = null, Type inner = null, string contains = null)
{
return new ExpectedException(typeof(IOException), substringExpected, inner);
}
public static ExpectedException XmlException(string substringExpected = null, Type inner = null, string contains = null)
{
return new ExpectedException(typeof(XmlException), substringExpected, inner);
}
public static ExpectedException SamlSecurityTokenReadException(string substringExpected = null, Type inner = null, string contains = null)
{
return new ExpectedException(typeof(SamlSecurityTokenReadException), substringExpected, inner);
}
public static ExpectedException Saml2SecurityTokenReadException(string substringExpected = null, Type inner = null, string contains = null)
{
return new ExpectedException(typeof(Saml2SecurityTokenReadException), substringExpected, inner);
}
public static ExpectedException NoExceptionExpected
{
get { return new ExpectedException(); }
}
public static ExpectedException NotSupportedException(string substringExpected = null, Type inner = null, string contains = null)
{
return new ExpectedException(typeof(NotSupportedException), substringExpected, inner);
}
public static ExpectedException ObjectDisposedException
{
get
{
return new ExpectedException(typeof(ObjectDisposedException));
}
}
public void ProcessException(Exception exception, CompareContext context)
{
ProcessException(exception, context.Diffs);
}
public void ProcessException(Exception exception, List<string> errors = null)
{
if (TypeExpected == null && InnerTypeExpected != null)
{
HandleError("(TypeExpected == null && InnerTypeExpected != null. TypeExpected == null && InnerTypeExpected != null.", errors);
return;
}
if (TypeExpected == null)
{
HandleError("exception != null, expectedException.TypeExpected == null.\nexception: " + exception, errors);
return;
}
if (exception == null)
{
HandleError("exception == null, expectedException.TypeExpected != null.\nexpectedException.TypeExpected: " + TypeExpected, errors);
return;
}
if (!IgnoreExceptionType)
{
if (exception.GetType() != TypeExpected)
{
HandleError("exception.GetType() != expectedException.TypeExpected:\nexception.GetType(): " + exception.GetType() + "\nexpectedException.TypeExpected: " + TypeExpected, errors);
return;
}
if (!string.IsNullOrWhiteSpace(SubstringExpected) && !exception.Message.Contains(SubstringExpected))
{
HandleError($"!exception.Message.Contains('{SubstringExpected}').\nexception.Message: {exception.Message} \nexpectedException.SubstringExpected: {SubstringExpected}", errors);
return;
}
if (exception.InnerException != null && InnerTypeExpected == null && !IgnoreInnerException)
{
HandleError("exception.InnerException != null && expectedException.InnerTypeExpected == null && !IgnoreInnerException.\nexception.InnerException: " + exception.InnerException, errors);
return;
}
if (exception.InnerException == null && InnerTypeExpected != null && !IgnoreInnerException)
{
HandleError("exception.InnerException == null && expectedException.InnerTypeExpected != null && !IgnoreInnerException.\nexpectedException.InnerTypeExpected: " + InnerTypeExpected, errors);
return;
}
if ((InnerTypeExpected != null) && (exception.InnerException.GetType() != InnerTypeExpected) && !IgnoreInnerException)
{
HandleError("exception.InnerException != expectedException.InnerTypeExpected." + "\nexception.InnerException: '" + exception.InnerException + "\nInnerTypeExpected: " + InnerTypeExpected, errors);
}
if (!string.IsNullOrWhiteSpace(InnerSubstringExpected) && !exception.InnerException.Message.Contains(InnerSubstringExpected))
{
HandleError($"!InnerException.Message.Contains('{InnerSubstringExpected}').\nInnerException.Message: {exception.InnerException.Message} \nexpectedException.InnerSubstringExpected: {InnerSubstringExpected}", errors);
return;
}
}
if (PropertiesExpected != null && PropertiesExpected.Count > 0)
{
foreach (KeyValuePair<string, object> property in PropertiesExpected)
{
PropertyInfo propertyInfo = TypeExpected.GetProperty(property.Key);
if (propertyInfo == null)
{
HandleError("exception type " + TypeExpected + " does not have expected property " + property.Key, errors);
}
object runtimeValue = propertyInfo.GetValue(exception);
bool expectedTypeIsNullable = propertyInfo.PropertyType.GetTypeInfo().IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
Type expectedTypeNonNullable = expectedTypeIsNullable ? propertyInfo.PropertyType.GetGenericArguments()[0] : propertyInfo.PropertyType;
if (runtimeValue != null && runtimeValue.GetType() != expectedTypeNonNullable && !expectedTypeNonNullable.IsAssignableFrom(runtimeValue.GetType()))
{
HandleError("exception type " + TypeExpected + " does not match the expected property " + property.Key + " type.\nexpected type: " + expectedTypeNonNullable + ", actual type: " + runtimeValue.GetType(), errors);
}
if (runtimeValue != property.Value &&
((runtimeValue != null && !runtimeValue.Equals(property.Value)) ||
(property.Value != null && !property.Value.Equals(runtimeValue))))
{
HandleError("exception type " + TypeExpected + " doesn't have the expected property value " + property.Key + " value.\nexpected value: " + property.Value + ", actual value: " + runtimeValue, errors);
}
}
}
if (DefaultVerbose || Verbose)
Console.WriteLine(Environment.NewLine + "Exception displayed to user: " + Environment.NewLine + Environment.NewLine + exception);
}
public void ProcessNoException(List<string> errors = null)
{
if (TypeExpected != null)
{
if (errors != null)
errors.Add("Inside ProcessNoException: expectedException.TypeExpected != null: " + TypeExpected);
else
throw new TestException("Inside ProcessNoException: expectedException.TypeExpected != null: '" + TypeExpected);
}
}
public void ProcessNoException(CompareContext context)
{
if (TypeExpected != null)
context.Diffs.Add("expectedException.TypeExpected != null: " + TypeExpected);
}
private static void HandleError(string error, List<string> errors)
{
if (errors != null)
errors.Add(error);
else
throw new TestException($"List<string> errors == null, error in test: {error}.");
}
public static ExpectedException SecurityTokenArgumentNullException(string substringExpected = null, Type inner = null)
{
return new ExpectedException(typeof(SecurityTokenArgumentNullException), substringExpected, inner);
}
public static ExpectedException SecurityTokenEncryptionKeyNotFoundException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenEncryptionKeyNotFoundException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenUnableToValidateException(string substringExpected = null, Type innerTypeExpected = null)
{
#pragma warning disable CS0618 // Type or member is obsolete
return new ExpectedException(typeof(SecurityTokenUnableToValidateException), substringExpected, innerTypeExpected);
#pragma warning restore CS0618 // Type or member is obsolete
}
public static ExpectedException SecurityTokenEncryptionFailedException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenEncryptionFailedException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenException(string substringExpected = null, Type innertypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenException), substringExpected, innertypeExpected);
}
public static ExpectedException SecurityTokenExpiredException(string substringExpected = null, Type innerTypeExpected = null, Dictionary<string, object> propertiesExpected = null)
{
return new ExpectedException(typeof(SecurityTokenExpiredException), substringExpected, innerTypeExpected, propertiesExpected: propertiesExpected);
}
public static ExpectedException SecurityTokenInvalidAudienceException(string substringExpected = null, Type innerTypeExpected = null, Dictionary<string, object> propertiesExpected = null)
{
return new ExpectedException(typeof(SecurityTokenInvalidAudienceException), substringExpected, innerTypeExpected, propertiesExpected: propertiesExpected);
}
public static ExpectedException SecurityTokenInvalidIssuerException(string substringExpected = null, Type innerTypeExpected = null, Dictionary<string, object> propertiesExpected = null)
{
return new ExpectedException(typeof(SecurityTokenInvalidIssuerException), substringExpected, innerTypeExpected, propertiesExpected: propertiesExpected);
}
public static ExpectedException SecurityTokenInvalidCloudInstanceException(string substringExpected = null, Type innerTypeExpected = null, Dictionary<string, object> propertiesExpected = null)
{
return new ExpectedException(typeof(SecurityTokenInvalidCloudInstanceException), substringExpected, innerTypeExpected, propertiesExpected: propertiesExpected);
}
public static ExpectedException SecurityTokenKeyWrapException(string substringExpected = null, Type innerTypeExpected = null, Dictionary<string, object> propertiesExpected = null)
{
return new ExpectedException(typeof(SecurityTokenKeyWrapException), substringExpected, innerTypeExpected, propertiesExpected: propertiesExpected);
}
public static ExpectedException SecurityTokenInvalidAlgorithmException(string substringExpected = null, Type innerTypeExpected = null, Dictionary<string, object> propertiesExpected = null)
{
return new ExpectedException(typeof(SecurityTokenInvalidAlgorithmException), substringExpected, innerTypeExpected, propertiesExpected: propertiesExpected);
}
public static ExpectedException SecurityTokenInvalidLifetimeException(string substringExpected = null, Type innerTypeExpected = null, Dictionary<string, object> propertiesExpected = null)
{
return new ExpectedException(typeof(SecurityTokenInvalidLifetimeException), substringExpected, innerTypeExpected, propertiesExpected: propertiesExpected);
}
public static ExpectedException SecurityTokenInvalidSignatureException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenInvalidSignatureException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenInvalidTypeException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenInvalidTypeException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenNoExpirationException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenNoExpirationException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenNotYetValidException(string substringExpected = null, Type innerTypeExpected = null, Dictionary<string, object> propertiesExpected = null)
{
return new ExpectedException(typeof(SecurityTokenNotYetValidException), substringExpected, innerTypeExpected, propertiesExpected: propertiesExpected);
}
public static ExpectedException SecurityTokenReplayAddFailed(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenReplayAddFailedException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenReplayDetected(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenReplayDetectedException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenSignatureKeyNotFoundException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenSignatureKeyNotFoundException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenValidationException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenValidationException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenInvalidSigningKeyException(string substringExpected = null, Type innerTypeExpected = null, Dictionary<string, object> propertiesExpected = null)
{
return new ExpectedException(typeof(SecurityTokenInvalidSigningKeyException), substringExpected, innerTypeExpected, propertiesExpected: propertiesExpected);
}
public static ExpectedException KeyWrapException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenKeyWrapException), substringExpected, innerTypeExpected);
}
public static ExpectedException JsonException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(JsonException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenDecompressionFailedException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenDecompressionFailedException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenMalformedException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenMalformedException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenReplayDetectedException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenReplayDetectedException), substringExpected, innerTypeExpected);
}
public static ExpectedException SecurityTokenReplayAddFailedException(string substringExpected = null, Type innerTypeExpected = null)
{
return new ExpectedException(typeof(SecurityTokenReplayAddFailedException), substringExpected, innerTypeExpected);
}
public bool IgnoreExceptionType { get; set; }
public bool IgnoreInnerException { get; set; }
public Type InnerTypeExpected { get; set; }
public Dictionary<string, object> PropertiesExpected { get; set; } = new Dictionary<string, object>();
public string SubstringExpected { get; set; }
public string InnerSubstringExpected { get; set; }
public override string ToString()
{
if (TypeExpected == null)
return $"NoExceptionExpected";
else
return $"{TypeExpected}, Substring: {SubstringExpected}";
}
public Type TypeExpected { get; set; }
public bool Verbose { get; set; }
}
}