Legacy testing lib added..

This commit is contained in:
Oliver Hauff 2021-07-12 21:31:46 +02:00
parent 0167fc321f
commit 47ed05837e
118 changed files with 17505 additions and 0 deletions

View file

@ -0,0 +1,85 @@
using NUnit.Framework;
using System.Linq;
using System.Text;
using TINK.Services.BluetoothLock.Crypto;
namespace TestTINKLib.Fixtures.ObjectTests.Services.BluetoothLock.Crypto
{
[TestFixture]
public class TestCryptoHelper
{
/// <summary>
/// Ensures that decyption from haveltec- lib produces the same results than sharee lib.
/// </summary>
[Test]
public void Test_DecryptStringFromBytes_Aes()
{
// keyCopri (value copied from debugging session of sharing_ble_lib/ haveltec code)
var keyCopri = (new sbyte[] { -6, 53, 29, -112, 7, -83, -41, -7, 30, 45, -13, -2, -108, -29, -90, 71, 15, -74, -76, 32, 0, 0, 0, 0 }).Select(x => (byte)x).ToArray();
// Encrypted seed value from lock (value copied from debugging session of sharing_ble_lib/ haveltec code)
var seedLockEnc = (new sbyte[] { 50, 51, -40, 64, 42, 82, 97, -24, 20, -39, -15, 126, 119, -110, 47, -18 }).Select(x => (byte)x).ToArray();
// Decrypted seed value from lock (value copied from debugging session of sharing_ble_lib/ haveltec code)
var acces_key = (new sbyte[] { 19, -66, 55, 18, -106, -92, 70, -40, 117, -87, -19, 124, 19, 54, -18, -82 }).Select(x => (byte)x).ToArray();
var decrypt = new Cipher().Decrypt(keyCopri, seedLockEnc);
Assert.IsTrue(acces_key.SequenceEqual(decrypt));
}
[Test]
public void TestGetSeedLock()
{
// seed copri (value copied from debugging session of sharing_ble_lib/ haveltec code)
byte[] seedCopri = (new sbyte[] { -7, -69, 16, -52, 88, 38, -92, 82, -99, -79, 19, 16, -41, -127, 51, 24 }).Select(x => (byte)x).ToArray();
// keyCopri (value copied from debugging session of sharing_ble_lib/ haveltec code)
byte[] keyCopri = (new sbyte[] { -6, 53, 29, -112, 7, -83, -41, -7, 30, 45, -13, -2, -108, -29, -90, 71, 15, -74, -76, 32, 0, 0, 0, 0 }).Select(x => (byte)x).ToArray();
// Encrypted seed value from lock (value copied from debugging session of sharing_ble_lib/ haveltec code)
byte[] seedLockEnc = (new sbyte[] { 92, 80, -36, -2, 101, -31, -23, -43, 71, 62, 126, -70, 54, -53, -119, -56 }).Select(x => (byte)x).ToArray();
//// Decryped seed value? access values? from lock (value copied from debugging session of sharing_ble_lib/ haveltec code)
byte[] seedLockDec = (new sbyte[] { 62, -51, 96, -80, 7, -84, 48, -104, 47, 51, -22, -23, 30, -10, -88, -97 }).Select(x => (byte)x).ToArray();
var crypto = new AuthCryptoHelper(
seedLockEnc,
keyCopri,
null);
var result = crypto.GetSeedLock();
Assert.IsTrue(seedLockDec.SequenceEqual(result));
}
[Test]
public void TestGetAccessKeyEncrypted()
{
// seed copri (value copied from debugging session of sharing_ble_lib/ haveltec code)
byte[] seedCopri = (new sbyte[] { -7, -69, 16, -52, 88, 38, -92, 82, -99, -79, 19, 16, -41, -127, 51, 24 }).Select(x => (byte)x).ToArray();
// keyCopri (value copied from debugging session of sharing_ble_lib/ haveltec code)
byte[] keyCopri = (new sbyte[] { -6, 53, 29, -112, 7, -83, -41, -7, 30, 45, -13, -2, -108, -29, -90, 71, 15, -74, -76, 32, 0, 0, 0, 0 }).Select(x => (byte)x).ToArray();
// Encrypted seed value from lock (value copied from debugging session of sharing_ble_lib/ haveltec code)
byte[] seedLockEnc = (new sbyte[] { 92, 80, -36, -2, 101, -31, -23, -43, 71, 62, 126, -70, 54, -53, -119, -56 }).Select(x => (byte)x).ToArray();
// Decryped seed value? access values? from lock (value copied from debugging session of sharing_ble_lib/ haveltec code)
byte[] seedLockDec = (new sbyte[] { 62, -51, 96, -80, 7, -84, 48, -104, 47, 51, -22, -23, 30, -10, -88, -97 }).Select(x => (byte)x).ToArray();
var crypto = new AuthCryptoHelper(
seedLockEnc,
keyCopri,
null);
var result = crypto.GetSeedLock();
Assert.AreEqual(
Encoding.UTF8.GetString(seedLockDec),
Encoding.UTF8.GetString(result));
}
}
}

