summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/dhcpv6_test.go
diff options
context:
space:
mode:
authorMikoĊ‚aj Walczak <mikiwalczak+github@gmail.com>2018-11-26 22:01:44 +0100
committerinsomniac <insomniacslk@users.noreply.github.com>2018-11-27 16:24:39 +0000
commit0b70b1f1fb1bf27e16203b02c4fc74bea90d5b1e (patch)
tree0b1a1b6e52af048ca0a0a20b2a6603c812e90abf /dhcpv6/dhcpv6_test.go
parente82b966866c8e05d9d6495deedfe2c0928950cd5 (diff)
Test TransactionID generation
Diffstat (limited to 'dhcpv6/dhcpv6_test.go')
-rw-r--r--dhcpv6/dhcpv6_test.go51
1 files changed, 46 insertions, 5 deletions
diff --git a/dhcpv6/dhcpv6_test.go b/dhcpv6/dhcpv6_test.go
index 0da839a..49fecca 100644
--- a/dhcpv6/dhcpv6_test.go
+++ b/dhcpv6/dhcpv6_test.go
@@ -1,12 +1,15 @@
package dhcpv6
import (
+ "crypto/rand"
"encoding/binary"
+ "errors"
"net"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "github.com/stretchr/testify/suite"
"github.com/insomniacslk/dhcp/iana"
)
@@ -50,11 +53,50 @@ func TestBytesToTransactionIDShortData(t *testing.T) {
require.Nil(t, tid)
}
-func TestGenerateTransactionID(t *testing.T) {
+func randomReadMock(value []byte, n int, err error) func([]byte) (int, error) {
+ return func(b []byte) (int, error) {
+ copy(b, value)
+ return n, err
+ }
+}
+
+type GenerateTransactionIDTestSuite struct {
+ suite.Suite
+ random []byte
+}
+
+func (s *GenerateTransactionIDTestSuite) SetupTest() {
+ s.random = make([]byte, 16)
+}
+
+func (s *GenerateTransactionIDTestSuite) TearDown() {
+ randomRead = rand.Read
+}
+
+func (s *GenerateTransactionIDTestSuite) TestErrors() {
+ // Error is returned from random number generator
+ e := errors.New("mocked error")
+ randomRead = randomReadMock(s.random, 0, e)
tid, err := GenerateTransactionID()
- require.NoError(t, err)
- require.NotNil(t, tid)
- require.True(t, *tid <= 0xffffff, "transaction ID should be smaller than 0xffffff")
+ s.Assert().Equal(e, err)
+ s.Assert().Nil(tid)
+
+ // Less than 4 bytes are generated
+ randomRead = randomReadMock(s.random, 3, nil)
+ tid, err = GenerateTransactionID()
+ s.Assert().EqualError(err, "invalid random sequence: shorter than 4 bytes")
+}
+
+func (s *GenerateTransactionIDTestSuite) TestSuccess() {
+ binary.BigEndian.PutUint32(s.random, 0x01020304)
+ randomRead = randomReadMock(s.random, 4, nil)
+ tid, err := GenerateTransactionID()
+ s.Require().NoError(err)
+ s.Assert().Equal(*tid, uint32(0x00010203))
+}
+
+func TestGenerateTransactionIDTestSuite(t *testing.T) {
+ suite.Run(t, new(GenerateTransactionIDTestSuite))
}
func TestNewMessage(t *testing.T) {
@@ -249,7 +291,6 @@ func TestNewMessageTypeSolicitWithCID(t *testing.T) {
require.Equal(t, len(opts), 2)
}
-
func TestIsUsingUEFIArchTypeTrue(t *testing.T) {
msg := DHCPv6Message{}
opt := OptClientArchType{ArchTypes: []iana.ArchType{iana.EFI_BC}}