diff options
author | MikoĊaj Walczak <mikiwalczak+github@gmail.com> | 2018-11-26 12:49:49 +0100 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-11-26 14:04:50 +0100 |
commit | d53d291e41a2eea81e208d28828caefff78a3164 (patch) | |
tree | e9d113ea88863e4bf47d4c86d39ed396165aef70 /dhcpv6/dhcpv6_test.go | |
parent | ac7761d0f4854317f6047e384a697ae6881033bf (diff) |
Add missing error check in GenerateTransactionID
Diffstat (limited to 'dhcpv6/dhcpv6_test.go')
-rw-r--r-- | dhcpv6/dhcpv6_test.go | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/dhcpv6/dhcpv6_test.go b/dhcpv6/dhcpv6_test.go index 0b7c895..0da839a 100644 --- a/dhcpv6/dhcpv6_test.go +++ b/dhcpv6/dhcpv6_test.go @@ -1,18 +1,46 @@ package dhcpv6 import ( + "encoding/binary" "net" "testing" - "github.com/insomniacslk/dhcp/iana" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/insomniacslk/dhcp/iana" ) func TestBytesToTransactionID(t *testing.T) { - // only the first three bytes should be used - tid, err := BytesToTransactionID([]byte{0x11, 0x22, 0x33, 0xaa}) + // Check if the function transforms the bytes for the exact length input + b := make([]byte, 4) + binary.LittleEndian.PutUint32(b, 0x01020304) + tid, err := BytesToTransactionID(b) + require.NoError(t, err) + require.NotNil(t, tid) + assert.Equal(t, *tid, uint32(0x000040302)) + + binary.BigEndian.PutUint32(b, 0x01020304) + tid, err = BytesToTransactionID(b) + require.NoError(t, err) + require.NotNil(t, tid) + assert.Equal(t, *tid, uint32(0x00010203)) + + // Check if the function transforms only the first bytes for a longer input + b = make([]byte, 8) + binary.LittleEndian.PutUint32(b, 0x01020304) + binary.LittleEndian.PutUint32(b[4:], 0x11121314) + tid, err = BytesToTransactionID(b) + require.NoError(t, err) + require.NotNil(t, tid) + assert.Equal(t, *tid, uint32(0x000040302)) + + binary.BigEndian.PutUint32(b, 0x01020304) + binary.BigEndian.PutUint32(b[4:], 0x11121314) + tid, err = BytesToTransactionID(b) require.NoError(t, err) - require.Equal(t, uint32(0x112233), *tid) + require.NotNil(t, tid) + assert.Equal(t, *tid, uint32(0x00010203)) } func TestBytesToTransactionIDShortData(t *testing.T) { @@ -25,7 +53,7 @@ func TestBytesToTransactionIDShortData(t *testing.T) { func TestGenerateTransactionID(t *testing.T) { tid, err := GenerateTransactionID() require.NoError(t, err) - require.NotNil(t, *tid) + require.NotNil(t, tid) require.True(t, *tid <= 0xffffff, "transaction ID should be smaller than 0xffffff") } |