|
| 1 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace JwtManagerTests |
| 7 | +{ |
| 8 | + [TestClass] |
| 9 | + public class JwtHeaderTests |
| 10 | + { |
| 11 | + class TestResult |
| 12 | + { |
| 13 | + public JwtManager.Helpers.Algorithm algorithm { get; set; } |
| 14 | + public JwtManager.Helpers.KeySize size { get; set; } |
| 15 | + public string alg { get; set; } |
| 16 | + public string typ { get; set; } |
| 17 | + } |
| 18 | + |
| 19 | + [TestMethod] |
| 20 | + public void ValidCases() |
| 21 | + { |
| 22 | + JwtManager.JwtHeader header = new JwtManager.JwtHeader(); |
| 23 | + |
| 24 | + TestResult[] tests = { |
| 25 | + new TestResult { algorithm = JwtManager.Helpers.Algorithm.RSA, size = JwtManager.Helpers.KeySize.S256, alg = "RS256", typ = "JWT" }, |
| 26 | + new TestResult { algorithm = JwtManager.Helpers.Algorithm.RSA, size = JwtManager.Helpers.KeySize.S384, alg = "RS384", typ = "JWT" }, |
| 27 | + new TestResult { algorithm = JwtManager.Helpers.Algorithm.RSA, size = JwtManager.Helpers.KeySize.S512, alg = "RS512", typ = "JWT" }, |
| 28 | + new TestResult { algorithm = JwtManager.Helpers.Algorithm.HMAC, size = JwtManager.Helpers.KeySize.S256, alg = "HS256", typ = "JWT" }, |
| 29 | + new TestResult { algorithm = JwtManager.Helpers.Algorithm.HMAC, size = JwtManager.Helpers.KeySize.S384, alg = "HS384", typ = "JWT" }, |
| 30 | + new TestResult { algorithm = JwtManager.Helpers.Algorithm.HMAC, size = JwtManager.Helpers.KeySize.S512, alg = "HS512", typ = "JWT" } |
| 31 | + }; |
| 32 | + |
| 33 | + foreach(TestResult test in tests) |
| 34 | + { |
| 35 | + header.Set(test.algorithm, test.size); |
| 36 | + Assert.AreEqual(header.alg, test.alg); |
| 37 | + Assert.AreEqual(header.typ, test.typ); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + [TestMethod] |
| 42 | + public void CheckEP512Exception() |
| 43 | + { |
| 44 | + JwtManager.JwtHeader header = new JwtManager.JwtHeader(); |
| 45 | + Exception e = null; |
| 46 | + |
| 47 | + try |
| 48 | + { |
| 49 | + header.Set(JwtManager.Helpers.Algorithm.RSASSA, JwtManager.Helpers.KeySize.S512); |
| 50 | + } |
| 51 | + catch(Exception ex) |
| 52 | + { |
| 53 | + e = ex; |
| 54 | + } |
| 55 | + |
| 56 | + Assert.IsNotNull(e); |
| 57 | + Assert.AreEqual(e.Message, "Invalid size for algorithm."); |
| 58 | + } |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + public void CheckNonValidOptions() |
| 62 | + { |
| 63 | + JwtManager.JwtHeader header = new JwtManager.JwtHeader(); |
| 64 | + Exception e = null; |
| 65 | + |
| 66 | + try |
| 67 | + { |
| 68 | + header.Set(JwtManager.Helpers.Algorithm.RSASSA, (JwtManager.Helpers.KeySize)555); |
| 69 | + } |
| 70 | + catch (Exception ex) |
| 71 | + { |
| 72 | + e = ex; |
| 73 | + } |
| 74 | + |
| 75 | + Assert.IsNotNull(e); |
| 76 | + Assert.AreEqual(e.Message, "Invalid values for algorithm or size."); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments