diff options
-rw-r--r-- | dhcpv6/dhcpv6message.go | 6 | ||||
-rw-r--r-- | dhcpv6/dhcpv6relay_test.go | 2 | ||||
-rw-r--r-- | dhcpv6/option_elapsedtime.go | 16 | ||||
-rw-r--r-- | dhcpv6/option_elapsedtime_test.go | 8 | ||||
-rw-r--r-- | dhcpv6/option_nontemporaryaddress.go | 60 | ||||
-rw-r--r-- | dhcpv6/option_nontemporaryaddress_test.go | 58 | ||||
-rw-r--r-- | dhcpv6/option_relaymsg_test.go | 2 |
7 files changed, 73 insertions, 79 deletions
diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go index 0cd34f0..ab014b3 100644 --- a/dhcpv6/dhcpv6message.go +++ b/dhcpv6/dhcpv6message.go @@ -96,9 +96,9 @@ func NewSolicitForInterface(ifname string, modifiers ...Modifier) (DHCPv6, error d.AddOption(&OptElapsedTime{}) // FIXME use real values for IA_NA iaNa := OptIANA{} - iaNa.SetIAID([4]byte{0xfa, 0xce, 0xb0, 0x0c}) - iaNa.SetT1(0xe10) - iaNa.SetT2(0x1518) + iaNa.IaId = [4]byte{0xfa, 0xce, 0xb0, 0x0c} + iaNa.T1 = 0xe10 + iaNa.T2 = 0x1518 d.AddOption(&iaNa) // apply modifiers diff --git a/dhcpv6/dhcpv6relay_test.go b/dhcpv6/dhcpv6relay_test.go index 1c5947e..c5989e5 100644 --- a/dhcpv6/dhcpv6relay_test.go +++ b/dhcpv6/dhcpv6relay_test.go @@ -93,7 +93,7 @@ func TestDHCPv6RelayToBytes(t *testing.T) { transactionID: 0xaabbcc, options: []Option{ &OptElapsedTime{ - elapsedTime: 0, + ElapsedTime: 0, }, }, }, diff --git a/dhcpv6/option_elapsedtime.go b/dhcpv6/option_elapsedtime.go index 1f5fd03..e0a1094 100644 --- a/dhcpv6/option_elapsedtime.go +++ b/dhcpv6/option_elapsedtime.go @@ -9,7 +9,7 @@ import ( ) type OptElapsedTime struct { - elapsedTime uint16 + ElapsedTime uint16 } func (op *OptElapsedTime) Code() OptionCode { @@ -20,24 +20,16 @@ func (op *OptElapsedTime) ToBytes() []byte { buf := make([]byte, 6) binary.BigEndian.PutUint16(buf[0:2], uint16(OPTION_ELAPSED_TIME)) binary.BigEndian.PutUint16(buf[2:4], 2) - binary.BigEndian.PutUint16(buf[4:6], uint16(op.elapsedTime)) + binary.BigEndian.PutUint16(buf[4:6], uint16(op.ElapsedTime)) return buf } -func (op *OptElapsedTime) ElapsedTime() uint16 { - return op.elapsedTime -} - -func (op *OptElapsedTime) SetElapsedTime(elapsedTime uint16) { - op.elapsedTime = elapsedTime -} - func (op *OptElapsedTime) Length() int { return 2 } func (op *OptElapsedTime) String() string { - return fmt.Sprintf("OptElapsedTime{elapsedtime=%v}", op.elapsedTime) + return fmt.Sprintf("OptElapsedTime{elapsedtime=%v}", op.ElapsedTime) } // build an OptElapsedTime structure from a sequence of bytes. @@ -47,6 +39,6 @@ func ParseOptElapsedTime(data []byte) (*OptElapsedTime, error) { if len(data) != 2 { return nil, fmt.Errorf("Invalid elapsed time data length. Expected 2 bytes, got %v", len(data)) } - opt.elapsedTime = binary.BigEndian.Uint16(data) + opt.ElapsedTime = binary.BigEndian.Uint16(data) return &opt, nil } diff --git a/dhcpv6/option_elapsedtime_test.go b/dhcpv6/option_elapsedtime_test.go index 8e99fa8..69e879c 100644 --- a/dhcpv6/option_elapsedtime_test.go +++ b/dhcpv6/option_elapsedtime_test.go @@ -13,7 +13,7 @@ func TestOptElapsedTime(t *testing.T) { if optLen := opt.Length(); optLen != 2 { t.Fatalf("Invalid length. Expected 2, got %v", optLen) } - if elapsedTime := opt.ElapsedTime(); elapsedTime != 0xaabb { + if elapsedTime := opt.ElapsedTime; elapsedTime != 0xaabb { t.Fatalf("Invalid elapsed time. Expected 0xaabb, got %v", elapsedTime) } } @@ -28,15 +28,15 @@ func TestOptElapsedTimeToBytes(t *testing.T) { func TestOptElapsedTimeSetGetElapsedTime(t *testing.T) { opt := OptElapsedTime{} - opt.SetElapsedTime(10) - if elapsedTime := opt.ElapsedTime(); elapsedTime != 10 { + opt.ElapsedTime = 10 + if elapsedTime := opt.ElapsedTime; elapsedTime != 10 { t.Fatalf("Invalid elapsed time. Expected 10, got %v", elapsedTime) } } func TestOptElapsedTimeString(t *testing.T) { opt := OptElapsedTime{} - opt.SetElapsedTime(10) + opt.ElapsedTime = 10 expected := "OptElapsedTime{elapsedtime=10}" if optString := opt.String(); optString != expected { t.Fatalf("Invalid elapsed time string. Expected %v, got %v", expected, optString) diff --git a/dhcpv6/option_nontemporaryaddress.go b/dhcpv6/option_nontemporaryaddress.go index 24cb20f..e303ae7 100644 --- a/dhcpv6/option_nontemporaryaddress.go +++ b/dhcpv6/option_nontemporaryaddress.go @@ -9,10 +9,10 @@ import ( ) type OptIANA struct { - iaId [4]byte - t1 uint32 - t2 uint32 - options []Option + IaId [4]byte + T1 uint32 + T2 uint32 + Options []Option } func (op *OptIANA) Code() OptionCode { @@ -23,50 +23,18 @@ func (op *OptIANA) ToBytes() []byte { buf := make([]byte, 16) binary.BigEndian.PutUint16(buf[0:2], uint16(OPTION_IA_NA)) binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length())) - copy(buf[4:8], op.iaId[:]) - binary.BigEndian.PutUint32(buf[8:12], op.t1) - binary.BigEndian.PutUint32(buf[12:16], op.t2) - for _, opt := range op.options { + copy(buf[4:8], op.IaId[:]) + binary.BigEndian.PutUint32(buf[8:12], op.T1) + binary.BigEndian.PutUint32(buf[12:16], op.T2) + for _, opt := range op.Options { buf = append(buf, opt.ToBytes()...) } return buf } -func (op *OptIANA) IAID() []byte { - return op.iaId[:] -} - -func (op *OptIANA) SetIAID(iaId [4]byte) { - op.iaId = iaId -} - -func (op *OptIANA) T1() uint32 { - return op.t1 -} - -func (op *OptIANA) SetT1(t1 uint32) { - op.t1 = t1 -} - -func (op *OptIANA) T2() uint32 { - return op.t2 -} - -func (op *OptIANA) SetT2(t2 uint32) { - op.t2 = t2 -} - -func (op *OptIANA) Options() []Option { - return op.options -} - -func (op *OptIANA) SetOptions(options []Option) { - op.options = options -} - func (op *OptIANA) Length() int { l := 12 - for _, opt := range op.options { + for _, opt := range op.Options { l += 4 + opt.Length() } return l @@ -74,7 +42,7 @@ func (op *OptIANA) Length() int { func (op *OptIANA) String() string { return fmt.Sprintf("OptIANA{IAID=%v, t1=%v, t2=%v, options=%v}", - op.iaId, op.t1, op.t2, op.options) + op.IaId, op.T1, op.T2, op.Options) } // build an OptIANA structure from a sequence of bytes. @@ -84,12 +52,12 @@ func ParseOptIANA(data []byte) (*OptIANA, error) { if len(data) < 12 { return nil, fmt.Errorf("Invalid IA for Non-temporary Addresses data length. Expected at least 12 bytes, got %v", len(data)) } - copy(opt.iaId[:], data[:4]) - opt.t1 = binary.BigEndian.Uint32(data[4:8]) - opt.t2 = binary.BigEndian.Uint32(data[8:12]) + copy(opt.IaId[:], data[:4]) + opt.T1 = binary.BigEndian.Uint32(data[4:8]) + opt.T2 = binary.BigEndian.Uint32(data[8:12]) var err error if len(data[12:]) > 0 { - opt.options, err = OptionsFromBytes(data[12:]) + opt.Options, err = OptionsFromBytes(data[12:]) if err != nil { return nil, err } diff --git a/dhcpv6/option_nontemporaryaddress_test.go b/dhcpv6/option_nontemporaryaddress_test.go index 93a595e..4d87122 100644 --- a/dhcpv6/option_nontemporaryaddress_test.go +++ b/dhcpv6/option_nontemporaryaddress_test.go @@ -1,28 +1,62 @@ package dhcpv6 import ( - _ "testing" + "testing" + + "github.com/stretchr/testify/require" ) -/* temporarily disabled, sorry for the clowniness :( func TestOptIANAParseOptIANA(t *testing.T) { data := []byte{ - 02, // advertise - 0, 0x80, 0x8b, // transaction ID - // IA_NA option - 0, 3, // option code - 0, 40, // option length 1, 0, 0, 0, // IAID 0, 0, 0, 1, // T1 0, 0, 0, 2, // T2 0, 5, 0, 0x18, 0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0, 0, 0, 0xb2, 0x7a, 0, 0, 0xc0, 0x8a, // options } opt, err := ParseOptIANA(data) - if err != nil { - t.Fatal(err) + require.NoError(t, err) + require.Equal(t, len(data), opt.Length()) +} + +func TestOptIANAParseOptIANAInvalidLength(t *testing.T) { + data := []byte{ + 1, 0, 0, 0, // IAID + 0, 0, 0, 1, // T1 + // truncated from here + } + _, err := ParseOptIANA(data) + require.Error(t, err) +} + +func TestOptIANAParseOptIANAInvalidOptions(t *testing.T) { + data := []byte{ + 1, 0, 0, 0, // IAID + 0, 0, 0, 1, // T1 + 0, 0, 0, 2, // T2 + 0, 5, 0, 0x18, 0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0, 0, 0, 0xb2, 0x7a, // truncated options + } + _, err := ParseOptIANA(data) + require.Error(t, err) +} + +func TestOptIANAToBytes(t *testing.T) { + opt := OptIANA{ + IaId: [4]byte{1, 2, 3, 4}, + T1: 12345, + T2: 54321, + Options: []Option{ + &OptElapsedTime{ + ElapsedTime: 0xaabb, + }, + }, } - if oLen := opt.Length(); oLen != len(data) { - t.Fatalf("Invalid IANA option length. Expected %v, got %v", len(data), oLen) + expected := []byte{ + 0, 3, // OPTION_IA_NA + 0, 18, // length + 1, 2, 3, 4, // IA ID + 0, 0, 0x30, 0x39, // T1 = 12345 + 0, 0, 0xd4, 0x31, // T2 = 54321 + 0, 8, 0, 2, 0xaa, 0xbb, } + require.Equal(t, expected, opt.ToBytes()) } -*/ diff --git a/dhcpv6/option_relaymsg_test.go b/dhcpv6/option_relaymsg_test.go index f0b2ee6..6d9d6a6 100644 --- a/dhcpv6/option_relaymsg_test.go +++ b/dhcpv6/option_relaymsg_test.go @@ -112,7 +112,7 @@ func TestRelayMsgParseOptRelayMsgSingleEncapsulation(t *testing.T) { reflect.TypeOf(innerOpt), ) } - if eTime := eto.ElapsedTime(); eTime != 0x1122 { + if eTime := eto.ElapsedTime; eTime != 0x1122 { t.Fatalf("Invalid elapsed time. Expected 0x1122, got 0x%04x", eTime) } } |