/* ========================================================================
* 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;
using System.IO;
using NUnit.Framework;
using Opc.Ua.Server.UserDatabase;
using Opc.Ua.Tests;
namespace Opc.Ua.Server.Tests
{
///
/// Verifies that accepted user mutations persist and rejected mutations leave stored credentials unchanged.
///
[TestFixture]
[Category("Server")]
[Parallelizable(ParallelScope.All)]
public sealed class UserDatabasePersistenceRegressionTests
{
///
/// Verifies that deleting a user or changing a password remains effective after reloading the database.
///
[TestCase(false)]
[TestCase(true)]
public void SuccessfulUserMutationSurvivesDatabaseReload(bool delete)
{
string path = Path.Combine(Path.GetTempPath(), $"opcua-user-{Guid.NewGuid():N}.json");
try
{
var original = new JsonUserDatabase(path);
Assert.That(original.CreateUser("user", "before"u8, [Role.AuthenticatedUser]), Is.True);
bool changed = delete
? original.DeleteUser("user")
: original.ChangePassword("user", "before"u8, "after"u8);
Assert.That(changed, Is.True);
IUserDatabase reloaded = JsonUserDatabase.Load(path, NUnitTelemetryContext.Create());
Assert.That(reloaded.CheckCredentials("user", "before"u8), Is.False);
if (delete)
{
Assert.That(reloaded.GetUsers(), Is.Empty);
}
else
{
Assert.That(reloaded.CheckCredentials("user", "after"u8), Is.True);
Assert.That(reloaded.GetUserRoles("user"), Does.Contain(Role.AuthenticatedUser));
}
}
finally
{
File.Delete(path);
}
}
///
/// Verifies that rejected deletion and password changes preserve both file bytes and reloadable credentials.
///
[Test]
public void RejectedUserMutationsPreservePersistedCredentials()
{
string path = Path.Combine(Path.GetTempPath(), $"opcua-user-{Guid.NewGuid():N}.json");
try
{
var original = new JsonUserDatabase(path);
original.CreateUser("user", "before"u8, [Role.AuthenticatedUser]);
byte[] before = File.ReadAllBytes(path);
Assert.That(original.DeleteUser("missing"), Is.False);
Assert.That(original.ChangePassword("user", "wrong"u8, "after"u8), Is.False);
Assert.That(File.ReadAllBytes(path), Is.EqualTo(before));
IUserDatabase reloaded = JsonUserDatabase.Load(path, NUnitTelemetryContext.Create());
Assert.That(reloaded.CheckCredentials("user", "before"u8), Is.True);
Assert.That(reloaded.CheckCredentials("user", "after"u8), Is.False);
}
finally
{
File.Delete(path);
}
}
}
}