feat(batch10): task4 wire ocsp cache load save and server lifecycle
This commit is contained in:
@@ -15,4 +15,40 @@ public sealed class OcspResponseCacheParserTests
|
||||
config.PreserveRevoked.ShouldBeFalse();
|
||||
config.SaveInterval.ShouldBe(TimeSpan.FromMinutes(5).TotalSeconds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseOCSPResponseCache_StringDurationBelowMinimum_ClampsToOneSecond()
|
||||
{
|
||||
var input = new Dictionary<string, object?>
|
||||
{
|
||||
["type"] = "local",
|
||||
["save_interval"] = "500ms",
|
||||
};
|
||||
|
||||
var (config, error) = OcspHandler.ParseOCSPResponseCache(input);
|
||||
|
||||
error.ShouldBeNull();
|
||||
config.ShouldNotBeNull();
|
||||
config.SaveInterval.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseOCSPResponseCache_NoneType_AcceptsConfiguration()
|
||||
{
|
||||
var input = new Dictionary<string, object?>
|
||||
{
|
||||
["type"] = "none",
|
||||
["local_store"] = "_rc_",
|
||||
["preserve_revoked"] = true,
|
||||
["save_interval"] = 25.0,
|
||||
};
|
||||
|
||||
var (config, error) = OcspHandler.ParseOCSPResponseCache(input);
|
||||
|
||||
error.ShouldBeNull();
|
||||
config.ShouldNotBeNull();
|
||||
config.Type.ShouldBe("none");
|
||||
config.PreserveRevoked.ShouldBeTrue();
|
||||
config.SaveInterval.ShouldBe(25.0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the Apache License, Version 2.0
|
||||
|
||||
using Shouldly;
|
||||
using System.Text.Json;
|
||||
using ZB.MOM.NatsNet.Server.Auth.CertificateIdentityProvider;
|
||||
using ZB.MOM.NatsNet.Server.Auth.Ocsp;
|
||||
|
||||
@@ -92,6 +93,84 @@ public sealed class OcspResponseCacheTests
|
||||
decompressed.ShouldBe(payload);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LocalDirCache_LoadCache_MissingFile_LeavesCacheEmpty()
|
||||
{
|
||||
var dir = CreateTempDir();
|
||||
try
|
||||
{
|
||||
var cache = new LocalDirCache(dir);
|
||||
var server = NewServer(new ServerOptions { NoSystemAccount = true });
|
||||
|
||||
cache.LoadCache(server);
|
||||
cache.Start();
|
||||
|
||||
var stats = cache.Stats();
|
||||
stats.ShouldNotBeNull();
|
||||
stats.Responses.ShouldBe(0);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(dir, recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LocalDirCache_LoadCache_ValidFile_PopulatesCache()
|
||||
{
|
||||
var dir = CreateTempDir();
|
||||
try
|
||||
{
|
||||
var cache = new LocalDirCache(dir);
|
||||
var cacheFile = Path.Combine(dir, "cache.json");
|
||||
var seed = new Dictionary<string, OcspResponseCacheItem>
|
||||
{
|
||||
["k1"] = new()
|
||||
{
|
||||
Subject = "subj",
|
||||
CachedAt = DateTime.UtcNow,
|
||||
RespStatus = OcspStatusAssertion.Good,
|
||||
RespExpires = DateTime.UtcNow.AddMinutes(5),
|
||||
Resp = cache.Compress([1, 2, 3]).compressed!,
|
||||
},
|
||||
};
|
||||
|
||||
File.WriteAllBytes(cacheFile, JsonSerializer.SerializeToUtf8Bytes(seed));
|
||||
|
||||
var server = NewServer(new ServerOptions { NoSystemAccount = true });
|
||||
cache.LoadCache(server);
|
||||
cache.Start();
|
||||
|
||||
cache.Get("k1").ShouldBe([1, 2, 3]);
|
||||
cache.Stats()!.Responses.ShouldBe(1);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(dir, recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LocalDirCache_SaveCache_DirtyWritesCacheFile()
|
||||
{
|
||||
var dir = CreateTempDir();
|
||||
try
|
||||
{
|
||||
var cache = new LocalDirCache(dir);
|
||||
var server = NewServer(new ServerOptions { NoSystemAccount = true });
|
||||
cache.Start();
|
||||
cache.Put("k1", CreateResponse(OcspStatusAssertion.Good, [4, 5, 6]), "subj");
|
||||
|
||||
cache.SaveCache(server);
|
||||
|
||||
File.Exists(Path.Combine(dir, "cache.json")).ShouldBeTrue();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(dir, recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NoOpCache_LifecycleAndStats_ShouldNoOpSafely()
|
||||
{
|
||||
@@ -122,4 +201,19 @@ public sealed class OcspResponseCacheTests
|
||||
NextUpdate = DateTime.UtcNow.AddMinutes(10),
|
||||
Raw = raw,
|
||||
};
|
||||
|
||||
private static NatsServer NewServer(ServerOptions options)
|
||||
{
|
||||
var (server, err) = NatsServer.NewServer(options);
|
||||
err.ShouldBeNull();
|
||||
server.ShouldNotBeNull();
|
||||
return server!;
|
||||
}
|
||||
|
||||
private static string CreateTempDir()
|
||||
{
|
||||
var dir = Path.Combine(Path.GetTempPath(), "ocsp-cache-" + Path.GetRandomFileName());
|
||||
Directory.CreateDirectory(dir);
|
||||
return dir;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user