improvements

This commit is contained in:
2025-11-02 15:52:04 +01:00
parent b45a65dfa0
commit 85c0354d2a
33 changed files with 977 additions and 119 deletions

View File

@@ -0,0 +1,20 @@
using CertMgr.Core.Storage;
using NUnit.Framework;
namespace certmgrTest;
public class CertStoreStorageTest
{
[Test]
public async Task First()
{
using (MemoryStream target = new MemoryStream())
{
CertStoreStorage storage = new CertStoreStorage();
CertStoreStorageContext context = new CertStoreStorageContext(CertStoreLocation.User, CertStoreName.My, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.DefaultKeySet);
context.Password = "aaa";
await storage.ReadAsync(target, context, CancellationToken.None);
}
}
}

View File

@@ -0,0 +1,82 @@

using System.Diagnostics;
using System.Reflection;
using CertMgr.Core;
using CertMgr.Core.Attributes;
using CertMgr.Core.Jobs;
using CertMgr.Core.Validation;
using NUnit.Framework;
namespace certmgrTest;
public class PropertyDescriptorTest
{
[Test]
public void First()
{
PropertyDescriptor? descriptor = GetProperty(typeof(TestSettings), nameof(TestSettings.StringItems));
if (descriptor == null)
{
throw new Exception(string.Format("property not found"));
}
IValueValidator? validator = descriptor.CustomValidator;
}
private static PropertyDescriptor? GetProperty(Type settingsType, string name)
{
PropertyDescriptor? result = null;
foreach (PropertyInfo propertyInfo in settingsType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (propertyInfo.Name != name)
{
continue;
}
SettingAttribute? settingAttribute = propertyInfo.GetCustomAttribute<SettingAttribute>();
if (settingAttribute is null)
{
continue;
}
result = new PropertyDescriptor(propertyInfo, settingAttribute, settingsType);
break;
}
return result;
}
private sealed class TestSettings : JobSettings
{
[Setting("string-names", Validator = typeof(StringItemValidator))]
public IReadOnlyCollection<string>? StringItems { get; set; }
}
private sealed class StringItemValidator : IValueValidator<string>, IValueValidator<IEnumerable<string>>
{
public StringItemValidator(string settingName)
{
ValueName = settingName;
}
public string ValueName { [DebuggerStepThrough] get; }
Task<IValidationResult> IValueValidator<string>.ValidateAsync(string? settingValue, CancellationToken cancellationToken)
{
return Task.FromResult((IValidationResult)new ValidationResult(ValueName, true, "OK"));
}
Task<IValidationResult> IValueValidator.ValidateAsync(object? value, CancellationToken cancellationToken)
{
return Task.FromResult((IValidationResult)new ValidationResult(ValueName, true, "OK"));
}
Task<IValidationResult> IValueValidator<IEnumerable<string>>.ValidateAsync(IEnumerable<string>? settingValue, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
}

View File

@@ -15,7 +15,7 @@ public class SubjectValidatorTest
public async Task First(string subj, bool expectedSuccess)
{
SubjectValidator validator = new SubjectValidator("TestSetting");
ValidationResult result = await validator.ValidateAsync(subj, CancellationToken.None);
IValidationResult result = await validator.ValidateAsync(subj, CancellationToken.None);
Assert.That(result.IsValid, Is.EqualTo(expectedSuccess), result.Justification);
}
}