Skip to content

Commit a217412

Browse files
committed
test(internal/linkstatus): add unit testing for link status
1 parent 3d4e206 commit a217412

1 file changed

Lines changed: 154 additions & 0 deletions

File tree

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// Copyright 2026 The HuaTuo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package linkstatus
16+
17+
import (
18+
"reflect"
19+
"testing"
20+
21+
"golang.org/x/sys/unix"
22+
)
23+
24+
func TestTypes_String(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
in Types
28+
want string
29+
}{
30+
{"Unknown", Unknown, "linkstatus_unknown"},
31+
{"AdminUp", AdminUp, "linkstatus_adminup"},
32+
{"AdminDown", AdminDown, "linkstatus_admindown"},
33+
{"CarrierUp", CarrierUp, "linkstatus_carrierup"},
34+
{"CarrierDown", CarrierDown, "linkstatus_carrierdown"},
35+
}
36+
37+
for _, tt := range tests {
38+
t.Run(tt.name, func(t *testing.T) {
39+
if got := tt.in.String(); got != tt.want {
40+
t.Errorf("String() = %q, want %q", got, tt.want)
41+
}
42+
})
43+
}
44+
}
45+
46+
func TestChanged(t *testing.T) {
47+
tests := []struct {
48+
name string
49+
flags uint32
50+
change uint32
51+
want []Types
52+
}{
53+
{
54+
name: "no_change",
55+
flags: 0,
56+
change: 0,
57+
want: nil,
58+
},
59+
{
60+
name: "admin_up",
61+
flags: unix.IFF_UP,
62+
change: unix.IFF_UP,
63+
want: []Types{AdminUp},
64+
},
65+
{
66+
name: "admin_down",
67+
flags: 0,
68+
change: unix.IFF_UP,
69+
want: []Types{AdminDown},
70+
},
71+
{
72+
name: "carrier_up",
73+
flags: unix.IFF_LOWER_UP,
74+
change: unix.IFF_LOWER_UP,
75+
want: []Types{CarrierUp},
76+
},
77+
{
78+
name: "carrier_down",
79+
flags: 0,
80+
change: unix.IFF_LOWER_UP,
81+
want: []Types{CarrierDown},
82+
},
83+
{
84+
name: "admin_and_carrier_up",
85+
flags: unix.IFF_UP | unix.IFF_LOWER_UP,
86+
change: unix.IFF_UP | unix.IFF_LOWER_UP,
87+
want: []Types{AdminUp, CarrierUp},
88+
},
89+
{
90+
name: "admin_down_carrier_up",
91+
flags: unix.IFF_LOWER_UP,
92+
change: unix.IFF_UP | unix.IFF_LOWER_UP,
93+
want: []Types{AdminDown, CarrierUp},
94+
},
95+
{
96+
name: "ignore_irrelevant_change_bits",
97+
flags: 0,
98+
change: unix.IFF_PROMISC,
99+
want: nil,
100+
},
101+
}
102+
103+
for _, tt := range tests {
104+
t.Run(tt.name, func(t *testing.T) {
105+
got := Changed(tt.flags, tt.change)
106+
if !reflect.DeepEqual(got, tt.want) {
107+
t.Errorf("Changed(%#x, %#x) = %#v, want %#v", tt.flags, tt.change, got, tt.want)
108+
}
109+
})
110+
}
111+
}
112+
113+
func TestFlagsRaw(t *testing.T) {
114+
tests := []struct {
115+
name string
116+
flags uint32
117+
want []string
118+
}{
119+
{
120+
name: "invalid_zero",
121+
flags: 0,
122+
want: []string{"unknown"},
123+
},
124+
{
125+
name: "admin_up_carrier_up",
126+
flags: unix.IFF_UP | unix.IFF_LOWER_UP,
127+
want: []string{"linkstatus_adminup", "linkstatus_carrierup"},
128+
},
129+
{
130+
name: "admin_down_carrier_down",
131+
flags: unix.IFF_BROADCAST, // 非 0,且不含 IFF_UP/IFF_LOWER_UP
132+
want: []string{"linkstatus_admindown", "linkstatus_carrierdown"},
133+
},
134+
{
135+
name: "admin_up_carrier_down",
136+
flags: unix.IFF_UP,
137+
want: []string{"linkstatus_adminup", "linkstatus_carrierdown"},
138+
},
139+
{
140+
name: "admin_down_carrier_up",
141+
flags: unix.IFF_LOWER_UP,
142+
want: []string{"linkstatus_admindown", "linkstatus_carrierup"},
143+
},
144+
}
145+
146+
for _, tt := range tests {
147+
t.Run(tt.name, func(t *testing.T) {
148+
got := FlagsRaw(tt.flags)
149+
if !reflect.DeepEqual(got, tt.want) {
150+
t.Errorf("FlagsRaw(%#x) = %#v, want %#v", tt.flags, got, tt.want)
151+
}
152+
})
153+
}
154+
}

0 commit comments

Comments
 (0)