mirror of
https://github.com/gortc/stun.git
synced 2025-09-26 20:41:36 +08:00
all: improve coverage (fix #26)
This commit is contained in:
46
addr_test.go
46
addr_test.go
@@ -11,6 +11,52 @@ func TestMappedAddress(t *testing.T) {
|
|||||||
IP: net.ParseIP("122.12.34.5"),
|
IP: net.ParseIP("122.12.34.5"),
|
||||||
Port: 5412,
|
Port: 5412,
|
||||||
}
|
}
|
||||||
|
if addr.String() != "122.12.34.5:5412" {
|
||||||
|
t.Error("bad string", addr)
|
||||||
|
}
|
||||||
|
t.Run("Bad length", func(t *testing.T) {
|
||||||
|
badAddr := &MappedAddress{
|
||||||
|
IP: net.IP{1, 2, 3},
|
||||||
|
}
|
||||||
|
if err := badAddr.AddTo(m); err == nil {
|
||||||
|
t.Error("should error")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("AddTo", func(t *testing.T) {
|
||||||
|
if err := addr.AddTo(m); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
t.Run("GetFrom", func(t *testing.T) {
|
||||||
|
got := new(MappedAddress)
|
||||||
|
if err := got.GetFrom(m); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if !got.IP.Equal(addr.IP) {
|
||||||
|
t.Error("got bad IP: ", got.IP)
|
||||||
|
}
|
||||||
|
t.Run("Not found", func(t *testing.T) {
|
||||||
|
message := new(Message)
|
||||||
|
if err := got.GetFrom(message); err != ErrAttributeNotFound {
|
||||||
|
t.Error("should be not found: ", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("Bad family", func(t *testing.T) {
|
||||||
|
v, _ := m.Attributes.Get(AttrMappedAddress)
|
||||||
|
v.Value[0] = 32
|
||||||
|
if err := got.GetFrom(m); err == nil {
|
||||||
|
t.Error("should error")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMappedAddressV6(t *testing.T) {
|
||||||
|
m := new(Message)
|
||||||
|
addr := &MappedAddress{
|
||||||
|
IP: net.ParseIP("::"),
|
||||||
|
Port: 5412,
|
||||||
|
}
|
||||||
t.Run("AddTo", func(t *testing.T) {
|
t.Run("AddTo", func(t *testing.T) {
|
||||||
if err := addr.AddTo(m); err != nil {
|
if err := addr.AddTo(m); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
@@ -59,3 +59,14 @@ func TestPadding(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAttrLengthError_Error(t *testing.T) {
|
||||||
|
err := AttrLengthError{
|
||||||
|
Got: 100,
|
||||||
|
Max: 50,
|
||||||
|
Type: AttrLifetime,
|
||||||
|
}
|
||||||
|
if err.Error() != "Length of LIFETIME attribute 100 exceeds maximum 50" {
|
||||||
|
t.Error("bad error string", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -71,3 +71,25 @@ func TestMessage_AddErrorCode(t *testing.T) {
|
|||||||
t.Error("bad reason", string(errCodeAttr.Reason))
|
t.Error("bad reason", string(errCodeAttr.Reason))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestErrorCode(t *testing.T) {
|
||||||
|
a := &ErrorCodeAttribute{
|
||||||
|
Code: 404,
|
||||||
|
Reason: []byte("not found!"),
|
||||||
|
}
|
||||||
|
if a.String() != "404: not found!" {
|
||||||
|
t.Error("bad string", a)
|
||||||
|
}
|
||||||
|
m := New()
|
||||||
|
cod := ErrorCode(666)
|
||||||
|
if err := cod.AddTo(m); err != ErrNoDefaultReason {
|
||||||
|
t.Error("should be ErrNoDefaultReason", err)
|
||||||
|
}
|
||||||
|
if err := a.GetFrom(m); err == nil {
|
||||||
|
t.Error("attr should not be in message")
|
||||||
|
}
|
||||||
|
a.Reason = make([]byte, 2048)
|
||||||
|
if err := a.AddTo(m); err == nil {
|
||||||
|
t.Error("should error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -39,6 +39,19 @@ func TestFingerprint_Check(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 err := Fingerprint.Check(m); err == nil {
|
||||||
|
t.Error("should error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkFingerprint_Check(b *testing.B) {
|
func BenchmarkFingerprint_Check(b *testing.B) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
m := new(Message)
|
m := new(Message)
|
||||||
|
@@ -1,6 +1,9 @@
|
|||||||
package stun
|
package stun
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func BenchmarkBuildOverhead(b *testing.B) {
|
func BenchmarkBuildOverhead(b *testing.B) {
|
||||||
var (
|
var (
|
||||||
@@ -66,3 +69,33 @@ func TestMessage_Apply(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type errReturner struct {
|
||||||
|
Err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e errReturner) AddTo(m *Message) error {
|
||||||
|
return e.Err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e errReturner) Check(m *Message) error {
|
||||||
|
return e.Err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e errReturner) GetFrom(m *Message) error {
|
||||||
|
return e.Err
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHelpersErrorHandling(t *testing.T) {
|
||||||
|
m := New()
|
||||||
|
e := errReturner{Err: errors.New("tError")}
|
||||||
|
if err := m.Build(e); err != e.Err {
|
||||||
|
t.Error(err, "!=", e.Err)
|
||||||
|
}
|
||||||
|
if err := m.Check(e); err != e.Err {
|
||||||
|
t.Error(err, "!=", e.Err)
|
||||||
|
}
|
||||||
|
if err := m.Parse(e); err != e.Err {
|
||||||
|
t.Error(err, "!=", e.Err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -3,6 +3,7 @@ package stun
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -47,6 +48,12 @@ func TestMessageIntegrityWithFingerprint(t *testing.T) {
|
|||||||
m.WriteHeader()
|
m.WriteHeader()
|
||||||
NewSoftware("software").AddTo(m)
|
NewSoftware("software").AddTo(m)
|
||||||
i := NewShortTermIntegrity("pwd")
|
i := NewShortTermIntegrity("pwd")
|
||||||
|
if i.String() != "KEY: 0x707764" {
|
||||||
|
t.Error("bad string", i)
|
||||||
|
}
|
||||||
|
if err := i.Check(m); err == nil {
|
||||||
|
t.Error("should error")
|
||||||
|
}
|
||||||
if err := i.AddTo(m); err != nil {
|
if err := i.AddTo(m); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -56,6 +63,14 @@ func TestMessageIntegrityWithFingerprint(t *testing.T) {
|
|||||||
if err := i.Check(m); err != nil {
|
if err := i.Check(m); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
m.Raw[24] = 33
|
||||||
|
errStr := fmt.Sprintf("Integrity check failed: 0x%s (expected) !- 0x%s (actual)",
|
||||||
|
"19985afb819c098acfe1c2771881227f14c70eaf",
|
||||||
|
"ef9da0e0caf0b0e4ff321e7b56f1e114c802cb7e",
|
||||||
|
)
|
||||||
|
if err := i.Check(m); err.Error() != errStr {
|
||||||
|
t.Fatal(err, "!=", errStr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMessageIntegrity(t *testing.T) {
|
func TestMessageIntegrity(t *testing.T) {
|
||||||
@@ -72,6 +87,17 @@ func TestMessageIntegrity(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMessageIntegrityBeforeFingerprint(t *testing.T) {
|
||||||
|
m := new(Message)
|
||||||
|
//NewSoftware("software")
|
||||||
|
m.WriteHeader()
|
||||||
|
Fingerprint.AddTo(m)
|
||||||
|
i := NewShortTermIntegrity("password")
|
||||||
|
if err := i.AddTo(m); err == nil {
|
||||||
|
t.Error("should error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkMessageIntegrity_AddTo(b *testing.B) {
|
func BenchmarkMessageIntegrity_AddTo(b *testing.B) {
|
||||||
m := new(Message)
|
m := new(Message)
|
||||||
integrity := NewShortTermIntegrity("password")
|
integrity := NewShortTermIntegrity("password")
|
||||||
|
@@ -13,10 +13,13 @@ func TestUnknownAttributes(t *testing.T) {
|
|||||||
if a.String() != "DONT-FRAGMENT, CHANNEL-NUMBER" {
|
if a.String() != "DONT-FRAGMENT, CHANNEL-NUMBER" {
|
||||||
t.Error("bad String:", a)
|
t.Error("bad String:", a)
|
||||||
}
|
}
|
||||||
|
if (UnknownAttributes{}).String() != "<nil>" {
|
||||||
|
t.Error("bad blank stirng")
|
||||||
|
}
|
||||||
if err := a.AddTo(m); err != nil {
|
if err := a.AddTo(m); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
t.Run("AppendFrom", func(t *testing.T) {
|
t.Run("GetFrom", func(t *testing.T) {
|
||||||
attrs := make(UnknownAttributes, 10)
|
attrs := make(UnknownAttributes, 10)
|
||||||
if err := attrs.GetFrom(m); err != nil {
|
if err := attrs.GetFrom(m); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
@@ -26,6 +29,14 @@ func TestUnknownAttributes(t *testing.T) {
|
|||||||
t.Error("expected", 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")
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user