diff --git a/src/VectorNNTPD.BackFillerService/ArticleWork/ArticleWorkRequestParser.cs b/src/VectorNNTPD.BackFillerService/ArticleWork/ArticleWorkRequestParser.cs index 6e2a516f..798ab86e 100644 --- a/src/VectorNNTPD.BackFillerService/ArticleWork/ArticleWorkRequestParser.cs +++ b/src/VectorNNTPD.BackFillerService/ArticleWork/ArticleWorkRequestParser.cs @@ -13,12 +13,19 @@ namespace VectorNNTPD.BackFillerService.ArticleWork /// never synthesized. AMQP RequestId is not a JSON field: when present, it must /// match JSON requestId; when absent, the protocol still accepts the delivery /// (required AMQP fields are only CorrelationId and ReplyTo). + /// The body is read once with . Duplicate top-level + /// version, requestId, and backbone properties keep the last value. + /// Duplicate messageId properties keep the first value. A JSON null messageId + /// is treated as absent. /// internal static class ArticleWorkRequestParser { /// Supported application protocol versions. private const int CurrentVersion = 1; + /// Stack buffer for an ordinary requestId. Longer values use . + private const int RequestIdStackBytes = 128; + /// Required AMQP content type when the property is present. internal const string JsonContentType = "application/json"; @@ -59,194 +66,179 @@ internal static ArticleWorkParseResult Parse( return ArticleWorkParseResult.Invalid("RabbitMQ article-work payload was empty.", default); } - JsonDocument document; + PayloadFields fields; try { - document = JsonDocument.Parse(delivery.Body); + fields = ReadPayload(delivery.Body.Span); } catch (JsonException) { return ArticleWorkParseResult.Invalid("RabbitMQ article-work payload was not valid JSON.", default); } - using (document) + if (!fields.IsObject) { - var root = document.RootElement; - if (root.ValueKind != JsonValueKind.Object) - { - return ArticleWorkParseResult.Invalid("RabbitMQ article-work payload must be a JSON object.", default); - } - - _ = TryReadGuid(root, "requestId", out var requestId); - var messageIdRead = TryReadValidatedMessageId(delivery.Body.Span); - var messageId = messageIdRead.MessageId; - var rawMessageId = messageIdRead.PropertySeen ? messageId ?? "\u0001" : null; - _ = TryReadString(root, "backbone", out var rawBackbone); - var backbone = !string.IsNullOrWhiteSpace(rawBackbone) ? rawBackbone : null; - var identities = new ArticleWorkParsedIdentities(requestId, messageId, backbone); + return ArticleWorkParseResult.Invalid("RabbitMQ article-work payload must be a JSON object.", default); + } - if (!TryReadInt32(root, "version", out var version)) - { - return ArticleWorkParseResult.Invalid( - "RabbitMQ article-work payload is missing required integer property 'version'.", - identities); - } + var requestId = fields.RequestId; + var messageId = fields.MessageId; + var rawMessageId = fields.MessageIdPropertySeen ? messageId ?? "\u0001" : null; + var backbone = fields.Backbone; + var identities = new ArticleWorkParsedIdentities(requestId, messageId, backbone); - if (version != CurrentVersion) - { - return ArticleWorkParseResult.Invalid( - $"RabbitMQ article-work payload uses unsupported version '{version}'.", - identities); - } + if (!fields.HasVersion) + { + return ArticleWorkParseResult.Invalid( + "RabbitMQ article-work payload is missing required integer property 'version'.", + identities); + } - if (requestId is null) - { - return ArticleWorkParseResult.Invalid( - "RabbitMQ article-work payload contains missing or invalid 'requestId'.", - identities); - } + if (fields.Version != CurrentVersion) + { + return ArticleWorkParseResult.Invalid( + $"RabbitMQ article-work payload uses unsupported version '{fields.Version}'.", + identities); + } - if (string.IsNullOrWhiteSpace(rawMessageId)) - { - return ArticleWorkParseResult.Invalid( - "RabbitMQ article-work payload contains missing or invalid 'messageId'.", - identities with { MessageId = null }); - } + if (requestId is null) + { + return ArticleWorkParseResult.Invalid( + "RabbitMQ article-work payload contains missing or invalid 'requestId'.", + identities); + } - if (messageId is null) - { - return ArticleWorkParseResult.Invalid( - "RabbitMQ article-work payload 'messageId' is not a canonical NNTP Message-ID.", - identities with { MessageId = null }); - } + if (string.IsNullOrWhiteSpace(rawMessageId)) + { + return ArticleWorkParseResult.Invalid( + "RabbitMQ article-work payload contains missing or invalid 'messageId'.", + identities with { MessageId = null }); + } - if (backbone is null) - { - return ArticleWorkParseResult.Invalid( - "RabbitMQ article-work payload contains missing or invalid 'backbone'.", - identities); - } + if (messageId is null) + { + return ArticleWorkParseResult.Invalid( + "RabbitMQ article-work payload 'messageId' is not a canonical NNTP Message-ID.", + identities with { MessageId = null }); + } - if (!string.Equals(backbone, consumingBackbone, StringComparison.OrdinalIgnoreCase)) - { - return ArticleWorkParseResult.Invalid( - "RabbitMQ article-work payload backbone does not match the consuming queue backbone context.", - identities); - } + if (backbone is null) + { + return ArticleWorkParseResult.Invalid( + "RabbitMQ article-work payload contains missing or invalid 'backbone'.", + identities); + } - if (string.IsNullOrWhiteSpace(delivery.CorrelationId)) - { - return ArticleWorkParseResult.Invalid( - "RabbitMQ delivery is missing required AMQP CorrelationId property.", - identities); - } + if (!string.Equals(backbone, consumingBackbone, StringComparison.OrdinalIgnoreCase)) + { + return ArticleWorkParseResult.Invalid( + "RabbitMQ article-work payload backbone does not match the consuming queue backbone context.", + identities); + } - if (string.IsNullOrWhiteSpace(delivery.ReplyTo)) - { - return ArticleWorkParseResult.Invalid( - "RabbitMQ delivery is missing required AMQP ReplyTo property.", - identities); - } + if (string.IsNullOrWhiteSpace(delivery.CorrelationId)) + { + return ArticleWorkParseResult.Invalid( + "RabbitMQ delivery is missing required AMQP CorrelationId property.", + identities); + } - if (string.IsNullOrWhiteSpace(delivery.RequestIdHeader)) - return ArticleWorkParseResult.Valid(new ArticleWorkRequest( - CurrentVersion, - requestId.Value, - messageId, - backbone)); - if (!Guid.TryParse(delivery.RequestIdHeader, out var headerId) - || headerId == Guid.Empty - || headerId != requestId.Value) - { - return ArticleWorkParseResult.Invalid( - "RabbitMQ AMQP RequestId header does not match JSON requestId.", - identities); - } + if (string.IsNullOrWhiteSpace(delivery.ReplyTo)) + { + return ArticleWorkParseResult.Invalid( + "RabbitMQ delivery is missing required AMQP ReplyTo property.", + identities); + } + if (string.IsNullOrWhiteSpace(delivery.RequestIdHeader)) + { return ArticleWorkParseResult.Valid(new ArticleWorkRequest( CurrentVersion, requestId.Value, messageId, backbone)); } - } - /// - /// Validates the top-level messageId JSON string from the original payload bytes. - /// - /// Article-work request body. - /// The accepted ASCII Message-ID when the property is a valid token. - private static MessageIdRead TryReadValidatedMessageId(ReadOnlySpan json) - { - Span buffer = stackalloc byte[NntpMessageIdValidation.MaxMessageIdLength]; - switch (TryCopyTopLevelJsonString(json, "messageId"u8, buffer, out var length)) + if (!Guid.TryParse(delivery.RequestIdHeader, out var headerId) + || headerId == Guid.Empty + || headerId != requestId.Value) { - case TopLevelJsonString.Copied: - var token = buffer[..length]; - return NntpMessageIdValidation.IsValidMessageId(token) - ? new MessageIdRead(true, Encoding.ASCII.GetString(token)) - : new MessageIdRead(true, null); - case TopLevelJsonString.NotString: - case TopLevelJsonString.TooLong: - return new MessageIdRead(true, null); - default: - return new MessageIdRead(false, null); + return ArticleWorkParseResult.Invalid( + "RabbitMQ AMQP RequestId header does not match JSON requestId.", + identities); } - } - - /// Outcome of reading the top-level messageId string. - /// - /// when a top-level messageId property was present, including null, non-string, and overlong values. - /// when the property was absent. - /// - /// - /// The accepted ASCII Message-ID when the property was a valid token; otherwise . - /// - private readonly record struct MessageIdRead(bool PropertySeen, string? MessageId); - - /// How a top-level JSON string copy finished. Callers treat only as a candidate token. - private enum TopLevelJsonString - { - /// The property was absent, or the payload was not a JSON object. - Missing, - - /// The property was present and JSON null. - Null, - - /// The property was present and was not a JSON string. - NotString, - /// The property was a string longer than the destination buffer. - TooLong, - - /// The unescaped string bytes were copied into the destination. - Copied, + return ArticleWorkParseResult.Valid(new ArticleWorkRequest( + CurrentVersion, + requestId.Value, + messageId, + backbone)); } - /// Copies one top-level JSON string property into without a heap allocation. - /// UTF-8 JSON object. A payload that is not an object is . - /// Exact top-level property name. Nested properties are skipped. - /// Buffer that receives the unescaped string bytes. - /// Bytes copied when the result is ; otherwise zero. - /// Why the copy stopped. means the string did not fit . - private static TopLevelJsonString TryCopyTopLevelJsonString( - ReadOnlySpan json, - ReadOnlySpan propertyName, - Span destination, - out int written) + /// Fields taken from one complete JSON value. + /// when the root value is a JSON object. + /// when the last top-level version is an integer. + /// That integer when is ; otherwise zero. + /// The last top-level requestId when it is a non-empty GUID. + /// + /// when the first top-level messageId was present and was not JSON null. + /// + /// The accepted ASCII Message-ID from that first property; otherwise . + /// The last top-level backbone string when it is not blank. + private readonly record struct PayloadFields( + bool IsObject, + bool HasVersion, + int Version, + Guid? RequestId, + bool MessageIdPropertySeen, + string? MessageId, + string? Backbone); + + /// Reads one JSON value. Malformed, truncated, or trailing JSON throws . + /// UTF-8 payload bytes. The span is not retained. + /// Root-object fields, or a default result when the complete value is not an object. + private static PayloadFields ReadPayload(ReadOnlySpan json) { - written = 0; var reader = new Utf8JsonReader(json); - if (!reader.Read() || reader.TokenType != JsonTokenType.StartObject) + if (!reader.Read()) { - return TopLevelJsonString.Missing; + throw new JsonException(); } + if (reader.TokenType != JsonTokenType.StartObject) + { + SkipValueIfContainer(ref reader); + if (reader.Read()) + { + throw new JsonException(); + } + + return default; + } + + var version = default(VersionRead); + Guid? requestId = null; + var messageIdLocked = false; + var messageId = default(MessageIdRead); + string? backbone = null; + while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject && reader.CurrentDepth == 0) { - return TopLevelJsonString.Missing; + if (reader.Read()) + { + throw new JsonException(); + } + + return new PayloadFields( + true, + version.HasVersion, + version.Version, + requestId, + messageId.PropertySeen, + messageId.MessageId, + backbone); } if (reader.TokenType != JsonTokenType.PropertyName || reader.CurrentDepth != 1) @@ -254,116 +246,213 @@ private static TopLevelJsonString TryCopyTopLevelJsonString( continue; } - var match = reader.ValueTextEquals(propertyName); + var versionName = reader.ValueTextEquals("version"u8); + var requestName = reader.ValueTextEquals("requestId"u8); + var messageName = reader.ValueTextEquals("messageId"u8); + var backboneName = reader.ValueTextEquals("backbone"u8); if (!reader.Read()) { - return TopLevelJsonString.Missing; + throw new JsonException(); } - if (!match) + if (versionName) + { + ApplyVersion(ref reader, ref version); + } + else if (requestName) + { + requestId = ReadRequestId(ref reader); + } + else if (messageName) { - if (reader.TokenType is JsonTokenType.StartObject or JsonTokenType.StartArray) + if (messageIdLocked) { - reader.Skip(); + SkipValueIfContainer(ref reader); + } + else + { + messageIdLocked = true; + ApplyMessageId(ref reader, ref messageId); } - - continue; } - - if (reader.TokenType == JsonTokenType.Null) + else if (backboneName) { - return TopLevelJsonString.Null; + backbone = ReadBackbone(ref reader); } - - if (reader.TokenType != JsonTokenType.String) + else { - return TopLevelJsonString.NotString; + SkipValueIfContainer(ref reader); } + } + + throw new JsonException(); + } + + /// Last top-level integer version. A later non-integer clears . + private struct VersionRead + { + /// when the current value is an integer. + public bool HasVersion; + + /// The accepted integer, or zero when is . + public int Version; + } + + /// Applies the current top-level version value. A non-integer clears any earlier integer. + /// Reader positioned on the property value. + /// Version state updated in place. + private static void ApplyVersion(ref Utf8JsonReader reader, ref VersionRead version) + { + if (reader.TokenType == JsonTokenType.Number && reader.TryGetInt32(out var parsed)) + { + version.HasVersion = true; + version.Version = parsed; + return; + } + version.HasVersion = false; + version.Version = 0; + SkipValueIfContainer(ref reader); + } + + /// Reads the current top-level requestId string as a non-empty GUID. + /// Reader positioned on the property value. The parser constructs it from one span. + /// The GUID, or when the value is missing, blank, not a GUID, or . + /// + /// A reader over one span has false, so + /// is the complete raw value. When that flag is true, + /// is only the first segment and + /// holds the rest, so those values use . + /// JSON escapes decode to no more UTF-8 bytes than their raw form, so a raw value that fits + /// is copied on the stack. A longer value uses + /// on the decoded string. + /// + private static Guid? ReadRequestId(ref Utf8JsonReader reader) + { + if (reader.TokenType != JsonTokenType.String) + { + SkipValueIfContainer(ref reader); + return null; + } + + if (!reader.HasValueSequence && reader.ValueSpan.Length <= RequestIdStackBytes) + { + Span bytes = stackalloc byte[RequestIdStackBytes]; + int written; if (!reader.ValueIsEscaped) { - var value = reader.ValueSpan; - if (value.Length > destination.Length) - { - return TopLevelJsonString.TooLong; - } - - value.CopyTo(destination); - written = value.Length; - return TopLevelJsonString.Copied; + reader.ValueSpan.CopyTo(bytes); + written = reader.ValueSpan.Length; } - - try + else { - written = reader.CopyString(destination); - return TopLevelJsonString.Copied; + written = reader.CopyString(bytes); } - catch (ArgumentException) + + Span chars = stackalloc char[written]; + var charCount = Encoding.UTF8.GetChars(bytes[..written], chars); + var text = chars[..charCount]; + if (text.IsWhiteSpace() || !Guid.TryParse(text, out var parsed) || parsed == Guid.Empty) { - written = 0; - return TopLevelJsonString.TooLong; + return null; } + + return parsed; + } + + var owned = reader.GetString(); + if (string.IsNullOrWhiteSpace(owned) || !Guid.TryParse(owned, out var guid) || guid == Guid.Empty) + { + return null; } - return TopLevelJsonString.Missing; + return guid; } - /// Reads a case-sensitive JSON number property as a 32-bit integer. - /// Payload object. - /// Exact property name. - /// Parsed value when the result is ; otherwise zero. - /// - /// when exists, is a JSON number, and fits . - /// - private static bool TryReadInt32(JsonElement root, string name, out int value) + /// First top-level messageId. JSON null leaves false. + private struct MessageIdRead { - value = 0; - return root.TryGetProperty(name, out var property) - && property.ValueKind == JsonValueKind.Number - && property.TryGetInt32(out value); + /// when the value was not JSON null. + public bool PropertySeen; + + /// The accepted ASCII Message-ID, or when the value is not a valid token. + public string? MessageId; } - /// Reads a case-sensitive JSON string property without treating empty text as missing. - /// Payload object. - /// Exact property name. - /// The string when the result is ; otherwise null. - /// - /// when exists, is a JSON string, and is not JSON null. - /// - private static bool TryReadString(JsonElement root, string name, out string? value) + /// + /// Applies the first top-level messageId value. JSON null leaves the property unseen. + /// + /// Reader positioned on the property value. + /// Message-ID state updated in place. The caller does not invoke this for a later duplicate. + private static void ApplyMessageId(ref Utf8JsonReader reader, ref MessageIdRead messageId) { - value = null; - if (!root.TryGetProperty(name, out var property) || property.ValueKind != JsonValueKind.String) + if (reader.TokenType == JsonTokenType.Null) + { + return; + } + + messageId.PropertySeen = true; + if (reader.TokenType != JsonTokenType.String) + { + SkipValueIfContainer(ref reader); + return; + } + + Span buffer = stackalloc byte[NntpMessageIdValidation.MaxMessageIdLength]; + int written; + if (!reader.ValueIsEscaped) { - return false; + var value = reader.ValueSpan; + if (value.Length > buffer.Length) + { + return; + } + + value.CopyTo(buffer); + written = value.Length; + } + else + { + try + { + written = reader.CopyString(buffer); + } + catch (ArgumentException) + { + return; + } } - value = property.GetString(); - return value is not null; + var token = buffer[..written]; + if (NntpMessageIdValidation.IsValidMessageId(token)) + { + messageId.MessageId = Encoding.ASCII.GetString(token); + } } - /// Reads a case-sensitive JSON string property as a non-empty GUID. - /// Payload object. - /// Exact property name. - /// The GUID when the result is ; otherwise null. - /// - /// when the property is missing, not a string, blank, not a GUID, or . - /// - private static bool TryReadGuid(JsonElement root, string name, out Guid? value) + /// Reads the current top-level backbone string. + /// Reader positioned on the property value. + /// The unescaped string when it is not blank; otherwise . + private static string? ReadBackbone(ref Utf8JsonReader reader) { - value = null; - if (!TryReadString(root, name, out var text) || string.IsNullOrWhiteSpace(text)) + if (reader.TokenType != JsonTokenType.String) { - return false; + SkipValueIfContainer(ref reader); + return null; } - if (!Guid.TryParse(text, out var parsed) || parsed == Guid.Empty) + var text = reader.GetString(); + return string.IsNullOrWhiteSpace(text) ? null : text; + } + + /// Consumes an object or array value. Primitive values stay on the current token. + /// Reader positioned on a property value or a non-object root. + private static void SkipValueIfContainer(ref Utf8JsonReader reader) + { + if (reader.TokenType is JsonTokenType.StartObject or JsonTokenType.StartArray) { - return false; + reader.Skip(); } - - value = parsed; - return true; } } } diff --git a/tests/VectorNNTPD.BackFillerService.Tests/ArticleWork/ArticleWorkRequestParserAllocationProbe.cs b/tests/VectorNNTPD.BackFillerService.Tests/ArticleWork/ArticleWorkRequestParserAllocationProbe.cs new file mode 100644 index 00000000..49e750fa --- /dev/null +++ b/tests/VectorNNTPD.BackFillerService.Tests/ArticleWork/ArticleWorkRequestParserAllocationProbe.cs @@ -0,0 +1,75 @@ +using VectorNNTPD.BackFillerService.ArticleWork; +using VectorNNTPD.BackFillerService.Tests.Fixtures; +using System.Text.Json; + +namespace VectorNNTPD.BackFillerService.Tests.ArticleWork +{ + /// + /// Reports managed bytes allocated by one valid Article Work parse. + /// The counts are diagnostic. They are not a correctness contract. + /// + public sealed class ArticleWorkRequestParserAllocationProbe + { + /// Writes per-parse allocation counts for the reader path and a JsonDocument comparison. + [Fact] + public void Valid_payload_allocation_is_reported() + { + var delivery = ArticleWorkTestDeliveries.Canonical(); + const int warmup = 100; + const int iterations = 1000; + for (var i = 0; i < warmup; i++) + { + _ = ArticleWorkRequestParser.Parse(delivery, "Giganews", 1024); + _ = ParseWithJsonDocument(delivery.Body); + } + + var readerBytes = Measure(iterations, () => ArticleWorkRequestParser.Parse(delivery, "Giganews", 1024).Request!.MessageId.Length); + var documentBytes = Measure(iterations, () => ParseWithJsonDocument(delivery.Body).Request!.MessageId.Length); + + var readerPerParse = readerBytes / (double)iterations; + var documentPerParse = documentBytes / (double)iterations; + Console.WriteLine( + $"ArticleWorkRequestParser.Parse iterations={iterations} allocated={readerBytes} perParse={readerPerParse:F1}"); + Console.WriteLine( + $"JsonDocument comparison iterations={iterations} allocated={documentBytes} perParse={documentPerParse:F1}"); + Console.WriteLine( + "Measured after warmup on this thread. Both sides return ArticleWorkParseResult for the canonical payload. The JsonDocument side also copies requestId. It does not run the former parser."); + + Assert.True(ArticleWorkRequestParser.Parse(delivery, "Giganews", 1024).IsValid); + } + + /// Builds the same request through so the probe can compare allocator traffic. + /// Canonical UTF-8 payload. + /// The parse result constructed from the DOM. + private static ArticleWorkParseResult ParseWithJsonDocument(ReadOnlyMemory json) + { + using var document = JsonDocument.Parse(json); + var root = document.RootElement; + var requestId = Guid.Parse(root.GetProperty("requestId").GetString()!); + var messageId = root.GetProperty("messageId").GetString()!; + var backbone = root.GetProperty("backbone").GetString()!; + return ArticleWorkParseResult.Valid(new ArticleWorkRequest(1, requestId, messageId, backbone)); + } + + /// Returns bytes allocated on this thread by calls. + /// Number of measured calls. + /// Work to measure. Its result is consumed so the call stays live. + /// Allocated bytes for the whole loop. + private static long Measure(int iterations, Func action) + { + var before = GC.GetAllocatedBytesForCurrentThread(); + var sink = 0; + for (var i = 0; i < iterations; i++) + { + sink += action(); + } + + if (sink <= 0) + { + throw new InvalidOperationException("Measured work produced no output."); + } + + return GC.GetAllocatedBytesForCurrentThread() - before; + } + } +} diff --git a/tests/VectorNNTPD.BackFillerService.Tests/ArticleWork/ArticleWorkRequestParserTests.cs b/tests/VectorNNTPD.BackFillerService.Tests/ArticleWork/ArticleWorkRequestParserTests.cs index ce299143..1c465295 100644 --- a/tests/VectorNNTPD.BackFillerService.Tests/ArticleWork/ArticleWorkRequestParserTests.cs +++ b/tests/VectorNNTPD.BackFillerService.Tests/ArticleWork/ArticleWorkRequestParserTests.cs @@ -1,6 +1,7 @@ using VectorNNTPD.BackFillerService.ArticleWork; using VectorNNTPD.BackFillerService.Tests.Fixtures; using System.Text; +using System.Text.Json; namespace VectorNNTPD.BackFillerService.Tests.ArticleWork { @@ -546,6 +547,457 @@ public void Malformed_json_within_size_limit_produces_distinct_error() Assert.DoesNotContain("WorkRequestMaxPayloadBytes", parsed.Failure.Reason, StringComparison.Ordinal); } + /// + /// Content type and the AMQP request-id header are optional. The JSON identities still come from the body. + /// + [Fact] + public void Optional_content_type_and_request_id_header_may_be_omitted() + { + var parsed = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create( + ArticleWorkTestDeliveries.CanonicalRequestJson, + contentType: null, + requestIdHeader: null), + "Giganews", + MaxPayload); + + Assert.True(parsed.IsValid); + Assert.Equal(1, parsed.Request!.Version); + Assert.Equal(Guid.Parse(ArticleWorkTestDeliveries.CanonicalRequestId), parsed.Request.RequestId); + Assert.Equal(ArticleWorkTestDeliveries.CanonicalMessageId, parsed.Request.MessageId); + Assert.Equal("Giganews", parsed.Request.Backbone); + } + + /// + /// A requestId longer than the stack buffer is accepted when accepts it. + /// Whitespace-only text and other long non-GUID strings stay invalid. + /// + [Fact] + public void Long_request_ids_follow_guid_try_parse() + { + const string canonical = ArticleWorkTestDeliveries.CanonicalRequestId; + var padded = new string(' ', 100) + canonical + new string(' ', 100); + var expandedX = "{0x" + new string('0', 80) + "7c1cb8a0,0x95f9,0x4c13,{0x8e,0x53,0x33,0x97,0x73,0xe3,0xaf,0xaa}}"; + var spacedX = "{0x7c1cb8a0," + new string(' ', 80) + "0x95f9,0x4c13,{0x8e,0x53,0x33,0x97,0x73,0xe3,0xaf,0xaa}}"; + var invalid = new string('z', 160); + + AssertAcceptedRequestId(padded); + AssertAcceptedRequestId(expandedX); + AssertAcceptedRequestId(spacedX); + AssertRejectedRequestId(invalid); + AssertRejectedRequestId(" \t"); + } + + /// + /// Duplicate version, requestId, and backbone keep the last value. + /// Duplicate messageId keeps the first value. + /// + [Fact] + public void Duplicate_properties_keep_last_value_except_message_id() + { + var lastVersion = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create( + """{"version":9,"version":1,"requestId":"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa","requestId":"7c1cb8a0-95f9-4c13-8e53-339773e3afaa","messageId":"","messageId":"","backbone":"Other","backbone":"Giganews"}"""), + "Giganews", + MaxPayload); + var clearedVersion = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create( + """{"version":1,"version":"1","requestId":"7c1cb8a0-95f9-4c13-8e53-339773e3afaa","messageId":"<12345@example.invalid>","backbone":"Giganews"}"""), + "Giganews", + MaxPayload); + var clearedRequest = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create( + """{"version":1,"requestId":"7c1cb8a0-95f9-4c13-8e53-339773e3afaa","requestId":"not-a-guid","messageId":"<12345@example.invalid>","backbone":"Giganews"}"""), + "Giganews", + MaxPayload); + + Assert.True(lastVersion.IsValid); + Assert.Equal(Guid.Parse(ArticleWorkTestDeliveries.CanonicalRequestId), lastVersion.Request!.RequestId); + Assert.Equal("", lastVersion.Request.MessageId); + Assert.Equal("Giganews", lastVersion.Request.Backbone); + + Assert.False(clearedVersion.IsValid); + Assert.Contains("missing required integer property 'version'", clearedVersion.Failure!.Reason, StringComparison.Ordinal); + Assert.Equal(Guid.Parse(ArticleWorkTestDeliveries.CanonicalRequestId), clearedVersion.Failure.Identities.RequestId); + + Assert.False(clearedRequest.IsValid); + Assert.Contains("missing or invalid 'requestId'", clearedRequest.Failure!.Reason, StringComparison.Ordinal); + Assert.Null(clearedRequest.Failure.Identities.RequestId); + Assert.Equal(ArticleWorkTestDeliveries.CanonicalMessageId, clearedRequest.Failure.Identities.MessageId); + } + + /// + /// A JSON null messageId is absent, including when a later duplicate would have been valid. + /// A non-string value is present and invalid. + /// + [Fact] + public void Null_message_id_is_absent_and_non_string_message_id_is_not_canonical() + { + var absent = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create( + """{"version":1,"requestId":"7c1cb8a0-95f9-4c13-8e53-339773e3afaa","messageId":null,"messageId":"<12345@example.invalid>","backbone":"Giganews"}"""), + "Giganews", + MaxPayload); + var nonString = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create( + """{"version":1,"requestId":"7c1cb8a0-95f9-4c13-8e53-339773e3afaa","messageId":false,"backbone":"Giganews"}"""), + "Giganews", + MaxPayload); + + Assert.False(absent.IsValid); + Assert.Contains("missing or invalid 'messageId'", absent.Failure!.Reason, StringComparison.Ordinal); + Assert.Null(absent.Failure.Identities.MessageId); + Assert.Equal(Guid.Parse(ArticleWorkTestDeliveries.CanonicalRequestId), absent.Failure.Identities.RequestId); + + Assert.False(nonString.IsValid); + Assert.Contains("not a canonical NNTP Message-ID", nonString.Failure!.Reason, StringComparison.Ordinal); + Assert.Null(nonString.Failure.Identities.MessageId); + } + + /// + /// Nested objects and arrays are not root fields, including when their names match required properties. + /// + [Fact] + public void Nested_objects_and_arrays_do_not_supply_root_properties() + { + var json = + """{"extra":{"version":2,"requestId":"not-a-guid","messageId":"nested","backbone":"Other"},"items":[1,{"version":9}],"version":1,"requestId":"7c1cb8a0-95f9-4c13-8e53-339773e3afaa","messageId":"<12345@example.invalid>","backbone":"Giganews"}"""; + + var parsed = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create(json), + "Giganews", + MaxPayload); + + Assert.True(parsed.IsValid); + Assert.Equal(ArticleWorkTestDeliveries.CanonicalMessageId, parsed.Request!.MessageId); + Assert.Equal("Giganews", parsed.Request.Backbone); + } + + /// + /// A complete non-object root is a schema error. Trailing content after any complete value is invalid JSON. + /// + [Theory] + [InlineData("[]", "must be a JSON object")] + [InlineData("null", "must be a JSON object")] + [InlineData("1", "must be a JSON object")] + [InlineData("\"text\"", "must be a JSON object")] + [InlineData("[][]", "not valid JSON")] + [InlineData("1 2", "not valid JSON")] + [InlineData("{\"version\":1} {\"version\":1}", "not valid JSON")] + public void Wrong_roots_and_trailing_values_are_classified(string json, string reason) + { + var parsed = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create(json), + "Giganews", + MaxPayload); + + Assert.False(parsed.IsValid); + Assert.Contains(reason, parsed.Failure!.Reason, StringComparison.Ordinal); + Assert.Null(parsed.Failure.Identities.RequestId); + Assert.Null(parsed.Failure.Identities.MessageId); + Assert.Null(parsed.Failure.Identities.Backbone); + } + + /// + /// Truncated objects and arrays are invalid even when every required field appeared before the cut. + /// + [Theory] + [InlineData("{\"version\":1")] + [InlineData("{\"version\":")] + [InlineData("[")] + [InlineData("{\"version\":1,\"requestId\":\"7c1cb8a0-95f9-4c13-8e53-339773e3afaa\",\"messageId\":\"<12345@example.invalid>\",\"backbone\":\"Giganews\"")] + public void Truncated_json_is_not_a_valid_request(string json) + { + var parsed = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create(json), + "Giganews", + MaxPayload); + + Assert.False(parsed.IsValid); + Assert.Contains("not valid JSON", parsed.Failure!.Reason, StringComparison.Ordinal); + Assert.Null(parsed.Failure.Identities.RequestId); + } + + /// + /// Wrong JSON types for required properties use the same reasons as a missing property of that type. + /// + [Fact] + public void Wrong_property_types_are_rejected() + { + var version = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create( + """{"version":1.5,"requestId":"7c1cb8a0-95f9-4c13-8e53-339773e3afaa","messageId":"<12345@example.invalid>","backbone":"Giganews"}"""), + "Giganews", + MaxPayload); + var requestId = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create( + """{"version":1,"requestId":1,"messageId":"<12345@example.invalid>","backbone":"Giganews"}"""), + "Giganews", + MaxPayload); + var backbone = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create( + """{"version":1,"requestId":"7c1cb8a0-95f9-4c13-8e53-339773e3afaa","messageId":"<12345@example.invalid>","backbone":["Giganews"]}"""), + "Giganews", + MaxPayload); + var emptyMessage = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create( + """{"version":1,"requestId":"7c1cb8a0-95f9-4c13-8e53-339773e3afaa","messageId":"","backbone":"Giganews"}"""), + "Giganews", + MaxPayload); + + Assert.Contains("missing required integer property 'version'", version.Failure!.Reason, StringComparison.Ordinal); + Assert.Contains("missing or invalid 'requestId'", requestId.Failure!.Reason, StringComparison.Ordinal); + Assert.Contains("missing or invalid 'backbone'", backbone.Failure!.Reason, StringComparison.Ordinal); + Assert.Contains("not a canonical NNTP Message-ID", emptyMessage.Failure!.Reason, StringComparison.Ordinal); + Assert.Equal(Guid.Parse(ArticleWorkTestDeliveries.CanonicalRequestId), version.Failure.Identities.RequestId); + Assert.Null(requestId.Failure.Identities.RequestId); + Assert.Null(backbone.Failure.Identities.Backbone); + } + + /// + /// JSON string escapes are unescaped before validation. Property-name escapes use the same names. + /// + [Fact] + public void Escaped_strings_and_property_names_are_unescaped() + { + var json = + """{"\u0076ersion":1,"requestId":"\u0037c1cb8a0-95f9-4c13-8e53-339773e3afaa","messageId":"\u003c12345@example.invalid\u003e","backbone":"\u0047iganews"}"""; + + var parsed = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create(json), + "Giganews", + MaxPayload); + + Assert.True(parsed.IsValid); + Assert.Equal(Guid.Parse(ArticleWorkTestDeliveries.CanonicalRequestId), parsed.Request!.RequestId); + Assert.Equal(ArticleWorkTestDeliveries.CanonicalMessageId, parsed.Request.MessageId); + Assert.Equal("Giganews", parsed.Request.Backbone); + } + + /// + /// Non-ASCII backbone text is preserved as the unescaped JSON string and still participates in matching. + /// + [Fact] + public void Unicode_backbone_is_preserved() + { + var json = + """{"version":1,"requestId":"7c1cb8a0-95f9-4c13-8e53-339773e3afaa","messageId":"<12345@example.invalid>","backbone":"b\u00e4ck"}"""; + + var parsed = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create(json), + "bäck", + MaxPayload); + + Assert.True(parsed.IsValid); + Assert.Equal("bäck", parsed.Request!.Backbone); + } + + /// + /// Comments and trailing commas are rejected as invalid JSON. + /// + [Theory] + [InlineData("""{"version":1,}""")] + [InlineData("""{"version":1 /* note */}""")] + [InlineData(" \n\t")] + public void Comments_trailing_commas_and_whitespace_only_payloads_are_invalid_json(string json) + { + var parsed = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create(json), + "Giganews", + MaxPayload); + + Assert.False(parsed.IsValid); + Assert.Contains("not valid JSON", parsed.Failure!.Reason, StringComparison.Ordinal); + } + + /// + /// A payload of exactly the configured maximum is parsed. One byte over that maximum is rejected before JSON is read. + /// + [Fact] + public void Exact_maximum_payload_is_parsed_and_oversized_malformed_json_is_not() + { + var canonical = Encoding.UTF8.GetBytes(ArticleWorkTestDeliveries.CanonicalRequestJson); + var exact = new byte[MaxPayload]; + canonical.CopyTo(exact, 0); + exact.AsSpan(canonical.Length).Fill((byte)' '); + var exactParsed = ArticleWorkRequestParser.Parse(Delivery(exact), "Giganews", MaxPayload); + + var oversized = new byte[MaxPayload + 1]; + oversized[0] = (byte)'{'; + var oversizedParsed = ArticleWorkRequestParser.Parse(Delivery(oversized), "Giganews", MaxPayload); + + Assert.True(exactParsed.IsValid); + Assert.Equal(ArticleWorkTestDeliveries.CanonicalMessageId, exactParsed.Request!.MessageId); + Assert.False(oversizedParsed.IsValid); + Assert.Contains("WorkRequestMaxPayloadBytes", oversizedParsed.Failure!.Reason, StringComparison.Ordinal); + Assert.DoesNotContain("not valid JSON", oversizedParsed.Failure.Reason, StringComparison.Ordinal); + Assert.Null(oversizedParsed.Failure.Identities.RequestId); + Assert.Null(oversizedParsed.Failure.Identities.MessageId); + Assert.Null(oversizedParsed.Failure.Identities.Backbone); + } + + /// + /// A UTF-8 BOM is not JSON. rejects it, and the parser reports the same invalid-JSON result. + /// + [Fact] + public void Utf8_bom_is_not_valid_json() + { + var canonical = Encoding.UTF8.GetBytes(ArticleWorkTestDeliveries.CanonicalRequestJson); + var body = new byte[canonical.Length + 3]; + body[0] = 0xEF; + body[1] = 0xBB; + body[2] = 0xBF; + canonical.CopyTo(body.AsSpan(3)); + + var documentRejected = false; + try + { + using var document = JsonDocument.Parse(body); + } + catch (JsonException) + { + documentRejected = true; + } + + Assert.True(documentRejected); + + var parsed = ArticleWorkRequestParser.Parse(Delivery(body), "Giganews", MaxPayload); + + Assert.False(parsed.IsValid); + Assert.Contains("not valid JSON", parsed.Failure!.Reason, StringComparison.Ordinal); + Assert.Null(parsed.Failure.Identities.RequestId); + } + + /// + /// Nesting beyond the JSON maximum of 64 is invalid JSON. The root object counts, so 63 nested objects are the deepest accepted extra value. + /// + [Fact] + public void Excessive_nesting_is_invalid_json() + { + var accepted = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create(NestedExtra(63) + CanonicalFields), + "Giganews", + MaxPayload); + var rejected = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create(NestedExtra(64) + CanonicalFields), + "Giganews", + MaxPayload); + + Assert.True(accepted.IsValid); + Assert.False(rejected.IsValid); + Assert.Contains("not valid JSON", rejected.Failure!.Reason, StringComparison.Ordinal); + } + + /// + /// Builds a delivery whose body is the given bytes and whose other properties are the canonical fixture values. + /// + /// Application payload bytes. + /// A synthetic manual-ack delivery. + private static VectorNNTPD.Common.Messaging.RabbitMq.RabbitMqManualAckDelivery Delivery(byte[] body) + { + return new VectorNNTPD.Common.Messaging.RabbitMq.RabbitMqManualAckDelivery( + 1, + body, + ArticleWorkTestDeliveries.CanonicalCorrelationId, + ArticleWorkTestDeliveries.CanonicalReplyTo, + ArticleWorkRequestParser.JsonContentType, + ArticleWorkTestDeliveries.CanonicalRequestId, + false, + "backfiller.giganews", + "backfiller.giganews", + "ctag", + 1); + } + + /// Required canonical fields, without a leading comma, used after an extra property. + private const string CanonicalFields = + "\"version\":1,\"requestId\":\"7c1cb8a0-95f9-4c13-8e53-339773e3afaa\",\"messageId\":\"<12345@example.invalid>\",\"backbone\":\"Giganews\"}"; + + /// Builds {"extra": followed by nested objects. + /// Number of nested objects, including the outermost extra value. + /// A JSON prefix that is not yet closed. + private static string NestedExtra(int depth) + { + var json = new StringBuilder("{\"extra\":"); + for (var i = 0; i < depth; i++) + { + json.Append("{\"a\":"); + } + + json.Append('1'); + for (var i = 0; i < depth; i++) + { + json.Append('}'); + } + + json.Append(','); + return json.ToString(); + } + + /// + /// Accepts when the original string predicate accepts it, and requires the payload to stay under the size cap. + /// + /// JSON string value whose UTF-8 form is longer than the stack buffer. + private static void AssertAcceptedRequestId(string requestId) + { + Assert.True(Encoding.UTF8.GetByteCount(requestId) > 128); + Assert.False(IsRejectedRequestId(requestId)); + Assert.True(Guid.TryParse(requestId, out var expected)); + + var json = RequestJson(requestId); + Assert.True(Encoding.UTF8.GetByteCount(json) < MaxPayload); + + var parsed = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create(json), + "Giganews", + MaxPayload); + + Assert.True(parsed.IsValid); + Assert.Equal(expected, parsed.Request!.RequestId); + } + + /// + /// Rejects when the original string predicate rejects it. + /// + /// JSON string value that is blank or not a non-empty GUID. + private static void AssertRejectedRequestId(string requestId) + { + Assert.True(IsRejectedRequestId(requestId)); + + var json = RequestJson(requestId); + Assert.True(Encoding.UTF8.GetByteCount(json) < MaxPayload); + + var parsed = ArticleWorkRequestParser.Parse( + ArticleWorkTestDeliveries.Create(json), + "Giganews", + MaxPayload); + + Assert.False(parsed.IsValid); + Assert.Contains("missing or invalid 'requestId'", parsed.Failure!.Reason, StringComparison.Ordinal); + Assert.Null(parsed.Failure.Identities.RequestId); + Assert.Equal(ArticleWorkTestDeliveries.CanonicalMessageId, parsed.Failure.Identities.MessageId); + Assert.Equal("Giganews", parsed.Failure.Identities.Backbone); + } + + /// Original requestId rejection predicate. + /// Decoded JSON string. + /// when the original parser rejected the text. + private static bool IsRejectedRequestId(string requestId) + { + return string.IsNullOrWhiteSpace(requestId) + || !Guid.TryParse(requestId, out var guid) + || guid == Guid.Empty; + } + + /// Builds a v1 payload whose only varying field is . + /// Decoded request id inserted as a JSON string. + /// UTF-16 JSON text. + private static string RequestJson(string requestId) + { + return "{\"version\":1,\"requestId\":" + JsonSerializer.Serialize(requestId) + + ",\"messageId\":\"<12345@example.invalid>\",\"backbone\":\"Giganews\"}"; + } + /// /// Parses a minimal JSON envelope with the given message-id for validator matrix checks. ///