Skip to content

Commit

Permalink
cmds/core/ip: improve coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Wienand <[email protected]>
  • Loading branch information
RiSKeD authored and rminnich committed Aug 2, 2024
1 parent 719a74d commit d876f02
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmds/core/ip/ip_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (cmd *cmd) run() error {
func (cmd *cmd) batchCmds() error {
file, err := os.Open(cmd.Opts.Batch)
if err != nil {
log.Fatalf("Failed to open batch file: %v", err)
return err
}
defer file.Close()

Expand Down
18 changes: 17 additions & 1 deletion cmds/core/ip/ip_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ func TestRun(t *testing.T) {
name string
cmd cmd
expected string
wantErr bool
}{
{
name: "Normal execution",
Expand All @@ -560,6 +561,17 @@ func TestRun(t *testing.T) {
Cursor: 1,
ExpectedValues: []string{"arg1", "arg2"},
},
wantErr: true,
},
{
name: "Batch execution",
cmd: cmd{
Opts: flags{
Batch: "testdata/batch.txt",
},
Cursor: 0,
},
wantErr: true,
},
}

Expand All @@ -569,7 +581,11 @@ func TestRun(t *testing.T) {

test.cmd.Out = &out

_ = test.cmd.run()
err := test.cmd.run()

if test.wantErr && err == nil || !test.wantErr && err != nil {
t.Errorf("expected %v, got %v", test.wantErr, err)
}
})
}
}
21 changes: 15 additions & 6 deletions cmds/core/ip/neigh_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ func (cmd *cmd) neigh() error {
case "flush":
return cmd.neighFlush()
case "get":
ip, err := cmd.parseAddress()
if err != nil {
return err
}

iface, err := cmd.parseDeviceName(true)
ip, iface, err := cmd.parseNeighGet()
if err != nil {
return err
}
Expand All @@ -70,6 +65,20 @@ func (cmd *cmd) neigh() error {
return cmd.usage()
}

func (cmd cmd) parseNeighGet() (net.IP, netlink.Link, error) {
ip, err := cmd.parseAddress()
if err != nil {
return nil, nil, err
}

iface, err := cmd.parseDeviceName(true)
if err != nil {
return nil, nil, err
}

return ip, iface, nil
}

func (cmd *cmd) parseNeighAddDelReplaceParams() (*netlink.Neigh, error) {
addr, err := cmd.parseAddress()
if err != nil {
Expand Down
44 changes: 44 additions & 0 deletions cmds/core/ip/neigh_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,47 @@ func TestNeighFlagState(t *testing.T) {
})
}
}

func TestParseNeighGet(t *testing.T) {
tests := []struct {
name string
token []string
wantIP net.IP
wantIF netlink.Link
wantErr bool
}{
{
name: "Valid IP and interface",
token: []string{"address", "192.168.0.2", "dev", "lo"},
wantErr: false,
},
{
name: "Error in parseAddress",
token: []string{"abc"},

wantIP: nil,
wantIF: nil,
wantErr: true,
},
{
name: "Error in parseDeviceName",
token: []string{"address", "192.168.0.2", "xyz"},
wantIF: nil,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := cmd{
Cursor: -1,
Args: tt.token,
}
_, _, err := cmd.parseNeighGet()
if (err != nil) != tt.wantErr {
t.Errorf("parseNeighGet() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
16 changes: 11 additions & 5 deletions cmds/core/ip/tuntap_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ func (cmd *cmd) parseTunTap() (tuntapOptions, error) {
}

func (cmd *cmd) tuntapAdd(options tuntapOptions) error {
link := tunTapDevice(options)

if err := cmd.handle.LinkAdd(link); err != nil {
return err
}

return nil
}

func tunTapDevice(options tuntapOptions) *netlink.Tuntap {
link := &netlink.Tuntap{
LinkAttrs: netlink.LinkAttrs{
Name: options.Name,
Expand All @@ -127,11 +137,7 @@ func (cmd *cmd) tuntapAdd(options tuntapOptions) error {

link.Flags = options.Flags

if err := cmd.handle.LinkAdd(link); err != nil {
return err
}

return nil
return link
}

func (cmd *cmd) tuntapDel(options tuntapOptions) error {
Expand Down
77 changes: 77 additions & 0 deletions cmds/core/ip/tuntap_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,80 @@ func TestPrintTunTaps(t *testing.T) {
})
}
}

func TestTunTapDevice(t *testing.T) {
tests := []struct {
name string
options tuntapOptions
expected *netlink.Tuntap
}{
{
name: "Valid options",
options: tuntapOptions{
Name: "test0",
Mode: netlink.TUNTAP_MODE_TUN,
User: 1000,
Group: 1000,
Flags: netlink.TUNTAP_DEFAULTS,
},
expected: &netlink.Tuntap{
LinkAttrs: netlink.LinkAttrs{
Name: "test0",
},
Mode: netlink.TUNTAP_MODE_TUN,
Owner: 1000,
Group: 1000,
Flags: netlink.TUNTAP_DEFAULTS,
},
},
{
name: "User out of range",
options: tuntapOptions{
Name: "test1",
Mode: netlink.TUNTAP_MODE_TUN,
User: -1,
Group: 1000,
Flags: netlink.TUNTAP_DEFAULTS,
},
expected: &netlink.Tuntap{
LinkAttrs: netlink.LinkAttrs{
Name: "test1",
},
Mode: netlink.TUNTAP_MODE_TUN,
Group: 1000,
Flags: netlink.TUNTAP_DEFAULTS,
},
},
{
name: "Group out of range",
options: tuntapOptions{
Name: "test2",
Mode: netlink.TUNTAP_MODE_TUN,
User: 1000,
Group: -1,
Flags: netlink.TUNTAP_DEFAULTS,
},
expected: &netlink.Tuntap{
LinkAttrs: netlink.LinkAttrs{
Name: "test2",
},
Mode: netlink.TUNTAP_MODE_TUN,
Owner: 1000,
Flags: netlink.TUNTAP_DEFAULTS,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tunTapDevice(tt.options)
if result.LinkAttrs.Name != tt.expected.LinkAttrs.Name ||
result.Mode != tt.expected.Mode ||
result.Owner != tt.expected.Owner ||
result.Group != tt.expected.Group ||
result.Flags != tt.expected.Flags {
t.Errorf("tunTapDevice() = %v, want %v", result, tt.expected)
}
})
}
}

0 comments on commit d876f02

Please sign in to comment.