staros/network_test.go
starainrt d93a851d1b
feat: 完善 staros 系统能力并更新 wincmd 发布版依赖
- 重构 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 与平台适配回归测试
2026-06-09 18:10:19 +08:00

68 lines
1.7 KiB
Go

//go:build linux
// +build linux
package staros
import (
"testing"
"time"
)
func Test_TrimSpace(t *testing.T) {
}
func TestAnalyseNetFilesSkipsShortLines(t *testing.T) {
data := []byte("sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode\nshort\n")
res, err := analyseNetFiles(data, nil, "tcp")
if err != nil {
t.Fatal(err)
}
if len(res) != 0 {
t.Fatalf("expected no parsed connections, got %d", len(res))
}
}
func TestNetSpeedsRejectsInvalidDuration(t *testing.T) {
_, err := NetSpeeds(0)
if err == nil {
t.Fatal("expected invalid duration error")
}
_, err = NetSpeeds(-time.Second)
if err == nil {
t.Fatal("expected invalid duration error")
}
}
func TestUniqueStrings(t *testing.T) {
got := uniqueStrings([]string{"tcp", "udp", "tcp"})
if len(got) != 2 {
t.Fatalf("expected 2 unique values, got %d", len(got))
}
if got[0] != "tcp" || got[1] != "udp" {
t.Fatalf("unexpected order: %#v", got)
}
}
func TestParseProcStatusKB(t *testing.T) {
if got := parseProcStatusKB("12 kB"); got != 12*1024 {
t.Fatalf("expected 12288, got %d", got)
}
if got := parseProcStatusKB(""); got != 0 {
t.Fatalf("expected 0, got %d", got)
}
}
func TestProcStartTimeFromStatHandlesProcessNameWithSpacesAndParens(t *testing.T) {
stat := []byte("42 (name with ) parens) S 1 1 1 0 -1 4194560 0 0 0 0 0 0 0 0 20 0 1 0 12345")
got, ok := procStartTimeFromStat(stat)
if !ok {
t.Fatal("expected proc stat start time to parse")
}
ticks := int64(clockTicks())
want := time.Unix(StartTime().Unix()+12345/ticks, (12345%ticks)*int64(time.Second)/ticks)
if !got.Equal(want) {
t.Fatalf("unexpected start time: got %s want %s", got, want)
}
}