View file

@ -0,0 +1,24 @@
using NUnit.Framework;
using TINK.Services.BluetoothLock.Tdo;
namespace TestTINKLib.Fixtures.ObjectTests.Services.BluetoothLock.Tdo
{
[TestFixture]
public class TestLockInfoAuthTdo
{
[Test]
public void TestCtor()
{
var auth = new LockInfoAuthTdo.Builder
{
K_seed = null,
K_u = null,
K_a = null,
}.Build();
Assert.That(auth.K_seed, Is.Not.Null);
Assert.That(auth.K_u, Is.Not.Null);
Assert.That(auth.K_a, Is.Not.Null);
}
}
}

View file

@ -0,0 +1,92 @@
using NSubstitute;
using NUnit.Framework;
using Plugin.BLE.Abstractions.Contracts;
using System;
using System.Threading.Tasks;
using TINK.Services.BluetoothLock.Tdo;
using TINK.Services.BluetoothLock.Exception;
using TINK.Services.BluetoothLock.BLE;
using DeviceState = Plugin.BLE.Abstractions.DeviceState;
using System.Threading;
namespace TestTINKLib.Fixtures.ObjectTests.Services.BluetoothLock
{
[TestFixture]
public class TestLockItBaseService
{
[Test]
public void TestConnect_Connected_InvalidArgs()
{
var device = Rhino.Mocks.MockRepository.GenerateStub<IDevice>();
var adapter = Rhino.Mocks.MockRepository.GenerateStub<IAdapter>();
var cipher = Rhino.Mocks.MockRepository.GenerateStub<TINK.Model.Device.ICipher>();
Rhino.Mocks.RhinoMocksExtensions.Stub(device, x => x.State).Return(DeviceState.Disconnected);
var exception = Assert.Throws<AggregateException>(
() => { var lockIt = LockItEventBased.Authenticate(device, null, adapter, cipher).Result; },
"If connected no auth is requied.");
Assert.That(exception.InnerExceptions.Count > 0);
Assert.That(exception.InnerExceptions[0], Is.InstanceOf<BluetoothDisconnectedException>());
}
[Test]
public void TestConnect_Connected()
{
var device = Substitute.For<IDevice>();
var adapter = Substitute.For<IAdapter>();
var cipher = Substitute.For<TINK.Model.Device.ICipher>();
var auth = Substitute.For<ICharacteristic>();
var lockControl = Substitute.For<IService>();
var authTdo = new LockInfoAuthTdo.Builder
{
Id = 12,
K_seed = new byte[] { (byte)'i', (byte)'V', (byte)'F', (byte)'m', (byte)'u', (byte)'T', (byte)'n', (byte)'K', (byte)'q', (byte)'E', (byte)'Y', (byte)'h', (byte)'m', (byte)'T', (byte)'l', (byte)'e' },
K_u = new byte[16]
}.Build();
device.State.Returns(DeviceState.Connected);
device.GetServiceAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>()).Returns(Task.FromResult(lockControl));
lockControl.GetCharacteristicAsync(Arg.Any<Guid>()).Returns(Task.FromResult(auth));
auth.WriteAsync(Arg.Any<byte[]>()).Returns(Task.FromResult(true)); // Write COPRI seed to lock
auth.ReadAsync(Arg.Any<CancellationToken>()).Returns(Task.FromResult(new byte[8])); // Read lock seed
cipher.Decrypt(Arg.Any<byte[]>(), Arg.Any<byte[]>()).Returns(new byte[3]);
cipher.Encrypt(Arg.Any<byte[]>(), Arg.Any<byte[]>()).Returns(new byte[16]);
auth.WriteAsync(Arg.Any<byte[]>()).Returns(Task.FromResult(true)); // Write COPRI seed to lock
device.Name.Returns("Origin");
Assert.AreEqual(
"Origin",
LockItEventBased.Authenticate(device, authTdo, adapter, cipher).Result.Name,
"If connected no auth is requied.");
}
[Test]
public void TestAuth()
{
var authTdo = new LockInfoAuthTdo.Builder {
Id = 12,
K_seed = new byte[] { (byte)'c', (byte)'b', (byte)'z', (byte)'b', (byte)'y', (byte)'I', (byte)'q', (byte)'j', (byte)'v', (byte)'L', (byte)'V', (byte)'I', (byte)'t', (byte)'C', (byte)'B', (byte)'I' },
K_u = new byte[16]
}.Build();
var device = Rhino.Mocks.MockRepository.GenerateStub<IDevice>();
var adapter = Rhino.Mocks.MockRepository.GenerateStub<IAdapter>();
var cipher = Rhino.Mocks.MockRepository.GenerateStub<TINK.Model.Device.ICipher>();
Rhino.Mocks.RhinoMocksExtensions.Stub(device, x => x.State).Return(DeviceState.Disconnected);
// Use factory to create LockIt-object.
var exception = Assert.Throws<AggregateException>(
() => { var lockIt = LockItEventBased.Authenticate(device, authTdo, adapter, cipher).Result; },
"If connected no auth is requied.");
Assert.That(exception.InnerExceptions.Count > 0);
Assert.That(exception.InnerExceptions[0], Is.InstanceOf<BluetoothDisconnectedException>());
}
}
}

