Skip to content

Commit

Permalink
adding test for features reply
Browse files Browse the repository at this point in the history
  • Loading branch information
jonstout committed Mar 9, 2014
1 parent 0be3ff2 commit 7919aad
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
7 changes: 4 additions & 3 deletions core/switch.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package core

import (
"github.com/jonstout/ogo/protocol/ofp10"
"github.com/jonstout/ogo/protocol/ofpxx"
"github.com/jonstout/ogo/protocol/util"
"log"
"net"
"sync"

"github.com/jonstout/ogo/protocol/ofp10"
"github.com/jonstout/ogo/protocol/ofpxx"
"github.com/jonstout/ogo/protocol/util"
)

// A map from DPIDs to all Switches that have connected since
Expand Down
File renamed without changes.
File renamed without changes.
38 changes: 17 additions & 21 deletions protocol/ofp10/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ofp10

import (
"encoding/binary"
//"errors"
"net"

"github.com/jonstout/ogo/protocol/ofpxx"
Expand Down Expand Up @@ -30,6 +29,7 @@ func NewFeaturesRequest() *ofpxx.Header {
// FeaturesReply constructor
func NewFeaturesReply() *SwitchFeatures {
res := new(SwitchFeatures)
res.Header = ofpxx.NewOfp10Header()
res.Header.Type = Type_FeaturesReply
res.DPID = make([]byte, 8)
res.pad = make([]byte, 3)
Expand All @@ -49,26 +49,24 @@ func (s *SwitchFeatures) Len() (n uint16) {

func (s *SwitchFeatures) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(s.Len()))
s.Header.Length = s.Len()
bytes := make([]byte, 0)
next := 0

bytes, err := s.Header.MarshalBinary()
if err != nil {
return
}
s.Header.Length = s.Len()
bytes, err = s.Header.MarshalBinary()
copy(data[next:], bytes)
next = len(bytes)
next += len(bytes)
binary.BigEndian.PutUint32(data[next:], s.Buffers)
next += 4
data[next] = s.Tables
next += 1
copy(data, s.pad)
copy(data[next:], s.pad)
next += len(s.pad)
binary.BigEndian.PutUint32(data[next:], s.Capabilities)
next += 4
binary.BigEndian.PutUint32(data[next:], s.Actions)
next += 4

for _, p := range s.Ports {
bytes, err = p.MarshalBinary()
if err != nil {
Expand All @@ -81,32 +79,30 @@ func (s *SwitchFeatures) MarshalBinary() (data []byte, err error) {
}

func (s *SwitchFeatures) UnmarshalBinary(data []byte) error {
err := s.Header.UnmarshalBinary(data)
if err != nil {
return err
}
next := int(s.Header.Len())
var err error
next := 0

err = s.Header.UnmarshalBinary(data[next:])
next = int(s.Header.Len())
copy(s.DPID, data[next:])
next += len(s.DPID)
s.Buffers = binary.BigEndian.Uint32(data[next:])
next += 4
s.Tables = data[next]
next += 1
copy(s.pad, data[next:])
next += 3
next += len(s.pad)
s.Capabilities = binary.BigEndian.Uint32(data[next:])
next += 4
s.Actions = binary.BigEndian.Uint32(data[next:])
next += 4

for _, p := range s.Ports {
for next < len(data) {
p := NewPhyPort()
err = p.UnmarshalBinary(data[next:])
if err != nil {
return err
}
next += int(p.Len())
}
return nil
}
return err
}

// ofp_capabilities 1.0
Expand Down
26 changes: 26 additions & 0 deletions protocol/ofp10/features_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ofp10

import (
"encoding/hex"
"strings"
"testing"
)

func TestFeaturesReplyMarshalBinary(t *testing.T) {
b := " 01 06 00 20 00 00 00 02" + // Header
"00 00 00 00 00 00 00 00" + // DPID
"00 00 00 00" + // Buffers
"00 00 00 00" + // Tables and pad
"00 00 00 00" + // Capabilities
"00 00 00 00" // Actions
b = strings.Replace(b, " ", "", -1)

f := NewFeaturesReply()
data, _ := f.MarshalBinary()
d := hex.EncodeToString(data)
if (len(b) != len(d)) || (b != d) {
t.Log("Exp:", b)
t.Log("Rec:", d)
t.Errorf("Received length of %d, expected %d", len(d), len(b))
}
}

0 comments on commit 7919aad

Please sign in to comment.