Skip to content

Commit e657593

Browse files
committed
add: test case for handling nil IP address
1 parent 2b43fec commit e657593

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

route_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,61 @@ func TestRouteAddDel(t *testing.T) {
152152
}
153153
}
154154

155+
func TestRouteUnreachableEmptyDst(t *testing.T) {
156+
t.Cleanup(setUpNetlinkTest(t))
157+
158+
// Test adding unreachable/blackhole/prohibit routes with empty Dst.IP
159+
// These route types don't need RTA_DST to be serialized
160+
testCases := []struct {
161+
name string
162+
routeType int
163+
}{
164+
{"unreachable", unix.RTN_UNREACHABLE},
165+
{"blackhole", unix.RTN_BLACKHOLE},
166+
{"prohibit", unix.RTN_PROHIBIT},
167+
}
168+
169+
for _, tc := range testCases {
170+
t.Run(tc.name, func(t *testing.T) {
171+
route := &Route{
172+
Table: 100,
173+
Dst: &net.IPNet{
174+
IP: net.IP{},
175+
Mask: net.IPMask{},
176+
},
177+
Priority: 100,
178+
Type: tc.routeType,
179+
Scope: unix.RT_SCOPE_UNIVERSE,
180+
Family: FAMILY_V4,
181+
}
182+
183+
if err := RouteAdd(route); err != nil {
184+
t.Fatalf("failed to add %s route with empty Dst.IP: %v", tc.name, err)
185+
}
186+
187+
routes, err := RouteListFiltered(FAMILY_V4, &Route{Table: 100}, RT_FILTER_TABLE)
188+
if err != nil {
189+
t.Fatal(err)
190+
}
191+
192+
found := false
193+
for _, r := range routes {
194+
if r.Type == tc.routeType {
195+
found = true
196+
break
197+
}
198+
}
199+
if !found {
200+
t.Fatalf("%s route not found after adding", tc.name)
201+
}
202+
203+
if err := RouteDel(route); err != nil {
204+
t.Fatalf("failed to delete %s route: %v", tc.name, err)
205+
}
206+
})
207+
}
208+
}
209+
155210
func TestRoute6AddDel(t *testing.T) {
156211
t.Cleanup(setUpNetlinkTest(t))
157212

0 commit comments

Comments
 (0)