- 重构 sysconf 为文档模型 INI Parser 与 Config Framework - 强化 hosts 解析、插入校验、写回与异常输入处理 - 完善 StarCmd 生命周期、等待 API、流式输出与 IO 重定向 - 扩展跨平台文件时间、文件锁、内存、进程与网络能力 - 将 Windows 进程适配更新到 b612.me/wincmd v0.1.0 - 移除本地 wincmd/win32api replace,改用发布版依赖 - 将最低 Go 版本提升到 1.18 - 补充 hosts、sysconf、FileLock、StarCmd 与平台适配回归测试
122 lines
3.6 KiB
Go
122 lines
3.6 KiB
Go
//go:build windows
|
|
// +build windows
|
|
|
|
package staros
|
|
|
|
import (
|
|
"errors"
|
|
"syscall"
|
|
"testing"
|
|
|
|
"b612.me/win32api"
|
|
)
|
|
|
|
func TestWindowsNetConnectionTypes(t *testing.T) {
|
|
tcp, udp, err := windowsNetConnectionTypes("")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !tcp || !udp {
|
|
t.Fatalf("empty types should request tcp and udp, got tcp=%v udp=%v", tcp, udp)
|
|
}
|
|
|
|
tcp, udp, err = windowsNetConnectionTypes("all")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !tcp || !udp {
|
|
t.Fatalf("all types should request tcp and udp, got tcp=%v udp=%v", tcp, udp)
|
|
}
|
|
|
|
tcp, udp, err = windowsNetConnectionTypes("tcp")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !tcp || udp {
|
|
t.Fatalf("tcp types mismatch: tcp=%v udp=%v", tcp, udp)
|
|
}
|
|
|
|
tcp, udp, err = windowsNetConnectionTypes("TCP,UDP")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !tcp || !udp {
|
|
t.Fatalf("mixed tcp/udp types mismatch: tcp=%v udp=%v", tcp, udp)
|
|
}
|
|
|
|
tcp, udp, err = windowsNetConnectionTypes("udp")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tcp || !udp {
|
|
t.Fatalf("udp types mismatch: tcp=%v udp=%v", tcp, udp)
|
|
}
|
|
|
|
if _, _, err = windowsNetConnectionTypes("unix"); !errors.Is(err, ERR_UNSUPPORTED) {
|
|
t.Fatalf("unix should be unsupported on windows, got %v", err)
|
|
}
|
|
if _, _, err = windowsNetConnectionTypes("tcp,unix"); !errors.Is(err, ERR_UNSUPPORTED) {
|
|
t.Fatalf("mixed unix request should be unsupported on windows, got %v", err)
|
|
}
|
|
if _, _, err = windowsNetConnectionTypes("all,unix"); !errors.Is(err, ERR_UNSUPPORTED) {
|
|
t.Fatalf("all plus unix request should be unsupported on windows, got %v", err)
|
|
}
|
|
if _, _, err = windowsNetConnectionTypes("raw"); err == nil {
|
|
t.Fatal("unknown type should return error")
|
|
}
|
|
}
|
|
|
|
func TestWindowsIPv4FromDWORD(t *testing.T) {
|
|
if got := windowsIPv4FromDWORD(0x0100007f); got != "127.0.0.1" {
|
|
t.Fatalf("unexpected localhost conversion: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestWindowsTCPState(t *testing.T) {
|
|
cases := map[win32api.MIB_TCP_STATE]string{
|
|
win32api.MIB_TCP_STATE_ESTAB: TCP_STATE[TCP_ESTABLISHED],
|
|
win32api.MIB_TCP_STATE_LISTEN: TCP_STATE[TCP_LISTEN],
|
|
win32api.MIB_TCP_STATE_SYN_SENT: TCP_STATE[TCP_SYN_SENT],
|
|
win32api.MIB_TCP_STATE_SYN_RCVD: TCP_STATE[TCP_SYN_RECV],
|
|
win32api.MIB_TCP_STATE_FIN_WAIT1: TCP_STATE[TCP_FIN_WAIT1],
|
|
win32api.MIB_TCP_STATE_FIN_WAIT2: TCP_STATE[TCP_FIN_WAIT2],
|
|
win32api.MIB_TCP_STATE_TIME_WAIT: TCP_STATE[TCP_TIME_WAIT],
|
|
win32api.MIB_TCP_STATE_CLOSED: TCP_STATE[TCP_CLOSE],
|
|
win32api.MIB_TCP_STATE_CLOSE_WAIT: TCP_STATE[TCP_CLOSE_WAIT],
|
|
win32api.MIB_TCP_STATE_LAST_ACK: TCP_STATE[TCP_LAST_ACK],
|
|
win32api.MIB_TCP_STATE_CLOSING: TCP_STATE[TCP_CLOSING],
|
|
}
|
|
for state, want := range cases {
|
|
if got := windowsTCPState(state); got != want {
|
|
t.Fatalf("state %d mismatch: got=%s want=%s", state, got, want)
|
|
}
|
|
}
|
|
if got := windowsTCPState(win32api.MIB_TCP_STATE(0)); got != TCP_STATE[TCP_UNKNOWN] {
|
|
t.Fatalf("unknown state mismatch: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestIsOptionalWindowsNetTableError(t *testing.T) {
|
|
if !isOptionalWindowsNetTableError(windowsErrorNotSupported) {
|
|
t.Fatal("ERROR_NOT_SUPPORTED should be optional")
|
|
}
|
|
if isOptionalWindowsNetTableError(syscall.EINVAL) {
|
|
t.Fatal("EINVAL should not be optional")
|
|
}
|
|
}
|
|
|
|
func TestAttachWindowsProcess(t *testing.T) {
|
|
conn := NetConn{}
|
|
cache := map[int64]*Process{}
|
|
attachWindowsProcess(&conn, 123, false, cache)
|
|
if conn.Pid != 0 || conn.Process != nil {
|
|
t.Fatalf("analysePid=false should not populate process fields: %#v", conn)
|
|
}
|
|
|
|
conn = NetConn{}
|
|
attachWindowsProcess(&conn, 0, true, cache)
|
|
if conn.Pid != 0 || conn.Process != nil {
|
|
t.Fatalf("pid 0 should not populate process fields: %#v", conn)
|
|
}
|
|
}
|