File: uattrs_test.go

package info (click to toggle)
golang-github-pion-stun 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 868 kB
  • sloc: sh: 50; makefile: 40
file content (76 lines) | stat: -rw-r--r-- 1,597 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package stun

import (
	"testing"
)

func TestUnknownAttributes(t *testing.T) {
	m := new(Message)
	a := &UnknownAttributes{
		AttrDontFragment,
		AttrChannelNumber,
	}
	if a.String() != "DONT-FRAGMENT, CHANNEL-NUMBER" {
		t.Error("bad String:", a)
	}
	if (UnknownAttributes{}).String() != "<nil>" {
		t.Error("bad blank string")
	}
	if err := a.AddTo(m); err != nil {
		t.Error(err)
	}
	t.Run("GetFrom", func(t *testing.T) {
		attrs := make(UnknownAttributes, 10)
		if err := attrs.GetFrom(m); err != nil {
			t.Error(err)
		}
		for i, at := range *a {
			if at != attrs[i] {
				t.Error("expected", at, "!=", attrs[i])
			}
		}
		mBlank := new(Message)
		if err := attrs.GetFrom(mBlank); err == nil {
			t.Error("should error")
		}
		mBlank.Add(AttrUnknownAttributes, []byte{1, 2, 3})
		if err := attrs.GetFrom(mBlank); err == nil {
			t.Error("should error")
		}
	})
}

func BenchmarkUnknownAttributes(b *testing.B) {
	m := new(Message)
	a := UnknownAttributes{
		AttrDontFragment,
		AttrChannelNumber,
		AttrRealm,
		AttrMessageIntegrity,
	}
	b.Run("AddTo", func(b *testing.B) {
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			if err := a.AddTo(m); err != nil {
				b.Fatal(err)
			}
			m.Reset()
		}
	})
	b.Run("GetFrom", func(b *testing.B) {
		b.ReportAllocs()
		if err := a.AddTo(m); err != nil {
			b.Fatal(err)
		}
		attrs := make(UnknownAttributes, 0, 10)
		for i := 0; i < b.N; i++ {
			if err := attrs.GetFrom(m); err != nil {
				b.Fatal(err)
			}
			attrs = attrs[:0]
		}
	})
}