Skip to content

Commit

Permalink
Merge pull request #69 from aholappa/master
Browse files Browse the repository at this point in the history
Add more error details for CEA failure
  • Loading branch information
fiorix committed Jan 20, 2017
2 parents 63253bf + a88b1af commit 5215be3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
10 changes: 6 additions & 4 deletions diam/sm/smparser/cea.go
Expand Up @@ -21,18 +21,20 @@ type CEA struct {
AcctApplicationID []*diam.AVP `avp:"Acct-Application-Id"`
AuthApplicationID []*diam.AVP `avp:"Auth-Application-Id"`
VendorSpecificApplicationID []*diam.AVP `avp:"Vendor-Specific-Application-Id"`
FailedAVP []*diam.AVP `avp:"Failed-AVP"`
ErrorMessage string `avp:"Error-Message"`
appID []uint32 // List of supported application IDs.
}

// ErrFailedResultCode is returned by Dial or DialTLS when the handshake
// answer (CEA) contains a Result-Code AVP that is not success (2001).
type ErrFailedResultCode struct {
Code uint32
*CEA
}

// Error implements the error interface.
func (e *ErrFailedResultCode) Error() string {
return fmt.Sprintf("failed Result-Code AVP: %d", e.Code)
func (e ErrFailedResultCode) Error() string {
return fmt.Sprintf("failed Result-Code AVP: %d", e.CEA.ResultCode)
}

// Parse parses and validates the given message.
Expand All @@ -44,7 +46,7 @@ func (cea *CEA) Parse(m *diam.Message, localRole Role) (err error) {
return err
}
if cea.ResultCode != diam.Success {
return &ErrFailedResultCode{Code: cea.ResultCode}
return &ErrFailedResultCode{CEA: cea}
}
app := &Application{
AcctApplicationID: cea.AcctApplicationID,
Expand Down
21 changes: 19 additions & 2 deletions diam/sm/smparser/cea_test.go
Expand Up @@ -71,6 +71,12 @@ func TestCEA_MissingApplication(t *testing.T) {
func TestCEA_MissingApplicationWithError(t *testing.T) {
m := diam.NewMessage(diam.CapabilitiesExchange, 0, 0, 0, 0, dict.Default)
m.NewAVP(avp.ResultCode, avp.Mbit, 0, datatype.Unsigned32(diam.ResourcesExceeded))
m.NewAVP(avp.FailedAVP, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(1000)),
},
})

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))
Expand All @@ -83,8 +89,19 @@ func TestCEA_MissingApplicationWithError(t *testing.T) {
if !ok {
t.Fatalf("Unexpected error type. Want *ErrFailedResultCode, have %T", err)
}
if e.Code != diam.ResourcesExceeded {
t.Fatalf("Unexpected ResultCode. Want %d, have %d", diam.ResourcesExceeded, e.Code)
if e.ResultCode != diam.ResourcesExceeded {
t.Fatalf("Unexpected ResultCode. Want %d, have %d", diam.ResourcesExceeded, e.ResultCode)
}
g, ok := e.FailedAVP[0].Data.(*diam.GroupedAVP)
if !ok {
t.Fatalf("Unexpected type. Want *diam.GroupedAVP, have %T", e.FailedAVP[0].Data)
}
d, ok := g.AVP[0].Data.(datatype.Unsigned32)
if !ok {
t.Fatalf("Unexpected type. Want *datatype.Unsigned32, have %T", e.FailedAVP[0].Data)
}
if d != 1000 {
t.Fatalf("Wrong value for FailedAVP. Want 1000, have %d", d)
}
}

Expand Down

0 comments on commit 5215be3

Please sign in to comment.