<commit_before>// Copyright 2013-2014 go-diameter authors.  All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package diam

import (
	"bytes"
	"encoding/hex"
	"testing"

	"github.com/fiorix/go-diameter/diam/avp"
	"github.com/fiorix/go-diameter/diam/avp/format"
	"github.com/fiorix/go-diameter/diam/dict"
)

// testGroupedAVP is a Vendor-Specific-Application-Id Grouped AVP.
var testGroupedAVP = []byte{
	0x00, 0x00, 0x01, 0x04,
	0x40, 0x00, 0x00, 0x20,
	0x00, 0x00, 0x01, 0x02, // Auth-Application-Id
	0x40, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x01, 0x0a, // Vendor-Id
	0x40, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x28, 0xaf,
}

func TestGroupedAVP(t *testing.T) {
	a, err := DecodeAVP(testGroupedAVP, 0, dict.Default)
	if err != nil {
		t.Fatal(err)
	}
	if a.Data.Format() != GroupedAVPFormat {
		t.Fatal("AVP is not grouped")
	}
	b, err := a.Serialize()
	if !bytes.Equal(b, testGroupedAVP) {
		t.Fatalf("Unexpected value.\nWant:\n%s\nHave:\n%s",
			hex.Dump(testGroupedAVP), hex.Dump(b))
	}
	t.Log(a)
}

func TestDecodeMessageWithGroupedAVP(t *testing.T) {
	/*
		m := NewRequest(257, 0, dict.Default)
		m.NewAVP(264, 0x40, 0, format.DiameterIdentity("client"))
		a, _ := decode(testGroupedAVP, 0, dict.Default)
		m.AddAVP(a)
		t.Logf("Message:\n%s", m)
	*/
}

func TestMakeGroupedAVP(t *testing.T) {
	g := &GroupedAVP{
		AVP: []*AVP{
			NewAVP(avp.AuthApplicationId, avp.Mbit, 0, format.Unsigned32(4)),
			NewAVP(avp.VendorId, avp.Mbit, 0, format.Unsigned32(10415)),
		},
	}
	a := NewAVP(avp.VendorSpecificApplicationId, avp.Mbit, 0, g)<commit_msg>Enable test for decoding message with grouped AVPs<commit_after>// Copyright 2013-2014 go-diameter authors.  All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package diam

import (
	"bytes"
	"encoding/hex"
	"testing"

	"github.com/fiorix/go-diameter/diam/avp"
	"github.com/fiorix/go-diameter/diam/avp/format"
	"github.com/fiorix/go-diameter/diam/dict"
)

// testGroupedAVP is a Vendor-Specific-Application-Id Grouped AVP.
var testGroupedAVP = []byte{
	0x00, 0x00, 0x01, 0x04,
	0x40, 0x00, 0x00, 0x20,
	0x00, 0x00, 0x01, 0x02, // Auth-Application-Id
	0x40, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x01, 0x0a, // Vendor-Id
	0x40, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x28, 0xaf,
}

func TestGroupedAVP(t *testing.T) {
	a, err := DecodeAVP(testGroupedAVP, 0, dict.Default)
	if err != nil {
		t.Fatal(err)
	}
	if a.Data.Format() != GroupedAVPFormat {
		t.Fatal("AVP is not grouped")
	}
	b, err := a.Serialize()
	if !bytes.Equal(b, testGroupedAVP) {
		t.Fatalf("Unexpected value.\nWant:\n%s\nHave:\n%s",
			hex.Dump(testGroupedAVP), hex.Dump(b))
	}
	t.Log(a)
}

func TestDecodeMessageWithGroupedAVP(t *testing.T) {
	m := NewRequest(257, 0, dict.Default)
	m.NewAVP(264, 0x40, 0, format.DiameterIdentity("client"))
	a, _ := DecodeAVP(testGroupedAVP, 0, dict.Default)
	m.AddAVP(a)
	t.Logf("Message:\n%s", m)
}

func TestMakeGroupedAVP(t *testing.T) {
	g := &GroupedAVP{
		AVP: []*AVP{
			NewAVP(avp.AuthApplicationId, avp.Mbit, 0, format.Unsigned32(4)),
			NewAVP(avp.VendorId, avp.Mbit, 0, format.Unsigned32(10415)),
		},
	}
	a := NewAVP(avp.VendorSpecificApplicationId, avp.Mbit, 0, g)
{'fraction_non_alphanumeric': 0.16036308623298035, 'fraction_numerical': 0.07564296520423601, 'mean_word_length': 4.109737248840804, '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': '3992053', 'n_tokens_mistral': 1711, 'n_tokens_neox': 1506, 'n_words': 300}