借助网络配置,控制Modbus工具的日志显示,避免卡死界面
智能大石头 authored at 2021-12-11 22:09:19
4.18 KiB
XCoder
using System;
using System.ComponentModel;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using NewLife.XCoder.Tools.Net;
using Xunit;

namespace XCoder.App.Tests;

/// <summary>网络调试报文格式化</summary>
public class NetDebugFormatterTests
{
    [Fact]
    [DisplayName("文本模式把回车换行统一为 CRLF")]
    public void BuildPayload_Text_NormalizesNewLine()
    {
        var bytes = NetDebugFormatter.BuildPayload("a\nb", false);

        Assert.Equal("a\r\nb", Encoding.UTF8.GetString(bytes));
    }

    [Fact]
    [DisplayName("HEX 模式容忍空格、逗号与 0x 前缀")]
    public void BuildPayload_Hex_Tolerant()
    {
        Assert.Equal([0x48, 0x65, 0x6C], NetDebugFormatter.BuildPayload("48 65 6C", true));
        Assert.Equal([0x48, 0x65], NetDebugFormatter.BuildPayload("0x48,0x65", true));
        Assert.Equal([0x01, 0x02], NetDebugFormatter.BuildPayload("01\r\n02", true));
    }

    [Fact]
    [DisplayName("空输入返回空数组")]
    public void BuildPayload_Empty_ReturnsEmpty()
    {
        Assert.Empty(NetDebugFormatter.BuildPayload("", false));
        Assert.Empty(NetDebugFormatter.BuildPayload(null, true));
        Assert.Empty(NetDebugFormatter.ParseHex("   "));
    }

    [Theory]
    [DisplayName("HEX 非法输入报参数错误")]
    [InlineData("4")]
    [InlineData("48 6")]
    [InlineData("ZZ")]
    [InlineData("4x")]
    public void ParseHex_Invalid_Throws(String text)
    {
        Assert.Throws<ArgumentException>(() => NetDebugFormatter.ParseHex(text));
    }

    [Fact]
    [DisplayName("字节数组转十六进制文本用空格分隔")]
    public void ToHexText_Works()
    {
        Assert.Equal("00 0A FF", NetDebugFormatter.ToHexText([0x00, 0x0A, 0xFF]));
        Assert.Equal("", NetDebugFormatter.ToHexText([]));
        Assert.Equal("", NetDebugFormatter.ToHexText(null));
    }

    [Fact]
    [DisplayName("接收日志包含时间、长度、十六进制与可见文本")]
    public void FormatReceive_WithText()
    {
        var time = new DateTime(2026, 11, 11, 10, 21, 33, 123);

        var line = NetDebugFormatter.FormatReceive("Hello"u8.ToArray(), true, true, time);

        Assert.StartsWith("10:21:33.123 æ”¶[5] 48 65 6C 6C 6F", line);
        Assert.EndsWith("| Hello", line);
    }

    [Fact]
    [DisplayName("不可见字节在文本区显示为点,换行显示为转义")]
    public void FormatReceive_InvisibleBytes()
    {
        var line = NetDebugFormatter.FormatReceive([0x41, 0x01, 0x0A, 0x42], false, false);

        Assert.EndsWith("A.\\nB", line);
        Assert.Equal("", NetDebugFormatter.FormatReceive([], true, true));
        Assert.Equal("", NetDebugFormatter.FormatReceive(null, true, true));
    }

    [Fact]
    [DisplayName("统计文本按易读单位格式化")]
    public void FormatStat_And_Size()
    {
        Assert.Equal("512 B", NetDebugFormatter.FormatSize(512));
        Assert.Equal("1.5 KB", NetDebugFormatter.FormatSize(1536));
        Assert.Equal("2.0 MB", NetDebugFormatter.FormatSize(2 * 1024 * 1024));

        var stat = NetDebugFormatter.FormatStat(1536, 512, 2);
        Assert.Equal("发送 1.5 KB / 接收 512 B / 连接 2", stat);
    }

    [Fact]
    [DisplayName("构造的报文能被真实 TCP 服务端原样收到")]
    public async Task BuildPayload_EndToEndOverTcp()
    {
        var listener = new TcpListener(IPAddress.Loopback, 0);
        listener.Start();
        var port = ((IPEndPoint)listener.LocalEndpoint).Port;

        var server = Task.Run(async () =>
        {
            using var client = await listener.AcceptTcpClientAsync();
            using var stream = client.GetStream();

            var buffer = new Byte[64];
            var read = await stream.ReadAsync(buffer);

            return buffer[..read];
        });

        var payload = NetDebugFormatter.BuildPayload("0x48 0x69", true);
        using (var client = new TcpClient())
        {
            await client.ConnectAsync(IPAddress.Loopback, port);
            await client.GetStream().WriteAsync(payload);
        }

        var received = await server.WaitAsync(TimeSpan.FromSeconds(5));
        listener.Stop();

        Assert.Equal("Hi"u8.ToArray(), received);
    }
}