using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Deltares.Authorization;
using Deltares.Standard.TestUtils;
using NUnit.Framework;
namespace AuthorizationTest
{
[TestFixture]
public abstract class AuthorizationTestBase
{
///
/// 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();
[Test]
public void TestInteropDAuthPublicKey()
{
const string expected = "379d563066715186";
var asm = Assembly.GetAssembly(typeof (DAuthClient));
var pktb = asm.GetName().GetPublicKeyToken();
var pkt = BitConverter.ToString(pktb).Replace("-", "").ToLowerInvariant();
Assert.AreEqual(expected, pkt);
}
[Test]
public void TestLicenseNotFound()
{
using (var aw = new DAuthClient(false, true))
{
Assert.IsFalse(aw.CheckOut("NotFound", "1.0"));
Assert.IsTrue(aw.GetLastErrorMessage().Contains("could not borrow license"));
}
}
[Test]
public void TestVersionSupported()
{
using (var aw = new DAuthClient(false, true))
{
var feature = GetFeature();
foreach (var supportedFeatureVersion in GetSupportedFeatureVersions())
{
Assert.IsTrue(aw.CheckOut(feature, supportedFeatureVersion),
string.Format("{0} - {1} not accepted.", feature, supportedFeatureVersion));
}
foreach (var unsupportedFeatureVersion in GetUnsupportedFeatureVersions())
{
Assert.IsFalse(aw.CheckOut(feature, unsupportedFeatureVersion),
string.Format("{0} - {1} accepted while it shouldn't.", feature, unsupportedFeatureVersion));
Assert.IsTrue(aw.GetLastErrorMessage().Contains("could not borrow license"));
}
}
}
[Test]
public void CheckOutCheckIn()
{
using (var aw = new DAuthClient(false, true))
{
Assert.IsTrue(aw.CheckOut(GetFeature(), GetSupportedFeatureVersions().First()));
Assert.IsTrue(aw.CheckIn(GetFeature()));
}
}
[Test]
public void TestGetVendor()
{
using (var aw = new DAuthClient(false, true))
{
String awres = "";
Assert.IsTrue(aw.GetVendor(GetFeature(), ref awres));
Assert.AreEqual("Deltares", awres);
}
}
[Test]
public void TestDistributor()
{
using (var aw = new DAuthClient(false, true))
{
String awres = "";
Assert.IsTrue(aw.GetDistributor(GetFeature(), ref awres));
Assert.AreEqual("Deltares", awres);
}
}
[Test]
public void TestGetNotice()
{
using (var aw = new DAuthClient(false, true))
{
String awres = null;
Assert.IsTrue(aw.GetNotice(GetFeature(), ref awres));
Assert.AreEqual("This product is licensed to Stichting Deltares.", awres);
}
}
[Test]
public void TestGetUserName()
{
using (var aw = new DAuthClient(false, true))
{
String awres = null;
Assert.IsTrue(aw.GetUsername(GetFeature(), ref awres));
Assert.AreEqual("Stichting Deltares", awres);
}
}
[Test]
public void TestGetCount()
{
using (var aw = new DAuthClient(false, true))
{
int firstCount = -1;
aw.GetCount(GetFeature(), ref firstCount);
Assert.IsTrue(aw.CheckOut(GetFeature(), GetSupportedFeatureVersions().First()));
int secondCount = -1;
aw.GetCount(GetFeature(), ref secondCount);
Assert.AreEqual(firstCount+1, secondCount);
Assert.IsTrue(aw.CheckIn(GetFeature()));
aw.GetCount(GetFeature(), ref secondCount);
Assert.AreEqual(firstCount, secondCount);
}
}
[Test]
public void TestGetExpiration()
{
using (var aw = new DAuthClient(false, true))
{
DateTime expiration = new DateTime(1900, 1, 1, 0, 0, 0);
aw.GetExpiration(GetFeature(), ref expiration);
Assert.AreEqual(2000, expiration.Year);
}
}
[Test]
public void TestGetCurrentUsers()
{
using (var aw = new DAuthClient(false, true))
{
String[] currentUsers = null;
var feature = GetFeature();
Assert.IsTrue(aw.CheckOut(feature, GetSupportedFeatureVersions().First()));
Assert.IsTrue(aw.GetCurrentUsers(feature, ref currentUsers));
Assert.IsNotEmpty(currentUsers);
Assert.IsTrue(currentUsers.Any(u => u == Environment.UserName));
Assert.IsTrue(aw.CheckIn(feature));
}
}
[Test]
public void TestUsedLicenses()
{
using (var aw = new DAuthClient(false, true))
{
int count = -1;
var feature = GetFeature();
Assert.IsTrue(aw.GetUsedLicenses(feature, Environment.UserName, ref count));
Assert.AreEqual(0, count);
Assert.IsTrue(aw.CheckOut(feature, GetSupportedFeatureVersions().First()));
Assert.IsTrue(aw.GetUsedLicenses(feature, Environment.UserName, ref count));
Assert.AreEqual(1, count);
Assert.IsTrue(aw.CheckIn(feature));
}
}
[Test]
public void TestAvailableLicenses()
{
using (var aw = new DAuthClient(false, true))
{
var count = 0;
Assert.IsTrue(aw.GetAvailableLicenses(GetFeature(), ref count));
Assert.AreEqual(500, count);
}
}
[Test]
public void TestGetAvailableFeatures()
{
using (var aw = new DAuthClient(false, true))
{
String[] features = null;
Assert.IsTrue(aw.GetAvailableFeatures(ref features));
Assert.IsNotEmpty(features);
var feature = GetFeature();
Assert.IsTrue(features.Any(f => f == feature));
}
}
[Test]
public void TestIsServer()
{
using (var aw = new DAuthClient(false, true))
{
bool isServer = false;
Assert.IsTrue(aw.IsServer(GetFeature(), ref isServer));
Assert.IsTrue(isServer);
}
}
[Test]
[Category(Categories.Performance)]
public void PerformanceTest()
{
// average 3 calls per second
using (var aw = new DAuthClient(false, true))
{
var feature = GetFeature();
var version = GetSupportedFeatureVersions().First();
var stopwatch = new Stopwatch();
stopwatch.Start();
for (var i = 0; i < 10; i++)
{
Assert.IsTrue(aw.CheckOut(feature, version));
Assert.IsTrue(aw.CheckIn(feature));
Console.Error.WriteLine("Round :" + i);
}
stopwatch.Stop();
Assert.Less(stopwatch.Elapsed, new TimeSpan(0,0,0,3));
}
}
}
}