2023-08-31 12:20:06 +02:00
using System ;
2022-08-30 15:42:25 +02:00
using System.Threading ;
using System.Threading.Tasks ;
using NSubstitute ;
2021-05-13 17:25:46 +02:00
using NUnit.Framework ;
using Plugin.BLE.Abstractions.Contracts ;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock ;
using ShareeBike.Services.BluetoothLock.BLE ;
using ShareeBike.Services.BluetoothLock.Exception ;
using ShareeBike.Services.BluetoothLock.Tdo ;
2021-05-13 17:25:46 +02:00
using DeviceState = Plugin . BLE . Abstractions . DeviceState ;
2024-04-09 12:53:23 +02:00
namespace LockIt.BLE.Tests.Services.BluetoothLock.BLE
2021-05-13 17:25:46 +02:00
{
2022-09-06 16:08:19 +02:00
public class TestLockItPolling
{
[Test]
public void TestOpen ( )
{
var device = Substitute . For < IDevice > ( ) ;
var adapter = Substitute . For < IAdapter > ( ) ;
2024-04-09 12:53:23 +02:00
var cipher = Substitute . For < ShareeBike . Model . Device . ICipher > ( ) ;
2022-09-06 16:08:19 +02:00
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 ) 'o' , ( byte ) 'a' , ( byte ) 'w' , ( byte ) 'm' , ( byte ) 'X' , ( byte ) '8' , ( byte ) 'T' , ( byte ) 'X' , ( byte ) 'Q' , ( byte ) 'Z' , ( byte ) 'd' , ( byte ) 'l' , ( byte ) 'k' , ( byte ) '3' , ( byte ) 'e' , ( byte ) 'V' , } ,
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 ) ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
auth . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ 8 ] , LockItBase . ERRORCODE_SUCCESS ) ) ) ;
2022-09-06 16:08:19 +02:00
cipher . Decrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 3 ] ) ;
cipher . Encrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 16 ] ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( LockItBase . ERRORCODE_SUCCESS ) ;
2022-09-06 16:08:19 +02:00
device . Name . Returns ( "ISHAREIT+123" ) ;
// Calls related to open functionality.
controlService . GetCharacteristicAsync ( new Guid ( "0000baaa-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( controlCharacteristic ) ) ;
2024-04-09 12:53:23 +02:00
controlCharacteristic . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ ] { 1 /* state read after sending open commant: closed */ } , LockItBase . ERRORCODE_SUCCESS ) ) , Task . FromResult ( ( new byte [ ] { 0 /* state read after sending open commant: open */ } , LockItBase . ERRORCODE_SUCCESS ) ) ) ;
2022-09-06 16:08:19 +02:00
controlService . GetCharacteristicAsync ( new Guid ( "0000beee-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( activateLock ) ) ;
2024-04-09 12:53:23 +02:00
activateLock . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
2022-09-06 16:08:19 +02:00
// Use factory to create LockIt-object.
var lockIt = LockItPolling . Authenticate ( device , authTdo , adapter , cipher ) . Result ;
Assert . That ( lockIt . OpenAsync ( ) . Result , Is . EqualTo ( LockitLockingState . Open ) ) ;
}
[Test]
public void TestOpen_ThrowsCouldntOpenInconsistentStateExecption ( )
{
var device = Substitute . For < IDevice > ( ) ;
var adapter = Substitute . For < IAdapter > ( ) ;
2024-04-09 12:53:23 +02:00
var cipher = Substitute . For < ShareeBike . Model . Device . ICipher > ( ) ;
2022-09-06 16:08:19 +02:00
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 ) 'n' , ( byte ) 'l' , ( byte ) 'Q' , ( byte ) 'I' , ( byte ) 'S' , ( byte ) 'z' , ( byte ) 'p' , ( byte ) 'H' , ( byte ) 'm' , ( byte ) 'n' , ( byte ) 'V' , ( byte ) 'n' , ( byte ) '7' , ( byte ) 'f' , ( byte ) 'i' , ( byte ) '3' } ,
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 ) ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
auth . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ 8 ] , LockItBase . ERRORCODE_SUCCESS ) ) ) ;
2022-09-06 16:08:19 +02:00
cipher . Decrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 3 ] ) ;
cipher . Encrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 16 ] ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( LockItBase . ERRORCODE_SUCCESS ) ;
2022-09-06 16:08:19 +02:00
device . Name . Returns ( "ISHAREIT+123" ) ;
// Calls related to open functionality.
controlService . GetCharacteristicAsync ( new Guid ( "0000baaa-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( controlCharacteristic ) ) ;
2024-04-09 12:53:23 +02:00
controlCharacteristic . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ ] { 1 /* closed */ } , LockItBase . ERRORCODE_SUCCESS ) ) ) ; // Closed is returned twice => Inconsistent state ....
2022-09-06 16:08:19 +02:00
controlService . GetCharacteristicAsync ( new Guid ( "0000beee-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( activateLock ) ) ;
2024-04-09 12:53:23 +02:00
activateLock . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
2022-09-06 16:08:19 +02:00
// Use factory to create LockIt-object.
var lockIt = LockItPolling . Authenticate ( device , authTdo , adapter , cipher ) . Result ;
Assert . That ( async ( ) = > { var result = await lockIt . OpenAsync ( ) ; } , Throws . InstanceOf < CouldntOpenInconsistentStateExecption > ( ) ) ;
}
[Test]
2023-08-31 12:20:06 +02:00
public void TestOpen_ThrowsCouldntOpenBoltBlockedException ( )
2022-09-06 16:08:19 +02:00
{
var device = Substitute . For < IDevice > ( ) ;
var adapter = Substitute . For < IAdapter > ( ) ;
2024-04-09 12:53:23 +02:00
var cipher = Substitute . For < ShareeBike . Model . Device . ICipher > ( ) ;
2022-09-06 16:08:19 +02:00
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 ) 'h' , ( byte ) 'r' , ( byte ) 'F' , ( byte ) 'h' , ( byte ) 'G' , ( byte ) 'T' , ( byte ) 'z' , ( byte ) 'P' , ( byte ) 'F' , ( byte ) 'Z' , ( byte ) 'n' , ( byte ) 'z' , ( byte ) 'Y' , ( byte ) 'B' , ( byte ) 's' , ( byte ) '9' } ,
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 ) ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
auth . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ 8 ] , LockItBase . ERRORCODE_SUCCESS ) ) ) ;
2022-09-06 16:08:19 +02:00
cipher . Decrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 3 ] ) ;
cipher . Encrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 16 ] ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( LockItBase . ERRORCODE_SUCCESS ) ;
2022-09-06 16:08:19 +02:00
device . Name . Returns ( "ISHAREIT+123" ) ;
// Calls related to open functionality.
controlService . GetCharacteristicAsync ( new Guid ( "0000baaa-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( controlCharacteristic ) ) ;
2024-04-09 12:53:23 +02:00
controlCharacteristic . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ ] { 1 /* closed */ } , LockItBase . ERRORCODE_SUCCESS ) ) , Task . FromResult ( ( new byte [ ] { 4 /* bold blocked */ } , LockItBase . ERRORCODE_SUCCESS ) ) ) ;
2022-09-06 16:08:19 +02:00
controlService . GetCharacteristicAsync ( new Guid ( "0000beee-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( activateLock ) ) ;
2024-04-09 12:53:23 +02:00
activateLock . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
2022-09-06 16:08:19 +02:00
// Use factory to create LockIt-object.
var lockIt = LockItPolling . Authenticate ( device , authTdo , adapter , cipher ) . Result ;
Assert . That ( async ( ) = > { var result = await lockIt . OpenAsync ( ) ; } , Throws . InstanceOf < CouldntOpenBoldIsBlockedException > ( ) ) ;
}
[Test]
public void TestClose ( )
{
var device = Substitute . For < IDevice > ( ) ;
var adapter = Substitute . For < IAdapter > ( ) ;
2024-04-09 12:53:23 +02:00
var cipher = Substitute . For < ShareeBike . Model . Device . ICipher > ( ) ;
2022-09-06 16:08:19 +02:00
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 ) 'H' , ( byte ) '7' , ( byte ) 'y' , ( byte ) 'B' , ( byte ) 'f' , ( byte ) 's' , ( byte ) 'v' , ( byte ) 'L' , ( byte ) 'G' , ( byte ) 'L' , ( byte ) '7' , ( byte ) 'b' , ( byte ) '7' , ( byte ) 'X' , ( byte ) 'z' , ( byte ) 't' } ,
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 ) ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
auth . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ 8 ] , LockItBase . ERRORCODE_SUCCESS ) ) ) ;
2022-09-06 16:08:19 +02:00
cipher . Decrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 3 ] ) ;
cipher . Encrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 16 ] ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( LockItBase . ERRORCODE_SUCCESS ) ;
2022-09-06 16:08:19 +02:00
device . Name . Returns ( "ISHAREIT+123" ) ;
// Calls related to close functionality.
controlService . GetCharacteristicAsync ( new Guid ( "0000baaa-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( controlCharacteristic ) ) ;
2024-04-09 12:53:23 +02:00
controlCharacteristic . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ ] { 0 /* open */ } , LockItBase . ERRORCODE_SUCCESS ) ) , Task . FromResult ( ( new byte [ ] { 1 /* closed */ } , LockItBase . ERRORCODE_SUCCESS ) ) ) ;
2022-09-06 16:08:19 +02:00
controlService . GetCharacteristicAsync ( new Guid ( "0000beee-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( activateLock ) ) ;
2024-04-09 12:53:23 +02:00
activateLock . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
2022-09-06 16:08:19 +02:00
// Use factory to create LockIt-object.
var lockIt = LockItPolling . Authenticate ( device , authTdo , adapter , cipher ) . Result ;
Assert . That ( lockIt . CloseAsync ( ) . Result , Is . EqualTo ( LockitLockingState . Closed ) ) ;
}
[Test]
2023-08-31 12:20:06 +02:00
public void TestClose_ThrowsCouldntCloseInconsistentStateExecption ( )
2022-09-06 16:08:19 +02:00
{
var device = Substitute . For < IDevice > ( ) ;
var adapter = Substitute . For < IAdapter > ( ) ;
2024-04-09 12:53:23 +02:00
var cipher = Substitute . For < ShareeBike . Model . Device . ICipher > ( ) ;
2022-09-06 16:08:19 +02:00
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 ) 'f' } ,
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 ) ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
auth . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ 8 ] , LockItBase . ERRORCODE_SUCCESS ) ) ) ;
2022-09-06 16:08:19 +02:00
cipher . Decrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 3 ] ) ;
cipher . Encrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 16 ] ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( LockItBase . ERRORCODE_SUCCESS ) ;
2022-09-06 16:08:19 +02:00
device . Name . Returns ( "ISHAREIT+123" ) ;
// Calls related to close functionality.
controlService . GetCharacteristicAsync ( new Guid ( "0000baaa-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( controlCharacteristic ) ) ;
2024-04-09 12:53:23 +02:00
controlCharacteristic . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ ] { 0 /* opened */ } , LockItBase . ERRORCODE_SUCCESS ) ) ) ;
2022-09-06 16:08:19 +02:00
controlService . GetCharacteristicAsync ( new Guid ( "0000beee-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( activateLock ) ) ;
2024-04-09 12:53:23 +02:00
activateLock . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
2022-09-06 16:08:19 +02:00
// 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 < CouldntCloseInconsistentStateExecption > ( ) ) ;
}
[Test]
2023-08-31 12:20:06 +02:00
public async Task TestClose_ThrowsCouldntCloseInconsistentStateExecptionUnknownState ( )
{
var device = Substitute . For < IDevice > ( ) ;
var adapter = Substitute . For < IAdapter > ( ) ;
2024-04-09 12:53:23 +02:00
var cipher = Substitute . For < ShareeBike . Model . Device . ICipher > ( ) ;
2023-08-31 12:20:06 +02:00
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 ) ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
auth . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ 8 ] , LockItBase . ERRORCODE_SUCCESS ) ) ) ;
2023-08-31 12:20:06 +02:00
cipher . Decrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 3 ] ) ;
cipher . Encrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 16 ] ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( LockItBase . ERRORCODE_SUCCESS ) ;
2023-08-31 12:20:06 +02:00
device . Name . Returns ( "ISHAREIT+123" ) ;
// Calls related to close functionality.
controlService . GetCharacteristicAsync ( new Guid ( "0000baaa-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( controlCharacteristic ) ) ;
2024-04-09 12:53:23 +02:00
controlCharacteristic . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ ] { 99 /* lock should never return this exception */ } , LockItBase . ERRORCODE_SUCCESS ) ) ) ;
2023-08-31 12:20:06 +02:00
controlService . GetCharacteristicAsync ( new Guid ( "0000beee-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( activateLock ) ) ;
2024-04-09 12:53:23 +02:00
activateLock . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
2023-08-31 12:20:06 +02:00
// 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 ( )
2022-09-06 16:08:19 +02:00
{
var device = Substitute . For < IDevice > ( ) ;
var adapter = Substitute . For < IAdapter > ( ) ;
2024-04-09 12:53:23 +02:00
var cipher = Substitute . For < ShareeBike . Model . Device . ICipher > ( ) ;
2022-09-06 16:08:19 +02:00
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 ) 'u' , ( byte ) 'f' , ( byte ) 'u' , ( byte ) 'v' , ( byte ) 'j' , ( byte ) 'E' , ( byte ) 'b' , ( byte ) 'x' , ( byte ) 'p' , ( byte ) 'z' , ( byte ) 'a' , ( byte ) 'h' , ( byte ) 'V' , ( byte ) '5' , ( byte ) '9' , ( byte ) 'i' } ,
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 ) ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
auth . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ 8 ] , LockItBase . ERRORCODE_SUCCESS ) ) ) ;
2022-09-06 16:08:19 +02:00
cipher . Decrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 3 ] ) ;
cipher . Encrypt ( Arg . Any < byte [ ] > ( ) , Arg . Any < byte [ ] > ( ) ) . Returns ( new byte [ 16 ] ) ;
2024-04-09 12:53:23 +02:00
auth . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( LockItBase . ERRORCODE_SUCCESS ) ;
2022-09-06 16:08:19 +02:00
device . Name . Returns ( "ISHAREIT+123" ) ;
// Calls related to Close functionality.
controlService . GetCharacteristicAsync ( new Guid ( "0000baaa-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( controlCharacteristic ) ) ;
2024-04-09 12:53:23 +02:00
controlCharacteristic . ReadAsync ( Arg . Any < CancellationToken > ( ) ) . Returns ( Task . FromResult ( ( new byte [ ] { 0 /* open */ } , LockItBase . ERRORCODE_SUCCESS ) ) , Task . FromResult ( ( new byte [ ] { 5 /* bold blocked */ } , LockItBase . ERRORCODE_SUCCESS ) ) ) ;
2022-09-06 16:08:19 +02:00
controlService . GetCharacteristicAsync ( new Guid ( "0000beee-1212-efde-1523-785fef13d123" ) ) . Returns ( Task . FromResult ( activateLock ) ) ;
2024-04-09 12:53:23 +02:00
activateLock . WriteAsync ( Arg . Any < byte [ ] > ( ) ) . Returns ( Task . FromResult ( LockItBase . ERRORCODE_SUCCESS ) ) ;
2022-09-06 16:08:19 +02:00
// Use factory to create LockIt-object.
var lockIt = LockItPolling . Authenticate ( device , authTdo , adapter , cipher ) . Result ;
2023-08-31 12:20:06 +02:00
Assert . That ( async ( ) = > { var result = await lockIt . CloseAsync ( ) ; } , Throws . InstanceOf < CouldntCloseBoltBlockedException > ( ) ) ;
2022-09-06 16:08:19 +02:00
}
}
2021-05-13 17:25:46 +02:00
}