/* ======================================================================== * Copyright (c) 2005-2026 The OPC Foundation, Inc. All rights reserved. * * OPC Foundation MIT License 1.00 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * The complete license agreement can be found here: * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Moq; using NUnit.Framework; using Opc.Ua.PubSub.Configuration; using Opc.Ua.PubSub.Security.Sks; using Opc.Ua.Redundancy; using Opc.Ua.Tests; namespace Opc.Ua.PubSub.Redundancy { /// /// Tests for . /// [TestFixture] [Category("PubSub")] [Category("Redundancy")] public sealed class PubSubRedundancyServiceCollectionExtensionsTests { [Test] public async Task LeaderElectionModeWiresElectionCoordinatorAndSharedStoresAsync() { await using ServiceProvider provider = BuildProvider( new InMemorySharedKeyValueStore(), PubSubRedundancyElection.LeaderElection, registerElection: true); Assert.That( provider.GetRequiredService(), Is.TypeOf()); Assert.That( provider.GetRequiredService(), Is.TypeOf()); Assert.That( provider.GetRequiredService(), Is.TypeOf()); } [Test] public async Task LeaseStoreModeWiresLeaseCoordinatorAndSharedLeaseStoreAsync() { await using ServiceProvider provider = BuildProvider( new InMemorySharedKeyValueStore(), PubSubRedundancyElection.LeaseStore, registerElection: false); Assert.That( provider.GetRequiredService(), Is.TypeOf()); Assert.That( provider.GetRequiredService(), Is.TypeOf()); } [Test] public async Task HotLeaderElectionModeWiresCheckpointStoreAndSharedStoresAsync() { await using ServiceProvider provider = BuildProvider( new InMemorySharedKeyValueStore(), PubSubRedundancyElection.LeaderElection, registerElection: true, PubSubRedundancyMode.Hot); Assert.That( provider.GetRequiredService(), Is.TypeOf()); Assert.That( provider.GetRequiredService(), Is.TypeOf()); Assert.That( provider.GetRequiredService(), Is.TypeOf()); } [Test] public async Task ColdModeKeepsProcessLocalRuntimeStateWhileWarmModeSharesItAsync() { await using ServiceProvider coldProvider = BuildProvider( new InMemorySharedKeyValueStore(), PubSubRedundancyElection.LeaderElection, registerElection: true, PubSubRedundancyMode.Cold); await using ServiceProvider warmProvider = BuildProvider( new InMemorySharedKeyValueStore(), PubSubRedundancyElection.LeaderElection, registerElection: true, PubSubRedundancyMode.Warm); Assert.Multiple(() => { Assert.That( coldProvider.GetRequiredService(), Is.TypeOf()); Assert.That( coldProvider.GetRequiredService(), Is.TypeOf()); Assert.That( warmProvider.GetRequiredService(), Is.TypeOf()); Assert.That( warmProvider.GetRequiredService(), Is.TypeOf()); }); } [Test] public async Task NetworkedStoreWithoutProtectorFailsClosedForSecurityKeyStoreAsync() { ISharedKeyValueStore networkedStore = new Mock(MockBehavior.Loose).Object; await using ServiceProvider provider = BuildProvider( networkedStore, PubSubRedundancyElection.LeaderElection, registerElection: true); Assert.That( () => provider.GetRequiredService(), Throws.InvalidOperationException); } [Test] public async Task NetworkedStoreWithProtectorResolvesSecurityKeyStoreAsync() { ISharedKeyValueStore networkedStore = new Mock(MockBehavior.Loose).Object; ServiceCollection services = CreateServices(networkedStore, registerElection: true); services.AddSingleton(new AesCbcHmacRecordProtector(new byte[32])); services.AddPubSubRedundancy(o => o.Election = PubSubRedundancyElection.LeaderElection); await using ServiceProvider provider = services.BuildServiceProvider(); Assert.That( provider.GetRequiredService(), Is.TypeOf()); } [Test] public void AddPubSubRedundancyRejectsNullServices() { Assert.That( () => PubSubRedundancyServiceCollectionExtensions.AddPubSubRedundancy(null!), Throws.ArgumentNullException); } private static ServiceCollection CreateServices(ISharedKeyValueStore store, bool registerElection) { var services = new ServiceCollection(); services.AddSingleton(store); services.AddSingleton( ServiceMessageContext.CreateEmpty(NUnitTelemetryContext.Create())); if (registerElection) { services.AddSingleton(new StaticLeaderElection(true)); } return services; } private static ServiceProvider BuildProvider( ISharedKeyValueStore store, PubSubRedundancyElection election, bool registerElection, PubSubRedundancyMode mode = PubSubRedundancyMode.Warm) { ServiceCollection services = CreateServices(store, registerElection); services.AddPubSubRedundancy(o => { o.Election = election; o.Mode = mode; }); return services.BuildServiceProvider(); } } }