|
| 1 | +/* |
| 2 | + * Copyright 2017 Systemic Pty Ltd |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 18 | +using Sif.Framework.Model.Authentication; |
| 19 | +using System; |
| 20 | + |
| 21 | +namespace Sif.Framework.Service.Authentication |
| 22 | +{ |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Unit tests for AuthorisationTokenService. |
| 26 | + /// </summary> |
| 27 | + [TestClass] |
| 28 | + public class AuthorisationTokenServiceTest |
| 29 | + { |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Use ClassInitialize to run code before running the first test in the class. |
| 33 | + /// </summary> |
| 34 | + /// <param name="testContext">Context information for the unit test.</param> |
| 35 | + [ClassInitialize()] |
| 36 | + public static void ClassInitialize(TestContext testContext) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Use ClassCleanup to run code after all tests in a class have run. |
| 42 | + /// </summary> |
| 43 | + [ClassCleanup()] |
| 44 | + public static void ClassCleanup() |
| 45 | + { |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Use TestInitialize to run code before running each test. |
| 50 | + /// </summary> |
| 51 | + [TestInitialize()] |
| 52 | + public void TestInitialize() |
| 53 | + { |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Use TestCleanup to run code after each test has run. |
| 58 | + /// </summary> |
| 59 | + [TestCleanup()] |
| 60 | + public void TestCleanup() |
| 61 | + { |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Delegate method for retrieving a shared secret. |
| 66 | + /// </summary> |
| 67 | + /// <param name="token">Token associated with the shared secret.</param> |
| 68 | + /// <returns>Shared secret.</returns> |
| 69 | + string SharedSecret(string token) |
| 70 | + { |
| 71 | + return "guest"; |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Authentication test using a Basic authorisation token. |
| 76 | + /// </summary> |
| 77 | + [TestMethod] |
| 78 | + public void BasicAuthorisationTest() |
| 79 | + { |
| 80 | + IAuthorisationTokenService service = new BasicAuthorisationTokenService(); |
| 81 | + AuthorisationToken authorisationToken = service.Generate("new", "guest"); |
| 82 | + Console.WriteLine("Authorisation token is " + authorisationToken.Token + "."); |
| 83 | + Console.WriteLine("Generated UTC ISO 8601 date is " + authorisationToken.Timestamp + "."); |
| 84 | + GetSharedSecret sharedSecret = SharedSecret; |
| 85 | + string sessionToken; |
| 86 | + bool authorised = service.Verify(authorisationToken, sharedSecret, out sessionToken); |
| 87 | + Assert.AreEqual(sessionToken, "new"); |
| 88 | + Assert.IsTrue(authorised); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Authentication test using a HMAC-SHA256 authorisation token. |
| 93 | + /// </summary> |
| 94 | + [TestMethod] |
| 95 | + public void HMACSHA256AuthorisationTest() |
| 96 | + { |
| 97 | + IAuthorisationTokenService service = new HmacShaAuthorisationTokenService(); |
| 98 | + AuthorisationToken authorisationToken = service.Generate("new", "guest"); |
| 99 | + Console.WriteLine("Authorisation token is " + authorisationToken.Token + "."); |
| 100 | + Console.WriteLine("Generated UTC ISO 8601 date is " + authorisationToken.Timestamp + "."); |
| 101 | + GetSharedSecret sharedSecret = SharedSecret; |
| 102 | + string sessionToken; |
| 103 | + bool authorised = service.Verify(authorisationToken, sharedSecret, out sessionToken); |
| 104 | + Assert.AreEqual(sessionToken, "new"); |
| 105 | + Assert.IsTrue(authorised); |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments