Skip to content
This repository has been archived by the owner on Jun 5, 2021. It is now read-only.

Commit

Permalink
attributes: implement Setter for RawAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Nov 16, 2018
1 parent 902ea39 commit db2ead4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/stun1.18.0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pkg github.com/gortc/stun, method (RawAttribute) AddTo(*Message) error
7 changes: 7 additions & 0 deletions attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ type RawAttribute struct {
Value []byte
}

// AddTo implements Setter, adding attribute as a.Type with a.Value and ignoring
// the Length field.
func (a RawAttribute) AddTo(m *Message) error {
m.Add(a.Type, a.Value)
return nil
}

// Equal returns true if a == b.
func (a RawAttribute) Equal(b RawAttribute) bool {
if a.Type != b.Type {
Expand Down
19 changes: 19 additions & 0 deletions attributes_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package stun

import (
"bytes"
"testing"
)

Expand All @@ -21,6 +22,24 @@ func BenchmarkMessage_Get(b *testing.B) {
}
}

func TestRawAttribute_AddTo(t *testing.T) {
v := []byte{1, 2, 3, 4}
m, err := Build(RawAttribute{
Type: AttrData,
Value: v,
})
if err != nil {
t.Fatal(err)
}
gotV, gotErr := m.Get(AttrData)
if gotErr != nil {
t.Fatal(gotErr)
}
if !bytes.Equal(gotV, v) {
t.Error("value mismatch")
}
}

func TestMessage_GetNoAllocs(t *testing.T) {
m := New()
NewSoftware("c").AddTo(m)
Expand Down

0 comments on commit db2ead4

Please sign in to comment.