File: fingerprint_test.go

package info (click to toggle)
golang-github-pion-stun 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 868 kB
  • sloc: sh: 50; makefile: 40
file content (79 lines) | stat: -rw-r--r-- 1,773 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
77
78
79
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

//go:build !js
// +build !js

package stun

import (
	"net"
	"testing"
)

func BenchmarkFingerprint_AddTo(b *testing.B) {
	b.ReportAllocs()
	m := new(Message)
	s := NewSoftware("software")
	addr := &XORMappedAddress{
		IP: net.IPv4(213, 1, 223, 5),
	}
	addAttr(b, m, addr)
	addAttr(b, m, s)
	b.SetBytes(int64(len(m.Raw)))
	for i := 0; i < b.N; i++ {
		Fingerprint.AddTo(m) //nolint:errcheck,gosec
		m.WriteLength()
		m.Length -= attributeHeaderSize + fingerprintSize
		m.Raw = m.Raw[:m.Length+messageHeaderSize]
		m.Attributes = m.Attributes[:len(m.Attributes)-1]
	}
}

func TestFingerprint_Check(t *testing.T) {
	m := new(Message)
	addAttr(t, m, NewSoftware("software"))
	m.WriteHeader()
	Fingerprint.AddTo(m) //nolint:errcheck,gosec
	m.WriteHeader()
	if err := Fingerprint.Check(m); err != nil {
		t.Error(err)
	}
	m.Raw[3]++
	if err := Fingerprint.Check(m); err == nil {
		t.Error("should error")
	}
}

func TestFingerprint_CheckBad(t *testing.T) {
	m := new(Message)
	addAttr(t, m, NewSoftware("software"))
	m.WriteHeader()
	if err := Fingerprint.Check(m); err == nil {
		t.Error("should error")
	}
	m.Add(AttrFingerprint, []byte{1, 2, 3})
	if !IsAttrSizeInvalid(Fingerprint.Check(m)) {
		t.Error("IsAttrSizeInvalid should be true")
	}
}

func BenchmarkFingerprint_Check(b *testing.B) {
	b.ReportAllocs()
	m := new(Message)
	s := NewSoftware("software")
	addr := &XORMappedAddress{
		IP: net.IPv4(213, 1, 223, 5),
	}
	addAttr(b, m, addr)
	addAttr(b, m, s)
	m.WriteHeader()
	Fingerprint.AddTo(m) //nolint:errcheck,gosec
	m.WriteHeader()
	b.SetBytes(int64(len(m.Raw)))
	for i := 0; i < b.N; i++ {
		if err := Fingerprint.Check(m); err != nil {
			b.Fatal(err)
		}
	}
}