View file

@ -0,0 +1,34 @@
using System.Linq;
using System.Collections.Generic;
using NUnit.Framework;
using TINK.Model.Bike;
using TINK.Services.BluetoothLock;
using TINK.Services.BluetoothLock.Tdo;
using System;
namespace TestTINKLib.Fixtures.ObjectTests.Service.LockService
{
[TestFixture]
public class TestLockServiceSimulation
{
[Test]
public void TestUpdateSimulationInstance_Update()
{
var service = new LocksServiceInReach();
var bikes = new BikeCollection(new Dictionary<string, TINK.Model.Bike.BC.BikeInfo>()
{
{ "42", new TINK.Model.Bike.BluetoothLock.BikeInfo("42", 1, new Guid(),new byte[] { 1, 4 }, new byte[] { 3, 4 }, new byte[] { 3, 4 }, DateTime.Now, "a@b", "1" , null /*operator uri*/) },
{ "43", new TINK.Model.Bike.BluetoothLock.BikeInfo("43", 3, new Guid(),new byte[] { 4, 4 }, new byte[] { 4, 7 }, new byte[] { 5, 4 }, DateTime.Now, "c@b", "1" , null /*operator uri*/) }
}
);
if (service is ILocksServiceFake serviceFake)
{
serviceFake.UpdateSimulation(bikes);
}
Assert.AreEqual(2, service.GetLocksStateAsync(new List<LockInfoAuthTdo>(), new TimeSpan(0, 0, 3)).Result.Count());
}
}
}

View file

@ -0,0 +1,29 @@
using NSubstitute;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using TINK.Model.Device;
using TINK.Services.BluetoothLock;
using TINK.Services.BluetoothLock.BLE;
namespace TestTINKLib.Fixtures.ObjectTests.Services
{
[TestFixture]
public class TestLocksServicesContainerMutable
{
[Test]
public void TestCtorCustomServices()
{
var ciper = NSubstitute.Substitute.For<ICipher>();
var firstService = NSubstitute.Substitute.For<ILocksService>();
Assert.That(
new LocksServicesContainerMutable(
firstService.GetType().FullName,
new HashSet<ILocksService>() { firstService }
).Count,
Is.EqualTo(1));
}
}
}

View file

@ -0,0 +1,45 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using TINK.Services;
namespace TestTINKLib.Fixtures.ObjectTests.Services
{
[TestFixture]
public class TestServicesContainerMutable
{
[Test]
public void TestCtor()
{
var container = new ServicesContainerMutable<object>(new List<object> { new A(), new B() }, typeof(B).FullName);
Assert.That(container.Active.GetType().FullName, Is.EqualTo("TestTINKLib.Fixtures.ObjectTests.Services.TestServicesContainerMutable+B"));
}
[Test]
public void TestCtorExceptionDupes()
{
Assert.That(() => new ServicesContainerMutable<object>(new List<object> { new A(), new B(), new A() }, typeof(B).FullName), Throws.InstanceOf<Exception>());
}
[Test]
public void TestCtorExceptionActiveNotFound()
{
Assert.That(() => new ServicesContainerMutable<object>(new List<object> { new A(), new B() }, "C"), Throws.InstanceOf<ArgumentException>());
}
[Test]
public void TestSetActive()
{
var container = new ServicesContainerMutable<object>(new List<object> { new A(), new B() }, typeof(B).FullName);
container.SetActive(typeof(A).FullName);
Assert.That(container.Active.GetType().FullName, Is.EqualTo("TestTINKLib.Fixtures.ObjectTests.Services.TestServicesContainerMutable+A"));
}
private class A
{ }
private class B
{ }
}
}