Version 3.0.370

This commit is contained in:
Anja 2023-08-31 12:20:06 +02:00
parent f5cf9bb22f
commit bdb2dec1c1
233 changed files with 10252 additions and 6779 deletions

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.Threading;
using System.Threading.Tasks;
using NSubstitute;
@ -161,6 +161,45 @@ namespace TestTINKLib.Fixtures.ObjectTests.Settings.BluetoothLock
Assert.That((await lockIt.GetLockStateAsync()).State, Is.EqualTo(LockitLockingState.Closed));
}
[Test]
public async Task TestAuthenticate_GetLockState_UndefinedState()
{
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 state = Substitute.For<ICharacteristic>();
var authInfo = new LockInfoAuthTdo.Builder
{
K_seed = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 },
K_u = new byte[] { 1 }
}.Build();
// Calls related to Authenticate functionality.
device.State.Returns(DeviceState.Connected);
device.Id.Returns(new Guid("0000f00d-1212-efde-1523-785fef13d123"));
device.GetServiceAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>()).Returns(Task.FromResult(lockControl));
lockControl.GetCharacteristicAsync(new Guid("0000baab-1212-efde-1523-785fef13d123")).Returns(Task.FromResult(auth));
auth.WriteAsync(Arg.Any<byte[]>()).Returns(Task.FromResult(true));
auth.ReadAsync(Arg.Any<CancellationToken>()).Returns(Task.FromResult(new byte[8]));
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(true);
device.State.Returns(DeviceState.Connected);
device.Id.Returns(new Guid("00000000-0000-0000-0000-000000000001"));
// Calls related to get lock state.
// Call authenticate to invalidate seed
lockControl.GetCharacteristicAsync(new Guid("0000baaa-1212-efde-1523-785fef13d123")).Returns(Task.FromResult(state));
state.ReadAsync(Arg.Any<CancellationToken>()).Returns(new byte[] { 99 }); // This value should never be returned from ILockit.
var lockIt = LockItEventBased.Authenticate(device, authInfo, adapter, cipher).Result;
Assert.That((await lockIt.GetLockStateAsync()).State, Is.Null);
}
[Test]
public void TestAuthenticate_GetLockState_GetStateCharacteristicThrowsException()
{

View file

@ -12,7 +12,7 @@ using DeviceState = Plugin.BLE.Abstractions.DeviceState;
namespace TestLockItBLE
{
public class Tests
public class TestLockItEventBased
{
[Test]
public void TestOpen()
@ -111,7 +111,7 @@ namespace TestLockItBLE
}
[Test]
public void TestOpen_ThrowsCouldntOpenBoldBlockedException()
public void TestOpen_ThrowsCouldntOpenBoltBlockedException()
{
var device = Substitute.For<IDevice>();
var adapter = Substitute.For<IAdapter>();
@ -147,7 +147,7 @@ namespace TestLockItBLE
controlCharacteristic.ReadAsync(Arg.Any<CancellationToken>()).Returns(Task.FromResult(new byte[] { 1 /* closed */}), Task.FromResult(new byte[] { 4 /* bold blocked */ }));
lockControl.GetCharacteristicAsync(new Guid("0000beee-1212-efde-1523-785fef13d123")).Returns(Task.FromResult(activateLock));
activateLock.WriteAsync(Arg.Any<byte[]>()).Returns(Task.FromResult(true));
stateCharacteristic.Value.Returns(new byte[] { (byte)LockitLockingState.CouldntOpenBoldBlocked /* State passed as event argument after opening. */});
stateCharacteristic.Value.Returns(new byte[] { (byte)LockitLockingState.CouldntOpenBoltBlocked /* State passed as event argument after opening. */});
var lockIt = LockItEventBased.Authenticate(device, authTdo, adapter, cipher).Result;
@ -261,7 +261,7 @@ namespace TestLockItBLE
}
[Test]
public void TestClose_ThrowsCouldntCloseBoldBlockedException()
public void TestClose_ThrowsCouldntCloseBoltBlockedException()
{
var device = Substitute.For<IDevice>();
var adapter = Substitute.For<IAdapter>();
@ -298,7 +298,7 @@ namespace TestLockItBLE
}));
lockControl.GetCharacteristicAsync(new Guid("0000beee-1212-efde-1523-785fef13d123")).Returns(Task.FromResult(activateLock));
activateLock.WriteAsync(Arg.Any<byte[]>()).Returns(Task.FromResult(true));
stateCharacteristic.Value.Returns(new byte[] { (byte)LockitLockingState.CouldntCloseBoldBlocked /* State passed as event argument after opening. */});
stateCharacteristic.Value.Returns(new byte[] { (byte)LockitLockingState.CouldntCloseBoltBlocked /* State passed as event argument after opening. */});
// Use factory to create LockIt-object.
var lockIt = LockItEventBased.Authenticate(device, authTdo, adapter, cipher).Result;
@ -310,7 +310,7 @@ namespace TestLockItBLE
controlCharacteristic.ValueUpdated += Raise.EventWith(new object(), new CharacteristicUpdatedEventArgs(stateCharacteristic));
await lockState;
},
Throws.InstanceOf<CouldntCloseBoldBlockedException>());
Throws.InstanceOf<CouldntCloseBoltBlockedException>());
}
}
}

