[fix]修正UdpServer在接收广播时连续启动接收的错误,在StarAgent中,此时可能收到广播包,SocketFlags是Broadcast,需要清空,否则报错“参考的对象类型不支持尝试的操作”; 无需设置SocketOptionName.PacketInformation,在ReceiveMessageFromAsync时会自动设置,并且支持ipv6;
石头 编写于 2024-10-10 00:36:00 石头 提交于 2024-10-10 00:45:43
X
using System.Text;
using NewLife;
using NewLife.Security;
using Xunit;

namespace XUnitTest.Security;

public class ProtectedKeyTests
{
    [Fact]
    public void ProtectedData()
    {
        var pd = new ProtectedKey { Secret = "NewLife".GetBytes() };

        var plain = "Hello Stone";
        var str = pd.Protect(plain);
        Assert.Equal("$AES$RmqCGPyEC6W98aGJwUStpQ", str);

        var rs = pd.Unprotect(str);
        Assert.Equal(plain, rs);
    }

    [Fact]
    public void ProtectedData2()
    {
        var pd = new ProtectedKey { Secret = "NewLife".GetBytes() };

        var plain = "server=.;uid=root;pwd=\"Hello Stone\";database=iot";
        var str = pd.Protect(plain);
        Assert.Equal("server=.;uid=root;pwd=$AES$RmqCGPyEC6W98aGJwUStpQ;database=iot", str);

        plain = "server=.;uid=root;pwd=Hello Stone;database=iot";
        str = pd.Protect(plain);
        Assert.Equal("server=.;uid=root;pwd=$AES$RmqCGPyEC6W98aGJwUStpQ;database=iot", str);

        var rs = pd.Unprotect(str);
        Assert.Equal(plain, rs);
    }

    [Fact]
    public void ProtectedData3()
    {
        var pd = new ProtectedKey { Secret = "NewLife".GetBytes() };

        var plain = "server=.;uid=root;pwd=\"Hello Stone\";database=iot";
        var str = pd.Unprotect(plain);
        Assert.Equal("server=.;uid=root;pwd=\"Hello Stone\";database=iot", str);
    }

    [Fact]
    public void Hide()
    {
        var pd = new ProtectedKey { Secret = "NewLife".GetBytes() };

        var plain = "server=.;uid=root;pwd=\"Hello Stone\";database=iot";
        var str = pd.Hide(plain);
        Assert.Equal("server=.;uid=root;pwd={***};database=iot", str);
    }
}