From db2ead48d4cef17911747815cdbf66550858e634 Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Fri, 16 Nov 2018 04:45:20 +0300 Subject: [PATCH] attributes: implement Setter for RawAttribute --- api/stun1.18.0.txt | 1 + attributes.go | 7 +++++++ attributes_test.go | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 api/stun1.18.0.txt diff --git a/api/stun1.18.0.txt b/api/stun1.18.0.txt new file mode 100644 index 0000000..f5ac3d2 --- /dev/null +++ b/api/stun1.18.0.txt @@ -0,0 +1 @@ +pkg github.com/gortc/stun, method (RawAttribute) AddTo(*Message) error diff --git a/attributes.go b/attributes.go index ae8aba9..da0ca74 100644 --- a/attributes.go +++ b/attributes.go @@ -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 { diff --git a/attributes_test.go b/attributes_test.go index 1838db5..6df873a 100644 --- a/attributes_test.go +++ b/attributes_test.go @@ -1,6 +1,7 @@ package stun import ( + "bytes" "testing" ) @@ -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)