View file

@ -1,9 +1,10 @@
using System;
using System;
using System.Threading;
using System.Threading.Tasks;
using NSubstitute;
using NUnit.Framework;
using Plugin.BLE.Abstractions.Contracts;
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock;
using TINK.Services.BluetoothLock.BLE;
using TINK.Services.BluetoothLock.Exception;
using TINK.Services.BluetoothLock.Tdo;
@ -99,7 +100,7 @@ namespace TestLockItBLE.Services.BluetoothLock.BLE
}
[Test]
public void TestOpen_ThrowsCouldntOpenBoldBlockedException()
public void TestOpen_ThrowsCouldntOpenBoltBlockedException()
{
var device = Substitute.For<IDevice>();
var adapter = Substitute.For<IAdapter>();
@ -183,7 +184,7 @@ namespace TestLockItBLE.Services.BluetoothLock.BLE
}
[Test]
public void TestClose_ThrowsCouldntOpenInconsistentStateExecption()
public void TestClose_ThrowsCouldntCloseInconsistentStateExecption()
{
var device = Substitute.For<IDevice>();
var adapter = Substitute.For<IAdapter>();
@ -225,7 +226,51 @@ namespace TestLockItBLE.Services.BluetoothLock.BLE
}
[Test]
public void TestClose_ThrowsCouldntOpenBoldBlockedException()
public async Task TestClose_ThrowsCouldntCloseInconsistentStateExecptionUnknownState()
{
var device = Substitute.For<IDevice>();
var adapter = Substitute.For<IAdapter>();
var cipher = Substitute.For<TINK.Model.Device.ICipher>();
var auth = Substitute.For<ICharacteristic>();
var controlService = Substitute.For<IService>();
var controlCharacteristic = Substitute.For<ICharacteristic>();
var activateLock = Substitute.For<ICharacteristic>();
var authTdo = new LockInfoAuthTdo.Builder
{
Id = 12,
K_seed = new byte[] { (byte)'7', (byte)'q', (byte)'3', (byte)'9', (byte)'i', (byte)'6', (byte)'c', (byte)'g', (byte)'9', (byte)'L', (byte)'V', (byte)'7', (byte)'T', (byte)'G', (byte)'l', (byte)'g' },
K_u = new byte[16]
}.Build();
// Calls related to Authenticate functionality.
device.State.Returns(DeviceState.Connected);
device.Id.Returns(new Guid("0000f00d-1212-efde-1523-785fef13d123"));
device.GetServiceAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>()).Returns(Task.FromResult(controlService));
controlService.GetCharacteristicAsync(new Guid("0000baab-1212-efde-1523-785fef13d123")).Returns(Task.FromResult(auth));
auth.WriteAsync(Arg.Any<byte[]>()).Returns(Task.FromResult(true));
auth.ReadAsync(Arg.Any<CancellationToken>()).Returns(Task.FromResult(new byte[8]));
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(true);
device.Name.Returns("ISHAREIT+123");
// Calls related to close functionality.
controlService.GetCharacteristicAsync(new Guid("0000baaa-1212-efde-1523-785fef13d123")).Returns(Task.FromResult(controlCharacteristic));
controlCharacteristic.ReadAsync(Arg.Any<CancellationToken>()).Returns(Task.FromResult(new byte[] { 99 /* lock should never return this exception */}));
controlService.GetCharacteristicAsync(new Guid("0000beee-1212-efde-1523-785fef13d123")).Returns(Task.FromResult(activateLock));
activateLock.WriteAsync(Arg.Any<byte[]>()).Returns(Task.FromResult(true));
// Use factory to create LockIt-object.
var lockIt = LockItPolling.Authenticate(device, authTdo, adapter, cipher).Result;
Assert.That(
(await lockIt.CloseAsync()),
Is.Null);
}
[Test]
public void TestClose_ThrowsCouldntCloseBoltBlockedException()
{
var device = Substitute.For<IDevice>();
var adapter = Substitute.For<IAdapter>();
@ -264,7 +309,7 @@ namespace TestLockItBLE.Services.BluetoothLock.BLE
// Use factory to create LockIt-object.
var lockIt = LockItPolling.Authenticate(device, authTdo, adapter, cipher).Result;
Assert.That(async () => { var result = await lockIt.CloseAsync(); }, Throws.InstanceOf<CouldntCloseBoldBlockedException>());
Assert.That(async () => { var result = await lockIt.CloseAsync(); }, Throws.InstanceOf<CouldntCloseBoltBlockedException>());
}
}
}