<commit_before>package turn

import (
	"testing"

	"github.com/gortc/stun"
)

func TestRequestedAddressFamily(t *testing.T) {
	t.Run("String", func(t *testing.T) {
		if RequestedFamilyIPv4.String() != "IPv4" {
			t.Errorf("bad string %q, expected %q", RequestedFamilyIPv4,
				"IPv4",
			)
		}
		if RequestedFamilyIPv6.String() != "IPv6" {
			t.Errorf("bad string %q, expected %q", RequestedFamilyIPv6,
				"IPv6",
			)
		}
		if RequestedAddressFamily(0x04).String() != "unknown" {
			t.Error("should be unknown")
		}
	})
	t.Run("NoAlloc", func(t *testing.T) {
		m := &stun.Message{}
		if wasAllocs(func() {
			// On stack.
			r := RequestedFamilyIPv4
			r.AddTo(m)
			m.Reset()
		}) {
			t.Error("Unexpected allocations")
		}

		r := new(RequestedAddressFamily)
		*r = RequestedFamilyIPv4
		if wasAllocs(func() {
			// On heap.
			r.AddTo(m)
			m.Reset()
		}) {
			t.Error("Unexpected allocations")
		}
	})
	t.Run("AddTo", func(t *testing.T) {
		m := new(stun.Message)
		r := RequestedFamilyIPv4
		if err := r.AddTo(m); err != nil {
			t.Error(err)
		}
		m.WriteHeader()
		t.Run("GetFrom", func(t *testing.T) {
			decoded := new(stun.Message)
			if _, err := decoded.Write(m.Raw); err != nil {
				t.Fatal("failed to decode message:", err)
			}
			var req RequestedAddressFamily
			if err := req.GetFrom(decoded); err != nil {
				t.Fatal(err)
			}
			if req != r {
				t.Errorf("Decoded %q, expected %q", req, r)
			}
			if wasAllocs(func() {
				r.GetFrom(decoded)
			}) {
				t.Error("Unexpected allocations")
			}
			t.Run("HandleErr", func(t *testing.T) {
				m := new(stun.Message)
				var handle RequestedAddressFamily
				if err := handle.GetFrom(m); err != stun.ErrAttributeNotFound {
					t.Errorf("%v should be not found", err)
				}
				m.Add(stun.AttrRequestedAddressFamily, []byte{1, 2, 3})
				if !stun.IsAttrSizeInvalid(handle.GetFrom(m)) {
					t.Error("IsAttrSizeInvalid should be true")
				}
				m.Reset()
				m.Add(stun.AttrRequestedAddressFamily, []byte{5, 0, 0, 0}[:])<commit_msg>reqfamily: fix unslice<commit_after>package turn

import (
	"testing"

	"github.com/gortc/stun"
)

func TestRequestedAddressFamily(t *testing.T) {
	t.Run("String", func(t *testing.T) {
		if RequestedFamilyIPv4.String() != "IPv4" {
			t.Errorf("bad string %q, expected %q", RequestedFamilyIPv4,
				"IPv4",
			)
		}
		if RequestedFamilyIPv6.String() != "IPv6" {
			t.Errorf("bad string %q, expected %q", RequestedFamilyIPv6,
				"IPv6",
			)
		}
		if RequestedAddressFamily(0x04).String() != "unknown" {
			t.Error("should be unknown")
		}
	})
	t.Run("NoAlloc", func(t *testing.T) {
		m := &stun.Message{}
		if wasAllocs(func() {
			// On stack.
			r := RequestedFamilyIPv4
			r.AddTo(m)
			m.Reset()
		}) {
			t.Error("Unexpected allocations")
		}

		r := new(RequestedAddressFamily)
		*r = RequestedFamilyIPv4
		if wasAllocs(func() {
			// On heap.
			r.AddTo(m)
			m.Reset()
		}) {
			t.Error("Unexpected allocations")
		}
	})
	t.Run("AddTo", func(t *testing.T) {
		m := new(stun.Message)
		r := RequestedFamilyIPv4
		if err := r.AddTo(m); err != nil {
			t.Error(err)
		}
		m.WriteHeader()
		t.Run("GetFrom", func(t *testing.T) {
			decoded := new(stun.Message)
			if _, err := decoded.Write(m.Raw); err != nil {
				t.Fatal("failed to decode message:", err)
			}
			var req RequestedAddressFamily
			if err := req.GetFrom(decoded); err != nil {
				t.Fatal(err)
			}
			if req != r {
				t.Errorf("Decoded %q, expected %q", req, r)
			}
			if wasAllocs(func() {
				r.GetFrom(decoded)
			}) {
				t.Error("Unexpected allocations")
			}
			t.Run("HandleErr", func(t *testing.T) {
				m := new(stun.Message)
				var handle RequestedAddressFamily
				if err := handle.GetFrom(m); err != stun.ErrAttributeNotFound {
					t.Errorf("%v should be not found", err)
				}
				m.Add(stun.AttrRequestedAddressFamily, []byte{1, 2, 3})
				if !stun.IsAttrSizeInvalid(handle.GetFrom(m)) {
					t.Error("IsAttrSizeInvalid should be true")
				}
				m.Reset()
				m.Add(stun.AttrRequestedAddressFamily, []byte{5, 0, 0, 0})
{'fraction_non_alphanumeric': 0.17140009965122072, 'fraction_numerical': 0.01046337817638266, 'mean_word_length': 3.160621761658031, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7345603', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1561, 'n_words': 303}