本文整理匯總了Golang中github.com/fiorix/go-diameter/diam.NewAVP函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewAVP函數的具體用法?Golang NewAVP怎麽用?Golang NewAVP使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewAVP函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestClient_Handshake
func TestClient_Handshake(t *testing.T) {
srv := diamtest.NewServer(New(serverSettings), dict.Default)
defer srv.Close()
cli := &Client{
Handler: New(clientSettings),
SupportedVendorID: []*diam.AVP{
diam.NewAVP(avp.SupportedVendorID, avp.Mbit, 0, clientSettings.VendorID),
},
AcctApplicationID: []*diam.AVP{
diam.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(0)),
},
AuthApplicationID: []*diam.AVP{
diam.NewAVP(avp.AuthApplicationID, avp.Mbit, 0, datatype.Unsigned32(0)),
},
VendorSpecificApplicationID: []*diam.AVP{
diam.NewAVP(avp.VendorSpecificApplicationID, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(0)),
},
}),
},
}
c, err := cli.Dial(srv.Address)
if err != nil {
t.Fatal(err)
}
c.Close()
}
示例2: TestMetaValueExponent
func TestMetaValueExponent(t *testing.T) {
m := diam.NewRequest(diam.CreditControl, 4, nil)
m.NewAVP("Session-Id", avp.Mbit, 0, datatype.UTF8String("simuhuawei;1449573472;00002"))
m.NewAVP(avp.RequestedServiceUnit, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.CCMoney, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.UnitValue, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.ValueDigits, avp.Mbit, 0, datatype.Integer64(10000)),
diam.NewAVP(avp.Exponent, avp.Mbit, 0, datatype.Integer32(-5)),
},
}),
diam.NewAVP(avp.CurrencyCode, avp.Mbit, 0, datatype.Unsigned32(33)),
},
}),
},
})
if val, err := metaValueExponent(m, utils.ParseRSRFieldsMustCompile("Requested-Service-Unit>CC-Money>Unit-Value>Value-Digits;^|;Requested-Service-Unit>CC-Money>Unit-Value>Exponent", utils.INFIELD_SEP), 10); err != nil {
t.Error(err)
} else if val != "0.1" {
t.Error("Received: ", val)
}
if _, err = metaValueExponent(m, utils.ParseRSRFieldsMustCompile("Requested-Service-Unit>CC-Money>Unit-Value>Value-Digits;Requested-Service-Unit>CC-Money>Unit-Value>Exponent", utils.INFIELD_SEP), 10); err == nil {
t.Error("Should have received error") // Insufficient number arguments
}
}
示例3: TestFieldOutVal
func TestFieldOutVal(t *testing.T) {
m := diam.NewRequest(diam.CreditControl, 4, nil)
m.NewAVP("Session-Id", avp.Mbit, 0, datatype.UTF8String("simuhuawei;1449573472;00002"))
m.NewAVP("Subscription-Id", avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(450, avp.Mbit, 0, datatype.Enumerated(0)), // Subscription-Id-Type
diam.NewAVP(444, avp.Mbit, 0, datatype.UTF8String("33708000003")), // Subscription-Id-Data
}})
m.NewAVP("Subscription-Id", avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(450, avp.Mbit, 0, datatype.Enumerated(1)), // Subscription-Id-Type
diam.NewAVP(444, avp.Mbit, 0, datatype.UTF8String("208708000003")), // Subscription-Id-Data
}})
m.NewAVP("Service-Identifier", avp.Mbit, 0, datatype.Unsigned32(0))
m.NewAVP("Requested-Service-Unit", avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(420, avp.Mbit, 0, datatype.Unsigned32(360))}}) // CC-Time
cfgFld := &config.CfgCdrField{Tag: "StaticTest", Type: utils.META_COMPOSED, FieldId: utils.TOR,
Value: utils.ParseRSRFieldsMustCompile("^*voice", utils.INFIELD_SEP), Mandatory: true}
eOut := "*voice"
if fldOut, err := fieldOutVal(m, cfgFld, time.Duration(0)); err != nil {
t.Error(err)
} else if fldOut != eOut {
t.Errorf("Expecting: %s, received: %s", eOut, fldOut)
}
cfgFld = &config.CfgCdrField{Tag: "ComposedTest", Type: utils.META_COMPOSED, FieldId: utils.DESTINATION,
Value: utils.ParseRSRFieldsMustCompile("Requested-Service-Unit>CC-Time", utils.INFIELD_SEP), Mandatory: true}
eOut = "360"
if fldOut, err := fieldOutVal(m, cfgFld, time.Duration(0)); err != nil {
t.Error(err)
} else if fldOut != eOut {
t.Errorf("Expecting: %s, received: %s", eOut, fldOut)
}
// Without filter, we shoud get always the first subscriptionId
cfgFld = &config.CfgCdrField{Tag: "Grouped1", Type: utils.MetaGrouped, FieldId: "Account",
Value: utils.ParseRSRFieldsMustCompile("Subscription-Id>Subscription-Id-Data", utils.INFIELD_SEP), Mandatory: true}
eOut = "33708000003"
if fldOut, err := fieldOutVal(m, cfgFld, time.Duration(0)); err != nil {
t.Error(err)
} else if fldOut != eOut {
t.Errorf("Expecting: %s, received: %s", eOut, fldOut)
}
// Without groupedAVP, we shoud get the first subscriptionId
cfgFld = &config.CfgCdrField{Tag: "Grouped2", Type: utils.MetaGrouped, FieldId: "Account",
FieldFilter: utils.ParseRSRFieldsMustCompile("Subscription-Id>Subscription-Id-Type(1)", utils.INFIELD_SEP),
Value: utils.ParseRSRFieldsMustCompile("Subscription-Id>Subscription-Id-Data", utils.INFIELD_SEP), Mandatory: true}
eOut = "208708000003"
if fldOut, err := fieldOutVal(m, cfgFld, time.Duration(0)); err != nil {
t.Error(err)
} else if fldOut != eOut {
t.Errorf("Expecting: %s, received: %s", eOut, fldOut)
}
}
示例4: storedCdrToCCR
func storedCdrToCCR(cdr *engine.StoredCdr, originHost, originRealm string, vendorId int, productName string, firmwareRev int, debitInterval time.Duration, callEnded bool) *diam.Message {
sid := "session;" + strconv.Itoa(int(rand.Uint32()))
reqType, reqNr, ccTime := disectUsageForCCR(cdr.Usage, debitInterval, callEnded)
m := diam.NewRequest(272, 4, nil)
m.NewAVP(avp.SessionID, avp.Mbit, 0, datatype.UTF8String(sid))
m.NewAVP(avp.OriginHost, avp.Mbit, 0, datatype.DiameterIdentity(originHost))
m.NewAVP(avp.OriginRealm, avp.Mbit, 0, datatype.DiameterIdentity(originRealm))
m.NewAVP(avp.DestinationHost, avp.Mbit, 0, datatype.DiameterIdentity(originHost))
m.NewAVP(avp.DestinationRealm, avp.Mbit, 0, datatype.DiameterIdentity(originRealm))
m.NewAVP(avp.AuthApplicationID, avp.Mbit, 0, datatype.Unsigned32(4))
m.NewAVP(avp.ServiceContextID, avp.Mbit, 0, datatype.UTF8String("[email protected]"))
m.NewAVP(avp.CCRequestType, avp.Mbit, 0, datatype.Enumerated(reqType))
m.NewAVP(avp.CCRequestNumber, avp.Mbit, 0, datatype.Enumerated(reqNr))
m.NewAVP(avp.EventTimestamp, avp.Mbit, 0, datatype.Time(cdr.AnswerTime))
m.NewAVP(avp.SubscriptionID, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.SubscriptionIDType, avp.Mbit, 0, datatype.Enumerated(0)),
diam.NewAVP(avp.SubscriptionIDData, avp.Mbit, 0, datatype.UTF8String(cdr.Account)),
}})
m.NewAVP(avp.SubscriptionID, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.SubscriptionIDType, avp.Mbit, 0, datatype.Enumerated(1)),
diam.NewAVP(avp.SubscriptionIDData, avp.Mbit, 0, datatype.UTF8String("20921006232651")),
}})
m.NewAVP(avp.ServiceIdentifier, avp.Mbit, 0, datatype.Unsigned32(0))
m.NewAVP(avp.RequestedServiceUnit, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.CCTime, avp.Mbit, 0, datatype.Unsigned32(ccTime))}})
/*
m.NewAVP(avp.ServiceInformation, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(20300, avp.Mbit, 0, &diam.GroupedAVP{ // IN-Information
AVP: []*diam.AVP{
diam.NewAVP(avp.CallingPartyAddress, avp.Mbit, 0, datatype.UTF8String(cdr.Account)),
diam.NewAVP(avp.CalledPartyAddress, avp.Mbit, 0, datatype.UTF8String(cdr.Destination)),
diam.NewAVP(20327, avp.Mbit, 0, datatype.UTF8String(cdr.Destination)), // Real-Called-Number
diam.NewAVP(20339, avp.Mbit, 0, datatype.Unsigned32(0)), // Charge-Flow-Type
diam.NewAVP(20302, avp.Mbit, 0, datatype.UTF8String("33657954968")), // Calling-Vlr-Number
diam.NewAVP(20303, avp.Mbit, 0, datatype.UTF8String("31901485301525")), // Calling-CellID-Or-SAI
diam.NewAVP(avp.BearerCapability, avp.Mbit, 0, datatype.UTF8String("31901485301525")),
diam.NewAVP(20321, avp.Mbit, 0, datatype.UTF8String("31901485301525")), // Call-Reference-Number
diam.NewAVP(avp.MSCAddress, avp.Mbit, 0, datatype.UTF8String("")),
diam.NewAVP(20324, avp.Mbit, 0, datatype.UTF8String("0")), // Time-Zone
diam.NewAVP(20385, avp.Mbit, 0, datatype.UTF8String("")), // Called-Party-NP
diam.NewAVP(20386, avp.Mbit, 0, datatype.UTF8String("20091020120101")), // SSP-Time
},
}),
}})
*/
return m
}
示例5: NewDiameterClient
func NewDiameterClient(addr, originHost, originRealm string, vendorId int, productName string, firmwareRev int) (*DiameterClient, error) {
cfg := &sm.Settings{
OriginHost: datatype.DiameterIdentity(originHost),
OriginRealm: datatype.DiameterIdentity(originRealm),
VendorID: datatype.Unsigned32(vendorId),
ProductName: datatype.UTF8String(productName),
FirmwareRevision: datatype.Unsigned32(firmwareRev),
}
handlers := sm.New(cfg)
cli := &sm.Client{
Handler: handlers,
MaxRetransmits: 3,
RetransmitInterval: time.Second,
EnableWatchdog: true,
WatchdogInterval: 5 * time.Second,
AcctApplicationID: []*diam.AVP{
// Advertise that we want support for both
// Accounting applications 4 and 999.
diam.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(4)), // RFC 4006
},
}
conn, err := cli.Dial(addr)
if err != nil {
return nil, err
}
return &DiameterClient{conn: conn, handlers: handlers}, nil
}
示例6: TestAvpValAsString
func TestAvpValAsString(t *testing.T) {
originHostStr := "unit_test"
a := diam.NewAVP(avp.OriginHost, avp.Mbit, 0, datatype.DiameterIdentity(originHostStr))
if avpValStr := avpValAsString(a); avpValStr != originHostStr {
t.Errorf("Expected: %s, received: %s", originHostStr, avpValStr)
}
}
示例7: TestClient_Handshake_FailedResultCode
func TestClient_Handshake_FailedResultCode(t *testing.T) {
mux := diam.NewServeMux()
mux.HandleFunc("CER", func(c diam.Conn, m *diam.Message) {
cer := new(smparser.CER)
if _, err := cer.Parse(m); err != nil {
panic(err)
}
a := m.Answer(diam.NoCommonApplication)
a.NewAVP(avp.OriginHost, avp.Mbit, 0, clientSettings.OriginHost)
a.NewAVP(avp.OriginRealm, avp.Mbit, 0, clientSettings.OriginRealm)
a.AddAVP(cer.OriginStateID)
a.AddAVP(cer.AcctApplicationID[0]) // The one we send below.
a.WriteTo(c)
})
srv := diamtest.NewServer(mux, dict.Default)
defer srv.Close()
cli := &Client{
Handler: New(clientSettings),
AcctApplicationID: []*diam.AVP{
diam.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(0)),
},
}
_, err := cli.Dial(srv.Address)
if err == nil {
t.Fatal("Unexpected CER worked")
}
e, ok := err.(*ErrFailedResultCode)
if !ok {
t.Fatal(err)
}
if !strings.Contains(e.Error(), "failed Result-Code AVP") {
t.Fatal(e.Error())
}
}
示例8: TestClient_Watchdog_Timeout
func TestClient_Watchdog_Timeout(t *testing.T) {
sm := New(serverSettings)
var once sync.Once
sm.mux.HandleFunc("DWR", func(c diam.Conn, m *diam.Message) {
once.Do(func() { m.Answer(diam.UnableToComply).WriteTo(c) })
})
srv := diamtest.NewServer(sm, dict.Default)
defer srv.Close()
cli := &Client{
MaxRetransmits: 3,
RetransmitInterval: 50 * time.Millisecond,
EnableWatchdog: true,
WatchdogInterval: 50 * time.Millisecond,
Handler: New(clientSettings),
AcctApplicationID: []*diam.AVP{
diam.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(0)),
},
}
c, err := cli.Dial(srv.Address)
if err != nil {
t.Fatal(err)
}
defer c.Close()
select {
case <-c.(diam.CloseNotifier).CloseNotify():
case <-time.After(500 * time.Millisecond):
t.Fatal("Timeout waiting for watchdog to disconnect client")
}
}
示例9: TestClient_Watchdog
func TestClient_Watchdog(t *testing.T) {
srv := diamtest.NewServer(New(serverSettings), dict.Default)
defer srv.Close()
cli := &Client{
EnableWatchdog: true,
WatchdogInterval: 100 * time.Millisecond,
Handler: New(clientSettings),
AcctApplicationID: []*diam.AVP{
diam.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(0)),
},
}
c, err := cli.Dial(srv.Address)
if err != nil {
t.Fatal(err)
}
defer c.Close()
resp := make(chan struct{}, 1)
dwa := handleDWA(cli.Handler, resp)
cli.Handler.mux.HandleFunc("DWA", func(c diam.Conn, m *diam.Message) {
dwa(c, m)
})
select {
case <-resp:
case <-time.After(200 * time.Millisecond):
t.Fatal("Timeout waiting for DWA")
}
}
示例10: AsDiameterMessage
// Converts itself into DiameterMessage
func (self *CCA) AsDiameterMessage() (*diam.Message, error) {
if _, err := self.diamMessage.NewAVP("Session-Id", avp.Mbit, 0, datatype.UTF8String(self.SessionId)); err != nil {
return nil, err
}
if _, err := self.diamMessage.NewAVP("Origin-Host", avp.Mbit, 0, datatype.DiameterIdentity(self.OriginHost)); err != nil {
return nil, err
}
if _, err := self.diamMessage.NewAVP("Origin-Realm", avp.Mbit, 0, datatype.DiameterIdentity(self.OriginRealm)); err != nil {
return nil, err
}
if _, err := self.diamMessage.NewAVP("Auth-Application-Id", avp.Mbit, 0, datatype.Unsigned32(self.AuthApplicationId)); err != nil {
return nil, err
}
if _, err := self.diamMessage.NewAVP("CC-Request-Type", avp.Mbit, 0, datatype.Enumerated(self.CCRequestType)); err != nil {
return nil, err
}
if _, err := self.diamMessage.NewAVP("CC-Request-Number", avp.Mbit, 0, datatype.Enumerated(self.CCRequestNumber)); err != nil {
return nil, err
}
if _, err := self.diamMessage.NewAVP(avp.ResultCode, avp.Mbit, 0, datatype.Unsigned32(self.ResultCode)); err != nil {
return nil, err
}
ccTimeAvp, err := self.diamMessage.Dictionary().FindAVP(self.diamMessage.Header.ApplicationID, "CC-Time")
if err != nil {
return nil, err
}
if _, err := self.diamMessage.NewAVP("Granted-Service-Unit", avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(ccTimeAvp.Code, avp.Mbit, 0, datatype.Unsigned32(self.GrantedServiceUnit.CCTime))}}); err != nil {
return nil, err
}
return self.diamMessage, nil
}
示例11: TestClient_Handshake_RetransmitTimeout
func TestClient_Handshake_RetransmitTimeout(t *testing.T) {
mux := diam.NewServeMux()
var retransmits uint32
mux.HandleFunc("CER", func(c diam.Conn, m *diam.Message) {
// Do nothing to force timeout.
atomic.AddUint32(&retransmits, 1)
})
srv := diamtest.NewServer(mux, dict.Default)
defer srv.Close()
cli := &Client{
Handler: New(clientSettings),
MaxRetransmits: 3,
RetransmitInterval: time.Millisecond,
AcctApplicationID: []*diam.AVP{
diam.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(0)),
},
}
_, err := cli.Dial(srv.Address)
if err == nil {
t.Fatal("Unexpected CER worked")
}
if err != ErrHandshakeTimeout {
t.Fatal(err)
}
if n := atomic.LoadUint32(&retransmits); n != 4 {
t.Fatalf("Unexpected # of retransmits. Want 4, have %d", n)
}
}
示例12: messageSetAVPsWithPath
// messageAddAVPsWithPath will dynamically add AVPs into the message
// append: append to the message, on false overwrite if AVP is single or add to group if AVP is Grouped
func messageSetAVPsWithPath(m *diam.Message, path []interface{}, avpValStr string, appnd bool, timezone string) error {
if len(path) == 0 {
return errors.New("Empty path as AVP filter")
}
dictAVPs := make([]*dict.AVP, len(path)) // for each subpath, one dictionary AVP
for i, subpath := range path {
if dictAVP, err := m.Dictionary().FindAVP(m.Header.ApplicationID, subpath); err != nil {
return err
} else if dictAVP == nil {
return fmt.Errorf("Cannot find AVP with id: %s", path[len(path)-1])
} else {
dictAVPs[i] = dictAVP
}
}
if dictAVPs[len(path)-1].Data.Type == diam.GroupedAVPType {
return errors.New("Last AVP in path needs not to be GroupedAVP")
}
var msgAVP *diam.AVP // Keep a reference here towards last AVP
lastAVPIdx := len(path) - 1
for i := lastAVPIdx; i >= 0; i-- {
var typeVal datatype.Type
if i == lastAVPIdx {
avpValByte, err := serializeAVPValueFromString(dictAVPs[i], avpValStr, timezone)
if err != nil {
return err
}
typeVal, err = datatype.Decode(dictAVPs[i].Data.Type, avpValByte)
if err != nil {
return err
}
} else {
typeVal = &diam.GroupedAVP{
AVP: []*diam.AVP{msgAVP}}
}
newMsgAVP := diam.NewAVP(dictAVPs[i].Code, avp.Mbit, dictAVPs[i].VendorID, typeVal) // FixMe: maybe Mbit with dictionary one
if i == lastAVPIdx-1 && !appnd { // last AVP needs to be appended in group
avps, _ := m.FindAVPsWithPath(path[:lastAVPIdx], dict.UndefinedVendorID)
if len(avps) != 0 { // Group AVP already in the message
prevGrpData := avps[0].Data.(*diam.GroupedAVP)
prevGrpData.AVP = append(prevGrpData.AVP, msgAVP)
m.Header.MessageLength += uint32(msgAVP.Len())
return nil
}
}
msgAVP = newMsgAVP
}
if !appnd { // Not group AVP, replace the previous set one with this one
avps, _ := m.FindAVPsWithPath(path, dict.UndefinedVendorID)
if len(avps) != 0 { // Group AVP already in the message
m.Header.MessageLength -= uint32(avps[0].Len()) // decrease message length since we overwrite
*avps[0] = *msgAVP
m.Header.MessageLength += uint32(msgAVP.Len())
return nil
}
}
m.AVP = append(m.AVP, msgAVP)
m.Header.MessageLength += uint32(msgAVP.Len())
return nil
}
示例13: TestUnexpectedAVP_BadData
func TestUnexpectedAVP_BadData(t *testing.T) {
a := diam.NewAVP(avp.AcctApplicationID, 0, 0, datatype.OctetString("x"))
app := &Application{
AcctApplicationID: []*diam.AVP{a},
}
failedAVP, err := app.Parse(dict.Default)
if err == nil {
t.Fatal("Unexpected application parsed successfully")
}
if failedAVP != a {
t.Fatalf("Unexpected failed avp. Want %q, have %q", a, failedAVP)
}
}
示例14: TestClient_DialTLS_InvalidAddress
func TestClient_DialTLS_InvalidAddress(t *testing.T) {
cli := &Client{
Handler: New(clientSettings),
AcctApplicationID: []*diam.AVP{
diam.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(0)),
},
}
c, err := cli.DialTLS(":0", "", "")
if err == nil {
c.Close()
t.Fatal("Invalid client address succeeded")
}
}
示例15: TestUnexpectedAVP_BadGroup
func TestUnexpectedAVP_BadGroup(t *testing.T) {
a := diam.NewAVP(avp.AcctApplicationID, 0, 0, datatype.Unsigned32(0))
app := &Application{
VendorSpecificApplicationID: []*diam.AVP{a},
}
failedAVP, err := app.Parse(dict.Default)
if err == nil {
t.Fatal("Unexpected application parsed successfully")
}
if failedAVP != a {
t.Fatalf("Unexpected failed avp. Want %q, have %q", a, failedAVP)
}
}