Skip to content

Commit

Permalink
Add unit test for ListIPForwardRows
Browse files Browse the repository at this point in the history
Signed-off-by: Quan Tian <[email protected]>
  • Loading branch information
tnqn committed Sep 14, 2024
1 parent b96d22f commit b2e1f7a
Showing 1 changed file with 97 additions and 1 deletion.
98 changes: 97 additions & 1 deletion pkg/agent/util/syscall/syscall_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
"os"
"syscall"
"testing"
"unsafe"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRawSockAddrTranslation(t *testing.T) {
Expand Down Expand Up @@ -202,7 +204,7 @@ func TestIPForwardEntryOperations(t *testing.T) {
}
}

func TestListIPForwardRows(t *testing.T) {
func TestListIPForwardRowsFailure(t *testing.T) {
wantErr := os.NewSyscallError("iphlpapi.GetIpForwardTable", syscall.Errno(22))
testNetIO := NewTestNetIO(22)
// Skipping no error case because converting uintptr back to Pointer is not valid in general.
Expand All @@ -211,6 +213,100 @@ func TestListIPForwardRows(t *testing.T) {
assert.Equal(t, wantErr, gotErr)
}

func TestListIPForwardRowsSuccess(t *testing.T) {
// The table contains two rows. It will be returned by syscallN.
table := struct {
NumEntries uint32
Table [2]MibIPForwardRow
}{
NumEntries: 2,
Table: [2]MibIPForwardRow{
{
Luid: 10,
Index: 11,
DestinationPrefix: AddressPrefix{
Prefix: RawSockAddrInet{
Family: AF_INET,
data: [26]byte{10, 10, 10, 0},
},
prefixLength: 24,
},
NextHop: RawSockAddrInet{
Family: AF_INET,
data: [26]byte{11, 11, 11, 11},
},
},
{
Luid: 20,
Index: 21,
DestinationPrefix: AddressPrefix{
Prefix: RawSockAddrInet{
Family: AF_INET,
data: [26]byte{20, 20, 20, 0},
},
prefixLength: 24,
},
NextHop: RawSockAddrInet{
Family: AF_INET,
data: [26]byte{21, 21, 21, 21},
},
},
},
}

syscallN := func(trap uintptr, args ...uintptr) (r1, r2 uintptr, err syscall.Errno) {
// This mocks the 1st call to syscallN made by getIPForwardTable. It makes args[1] point to the table.
if len(args) > 1 {
ptr := unsafe.Pointer(args[1])
ptrIPForwardTable := (**MibIPForwardTable)(ptr)
*ptrIPForwardTable = (*MibIPForwardTable)(unsafe.Pointer(&table))
return 0, 0, 0
}
// This mocks the 2nd call to syscallN made by freeMibTable. It resets the data in the table.
table.Table[0] = MibIPForwardRow{}
table.Table[1] = MibIPForwardRow{}
return 0, 0, 0
}
testNetIO := &netIO{syscallN: syscallN}
gotRow, gotErr := testNetIO.ListIPForwardRows(AF_INET)
require.Nil(t, gotErr)
// It verifies that the returned rows are independent copies, not referencing to the original table's memory, by
// asserting they retain the exact same content as the original table after it has been set.
expectedRows := []MibIPForwardRow{
{
Luid: 10,
Index: 11,
DestinationPrefix: AddressPrefix{
Prefix: RawSockAddrInet{
Family: AF_INET,
data: [26]byte{10, 10, 10, 0},
},
prefixLength: 24,
},
NextHop: RawSockAddrInet{
Family: AF_INET,
data: [26]byte{11, 11, 11, 11},
},
},
{
Luid: 20,
Index: 21,
DestinationPrefix: AddressPrefix{
Prefix: RawSockAddrInet{
Family: AF_INET,
data: [26]byte{20, 20, 20, 0},
},
prefixLength: 24,
},
NextHop: RawSockAddrInet{
Family: AF_INET,
data: [26]byte{21, 21, 21, 21},
},
},
}
assert.Equal(t, expectedRows, gotRow)
}

func NewTestNetIO(wantR1 uintptr) NetIOInterface {
mockSyscallN := func(trap uintptr, args ...uintptr) (r1, r2 uintptr, err syscall.Errno) {
return wantR1, 0, 0
Expand Down

0 comments on commit b2e1f7a

Please sign in to comment.