// Copyright (C) Stichting Deltares 2025. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using Deltares.Authorization; using NUnit.Framework; namespace AuthorizationTest { [TestFixture] public abstract class AuthorizationTestBase { private const string LicenseNotFoundMessage = "could not checkout/borrow license"; [Test] public void TestInteropDAuthPublicKey() { const string expected = "379d563066715186"; var asm = Assembly.GetAssembly(typeof(DAuthClient)); byte[] pktb = asm.GetName().GetPublicKeyToken(); string pkt = BitConverter.ToString(pktb).Replace("-", "").ToLowerInvariant(); Assert.That(pkt, Is.EqualTo(expected)); } [Test] public void TestLicenseNotFound() { using (var aw = new DAuthClient(false, true)) { Assert.That(aw.CheckOut("NotFound", "1.0"), Is.False); string errorMessage = aw.GetLastErrorMessage(); Assert.That(errorMessage, Does.Contain(LicenseNotFoundMessage)); } } [Test] public void TestVersionSupported() { using (var aw = new DAuthClient(false, true)) { string feature = GetFeature(); foreach (string supportedFeatureVersion in GetSupportedFeatureVersions()) { Assert.That(aw.CheckOut(feature, supportedFeatureVersion), Is.True, $"{feature} - {supportedFeatureVersion} not accepted."); } foreach (string unsupportedFeatureVersion in GetUnsupportedFeatureVersions()) { Assert.That(aw.CheckOut(feature, unsupportedFeatureVersion), Is.False, $"{feature} - {unsupportedFeatureVersion} accepted while it shouldn't."); string errorMessage = aw.GetLastErrorMessage(); Assert.That(errorMessage, Does.Contain(LicenseNotFoundMessage)); } } } [Test] public void CheckOutCheckIn() { using (var aw = new DAuthClient(false, true)) { Assert.That(aw.CheckOut(GetFeature(), GetSupportedFeatureVersions().First()), Is.True); Assert.That(aw.CheckIn(GetFeature()), Is.True); } } [Test] public void TestGetVendor() { using (var aw = new DAuthClient(false, true)) { var awres = ""; Assert.That(aw.GetVendor(GetFeature(), ref awres), Is.True); Assert.That(awres, Is.EqualTo("Deltares")); } } [Test] public void TestDistributor() { using (var aw = new DAuthClient(false, true)) { var awres = ""; Assert.That(aw.GetDistributor(GetFeature(), ref awres), Is.True); Assert.That(awres, Is.EqualTo("Deltares")); } } [Test] public void TestGetNotice() { using (var aw = new DAuthClient(false, true)) { String awres = null; Assert.That(aw.GetNotice(GetFeature(), ref awres), Is.True); Assert.That(awres, Is.EqualTo("This product is licensed to Stichting Deltares.")); } } [Test] public void TestGetUserName() { using (var aw = new DAuthClient(false, true)) { String awres = null; Assert.That(aw.GetUsername(GetFeature(), ref awres), Is.True); Assert.That(awres, Is.EqualTo("Stichting Deltares")); } } [Test] public void TestGetCount() { using (var aw = new DAuthClient(false, true)) { int firstCount = -1; aw.GetCount(GetFeature(), ref firstCount); Assert.That(aw.CheckOut(GetFeature(), GetSupportedFeatureVersions().First()), Is.True); int secondCount = -1; aw.GetCount(GetFeature(), ref secondCount); Assert.That(secondCount, Is.EqualTo(firstCount + 1)); Assert.That(aw.CheckIn(GetFeature()), Is.True); aw.GetCount(GetFeature(), ref secondCount); Assert.That(secondCount, Is.EqualTo(firstCount)); } } [Test] public void TestGetExpiration() { using (var aw = new DAuthClient(false, true)) { var expiration = new DateTime(1900, 1, 1, 0, 0, 0); aw.GetExpiration(GetFeature(), ref expiration); Assert.That(expiration.Year, Is.EqualTo(2000)); } } [Test] public void TestGetCurrentUsers() { using (var aw = new DAuthClient(false, true)) { String[] currentUsers = null; string feature = GetFeature(); Assert.That(aw.CheckOut(feature, GetSupportedFeatureVersions().First()), Is.True); Assert.That(aw.GetCurrentUsers(feature, ref currentUsers), Is.True); Assert.That(currentUsers, Is.Not.Empty); Assert.That(currentUsers.Any(u => u == Environment.UserName), Is.True); Assert.That(aw.CheckIn(feature), Is.True); } } [Test] public void TestUsedLicenses() { using (var aw = new DAuthClient(false, true)) { int count = -1; string feature = GetFeature(); Assert.That(aw.GetUsedLicenses(feature, Environment.UserName, ref count), Is.True); Assert.That(count, Is.EqualTo(0)); Assert.That(aw.CheckOut(feature, GetSupportedFeatureVersions().First()), Is.True); Assert.That(aw.GetUsedLicenses(feature, Environment.UserName, ref count), Is.True); Assert.That(count, Is.EqualTo(1)); Assert.That(aw.CheckIn(feature), Is.True); } } [Test] public void TestAvailableLicenses() { using (var aw = new DAuthClient(false, true)) { var count = 0; Assert.That(aw.GetAvailableLicenses(GetFeature(), ref count), Is.True); Assert.That(count, Is.EqualTo(500)); } } [Test] public void TestGetAvailableFeatures() { using (var aw = new DAuthClient(false, true)) { String[] features = null; Assert.That(aw.GetAvailableFeatures(ref features), Is.True); Assert.That(features, Is.Not.Empty); string feature = GetFeature(); Assert.That(features.Any(f => f == feature), Is.True); } } [Test] public void TestIsServer() { using (var aw = new DAuthClient(false, true)) { var isServer = false; Assert.That(aw.IsServer(GetFeature(), ref isServer), Is.True); Assert.That(isServer, Is.True); } } [Test] [Category("Performance")] public void PerformanceTest() { // average 3 calls per second using (var aw = new DAuthClient(false, true)) { string feature = GetFeature(); string version = GetSupportedFeatureVersions().First(); var stopwatch = new Stopwatch(); stopwatch.Start(); for (var i = 0; i < 10; i++) { Assert.That(aw.CheckOut(feature, version), Is.True); Assert.That(aw.CheckIn(feature), Is.True); Console.Error.WriteLine("Round :" + i); } stopwatch.Stop(); Assert.That(stopwatch.Elapsed, Is.LessThan(new TimeSpan(0, 0, 0, 3))); } } /// /// Gets the feature name for that exists. /// public abstract string GetFeature(); /// /// Gets the version for that exists for feature returned /// by . /// public abstract IEnumerable GetSupportedFeatureVersions(); /// /// Gets the version for that do not exists for feature /// returned by . /// public abstract IEnumerable GetUnsupportedFeatureVersions(); } }