8
8
namespace JwtManagerTests
9
9
{
10
10
[ TestClass ]
11
- public class RsJwtTests
11
+ public abstract class RsJwtTests
12
12
{
13
- #region Keys
13
+ #region Private Members
14
14
private static string PrivateKey = string . Empty ;
15
15
private static string PublicKey = string . Empty ;
16
+
16
17
private static string KeySize = string . Empty ;
17
18
private static string OpenSslBinPath = string . Empty ;
19
+
18
20
private static string CurrentPath = string . Empty ;
19
21
#endregion
20
22
21
23
#region Setup Methods
22
24
[ ClassInitialize ]
23
- public static void ClassInit ( TestContext context )
25
+ public static void BaseClassInitialize ( TestContext context )
24
26
{
25
- KeySize = ConfigurationManager . OpenExeConfiguration ( Assembly . GetExecutingAssembly ( ) . Location ) . AppSettings . Settings [ "KeySize" ] . Value ;
26
- OpenSslBinPath = ConfigurationManager . OpenExeConfiguration ( Assembly . GetExecutingAssembly ( ) . Location ) . AppSettings . Settings [ "OpenSslBinPath" ] . Value ;
27
+ AppSettingsSection AppSettings = ConfigurationManager . OpenExeConfiguration ( Assembly . GetExecutingAssembly ( ) . Location ) . AppSettings ;
28
+
29
+ KeySize = AppSettings . Settings [ "KeySize" ] . Value ;
30
+ OpenSslBinPath = AppSettings . Settings [ "OpenSslBinPath" ] . Value ;
27
31
28
32
CurrentPath = Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ;
29
33
string commands = string . Format ( @"{0}openssl genrsa -out private{1}.key {1}
@@ -50,13 +54,13 @@ public static void ClassInit(TestContext context)
50
54
}
51
55
52
56
[ TestInitialize ]
53
- public void Initialize ( ) { }
57
+ public void BaseTestInitialize ( ) { }
54
58
55
59
[ TestCleanup ]
56
- public void Cleanup ( ) { }
60
+ public void BaseTestCleanup ( ) { }
57
61
58
62
[ ClassCleanup ]
59
- public static void ClassCleanup ( )
63
+ public static void BaseClassCleanup ( )
60
64
{
61
65
File . Delete ( string . Format ( "{0}\\ private{1}.key" , CurrentPath , KeySize ) ) ;
62
66
File . Delete ( string . Format ( "{0}\\ public{1}.pem" , CurrentPath , KeySize ) ) ;
@@ -77,6 +81,7 @@ public void SignData()
77
81
{
78
82
JwtManager . RsJwt jwt = new JwtManager . RsJwt
79
83
{
84
+ KeySize = HashKeySize ( ) ,
80
85
PrivateKey = PrivateKey
81
86
} ;
82
87
@@ -92,13 +97,14 @@ public void ValidateData()
92
97
string data = "{a:1,b:2}" ;
93
98
JwtManager . RsJwt signJwt = new JwtManager . RsJwt
94
99
{
100
+ KeySize = HashKeySize ( ) ,
95
101
PrivateKey = PrivateKey
96
102
} ;
97
103
string signedData = signJwt . Sign ( data ) ;
98
104
99
105
JwtManager . RsJwt jwt = new JwtManager . RsJwt
100
106
{
101
- PrivateKey = PrivateKey ,
107
+ KeySize = HashKeySize ( ) ,
102
108
PublicKey = PublicKey
103
109
} ;
104
110
string validatedData = jwt . Validate ( signedData ) ;
@@ -112,6 +118,7 @@ public void ValidateInvalidData()
112
118
Exception e = null ;
113
119
JwtManager . RsJwt jwt = new JwtManager . RsJwt
114
120
{
121
+ KeySize = HashKeySize ( ) ,
115
122
PublicKey = PublicKey
116
123
} ;
117
124
@@ -136,6 +143,7 @@ public void SignInvalidPrivateKey()
136
143
Exception e = null ;
137
144
JwtManager . RsJwt jwt = new JwtManager . RsJwt
138
145
{
146
+ KeySize = HashKeySize ( ) ,
139
147
PrivateKey = PrivateKey + "a"
140
148
} ;
141
149
@@ -159,6 +167,7 @@ public void ValidateInvalidPublicKey()
159
167
Exception e = null ;
160
168
JwtManager . RsJwt jwt = new JwtManager . RsJwt
161
169
{
170
+ KeySize = HashKeySize ( ) ,
162
171
PublicKey = PublicKey + "a"
163
172
} ;
164
173
@@ -182,10 +191,12 @@ public void ValidateOtherPublicKey1()
182
191
Exception e = null ;
183
192
JwtManager . RsJwt sJwt = new JwtManager . RsJwt
184
193
{
194
+ KeySize = HashKeySize ( ) ,
185
195
PrivateKey = PrivateKey
186
196
} ;
187
197
JwtManager . RsJwt vJwt = new JwtManager . RsJwt
188
198
{
199
+ KeySize = HashKeySize ( ) ,
189
200
PrivateKey = PrivateKey
190
201
} ;
191
202
@@ -210,6 +221,7 @@ public void ValidateOtherPublicKey2()
210
221
Exception e = null ;
211
222
JwtManager . RsJwt sJwt = new JwtManager . RsJwt
212
223
{
224
+ KeySize = HashKeySize ( ) ,
213
225
PublicKey = PublicKey
214
226
} ;
215
227
@@ -225,5 +237,168 @@ public void ValidateOtherPublicKey2()
225
237
226
238
Assert . IsNotNull ( e , "An exception should be thrown" ) ;
227
239
}
240
+
241
+ [ TestMethod ]
242
+ public void SignDataWithInvalidHashAlgorithm ( )
243
+ {
244
+ Exception e = null ;
245
+ JwtManager . RsJwt jwt = new JwtManager . RsJwt
246
+ {
247
+ KeySize = 555 ,
248
+ PrivateKey = PrivateKey
249
+ } ;
250
+
251
+ try
252
+ {
253
+ string data = "{a:1,b:2}" ;
254
+ string signedData = jwt . Sign ( data ) ;
255
+ }
256
+ catch ( Exception ex )
257
+ {
258
+ e = ex ;
259
+ }
260
+
261
+
262
+ Assert . IsNotNull ( e , "An exception should be thrown" ) ;
263
+ Assert . AreEqual ( e . Message , "Signer SHA555WITHRSA not recognised." ) ;
264
+ }
265
+
266
+ [ TestMethod ]
267
+ public void ValidateWithInvalidHashAlgorithm ( )
268
+ {
269
+ Exception e = null ;
270
+ JwtManager . RsJwt sJwt = new JwtManager . RsJwt
271
+ {
272
+ KeySize = HashKeySize ( ) ,
273
+ PrivateKey = PrivateKey
274
+ } ;
275
+ JwtManager . RsJwt vJwt = new JwtManager . RsJwt
276
+ {
277
+ KeySize = 555 ,
278
+ PublicKey = PublicKey
279
+ } ;
280
+
281
+ try
282
+ {
283
+ string data = "{a:1,b:2}" ;
284
+ string signedData = sJwt . Sign ( data ) ;
285
+ Assert . IsNull ( e , "An exception should not be thrown here" ) ;
286
+ string validatedData = vJwt . Validate ( signedData ) ;
287
+ }
288
+ catch ( Exception ex )
289
+ {
290
+ e = ex ;
291
+ }
292
+
293
+ Assert . IsNotNull ( e , "An exception should be thrown" ) ;
294
+ Assert . AreEqual ( e . Message , "Given key size is not valid." ) ;
295
+ }
296
+
297
+ protected abstract int HashKeySize ( ) ;
298
+ }
299
+
300
+ [ TestClass ]
301
+ public class Rs256Tests : RsJwtTests
302
+ {
303
+ #region Setup Methods
304
+ [ ClassInitialize ]
305
+ public static void ClassInit ( TestContext context )
306
+ {
307
+ BaseClassInitialize ( context ) ;
308
+ }
309
+
310
+ [ TestInitialize ]
311
+ public void Initialize ( )
312
+ {
313
+ BaseTestInitialize ( ) ;
314
+ }
315
+
316
+ [ TestCleanup ]
317
+ public void Cleanup ( )
318
+ {
319
+ BaseTestCleanup ( ) ;
320
+ }
321
+
322
+ [ ClassCleanup ]
323
+ public static void ClassCleanup ( )
324
+ {
325
+ BaseClassCleanup ( ) ;
326
+ }
327
+ #endregion
328
+
329
+ protected override int HashKeySize ( )
330
+ {
331
+ return 256 ;
332
+ }
333
+ }
334
+
335
+ [ TestClass ]
336
+ public class Rs384Tests : RsJwtTests
337
+ {
338
+ #region Setup Methods
339
+ [ ClassInitialize ]
340
+ public static void ClassInit ( TestContext context )
341
+ {
342
+ BaseClassInitialize ( context ) ;
343
+ }
344
+
345
+ [ TestInitialize ]
346
+ public void Initialize ( )
347
+ {
348
+ BaseTestInitialize ( ) ;
349
+ }
350
+
351
+ [ TestCleanup ]
352
+ public void Cleanup ( )
353
+ {
354
+ BaseTestCleanup ( ) ;
355
+ }
356
+
357
+ [ ClassCleanup ]
358
+ public static void ClassCleanup ( )
359
+ {
360
+ BaseClassCleanup ( ) ;
361
+ }
362
+ #endregion
363
+
364
+ protected override int HashKeySize ( )
365
+ {
366
+ return 384 ;
367
+ }
368
+ }
369
+
370
+ [ TestClass ]
371
+ public class Rs512Tests : RsJwtTests
372
+ {
373
+ #region Setup Methods
374
+ [ ClassInitialize ]
375
+ public static void ClassInit ( TestContext context )
376
+ {
377
+ BaseClassInitialize ( context ) ;
378
+ }
379
+
380
+ [ TestInitialize ]
381
+ public void Initialize ( )
382
+ {
383
+ BaseTestInitialize ( ) ;
384
+ }
385
+
386
+ [ TestCleanup ]
387
+ public void Cleanup ( )
388
+ {
389
+ BaseTestCleanup ( ) ;
390
+ }
391
+
392
+ [ ClassCleanup ]
393
+ public static void ClassCleanup ( )
394
+ {
395
+ BaseClassCleanup ( ) ;
396
+ }
397
+ #endregion
398
+
399
+ protected override int HashKeySize ( )
400
+ {
401
+ return 512 ;
402
+ }
228
403
}
229
404
}
0 commit comments