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

namespace XCoder.App.Tests;

/// <summary>本地凭据存储测试</summary>
public class CredentialStoreTests
{
    private static String TempFile() => Path.Combine(Path.GetTempPath(), "xcoder-cred-" + Guid.NewGuid().ToString("N") + ".dat");

    [Fact]
    [DisplayName("写入后可读回,并以密文落盘")]
    public void SetAndGet_Encrypted()
    {
        var path = TempFile();
        try
        {
            var store = new CredentialStore(path);
            store.Set("mysql.prod", "P@ssw0rd!");

            Assert.Equal("P@ssw0rd!", store.Get("mysql.prod"));

            var text = File.ReadAllText(path);
            Assert.DoesNotContain("P@ssw0rd!", text);
            Assert.Contains("mysql.prod", text);
        }
        finally
        {
            if (File.Exists(path)) File.Delete(path);
        }
    }

    [Fact]
    [DisplayName("新实例可从文件加载已有凭据")]
    public void Reload_FromFile()
    {
        var path = TempFile();
        try
        {
            new CredentialStore(path).Set("redis", "secret-value");

            var reopened = new CredentialStore(path);

            Assert.Equal("secret-value", reopened.Get("redis"));
            Assert.Contains("redis", reopened.Keys);
        }
        finally
        {
            if (File.Exists(path)) File.Delete(path);
        }
    }

    [Fact]
    [DisplayName("删除与清空生效")]
    public void RemoveAndClear()
    {
        var path = TempFile();
        try
        {
            var store = new CredentialStore(path);
            store.Set("a", "1");
            store.Set("b", "2");

            Assert.True(store.Remove("a"));
            Assert.False(store.Remove("a"));
            Assert.Null(store.Get("a"));
            Assert.Equal("2", store.Get("b"));

            store.Clear();
            Assert.Empty(store.Keys);
        }
        finally
        {
            if (File.Exists(path)) File.Delete(path);
        }
    }

    [Fact]
    [DisplayName("空键与空值按约定处理")]
    public void EdgeCases()
    {
        var path = TempFile();
        try
        {
            var store = new CredentialStore(path);

            Assert.Null(store.Get(""));
            Assert.Null(store.Get(null!));
            Assert.False(store.Remove(""));
            Assert.Throws<ArgumentException>(() => store.Set("", "x"));

            // 空值等价于删除
            store.Set("k", "v");
            store.Set("k", "");
            Assert.Null(store.Get("k"));
        }
        finally
        {
            if (File.Exists(path)) File.Delete(path);
        }
    }

    [Fact]
    [DisplayName("加密解密往返一致,非法密文返回空")]
    public void EncryptDecrypt_Roundtrip()
    {
        const String plain = "连接串=Data Source=db;Password=123456";
        var cipher = CredentialStore.Encrypt(plain);

        Assert.NotEqual(plain, cipher);
        Assert.Equal(plain, CredentialStore.Decrypt(cipher));
        Assert.Null(CredentialStore.Decrypt(""));
        Assert.Null(CredentialStore.Decrypt("not-base64"));
        Assert.Null(CredentialStore.Decrypt(Convert.ToBase64String(new Byte[8])));
    }

    [Fact]
    [DisplayName("同一明文两次加密结果不同(随机 IV)")]
    public void Encrypt_UsesRandomIv()
    {
        var a = CredentialStore.Encrypt("same");
        var b = CredentialStore.Encrypt("same");

        Assert.NotEqual(a, b);
        Assert.Equal("same", CredentialStore.Decrypt(a));
        Assert.Equal("same", CredentialStore.Decrypt(b));
    }
}