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() }
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 } }
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) } }
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("*****@*****.**")) 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 }
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 }
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) } }
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()) } }
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") } }
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") } }
// 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 }
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) } }
// 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 }
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) } }
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") } }
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) } }
func TestClient_Handshake_Notify(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)), }, }), }, } handshakeOK := make(chan struct{}) go func() { <-cli.Handler.HandshakeNotify() close(handshakeOK) }() c, err := cli.Dial(srv.Address) if err != nil { t.Fatal(err) } defer c.Close() select { case <-handshakeOK: case <-time.After(time.Second): t.Fatal("Handshake timed out") } }
func TestCCASetProcessorAVPs(t *testing.T) { ccr := &CCR{ // Bare information, just the one needed for answer SessionId: "routinga;1442095190;1476802709", AuthApplicationId: 4, CCRequestType: 1, CCRequestNumber: 0, } ccr.diamMessage = ccr.AsBareDiameterMessage() ccr.diamMessage.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 }}) ccr.debitInterval = time.Duration(300) * time.Second cca := NewBareCCAFromCCR(ccr, "CGR-DA", "cgrates.org") reqProcessor := &config.DARequestProcessor{Id: "UNIT_TEST", // Set template for tests CCAFields: []*config.CfgCdrField{ &config.CfgCdrField{Tag: "Subscription-Id/Subscription-Id-Type", Type: utils.META_COMPOSED, FieldId: "Subscription-Id>Subscription-Id-Type", Value: utils.ParseRSRFieldsMustCompile("Subscription-Id>Subscription-Id-Type", utils.INFIELD_SEP), Mandatory: true}, &config.CfgCdrField{Tag: "Subscription-Id/Subscription-Id-Data", Type: utils.META_COMPOSED, FieldId: "Subscription-Id>Subscription-Id-Data", Value: utils.ParseRSRFieldsMustCompile("Subscription-Id>Subscription-Id-Data", utils.INFIELD_SEP), Mandatory: true}, }, } eMessage := cca.AsDiameterMessage() eMessage.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 }}) if err := cca.SetProcessorAVPs(reqProcessor, map[string]string{}); err != nil { t.Error(err) } else if ccaMsg := cca.AsDiameterMessage(); !reflect.DeepEqual(eMessage, ccaMsg) { t.Errorf("Expecting: %+v, received: %+v", eMessage, ccaMsg) } }
func TestHandleCER_VS_Auth_Fail(t *testing.T) { sm := New(serverSettings) srv := diamtest.NewServer(sm, dict.Default) defer srv.Close() mc := make(chan *diam.Message, 1) mux := diam.NewServeMux() mux.HandleFunc("CEA", func(c diam.Conn, m *diam.Message) { mc <- m }) cli, err := diam.Dial(srv.Address, mux, dict.Default) if err != nil { t.Fatal(err) } defer cli.Close() m := diam.NewRequest(diam.CapabilitiesExchange, 0, dict.Default) m.NewAVP(avp.OriginHost, avp.Mbit, 0, clientSettings.OriginHost) m.NewAVP(avp.OriginRealm, avp.Mbit, 0, clientSettings.OriginRealm) m.NewAVP(avp.HostIPAddress, avp.Mbit, 0, localhostAddress) m.NewAVP(avp.VendorID, avp.Mbit, 0, clientSettings.VendorID) m.NewAVP(avp.ProductName, 0, 0, clientSettings.ProductName) m.NewAVP(avp.OriginStateID, avp.Mbit, 0, datatype.Unsigned32(1)) m.NewAVP(avp.VendorSpecificApplicationID, avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(avp.AuthApplicationID, avp.Mbit, 0, datatype.Unsigned32(1000)), }, }) m.NewAVP(avp.FirmwareRevision, avp.Mbit, 0, clientSettings.FirmwareRevision) _, err = m.WriteTo(cli) if err != nil { t.Fatal(err) } select { case resp := <-mc: if !testResultCode(resp, diam.NoCommonApplication) { t.Fatalf("Unexpected result code.\n%s", resp) } case err := <-mux.ErrorReports(): t.Fatal(err) case <-time.After(time.Second): t.Fatal("No message received") } }
func TestClient_Handshake_FailParseCEA(t *testing.T) { mux := diam.NewServeMux() mux.HandleFunc("CER", func(c diam.Conn, m *diam.Message) { a := m.Answer(diam.Success) // Missing Origin-Host and other mandatory AVPs. 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 != smparser.ErrMissingOriginHost { t.Fatal(err) } }
func main() { err := dict.Default.Load(bytes.NewReader(customApp)) if err != nil { log.Fatal(err) } m := diam.NewMessage(1111, diam.RequestFlag, 999, 1, 2, dict.Default) m.NewAVP(avp.ProductName, avp.Mbit, 0, datatype.Unsigned32(1)) m.NewAVP(7070, avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(avp.VendorID, avp.Mbit, 0, datatype.Unsigned32(10)), diam.NewAVP(8080, avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(avp.VendorID, avp.Mbit, 0, datatype.Unsigned32(20)), diam.NewAVP(9090, avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(avp.VendorID, avp.Mbit, 0, datatype.Unsigned32(30)), diam.NewAVP(avp.VendorID, avp.Mbit, 0, datatype.Unsigned32(40)), }, }), }, }), }, }) log.Printf("m:\n%s\n", m) var b bytes.Buffer _, err = m.WriteTo(&b) if err != nil { log.Fatal(err) } z, err := diam.ReadMessage(&b, dict.Default) if err != nil { log.Fatal(err) } log.Printf("z:\n%s\n", z) var y customMsg err = z.Unmarshal(&y) if err != nil { log.Fatal(err) } log.Printf("y:\n%+v\n", y) }
func TestCER_VSAuthAppID(t *testing.T) { m := diam.NewRequest(diam.CapabilitiesExchange, 0, dict.Default) m.NewAVP(avp.OriginHost, avp.Mbit, 0, datatype.DiameterIdentity("foobar")) m.NewAVP(avp.OriginRealm, avp.Mbit, 0, datatype.DiameterIdentity("test")) m.NewAVP(avp.OriginStateID, avp.Mbit, 0, datatype.Unsigned32(1)) m.NewAVP(avp.VendorSpecificApplicationID, avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(avp.AuthApplicationID, avp.Mbit, 0, datatype.Unsigned32(1002)), }, }) cer := new(CER) _, err := cer.Parse(m) if err != nil { t.Fatal(err) } if app := cer.Applications(); len(app) != 1 { if app[0] != 1002 { t.Fatalf("Unexpected app ID. Want 1002, have %d", app[0]) } } }
func TestCER_FailedVSAuthAppID(t *testing.T) { m := diam.NewRequest(diam.CapabilitiesExchange, 0, dict.Default) m.NewAVP(avp.OriginHost, avp.Mbit, 0, datatype.DiameterIdentity("foobar")) m.NewAVP(avp.OriginRealm, avp.Mbit, 0, datatype.DiameterIdentity("test")) m.NewAVP(avp.OriginStateID, avp.Mbit, 0, datatype.Unsigned32(1)) m.NewAVP(avp.VendorSpecificApplicationID, avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(avp.AuthApplicationID, avp.Mbit, 0, datatype.Unsigned32(1000)), }, }) cer := new(CER) _, err := cer.Parse(m) if err == nil { t.Fatal("Broken CER was parsed with no errors") } appErr, ok := err.(*ErrNoCommonApplication) if !ok { t.Fatal(err) } if appErr.ID != 1000 { t.Fatalf("Unexpected app ID. Want 1000, have %d", appErr.ID) } }
func NewDiameterClient(addr, originHost, originRealm string, vendorId int, productName string, firmwareRev int, dictsDir string) (*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), } dSM := sm.New(cfg) go func() { for err := range dSM.ErrorReports() { utils.Logger.Err(fmt.Sprintf("<DiameterClient> StateMachine error: %+v", err)) } }() cli := &sm.Client{ Handler: dSM, MaxRetransmits: 3, RetransmitInterval: time.Second, EnableWatchdog: true, WatchdogInterval: 5 * time.Second, AcctApplicationID: []*diam.AVP{ diam.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(4)), // RFC 4006 }, } if len(dictsDir) != 0 { if err := loadDictionaries(dictsDir, "DiameterClient"); err != nil { return nil, err } } conn, err := cli.Dial(addr) if err != nil { return nil, err } dc := &DiameterClient{conn: conn, handlers: dSM} dSM.HandleFunc("ALL", dc.handleALL) return dc, nil }
func TestCCRAsSMGenericEvent(t *testing.T) { ccr := &CCR{ // Bare information, just the one needed for answer SessionId: "ccrasgen1", AuthApplicationId: 4, CCRequestType: 3, } ccr.diamMessage = ccr.AsBareDiameterMessage() ccr.diamMessage.NewAVP("Multiple-Services-Credit-Control", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(446, avp.Mbit, 0, &diam.GroupedAVP{ // Used-Service-Unit AVP: []*diam.AVP{ diam.NewAVP(420, avp.Mbit, 0, datatype.Unsigned32(17)), // CC-Time diam.NewAVP(412, avp.Mbit, 0, datatype.Unsigned64(1341)), // CC-Input-Octets diam.NewAVP(414, avp.Mbit, 0, datatype.Unsigned64(3079)), // CC-Output-Octets }, }), diam.NewAVP(432, avp.Mbit, 0, datatype.Unsigned32(99)), }, }) ccr.diamMessage.NewAVP("Multiple-Services-Credit-Control", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(446, avp.Mbit, 0, &diam.GroupedAVP{ // Used-Service-Unit AVP: []*diam.AVP{ diam.NewAVP(452, avp.Mbit, 0, datatype.Enumerated(0)), // Tariff-Change-Usage diam.NewAVP(420, avp.Mbit, 0, datatype.Unsigned32(20)), // CC-Time diam.NewAVP(412, avp.Mbit, 0, datatype.Unsigned64(8046)), // CC-Input-Octets diam.NewAVP(414, avp.Mbit, 0, datatype.Unsigned64(46193)), // CC-Output-Octets }, }), diam.NewAVP(432, avp.Mbit, 0, datatype.Unsigned32(1)), }, }) ccr.diamMessage.NewAVP("FramedIPAddress", avp.Mbit, 0, datatype.OctetString("0AE40041")) cfgFlds := make([]*config.CfgCdrField, 0) eSMGEv := sessionmanager.SMGenericEvent{"EventName": "DIAMETER_CCR"} if rSMGEv, err := ccr.AsSMGenericEvent(cfgFlds); err != nil { t.Error(err) } else if !reflect.DeepEqual(eSMGEv, rSMGEv) { t.Errorf("Expecting: %+v, received: %+v", eSMGEv, rSMGEv) } cfgFlds = []*config.CfgCdrField{ &config.CfgCdrField{ Tag: "LastUsed", FieldFilter: utils.ParseRSRFieldsMustCompile("~Multiple-Services-Credit-Control>Used-Service-Unit>CC-Input-Octets:s/^(.*)$/test/(test);Multiple-Services-Credit-Control>Rating-Group(1)", utils.INFIELD_SEP), FieldId: "LastUsed", Type: "*handler", HandlerId: "*sum", Value: utils.ParseRSRFieldsMustCompile("Multiple-Services-Credit-Control>Used-Service-Unit>CC-Input-Octets;^|;Multiple-Services-Credit-Control>Used-Service-Unit>CC-Output-Octets", utils.INFIELD_SEP), Mandatory: true, }, } eSMGEv = sessionmanager.SMGenericEvent{"EventName": "DIAMETER_CCR", "LastUsed": "54239"} if rSMGEv, err := ccr.AsSMGenericEvent(cfgFlds); err != nil { t.Error(err) } else if !reflect.DeepEqual(eSMGEv, rSMGEv) { t.Errorf("Expecting: %+v, received: %+v", eSMGEv, rSMGEv) } cfgFlds = []*config.CfgCdrField{ &config.CfgCdrField{ Tag: "LastUsed", FieldFilter: utils.ParseRSRFieldsMustCompile("~Multiple-Services-Credit-Control>Used-Service-Unit>CC-Input-Octets:s/^(.*)$/test/(test);Multiple-Services-Credit-Control>Rating-Group(99)", utils.INFIELD_SEP), FieldId: "LastUsed", Type: "*handler", HandlerId: "*sum", Value: utils.ParseRSRFieldsMustCompile("Multiple-Services-Credit-Control>Used-Service-Unit>CC-Input-Octets;^|;Multiple-Services-Credit-Control>Used-Service-Unit>CC-Output-Octets", utils.INFIELD_SEP), Mandatory: true, }, } eSMGEv = sessionmanager.SMGenericEvent{"EventName": "DIAMETER_CCR", "LastUsed": "4420"} if rSMGEv, err := ccr.AsSMGenericEvent(cfgFlds); err != nil { t.Error(err) } else if !reflect.DeepEqual(eSMGEv, rSMGEv) { t.Errorf("Expecting: %+v, received: %+v", eSMGEv, rSMGEv) } }
func TestDmtAgentDryRun1(t *testing.T) { ccr := diam.NewRequest(diam.CreditControl, 4, nil) ccr.NewAVP(avp.SessionID, avp.Mbit, 0, datatype.UTF8String("cgrates;1451911932;00082")) ccr.NewAVP(avp.OriginHost, avp.Mbit, 0, datatype.DiameterIdentity("CGR-DA")) ccr.NewAVP(avp.OriginRealm, avp.Mbit, 0, datatype.DiameterIdentity("cgrates.org")) ccr.NewAVP(avp.DestinationRealm, avp.Mbit, 0, datatype.DiameterIdentity("cgrates.org")) ccr.NewAVP(avp.DestinationHost, avp.Mbit, 0, datatype.DiameterIdentity("CGR-DA")) ccr.NewAVP(avp.UserName, avp.Mbit, 0, datatype.UTF8String("CGR-DA")) ccr.NewAVP(avp.AuthApplicationID, avp.Mbit, 0, datatype.Unsigned32(4)) ccr.NewAVP(avp.ServiceContextID, avp.Mbit, 0, datatype.UTF8String("pubsub1")) // Match specific DryRun profile ccr.NewAVP(avp.CCRequestType, avp.Mbit, 0, datatype.Enumerated(2)) ccr.NewAVP(avp.CCRequestNumber, avp.Mbit, 0, datatype.Unsigned32(1)) ccr.NewAVP(avp.EventTimestamp, avp.Mbit, 0, datatype.Time(time.Date(2016, 1, 5, 11, 30, 10, 0, time.UTC))) ccr.NewAVP(avp.TerminationCause, avp.Mbit, 0, datatype.Enumerated(1)) ccr.NewAVP(443, avp.Mbit, 0, &diam.GroupedAVP{ // Subscription-Id AVP: []*diam.AVP{ diam.NewAVP(450, avp.Mbit, 0, datatype.Enumerated(0)), // Subscription-Id-Type diam.NewAVP(444, avp.Mbit, 0, datatype.UTF8String("1001")), // Subscription-Id-Data }}) ccr.NewAVP(443, avp.Mbit, 0, &diam.GroupedAVP{ // Subscription-Id AVP: []*diam.AVP{ diam.NewAVP(450, avp.Mbit, 0, datatype.Enumerated(1)), // Subscription-Id-Type diam.NewAVP(444, avp.Mbit, 0, datatype.UTF8String("208123456789")), // Subscription-Id-Data }}) ccr.NewAVP(439, avp.Mbit, 0, datatype.Unsigned32(0)) // Service-Identifier ccr.NewAVP(437, avp.Mbit, 0, &diam.GroupedAVP{ // Requested-Service-Unit AVP: []*diam.AVP{ diam.NewAVP(420, avp.Mbit, 0, datatype.Unsigned32(300)), // CC-Time }}) ccr.NewAVP(873, avp.Mbit|avp.Vbit, 10415, &diam.GroupedAVP{ // Service-information AVP: []*diam.AVP{ diam.NewAVP(20300, avp.Mbit, 2011, &diam.GroupedAVP{ // IN-Information AVP: []*diam.AVP{ diam.NewAVP(20336, avp.Mbit, 2011, datatype.UTF8String("1001")), // CallingPartyAdress diam.NewAVP(20337, avp.Mbit, 2011, datatype.UTF8String("1002")), // CalledPartyAdress diam.NewAVP(20339, avp.Mbit, 2011, datatype.Unsigned32(0)), // ChargeFlowType diam.NewAVP(20302, avp.Mbit, 2011, datatype.UTF8String("33609004940")), // CallingVlrNumber diam.NewAVP(20303, avp.Mbit, 2011, datatype.UTF8String("208104941749984")), // CallingCellID diam.NewAVP(20313, avp.Mbit, 2011, datatype.OctetString("0x8090a3")), // BearerCapability diam.NewAVP(20321, avp.Mbit, 2011, datatype.OctetString("0x401c4132ed665")), // CallreferenceNumber diam.NewAVP(20322, avp.Mbit, 2011, datatype.UTF8String("33609004940")), // MSCAddress diam.NewAVP(20386, avp.Mbit, 2011, datatype.UTF8String("20160501010101")), // SSPTime diam.NewAVP(20938, avp.Mbit, 2011, datatype.OctetString("0x00000001")), // HighLayerCharacteristics diam.NewAVP(20324, avp.Mbit, 2011, datatype.Integer32(8)), // Time-Zone }, }), }}) if _, err := ccr.NewAVP("Framed-IP-Address", avp.Mbit, 0, datatype.UTF8String("10.228.16.4")); err != nil { t.Error(err) } for i := 0; i < *interations; i++ { if err := dmtClient.SendMessage(ccr); err != nil { t.Error(err) } msg := dmtClient.ReceivedMessage(rplyTimeout) if msg == nil { t.Fatal("No message returned") } /* if avps, err := msg.FindAVPsWithPath([]interface{}{"Result-Code"}, dict.UndefinedVendorID); err != nil { t.Error(err) } else if len(avps) == 0 { t.Error("Result-Code") } else if strResult := avpValAsString(avps[0]); strResult != "300" { // Result-Code set in the template t.Errorf("Expecting 300, received: %s", strResult) } */ } }
func TestMessageSetAVPsWithPath(t *testing.T) { eMessage := diam.NewRequest(diam.CreditControl, 4, nil) eMessage.NewAVP("Session-Id", avp.Mbit, 0, datatype.UTF8String("simuhuawei;1449573472;00002")) m := diam.NewMessage(diam.CreditControl, diam.RequestFlag, 4, eMessage.Header.HopByHopID, eMessage.Header.EndToEndID, nil) if err := messageSetAVPsWithPath(m, []interface{}{"Session-Id", "Unknown"}, "simuhuawei;1449573472;00002", false, "UTC"); err == nil || err.Error() != "Could not find AVP Unknown" { t.Error(err) } if err := messageSetAVPsWithPath(m, []interface{}{"Session-Id"}, "simuhuawei;1449573472;00002", false, "UTC"); err != nil { t.Error(err) } else if !reflect.DeepEqual(eMessage, m) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } // test append eMessage.NewAVP("Session-Id", avp.Mbit, 0, datatype.UTF8String("simuhuawei;1449573472;00003")) if err := messageSetAVPsWithPath(m, []interface{}{"Session-Id"}, "simuhuawei;1449573472;00003", true, "UTC"); err != nil { t.Error(err) } else if !reflect.DeepEqual(eMessage, m) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } // test overwrite eMessage = diam.NewRequest(diam.CreditControl, 4, nil) eMessage.NewAVP("Session-Id", avp.Mbit, 0, datatype.UTF8String("simuhuawei;1449573472;00002")) m = diam.NewMessage(diam.CreditControl, diam.RequestFlag, 4, eMessage.Header.HopByHopID, eMessage.Header.EndToEndID, nil) if err := messageSetAVPsWithPath(m, []interface{}{"Session-Id"}, "simuhuawei;1449573472;00001", false, "UTC"); err != nil { t.Error(err) } if err := messageSetAVPsWithPath(m, []interface{}{"Session-Id"}, "simuhuawei;1449573472;00002", false, "UTC"); err != nil { t.Error(err) } else if !reflect.DeepEqual(eMessage, m) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } eMessage = diam.NewRequest(diam.CreditControl, 4, nil) eMessage.NewAVP("Subscription-Id", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(444, avp.Mbit, 0, datatype.UTF8String("33708000003")), // Subscription-Id-Data }}) m = diam.NewMessage(diam.CreditControl, diam.RequestFlag, 4, eMessage.Header.HopByHopID, eMessage.Header.EndToEndID, nil) if err := messageSetAVPsWithPath(m, []interface{}{"Subscription-Id", "Subscription-Id-Data"}, "33708000003", false, "UTC"); err != nil { t.Error(err) } else if !reflect.DeepEqual(eMessage, m) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } // test append eMessage.NewAVP("Subscription-Id", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(450, avp.Mbit, 0, datatype.Enumerated(0)), // Subscription-Id-Data }}) if err := messageSetAVPsWithPath(m, []interface{}{"Subscription-Id", "Subscription-Id-Type"}, "0", true, "UTC"); err != nil { t.Error(err) } else if !reflect.DeepEqual(eMessage, m) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } // test group append eMessage = diam.NewRequest(diam.CreditControl, 4, nil) eMessage.NewAVP("Subscription-Id", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(450, avp.Mbit, 0, datatype.Enumerated(0)), // Subscription-Id-Data diam.NewAVP(444, avp.Mbit, 0, datatype.UTF8String("33708000003")), // Subscription-Id-Data }}) eMsgSrl, _ := eMessage.Serialize() m = diam.NewMessage(diam.CreditControl, diam.RequestFlag, 4, eMessage.Header.HopByHopID, eMessage.Header.EndToEndID, nil) if err := messageSetAVPsWithPath(m, []interface{}{"Subscription-Id", "Subscription-Id-Type"}, "0", false, "UTC"); err != nil { t.Error(err) } if err := messageSetAVPsWithPath(m, []interface{}{"Subscription-Id", "Subscription-Id-Data"}, "33708000003", false, "UTC"); err != nil { t.Error(err) } else { mSrl, _ := m.Serialize() if !bytes.Equal(eMsgSrl, mSrl) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } } eMessage = diam.NewRequest(diam.CreditControl, 4, nil) eMessage.NewAVP("Granted-Service-Unit", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(420, avp.Mbit, 0, datatype.Unsigned32(300)), // Subscription-Id-Data }}) m = diam.NewMessage(diam.CreditControl, diam.RequestFlag, 4, eMessage.Header.HopByHopID, eMessage.Header.EndToEndID, nil) if err := messageSetAVPsWithPath(m, []interface{}{"Granted-Service-Unit", "CC-Time"}, "300", false, "UTC"); err != nil { t.Error(err) } else if !reflect.DeepEqual(eMessage, m) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } // Multiple append eMessage = diam.NewRequest(diam.CreditControl, 4, nil) eMessage.NewAVP("Multiple-Services-Credit-Control", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(431, avp.Mbit, 0, &diam.GroupedAVP{ // Granted-Service-Unit AVP: []*diam.AVP{ diam.NewAVP(420, avp.Mbit, 0, datatype.Unsigned32(3600)), diam.NewAVP(421, avp.Mbit, 0, datatype.Unsigned64(153600)), // "CC-Total-Octets" }, }), diam.NewAVP(432, avp.Mbit, 0, datatype.Unsigned32(10)), }, }) eMessage.NewAVP("Multiple-Services-Credit-Control", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(431, avp.Mbit, 0, &diam.GroupedAVP{ // Granted-Service-Unit AVP: []*diam.AVP{ diam.NewAVP(420, avp.Mbit, 0, datatype.Unsigned32(2600)), diam.NewAVP(421, avp.Mbit, 0, datatype.Unsigned64(143600)), // "CC-Total-Octets" }, }), diam.NewAVP(432, avp.Mbit, 0, datatype.Unsigned32(11)), // Rating-Group }, }) m = diam.NewMessage(diam.CreditControl, diam.RequestFlag, 4, eMessage.Header.HopByHopID, eMessage.Header.EndToEndID, nil) if err := messageSetAVPsWithPath(m, []interface{}{"Multiple-Services-Credit-Control", "Granted-Service-Unit", "CC-Time"}, "3600", false, "UTC"); err != nil { t.Error(err) } if err := messageSetAVPsWithPath(m, []interface{}{"Multiple-Services-Credit-Control", "Granted-Service-Unit", "CC-Total-Octets"}, "153600", false, "UTC"); err != nil { t.Error(err) } if err := messageSetAVPsWithPath(m, []interface{}{"Multiple-Services-Credit-Control", "Rating-Group"}, "10", false, "UTC"); err != nil { t.Error(err) } if err := messageSetAVPsWithPath(m, []interface{}{"Multiple-Services-Credit-Control", "Granted-Service-Unit", "CC-Time"}, "2600", true, "UTC"); err != nil { t.Error(err) } if err := messageSetAVPsWithPath(m, []interface{}{"Multiple-Services-Credit-Control", "Granted-Service-Unit", "CC-Total-Octets"}, "143600", false, "UTC"); err != nil { t.Error(err) } if err := messageSetAVPsWithPath(m, []interface{}{"Multiple-Services-Credit-Control", "Rating-Group"}, "11", false, "UTC"); err != nil { t.Error(err) } if fmt.Sprintf("%q", eMessage) != fmt.Sprintf("%q", m) { // test with fmt since reflect.DeepEqual does not perform properly here t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } }
// Used when sending from client to agent func (self *CCR) AsDiameterMessage() (*diam.Message, error) { m := self.AsBareDiameterMessage() if _, err := m.NewAVP("Destination-Host", avp.Mbit, 0, datatype.DiameterIdentity(self.DestinationHost)); err != nil { return nil, err } if _, err := m.NewAVP("Destination-Realm", avp.Mbit, 0, datatype.DiameterIdentity(self.DestinationRealm)); err != nil { return nil, err } if _, err := m.NewAVP("Service-Context-Id", avp.Mbit, 0, datatype.UTF8String(self.ServiceContextId)); err != nil { return nil, err } if _, err := m.NewAVP("Event-Timestamp", avp.Mbit, 0, datatype.Time(self.EventTimestamp)); err != nil { return nil, err } for _, subscriptionId := range self.SubscriptionId { if _, err := m.NewAVP("Subscription-Id", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(450, avp.Mbit, 0, datatype.Enumerated(subscriptionId.SubscriptionIdType)), // Subscription-Id-Type diam.NewAVP(444, avp.Mbit, 0, datatype.UTF8String(subscriptionId.SubscriptionIdData)), // Subscription-Id-Data }}); err != nil { return nil, err } } if _, err := m.NewAVP("Service-Identifier", avp.Mbit, 0, datatype.Unsigned32(self.ServiceIdentifier)); err != nil { return nil, err } if _, err := m.NewAVP("Requested-Service-Unit", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(420, avp.Mbit, 0, datatype.Unsigned32(self.RequestedServiceUnit.CCTime))}}); err != nil { // CC-Time return nil, err } if _, err := m.NewAVP("Used-Service-Unit", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(420, avp.Mbit, 0, datatype.Unsigned32(self.UsedServiceUnit.CCTime))}}); err != nil { // CC-Time return nil, err } if _, err := m.NewAVP(873, avp.Mbit, 10415, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(20300, avp.Mbit, 2011, &diam.GroupedAVP{ // IN-Information AVP: []*diam.AVP{ diam.NewAVP(831, avp.Mbit, 10415, datatype.UTF8String(self.ServiceInformation.INInformation.CallingPartyAddress)), // Calling-Party-Address diam.NewAVP(832, avp.Mbit, 10415, datatype.UTF8String(self.ServiceInformation.INInformation.CalledPartyAddress)), // Called-Party-Address diam.NewAVP(20327, avp.Mbit, 2011, datatype.UTF8String(self.ServiceInformation.INInformation.RealCalledNumber)), // Real-Called-Number diam.NewAVP(20339, avp.Mbit, 2011, datatype.Unsigned32(self.ServiceInformation.INInformation.ChargeFlowType)), // Charge-Flow-Type diam.NewAVP(20302, avp.Mbit, 2011, datatype.UTF8String(self.ServiceInformation.INInformation.CallingVlrNumber)), // Calling-Vlr-Number diam.NewAVP(20303, avp.Mbit, 2011, datatype.UTF8String(self.ServiceInformation.INInformation.CallingCellIDOrSAI)), // Calling-CellID-Or-SAI diam.NewAVP(20313, avp.Mbit, 2011, datatype.UTF8String(self.ServiceInformation.INInformation.BearerCapability)), // Bearer-Capability diam.NewAVP(20321, avp.Mbit, 2011, datatype.UTF8String(self.ServiceInformation.INInformation.CallReferenceNumber)), // Call-Reference-Number diam.NewAVP(20322, avp.Mbit, 2011, datatype.UTF8String(self.ServiceInformation.INInformation.MSCAddress)), // MSC-Address diam.NewAVP(20324, avp.Mbit, 2011, datatype.Unsigned32(self.ServiceInformation.INInformation.TimeZone)), // Time-Zone diam.NewAVP(20385, avp.Mbit, 2011, datatype.UTF8String(self.ServiceInformation.INInformation.CalledPartyNP)), // Called-Party-NP diam.NewAVP(20386, avp.Mbit, 2011, datatype.UTF8String(self.ServiceInformation.INInformation.SSPTime)), // SSP-Time }, }), }}); err != nil { return nil, err } return m, nil }
func main() { addr := flag.String("addr", "localhost:3868", "address in form of ip:port to connect to") ssl := flag.Bool("ssl", false, "connect to server using tls") host := flag.String("diam_host", "client", "diameter identity host") realm := flag.String("diam_realm", "go-diameter", "diameter identity realm") certFile := flag.String("cert_file", "", "tls client certificate file (optional)") keyFile := flag.String("key_file", "", "tls client key file (optional)") hello := flag.Bool("hello", false, "send a hello message, wait for the response and disconnect") bench := flag.Bool("bench", false, "benchmark the server by sending ACR messages") benchCli := flag.Int("bench_clients", 1, "number of client connections") benchMsgs := flag.Int("bench_msgs", 1000, "number of ACR messages to send") flag.Parse() if len(*addr) == 0 { flag.Usage() } // Load our custom dictionary on top of the default one. err := dict.Default.Load(bytes.NewReader([]byte(helloDictionary))) if err != nil { log.Fatal(err) } cfg := &sm.Settings{ OriginHost: datatype.DiameterIdentity(*host), OriginRealm: datatype.DiameterIdentity(*realm), VendorID: 13, ProductName: "go-diameter", OriginStateID: datatype.Unsigned32(time.Now().Unix()), FirmwareRevision: 1, } // Create the state machine (it's a diam.ServeMux) and client. mux := sm.New(cfg) cli := &sm.Client{ Dict: dict.Default, Handler: mux, 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 diam.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(helloApplication)), }, } // Set message handlers. done := make(chan struct{}, 1000) mux.Handle("HMA", handleHMA(done)) mux.Handle("ACA", handleACA(done)) // Print error reports. go printErrors(mux.ErrorReports()) connect := func() (diam.Conn, error) { return dial(cli, *addr, *certFile, *keyFile, *ssl) } if *bench { cli.EnableWatchdog = false benchmark(connect, cfg, *benchCli, *benchMsgs, done) return } if *hello { c, err := connect() if err != nil { log.Fatal(err) } err = sendHMR(c, cfg) if err != nil { log.Fatal(err) } select { case <-done: case <-time.After(5 * time.Second): log.Fatal("timeout: no hello answer received") } return } // Makes a persisent connection with back-off. log.Println("Use wireshark to see the messages, or try -hello") backoff := 1 for { c, err := connect() if err != nil { log.Println(err) backoff *= 2 if backoff > 20 { backoff = 20 } time.Sleep(time.Duration(backoff) * time.Second) continue } log.Println("Client connected, handshake ok") backoff = 1 <-c.(diam.CloseNotifier).CloseNotify() log.Println("Client disconnected") } }
// Used when sending from client to agent func (self *CCR) AsDiameterMessage() (*diam.Message, error) { m := diam.NewRequest(diam.CreditControl, 4, nil) if _, err := m.NewAVP("Session-Id", avp.Mbit, 0, datatype.UTF8String(self.SessionId)); err != nil { return nil, err } if _, err := m.NewAVP("Origin-Host", avp.Mbit, 0, datatype.DiameterIdentity(self.OriginHost)); err != nil { return nil, err } if _, err := m.NewAVP("Origin-Realm", avp.Mbit, 0, datatype.DiameterIdentity(self.OriginRealm)); err != nil { return nil, err } if _, err := m.NewAVP("Destination-Host", avp.Mbit, 0, datatype.DiameterIdentity(self.DestinationHost)); err != nil { return nil, err } if _, err := m.NewAVP("Destination-Realm", avp.Mbit, 0, datatype.DiameterIdentity(self.DestinationRealm)); err != nil { return nil, err } if _, err := m.NewAVP("Auth-Application-Id", avp.Mbit, 0, datatype.Unsigned32(self.AuthApplicationId)); err != nil { return nil, err } if _, err := m.NewAVP("Service-Context-Id", avp.Mbit, 0, datatype.UTF8String(self.ServiceContextId)); err != nil { return nil, err } if _, err := m.NewAVP("CC-Request-Type", avp.Mbit, 0, datatype.Enumerated(self.CCRequestType)); err != nil { return nil, err } if _, err := m.NewAVP("CC-Request-Number", avp.Mbit, 0, datatype.Enumerated(self.CCRequestNumber)); err != nil { return nil, err } if _, err := m.NewAVP("Event-Timestamp", avp.Mbit, 0, datatype.Time(self.EventTimestamp)); err != nil { return nil, err } subscriptionIdType, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "Subscription-Id-Type") if err != nil { return nil, err } subscriptionIdData, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "Subscription-Id-Data") if err != nil { return nil, err } for _, subscriptionId := range self.SubscriptionId { if _, err := m.NewAVP("Subscription-Id", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(subscriptionIdType.Code, avp.Mbit, 0, datatype.Enumerated(subscriptionId.SubscriptionIdType)), diam.NewAVP(subscriptionIdData.Code, avp.Mbit, 0, datatype.UTF8String(subscriptionId.SubscriptionIdData)), }}); err != nil { return nil, err } } if _, err := m.NewAVP("Service-Identifier", avp.Mbit, 0, datatype.Unsigned32(self.ServiceIdentifier)); err != nil { return nil, err } ccTimeAvp, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "CC-Time") if err != nil { return nil, err } if _, err := m.NewAVP("Requested-Service-Unit", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(ccTimeAvp.Code, avp.Mbit, 0, datatype.Unsigned32(self.RequestedServiceUnit.CCTime))}}); err != nil { return nil, err } inInformation, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "IN-Information") if err != nil { return nil, err } callingPartyAddress, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "Calling-Party-Address") if err != nil { return nil, err } calledPartyAddress, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "Called-Party-Address") if err != nil { return nil, err } realCalledNumber, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "Real-Called-Number") if err != nil { return nil, err } chargeFlowType, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "Charge-Flow-Type") if err != nil { return nil, err } callingVlrNumber, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "Calling-Vlr-Number") if err != nil { return nil, err } callingCellIdOrSai, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "Calling-CellID-Or-SAI") if err != nil { return nil, err } bearerCapability, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "Bearer-Capability") if err != nil { return nil, err } callReferenceNumber, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "Call-Reference-Number") if err != nil { return nil, err } mscAddress, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "MSC-Address") if err != nil { return nil, err } timeZone, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "Time-Zone") if err != nil { return nil, err } calledPartyNP, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "Called-Party-NP") if err != nil { return nil, err } sspTime, err := m.Dictionary().FindAVP(m.Header.ApplicationID, "SSP-Time") if err != nil { return nil, err } if _, err := m.NewAVP("Service-Information", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(inInformation.Code, avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(callingPartyAddress.Code, avp.Mbit, 0, datatype.UTF8String(self.ServiceInformation.INInformation.CallingPartyAddress)), diam.NewAVP(calledPartyAddress.Code, avp.Mbit, 0, datatype.UTF8String(self.ServiceInformation.INInformation.CalledPartyAddress)), diam.NewAVP(realCalledNumber.Code, avp.Mbit, 0, datatype.UTF8String(self.ServiceInformation.INInformation.RealCalledNumber)), diam.NewAVP(chargeFlowType.Code, avp.Mbit, 0, datatype.Unsigned32(self.ServiceInformation.INInformation.ChargeFlowType)), diam.NewAVP(callingVlrNumber.Code, avp.Mbit, 0, datatype.UTF8String(self.ServiceInformation.INInformation.CallingVlrNumber)), diam.NewAVP(callingCellIdOrSai.Code, avp.Mbit, 0, datatype.UTF8String(self.ServiceInformation.INInformation.CallingCellIDOrSAI)), diam.NewAVP(bearerCapability.Code, avp.Mbit, 0, datatype.UTF8String(self.ServiceInformation.INInformation.BearerCapability)), diam.NewAVP(callReferenceNumber.Code, avp.Mbit, 0, datatype.UTF8String(self.ServiceInformation.INInformation.CallReferenceNumber)), diam.NewAVP(mscAddress.Code, avp.Mbit, 0, datatype.UTF8String(self.ServiceInformation.INInformation.MSCAddress)), diam.NewAVP(timeZone.Code, avp.Mbit, 0, datatype.UTF8String(self.ServiceInformation.INInformation.TimeZone)), diam.NewAVP(calledPartyNP.Code, avp.Mbit, 0, datatype.UTF8String(self.ServiceInformation.INInformation.CalledPartyNP)), diam.NewAVP(sspTime.Code, avp.Mbit, 0, datatype.UTF8String(self.ServiceInformation.INInformation.SSPTime)), }, }), }}); err != nil { return nil, err } return m, nil }
func TestMessageSetAVPsWithPath(t *testing.T) { eMessage := diam.NewRequest(diam.CreditControl, 4, nil) eMessage.NewAVP("Session-Id", avp.Mbit, 0, datatype.UTF8String("simuhuawei;1449573472;00002")) m := diam.NewMessage(diam.CreditControl, diam.RequestFlag, 4, eMessage.Header.HopByHopID, eMessage.Header.EndToEndID, nil) if err := messageSetAVPsWithPath(m, []interface{}{"Session-Id", "Unknown"}, "simuhuawei;1449573472;00002", false, "UTC"); err == nil || err.Error() != "Could not find AVP Unknown" { t.Error(err) } if err := messageSetAVPsWithPath(m, []interface{}{"Session-Id"}, "simuhuawei;1449573472;00002", false, "UTC"); err != nil { t.Error(err) } else if !reflect.DeepEqual(eMessage, m) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } // test append eMessage.NewAVP("Session-Id", avp.Mbit, 0, datatype.UTF8String("simuhuawei;1449573472;00003")) if err := messageSetAVPsWithPath(m, []interface{}{"Session-Id"}, "simuhuawei;1449573472;00003", true, "UTC"); err != nil { t.Error(err) } else if !reflect.DeepEqual(eMessage, m) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } // test overwrite eMessage = diam.NewRequest(diam.CreditControl, 4, nil) eMessage.NewAVP("Session-Id", avp.Mbit, 0, datatype.UTF8String("simuhuawei;1449573472;00002")) m = diam.NewMessage(diam.CreditControl, diam.RequestFlag, 4, eMessage.Header.HopByHopID, eMessage.Header.EndToEndID, nil) if err := messageSetAVPsWithPath(m, []interface{}{"Session-Id"}, "simuhuawei;1449573472;00001", false, "UTC"); err != nil { t.Error(err) } if err := messageSetAVPsWithPath(m, []interface{}{"Session-Id"}, "simuhuawei;1449573472;00002", false, "UTC"); err != nil { t.Error(err) } else if !reflect.DeepEqual(eMessage, m) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } eMessage = diam.NewRequest(diam.CreditControl, 4, nil) eMessage.NewAVP("Subscription-Id", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(444, avp.Mbit, 0, datatype.UTF8String("33708000003")), // Subscription-Id-Data }}) m = diam.NewMessage(diam.CreditControl, diam.RequestFlag, 4, eMessage.Header.HopByHopID, eMessage.Header.EndToEndID, nil) if err := messageSetAVPsWithPath(m, []interface{}{"Subscription-Id", "Subscription-Id-Data"}, "33708000003", false, "UTC"); err != nil { t.Error(err) } else if !reflect.DeepEqual(eMessage, m) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } // test append eMessage.NewAVP("Subscription-Id", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(450, avp.Mbit, 0, datatype.Enumerated(0)), // Subscription-Id-Data }}) if err := messageSetAVPsWithPath(m, []interface{}{"Subscription-Id", "Subscription-Id-Type"}, "0", true, "UTC"); err != nil { t.Error(err) } else if !reflect.DeepEqual(eMessage, m) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } // test group append eMessage = diam.NewRequest(diam.CreditControl, 4, nil) eMessage.NewAVP("Subscription-Id", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(450, avp.Mbit, 0, datatype.Enumerated(0)), // Subscription-Id-Data diam.NewAVP(444, avp.Mbit, 0, datatype.UTF8String("33708000003")), // Subscription-Id-Data }}) eMsgSrl, _ := eMessage.Serialize() m = diam.NewMessage(diam.CreditControl, diam.RequestFlag, 4, eMessage.Header.HopByHopID, eMessage.Header.EndToEndID, nil) if err := messageSetAVPsWithPath(m, []interface{}{"Subscription-Id", "Subscription-Id-Type"}, "0", false, "UTC"); err != nil { t.Error(err) } if err := messageSetAVPsWithPath(m, []interface{}{"Subscription-Id", "Subscription-Id-Data"}, "33708000003", false, "UTC"); err != nil { t.Error(err) } else { mSrl, _ := m.Serialize() if !bytes.Equal(eMsgSrl, mSrl) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } } eMessage = diam.NewRequest(diam.CreditControl, 4, nil) eMessage.NewAVP("Granted-Service-Unit", avp.Mbit, 0, &diam.GroupedAVP{ AVP: []*diam.AVP{ diam.NewAVP(420, avp.Mbit, 0, datatype.Unsigned32(300)), // Subscription-Id-Data }}) m = diam.NewMessage(diam.CreditControl, diam.RequestFlag, 4, eMessage.Header.HopByHopID, eMessage.Header.EndToEndID, nil) if err := messageSetAVPsWithPath(m, []interface{}{"Granted-Service-Unit", "CC-Time"}, "300", false, "UTC"); err != nil { t.Error(err) } else if !reflect.DeepEqual(eMessage, m) { t.Errorf("Expecting: %+v, received: %+v", eMessage, m) } }