Version 3.0.381

This commit is contained in:
Anja 2024-04-09 12:53:23 +02:00
parent f963c0a219
commit 3a363acf3a
1525 changed files with 60589 additions and 125098 deletions

View file

@ -0,0 +1,84 @@
using System.Linq;
using System.Text;
using NUnit.Framework;
using ShareeBike.Services.BluetoothLock.Crypto;
namespace SharedBusinessLogic.Tests.Fixtures.ObjectTests.Services.BluetoothLock.Crypto
{
[TestFixture]
public class TestCryptoHelper
{
/// <summary>
/// Ensures that decryption 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.That(acces_key.SequenceEqual(decrypt), Is.True);
}
[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();
//// Decrypted 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.That(seedLockDec.SequenceEqual(result), Is.True);
}
[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();
// Decrypted 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.That(
Encoding.UTF8.GetString(result), Is.EqualTo(Encoding.UTF8.GetString(seedLockDec)));
}
}
}

View file

@ -0,0 +1,24 @@
using NUnit.Framework;
using ShareeBike.Services.BluetoothLock.Tdo;
namespace SharedBusinessLogic.Tests.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,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ShareeBike.Model.Bikes;
using ShareeBike.Model.Bikes.BikeInfoNS.BikeNS;
using ShareeBike.Model.Bikes.BikeInfoNS.DriveNS;
using ShareeBike.Services.BluetoothLock;
using ShareeBike.Services.BluetoothLock.Tdo;
namespace SharedBusinessLogic.Tests.Fixtures.ObjectTests.Service.LockService
{
[TestFixture]
public class TestLockServiceSimulation
{
[Test]
public void TestUpdateSimulationInstance_Update()
{
var service = new LocksServiceInReach();
var bikes = new BikeCollection(new Dictionary<string, ShareeBike.Model.Bikes.BikeInfoNS.BC.BikeInfo>()
{
{ "42", new ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfo(new ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Bike("42", LockModel.ILockIt), new DriveMutable(), ShareeBike.Model.Bikes.BikeInfoNS.BC.DataSource.Copri, 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 ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfo(new ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Bike("43", LockModel.ILockIt), new DriveMutable(), ShareeBike.Model.Bikes.BikeInfoNS.BC.DataSource.Copri, 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.That(service.GetLocksStateAsync(new List<LockInfoAuthTdo>(), new TimeSpan(0, 0, 3)).Result.Count(), Is.EqualTo(2));
}
}
}

View file

@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ShareeBike.Model.Device;
using ShareeBike.Services.BluetoothLock;
namespace SharedBusinessLogic.Tests.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,64 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using ShareeBike.Model.Services.CopriApi.ServerUris;
namespace SharedBusinessLogic.Tests.Fixtures.ObjectTests.Connector
{
[TestFixture]
public class TestCopriServerUriList
{
[Test]
public void TestConstruct()
{
var l_oUri = new CopriServerUriList();
Assert.That(l_oUri.Uris.Count, Is.GreaterThan(0), "There must be at least one uri");
Assert.That(l_oUri.ActiveUri, Is.Not.Null);
}
[Test]
public void TestConstruct_AryStringString()
{
var l_oUri = new CopriServerUriList(
(new List<Uri> { new Uri("http://1.2.3.4"), new Uri("http://2.3.4.5"), new Uri("http://3.4.5.6") }).ToArray(),
new Uri("http://2.3.4.5"));
Assert.That(l_oUri.Uris.Count, Is.EqualTo(3));
Assert.That(l_oUri.ActiveUri, Is.EqualTo(new Uri("http://2.3.4.5")));
}
[Test]
public void TestConstruct_AryStringString_NullList()
{
Assert.Throws<ArgumentException>(() => new CopriServerUriList(
null,
new Uri("http://2.3.4.5")));
}
[Test]
public void TestConstruct_AryStringString_InvalidList()
{
Assert.Throws<ArgumentException>(() => new CopriServerUriList(
(new List<Uri>()).ToArray(),
new Uri("http://2.3.4.5")));
}
[Test]
public void TestConstruct_AryStringString_InvalidActiveUri()
{
Assert.Throws<ArgumentException>(() => new CopriServerUriList(
(new List<Uri> { new Uri("http://1.2.3.4"), new Uri("http://2.3.4.5"), new Uri("http://3.4.5.6") }).ToArray(),
new Uri("http://9.9.9.9")));
}
[Test]
public void TestDefaultActiveUri()
{
Assert.That(
CopriServerUriList.DefaultActiveUri.AbsoluteUri, Is.EqualTo("https://shareeapp-primary.copri.eu/APIjsonserver"),
"In production environment, server address must always be app.tink-konstanz.de/APIjsonserver.");
}
}
}

View file

@ -0,0 +1,74 @@
using System;
using NUnit.Framework;
using ShareeBike.Model;
using ShareeBike.Model.Map;
using ShareeBike.Services.CopriApi;
namespace SharedBusinessLogic.Tests.Services.CopriApi
{
[TestFixture]
public class TestGeneralData
{
[Test]
public void TestCtor()
{
Assert.That(
new GeneralData().MerchantMessage,
Is.EqualTo(""));
Assert.That(
new GeneralData().ApiVersion,
Is.EqualTo(new Version(0, 0)));
Assert.That(
new GeneralData().InitialMapSpan?.IsValid,
Is.False,
"Object not not be null but invalid.");
}
[Test]
public void TestMessage()
{
Assert.That(
new GeneralData(
MapSpanFactory.Create(),
"Hello",
null,
new ResourceUrls()).MerchantMessage,
Is.EqualTo("Hello"));
}
[Test]
public void TestVersion()
{
Assert.That(
new GeneralData(
MapSpanFactory.Create(),
null,
new Version(1, 2),
new ResourceUrls()).ApiVersion,
Is.EqualTo(new Version(1, 2)));
}
[Test]
public void TestInitialMapSpan()
{
Assert.That(
new GeneralData(
MapSpanFactory.Create(PositionFactory.Create(0, 8), 15),
null,
new Version(1, 2),
new ResourceUrls()).InitialMapSpan.Center.Longitude,
Is.EqualTo(8));
Assert.That(
new GeneralData(
MapSpanFactory.Create(PositionFactory.Create(0, 8), 15),
null,
new Version(1, 2),
new ResourceUrls()).InitialMapSpan.Radius,
Is.EqualTo(15));
}
}
}

View file

@ -0,0 +1,105 @@
using NSubstitute;
using NUnit.Framework;
using ShareeBike.Model.Bikes.BikeInfoNS.CopriLock;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Services.CopriApi;
using ShareeBike.Repository.Request;
using System.Threading.Tasks;
using ShareeBike.Repository.Response;
using ShareeBike.Repository;
namespace SharedBusinessLogic.Tests.Services.CopriApi
{
[TestFixture]
public class TestPolling
{
[Test]
public async Task TestOpenAyncLockStateUnknownDisconnected()
{
var server = Substitute.For<ICachedCopriServer>();
var bike = Substitute.For<IBikeInfoMutable>();
bike.Id.Returns("XY19");
bike.OperatorUri.Returns(new System.Uri("https://1.2.3.4"));
await Polling.OpenAync(server, bike);
Received.InOrder(
() =>
{
server.UpdateLockingStateAsync(
"XY19",
lock_state.unlocking,
new System.Uri("https://1.2.3.4"));
bike.LockInfo.State = LockingState.UnknownDisconnected;
});
}
[Test]
public async Task TestOpenAyncLock()
{
var server = Substitute.For<ICachedCopriServer>();
var bike = Substitute.For<IBikeInfoMutable>();
bike.Id.Returns("XY19");
bike.OperatorUri.Returns(new System.Uri("https://1.2.3.4"));
server.GetBikesOccupied(false).Returns(
new Result<BikesReservedOccupiedResponse>(
typeof(CopriCallsHttps),
JsonConvertRethrow.DeserializeObject<BikesReservedOccupiedResponse>(@"
{
""bikes_occupied"": {
""FR1005"": {
""lock_state"": ""occupied"",
""system"": ""Sigo"",
""state"": ""occupied"",
""bike"": ""XY19"",
""station"": ""REN0"",
""lock_state"" : ""locked""
}
},
""copri_version"": ""4.1.12.5"",
}"),
new GeneralData()),
new Result<BikesReservedOccupiedResponse>(
typeof(CopriCallsHttps),
JsonConvertRethrow.DeserializeObject<BikesReservedOccupiedResponse>(@"
{
""bikes_occupied"": {
""FR1005"": {
""lock_state"": ""occupied"",
""system"": ""Sigo"",
""state"": ""occupied"",
""bike"": ""XY19"",
""station"": ""REN0"",
""lock_state"" : ""unlocked""
}
},
""copri_version"": ""4.1.12.5"",
}"),
new GeneralData()));
await Polling.OpenAync(server, bike);
Received.InOrder(
() =>
{
server.UpdateLockingStateAsync(
"XY19",
lock_state.unlocking,
new System.Uri("https://1.2.3.4"));
server.GetBikesOccupied(false);
server.GetBikesOccupied(false);
bike.LockInfo.State = LockingState.Open;
});
}
}
}

View file

@ -0,0 +1,33 @@
using System;
using NUnit.Framework;
using ShareeBike.Model;
using ShareeBike.Model.Map;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Services.CopriApi;
namespace SharedBusinessLogic.Tests.Services.CopriApi
{
[TestFixture]
public class TestResult
{
[Test]
public void TestCtor()
{
Assert.That(
new Result<object>(typeof(TestResult), new TestResult(), null).GeneralData,
Is.Not.Null);
}
[Test]
public void TestGeneralData()
{
Assert.That(
new Result<object>(typeof(TestResult), new TestResult(), new GeneralData(
MapSpanFactory.Create(),
"Hello",
new Version(0, 0),
new ResourceUrls())).GeneralData.MerchantMessage,
Is.EqualTo("Hello"));
}
}
}

View file

@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using ShareeBike.Services;
namespace SharedBusinessLogic.Tests.Services
{
[TestFixture]
public class TestServicesContainerMutable
{
[Test]
public void TestCtor()
{
var typeA = new MyTypeA();
var typeB = new MyTypeB();
var serviceContainer = new ServicesContainerMutableT<object>(
new List<object> { typeA, typeB, typeB },
typeA.GetType().FullName);
Assert.That(
serviceContainer,
Is.EqualTo(new List<object> { typeA, typeB }).AsCollection);
Assert.That(
serviceContainer.Active,
Is.EqualTo(typeA));
}
[Test]
public void TestSetActive()
{
var typeA = new MyTypeA();
var typeB = new MyTypeB();
var serviceContainer = new ServicesContainerMutableT<object>(
new List<object> { typeA, typeB },
typeA.GetType().FullName);
serviceContainer.SetActive(typeB.GetType().FullName);
Assert.That(
serviceContainer.Active,
Is.EqualTo(typeB));
}
[Test]
public void TestSetActiveNotExists()
{
var typeA = new MyTypeA();
var typeB = new MyTypeB();
var serviceContainer = new ServicesContainerMutableT<object>(
new List<object> { typeA, typeB },
typeA.GetType().FullName);
Assert.That(
() => serviceContainer.SetActive(new MyTypeC().GetType().FullName),
Throws.TypeOf<ArgumentException>());
}
private class MyTypeA { }
private class MyTypeB { }
private class MyTypeC { }
[Test]
public void TestCtor2()
{
var container = new ServicesContainerMutableT<object>(new List<object> { new MyTypeA(), new MyTypeB() }, typeof(MyTypeB).FullName);
Assert.That(container.Active.GetType().FullName, Is.EqualTo("SharedBusinessLogic.Tests.Services.TestServicesContainerMutable+MyTypeB"));
}
[Test]
public void TestCtorExceptionDupes()
{
Assert.That(() => new ServicesContainerMutableT<object>(new List<object> { new MyTypeA(), new MyTypeB(), new MyTypeA() }, typeof(MyTypeB).FullName), Throws.InstanceOf<Exception>());
}
[Test]
public void TestCtorExceptionActiveNotFound()
{
Assert.That(() => new ServicesContainerMutableT<object>(new List<object> { new MyTypeA(), new MyTypeB() }, "MyTypeF"), Throws.InstanceOf<ArgumentException>());
}
[Test]
public void TestSetActive2()
{
var container = new ServicesContainerMutableT<object>(new List<object> { new MyTypeA(), new MyTypeB() }, typeof(MyTypeB).FullName);
container.SetActive(typeof(MyTypeA).FullName);
Assert.That(container.Active.GetType().FullName, Is.EqualTo("SharedBusinessLogic.Tests.Services.TestServicesContainerMutable+MyTypeA"));
}
}
}