fix(grpc-data): H1 details hash formats every field invariant-culture (review 4)
This commit is contained in:
@@ -43,7 +43,7 @@ internal abstract record HistoryReadVariant
|
||||
public string ComputeDetailsHash() =>
|
||||
Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(CanonicalForm())));
|
||||
|
||||
/// <summary>The canonical text <see cref="ComputeDetailsHash"/> digests.</summary>
|
||||
/// <summary>The canonical text <see cref="ComputeDetailsHash"/> digests — every field invariant-culture formatted.</summary>
|
||||
/// <returns>An invariant, field-ordered rendering.</returns>
|
||||
protected abstract string CanonicalForm();
|
||||
}
|
||||
@@ -59,7 +59,7 @@ internal sealed record HistoryReadRawVariant(DateTime StartUtc, DateTime EndUtc,
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override string CanonicalForm() =>
|
||||
$"raw|{StartUtc.Ticks}|{EndUtc.Ticks}|{NumValuesPerNode}|{ReturnBounds}|{IsReadModified}";
|
||||
FormattableString.Invariant($"raw|{StartUtc.Ticks}|{EndUtc.Ticks}|{NumValuesPerNode}|{ReturnBounds}|{IsReadModified}");
|
||||
}
|
||||
|
||||
/// <summary>HistoryRead-Processed. The aggregate is per node (<see cref="HistoryReadPortItem.AggregateType"/>).</summary>
|
||||
@@ -74,8 +74,9 @@ internal sealed record HistoryReadProcessedVariant(DateTime StartUtc, DateTime E
|
||||
protected override string CanonicalForm()
|
||||
{
|
||||
var c = AggregateConfiguration;
|
||||
var config = c is null ? "null" : $"{c.UseServerCapabilitiesDefaults}|{c.TreatUncertainAsBad}|{c.PercentDataBad}|{c.PercentDataGood}|{c.UseSlopedExtrapolation}";
|
||||
return $"processed|{StartUtc.Ticks}|{EndUtc.Ticks}|{ProcessingInterval.Ticks}|{config}";
|
||||
var config = c is null ? "null" : FormattableString.Invariant(
|
||||
$"{c.UseServerCapabilitiesDefaults}|{c.TreatUncertainAsBad}|{c.PercentDataBad}|{c.PercentDataGood}|{c.UseSlopedExtrapolation}");
|
||||
return FormattableString.Invariant($"processed|{StartUtc.Ticks}|{EndUtc.Ticks}|{ProcessingInterval.Ticks}|{config}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +85,8 @@ internal sealed record HistoryReadProcessedVariant(DateTime StartUtc, DateTime E
|
||||
internal sealed record HistoryReadAtTimeVariant(IReadOnlyList<DateTime> TimestampsUtc) : HistoryReadVariant
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override string CanonicalForm() => "attime|" + string.Join(",", TimestampsUtc.Select(t => t.Ticks));
|
||||
protected override string CanonicalForm() =>
|
||||
"attime|" + string.Join(",", TimestampsUtc.Select(t => t.Ticks.ToString(System.Globalization.CultureInfo.InvariantCulture)));
|
||||
}
|
||||
|
||||
/// <summary>One node of a port request, addressed SDK-handle-free by <c>(realm, identifier)</c>.</summary>
|
||||
|
||||
@@ -222,6 +222,34 @@ public sealed class PeerHistoryContinuationStoreTests
|
||||
store.Count.ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_details_hash_is_culture_invariant()
|
||||
{
|
||||
var variants = new HistoryReadVariant[]
|
||||
{
|
||||
new HistoryReadRawVariant(DateTime.UnixEpoch, DateTime.UnixEpoch.AddHours(1), 10, ReturnBounds: true),
|
||||
new HistoryReadProcessedVariant(DateTime.UnixEpoch, DateTime.UnixEpoch.AddHours(1), TimeSpan.FromTicks(-5),
|
||||
new Opc.Ua.AggregateConfiguration { PercentDataBad = 100, PercentDataGood = 100 }),
|
||||
new HistoryReadAtTimeVariant([DateTime.UnixEpoch, DateTime.UnixEpoch.AddSeconds(1)]),
|
||||
};
|
||||
var invariant = variants.Select(v => v.ComputeDetailsHash()).ToArray();
|
||||
|
||||
var odd = (System.Globalization.CultureInfo)System.Globalization.CultureInfo.InvariantCulture.Clone();
|
||||
odd.NumberFormat.NegativeSign = "~";
|
||||
odd.NumberFormat.NumberGroupSeparator = "'";
|
||||
odd.TextInfo.ListSeparator = "#";
|
||||
var previous = System.Globalization.CultureInfo.CurrentCulture;
|
||||
try
|
||||
{
|
||||
System.Globalization.CultureInfo.CurrentCulture = odd;
|
||||
variants.Select(v => v.ComputeDetailsHash()).ToArray().ShouldBe(invariant);
|
||||
}
|
||||
finally
|
||||
{
|
||||
System.Globalization.CultureInfo.CurrentCulture = previous;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A monotonic test clock.</summary>
|
||||
private sealed class ManualClock : TimeProvider
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user