feat(batch1): add mapped proto scan helpers with boundary tests

This commit is contained in:
Joseph Doherty
2026-02-28 06:33:09 -05:00
parent d8d71eab95
commit c1ae46fc66
5 changed files with 148 additions and 4 deletions

View File

@@ -0,0 +1,77 @@
// Copyright 2012-2025 The NATS Authors
// Licensed under the Apache License, Version 2.0
using Shouldly;
using ZB.MOM.NatsNet.Server.Internal;
namespace ZB.MOM.NatsNet.Server.Tests.Internal;
public class ProtoWireTests
{
[Fact]
public void ProtoScanTag_ValidTag_ReturnsFieldTypeAndSize()
{
var (num, typ, size, err) = ProtoWire.ProtoScanTag([0x7A]); // field=15, type=2
err.ShouldBeNull();
num.ShouldBe(15);
typ.ShouldBe(2);
size.ShouldBe(1);
}
[Fact]
public void ProtoScanTag_InvalidFieldNumber_ReturnsError()
{
var (_, _, _, err) = ProtoWire.ProtoScanTag([0x02]); // field=0, type=2
err.ShouldNotBeNull();
err.Message.ShouldContain("invalid field number");
}
[Fact]
public void ProtoScanFieldValue_UnsupportedWireType_ReturnsError()
{
var (_, err) = ProtoWire.ProtoScanFieldValue(3, [0x01]);
err.ShouldNotBeNull();
err.Message.ShouldContain("unsupported type");
}
[Fact]
public void ProtoScanVarint_InsufficientData_ReturnsError()
{
var (_, _, err) = ProtoWire.ProtoScanVarint([0x80]);
err.ShouldNotBeNull();
err.Message.ShouldContain("insufficient data");
}
[Fact]
public void ProtoScanVarint_Overflow_ReturnsError()
{
var (_, _, err) = ProtoWire.ProtoScanVarint([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02]);
err.ShouldNotBeNull();
err.Message.ShouldContain("too much data");
}
[Fact]
public void ProtoScanBytes_LengthDelimited_ReturnsLengthPrefixPlusPayloadSize()
{
var (size, err) = ProtoWire.ProtoScanBytes([0x03, (byte)'a', (byte)'b', (byte)'c']);
err.ShouldBeNull();
size.ShouldBe(4);
}
[Fact]
public void ProtoScanField_ValidLengthDelimited_ReturnsTotalFieldSize()
{
var (num, typ, size, err) = ProtoWire.ProtoScanField([0x0A, 0x03, (byte)'a', (byte)'b', (byte)'c']); // field=1,type=2
err.ShouldBeNull();
num.ShouldBe(1);
typ.ShouldBe(2);
size.ShouldBe(5);
}
}