using System;
using System.Text;
using NewLife.NovaDb.Server;
using Xunit;
namespace XUnitTest.Server;
/// <summary>å议头å•元测试</summary>
public class ProtocolTests
{
[Fact(DisplayName = "测试å议头åºåˆ—化与ååºåˆ—化")]
public void TestHeaderRoundTrip()
{
var header = new ProtocolHeader
{
Version = 1,
RequestType = RequestType.Execute,
SequenceId = 12345,
PayloadLength = 100,
Status = ResponseStatus.Ok
};
var bytes = header.ToBytes();
Assert.Equal(ProtocolHeader.HeaderSize, bytes.Length);
var parsed = ProtocolHeader.FromBytes(bytes);
Assert.Equal(header.Version, parsed.Version);
Assert.Equal(header.RequestType, parsed.RequestType);
Assert.Equal(header.SequenceId, parsed.SequenceId);
Assert.Equal(header.PayloadLength, parsed.PayloadLength);
Assert.Equal(header.Status, parsed.Status);
}
[Fact(DisplayName = "测试åè®®é”æ•°éªŒè¯")]
public void TestMagicNumberValidation()
{
var bytes = new Byte[ProtocolHeader.HeaderSize];
// è®¾ç½®é”™è¯¯çš„é”æ•°
bytes[0] = 0xFF;
bytes[1] = 0xFF;
Assert.Throws<InvalidOperationException>(() => ProtocolHeader.FromBytes(bytes));
}
[Fact(DisplayName = "测试åè®®é”æ•°å€¼")]
public void TestMagicNumberValue()
{
Assert.Equal((UInt16)0x4E56, ProtocolHeader.Magic);
var header = new ProtocolHeader();
var bytes = header.ToBytes();
// 验è¯å‰ä¸¤ä¸ªå—èŠ‚ä¸ºé”æ•°
Assert.Equal(0x4E, bytes[0]);
Assert.Equal(0x56, bytes[1]);
}
[Fact(DisplayName = "测试所有请求类型åºåˆ—化")]
public void TestAllRequestTypes()
{
var requestTypes = new[]
{
RequestType.Handshake,
RequestType.Execute,
RequestType.Query,
RequestType.Fetch,
RequestType.Close,
RequestType.Ping,
RequestType.BeginTx,
RequestType.CommitTx,
RequestType.RollbackTx
};
foreach (var reqType in requestTypes)
{
var header = new ProtocolHeader { RequestType = reqType, SequenceId = (UInt32)reqType };
var bytes = header.ToBytes();
var parsed = ProtocolHeader.FromBytes(bytes);
Assert.Equal(reqType, parsed.RequestType);
Assert.Equal((UInt32)reqType, parsed.SequenceId);
}
}
[Fact(DisplayName = "æµ‹è¯•ç¼“å†²åŒºè¿‡å°æŠ›å‡ºå¼‚å¸¸")]
public void TestBufferTooSmallThrows()
{
var bytes = new Byte[8];
Assert.Throws<ArgumentException>(() => ProtocolHeader.FromBytes(bytes));
}
[Fact(DisplayName = "测试空缓冲区抛出异常")]
public void TestNullBufferThrows()
{
Assert.Throws<ArgumentNullException>(() => ProtocolHeader.FromBytes(null!));
}
[Fact(DisplayName = "测试头部大å°ä¸º 16 å—节")]
public void TestHeaderSize()
{
Assert.Equal(16, ProtocolHeader.HeaderSize);
var header = new ProtocolHeader();
var bytes = header.ToBytes();
Assert.Equal(16, bytes.Length);
}
[Fact(DisplayName = "测试大åºåˆ—å·")]
public void TestLargeSequenceId()
{
var header = new ProtocolHeader
{
SequenceId = UInt32.MaxValue,
PayloadLength = 1024
};
var bytes = header.ToBytes();
var parsed = ProtocolHeader.FromBytes(bytes);
Assert.Equal(UInt32.MaxValue, parsed.SequenceId);
Assert.Equal(1024, parsed.PayloadLength);
}
[Fact(DisplayName = "测试超大负载长度抛出异常")]
public void TestExcessivePayloadLengthThrows()
{
var header = new ProtocolHeader
{
PayloadLength = Int32.MaxValue
};
var bytes = header.ToBytes();
Assert.Throws<InvalidOperationException>(() => ProtocolHeader.FromBytes(bytes));
}
}
|