// Package miot provides white-box tests for miot_network.go network monitor. package miot import ( "testing" "time" ) func TestInterfaceStatus_String(t *testing.T) { tests := []struct { status InterfaceStatus want string }{ {InterfaceAdd, "add"}, {InterfaceUpdate, "update"}, {InterfaceRemove, "remove"}, {InterfaceStatus(99), "unknown"}, } for _, tt := range tests { t.Run(tt.want, func(t *testing.T) { if got := tt.status.String(); got != tt.want { t.Errorf("InterfaceStatus(%d).String() = %q, want %q", tt.status, got, tt.want) } }) } } func TestNetworkInfo_String(t *testing.T) { info := &NetworkInfo{ Name: "eth0", IP: "192.168.1.100", Netmask: "255.255.255.0", NetSeg: "192.168.1.0", } s := info.String() if s != "eth0: 192.168.1.100/255.255.255.0 (192.168.1.0)" { t.Errorf("NetworkInfo.String() = %q", s) } } func TestNewMIoTNetwork_Defaults(t *testing.T) { n := NewMIoTNetwork(nil, nil, 0) if n == nil { t.Fatal("NewMIoTNetwork returned nil") } } func TestNewMIoTNetwork_CustomInterval(t *testing.T) { n := NewMIoTNetwork(nil, nil, 15*time.Second) if n == nil { t.Fatal("NewMIoTNetwork returned nil") } } func TestNewMIoTNetwork_CustomAddresses(t *testing.T) { n := NewMIoTNetwork([]string{"1.1.1.1", "2.2.2.2"}, []string{"http://example.com"}, 60*time.Second) if n == nil { t.Fatal("NewMIoTNetwork returned nil") } } func TestGetNetworkStatus_InitiallyFalse(t *testing.T) { n := NewMIoTNetwork(nil, nil, 0) if n.GetNetworkStatus() { t.Error("network status should be false initially") } } func TestGetNetworkInfo_InitiallyEmpty(t *testing.T) { n := NewMIoTNetwork(nil, nil, 0) info := n.GetNetworkInfo() if info == nil { t.Error("GetNetworkInfo should not return nil") } } func TestSubUnsubNetworkStatus(t *testing.T) { n := NewMIoTNetwork(nil, nil, 0) called := false n.SubNetworkStatus("test_key", func(status bool) { called = true }) n.UnsubNetworkStatus("test_key") _ = called } func TestSubUnsubNetworkInfo(t *testing.T) { n := NewMIoTNetwork(nil, nil, 0) called := false n.SubNetworkInfo("test_key", func(status InterfaceStatus, info *NetworkInfo) { called = true }) n.UnsubNetworkInfo("test_key") _ = called } func TestUpdateAddrList(t *testing.T) { n := NewMIoTNetwork(nil, nil, 0) n.UpdateAddrList([]string{"1.1.1.1"}, []string{"http://test.com"}) } func TestUpdateAddrList_Nil(t *testing.T) { n := NewMIoTNetwork(nil, nil, 0) n.UpdateAddrList(nil, nil) } func TestNetworkInfo_Fields(t *testing.T) { info := &NetworkInfo{Name: "wlan0", IP: "10.0.0.1", Netmask: "255.0.0.0", NetSeg: "10.0.0.0"} if info.Name != "wlan0" { t.Errorf("Name = %q", info.Name) } }