Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions diam/internal/base/error_answer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// BuildErrorAnswer constructs RFC 6733 §§6.2 and 7 answers; Verified Errata
// 4808 and 4887 govern Failed-AVP and local Origin-Realm respectively.
// 4615 and 4887 govern Failed-AVP and local Origin-Realm respectively.
func BuildErrorAnswer(request *diam.Message, cfg Settings, resultCode uint32, failedAVPs []*diam.AVP, protocolError bool) (*diam.Message, error) {
answer := request.Answer(0)
dictionary := answer.Dictionary()
Expand Down Expand Up @@ -54,7 +54,7 @@ func BuildErrorAnswer(request *diam.Message, cfg Settings, resultCode uint32, fa
}
}
if len(failedAVPs) != 0 {
// RFC 6733 Section 7.5 and Verified Errata 4808 require one
// RFC 6733 §7.1.5 and Verified Erratum 4615 require one
// Failed-AVP container; it may retain the Grouped hierarchy.
failed := &diam.GroupedAVP{AVP: failedAVPs}
if _, err := answer.NewAVP(avp.FailedAVP, avp.Mbit, 0, failed); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion diam/internal/protocol/closing.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *Core) disconnect(es []Effect, in Input) []Effect {
es = c.arm(es, ReceiverDisconnectTimer, in.At, in.Timeout)
return c.reserve(es, diam.DisconnectPeer)
}
if c.phase != Open || in.Cause > 2 && !c.cfg.AllowAnyLocalDisconnectCause {
if c.phase != Open || in.Cause > 2 {
return append(es, c.notify(Notification{Kind: DisconnectNotice, Outcome: NotOpen}))
}
return c.beginClosing(es, in, true)
Expand Down
2 changes: 1 addition & 1 deletion diam/internal/protocol/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func (c *Core) reject(es []Effect, in Input, kind diam.ProtocolErrorKind, me *di
func (c *Core) failedRejection(es []Effect, in Input, kind diam.ProtocolErrorKind, code uint32, err error) []Effect {
initialCER := c.phase == PreCER && in.Message != nil && in.Message.Header.CommandCode == diam.CapabilitiesExchange
es = c.terminate(es, Closed)
n := Notification{Protocol: kind, ResultCode: code, Reply: diam.ProtocolReplyFailed, ReplyCode: code, Closing: true, Message: metadata(in.Message), ReplyError: copyHandshakeError(err, c.cfg.PreserveReplyErrorFormatWrapper)}
n := Notification{Protocol: kind, ResultCode: code, Reply: diam.ProtocolReplyFailed, ReplyCode: code, Closing: true, Message: metadata(in.Message), ReplyError: copyHandshakeError(err, true)}
if initialCER {
n.Diagnostic = "reject-cer"
}
Expand Down
208 changes: 84 additions & 124 deletions diam/internal/protocol/diagnostic_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/gomaja/go-diameter/diam/internal/base"
)

func policyProtocolNotice(t testing.TB, es []Effect) Notification {
func protocolNotice(t testing.TB, es []Effect) Notification {
t.Helper()
for _, e := range es {
if e.Kind == Notify && e.Notification.Protocol != "" {
Expand All @@ -24,97 +24,83 @@ func policyProtocolNotice(t testing.TB, es []Effect) Notification {
}

func TestReaderOwnsMessageErrorClosure(t *testing.T) {
for _, reader := range []bool{false, true} {
t.Run(fmt.Sprintf("reader=%t", reader), func(t *testing.T) {
c := opened(t, func(cfg *Config) { cfg.ReaderOwnsMessageErrorClosure = reader })
for _, owner := range []string{"core", "reader", "external-admission"} {
t.Run(owner, func(t *testing.T) {
reader := owner != "core"
c := opened(t, func(cfg *Config) { cfg.ReaderOwnsMessageErrorClosure = owner == "reader" })
if owner == "external-admission" {
c = admitted(t, true)
}
in := receive(request(t, diam.DeviceWatchdog))
in.ErrorInput = true
in.Error = &diam.MessageError{ResultCode: diam.InvalidAVPLength, Fatal: true}
sent := find(t, c.Handle(in), Send)
if !sent.Required || sent.CloseAfter == reader {
t.Fatalf("fatal reply closure=%t, reader ownership=%t", sent.CloseAfter, reader)
}
n := policyProtocolNotice(t, c.Handle(complete(sent)))
n := protocolNotice(t, c.Handle(complete(sent)))
if !n.Closing || (c.Phase() == Closed) == reader {
t.Fatalf("fatality/closure conflated: notice=%+v phase=%v", n, c.Phase())
}
})
}
}

func TestClosingMessageErrorFatalityPolicy(t *testing.T) {
for _, retain := range []bool{false, true} {
t.Run(fmt.Sprintf("retain=%t", retain), func(t *testing.T) {
c := opened(t, func(cfg *Config) {
cfg.ReaderOwnsMessageErrorClosure = true
cfg.RetainClosingErrorFatality = retain
})
func TestClosingMessageErrorRetainsFatality(t *testing.T) {
for _, reader := range []bool{false, true} {
t.Run(fmt.Sprintf("reader=%t", reader), func(t *testing.T) {
c := opened(t, func(cfg *Config) { cfg.ReaderOwnsMessageErrorClosure = reader })
c.Handle(event(Disconnect))
before := c.Snapshot().Timer(ClosingTimer)
in := receive(request(t, diam.DisconnectPeer))
in.ErrorInput = true
in.Error = &diam.MessageError{ResultCode: diam.InvalidAVPLength, Fatal: true}
n := policyProtocolNotice(t, c.Handle(in))
if n.Closing != retain || n.Reply != diam.ProtocolReplyNone || c.Phase() != Closing || c.Snapshot().Timer(ClosingTimer) != before {
t.Fatalf("Closing report altered transport ownership/deadline: %+v", n)
n := protocolNotice(t, c.Handle(in))
if !n.Closing || n.Reply != diam.ProtocolReplyNone || c.Phase() != Closing || c.Snapshot().Timer(ClosingTimer) != before {
t.Fatalf("Closing report altered fatality/transport ownership/deadline: %+v", n)
}
})
}
}

func TestHeaderDiagnosisRetainsSelectedDecoderFatality(t *testing.T) {
for _, retain := range []bool{false, true} {
t.Run(fmt.Sprintf("retain=%t", retain), func(t *testing.T) {
c := opened(t, func(cfg *Config) {
cfg.ReaderOwnsMessageErrorClosure = true
cfg.PreserveDecoderFatalityOnHeaderError = retain
})
func TestHeaderDiagnosisRetainsDecoderFatality(t *testing.T) {
for _, reader := range []bool{false, true} {
t.Run(fmt.Sprintf("reader=%t", reader), func(t *testing.T) {
c := opened(t, func(cfg *Config) { cfg.ReaderOwnsMessageErrorClosure = reader })
in := receive(request(t, diam.DeviceWatchdog))
in.Message.Header.CommandFlags |= diam.ProxiableFlag
in.ErrorInput = true
in.Error = &diam.MessageError{ResultCode: diam.InvalidAVPLength, Fatal: true}
sent := find(t, c.Handle(in), Send)
n := policyProtocolNotice(t, c.Handle(complete(sent)))
n := protocolNotice(t, c.Handle(complete(sent)))
cause, ok := n.CauseError.(*diam.MessageError)
if !ok || cause.ResultCode != diam.InvalidHDRBits || cause.Fatal != retain || !n.Closing || sent.CloseAfter || c.Phase() != Open {
t.Fatalf("replacement header cause or original report changed: %+v cause=%#v", n, cause)
if !ok || cause.ResultCode != diam.InvalidHDRBits || !cause.Fatal || !n.Closing || sent.CloseAfter == reader || (c.Phase() == Closed) == reader {
t.Fatalf("replacement header cause or original report changed: %+v cause=%#v phase=%v", n, cause, c.Phase())
}
})
}
}

func TestHeaderErrorDiagnosticClassificationPolicy(t *testing.T) {
for _, separate := range []bool{false, true} {
for _, closing := range []bool{false, true} {
t.Run(fmt.Sprintf("separate=%t/closing=%t", separate, closing), func(t *testing.T) {
c := opened(t, func(cfg *Config) {
cfg.ReaderOwnsMessageErrorClosure = true
cfg.SeparateHeaderErrorDiagnostics = separate
})
if closing {
c.Handle(event(Disconnect))
}
in := receive(request(t, diam.DisconnectPeer))
in.Message.Header.CommandFlags |= diam.ProxiableFlag
in.ErrorInput = true
in.Error = &diam.MessageError{ResultCode: diam.InvalidAVPLength}
es := c.Handle(in)
if !closing {
es = c.Handle(complete(find(t, es, Send)))
}
n := policyProtocolNotice(t, es)
kind, diagnostic := diam.ProtocolInvalidHeader, "external-message-error"
if separate {
diagnostic = "header-error"
if closing {
kind = diam.ProtocolMalformedMessage
}
}
if n.Protocol != kind || n.Diagnostic != diagnostic || n.ResultCode != diam.InvalidHDRBits {
t.Fatalf("header diagnostic=%+v, want %s/%s", n, kind, diagnostic)
}
})
}
func TestHeaderErrorRetainsInvalidHeaderClassification(t *testing.T) {
for _, closing := range []bool{false, true} {
t.Run(fmt.Sprintf("closing=%t", closing), func(t *testing.T) {
c := opened(t, func(cfg *Config) { cfg.ReaderOwnsMessageErrorClosure = true })
if closing {
c.Handle(event(Disconnect))
}
in := receive(request(t, diam.DisconnectPeer))
in.Message.Header.CommandFlags |= diam.ProxiableFlag
in.ErrorInput = true
in.Error = &diam.MessageError{ResultCode: diam.InvalidAVPLength}
es := c.Handle(in)
if !closing {
es = c.Handle(complete(find(t, es, Send)))
}
n := protocolNotice(t, es)
if n.Protocol != diam.ProtocolInvalidHeader || n.Diagnostic != "external-message-error" || n.ResultCode != diam.InvalidHDRBits {
t.Fatalf("header diagnostic=%+v", n)
}
})
}
}

Expand All @@ -123,7 +109,6 @@ func TestCERMessageErrorClosurePolicy(t *testing.T) {
t.Run(fmt.Sprintf("only-invalid-header-bits=%t", onlyBits), func(t *testing.T) {
c := opened(t, func(cfg *Config) {
cfg.ReaderOwnsMessageErrorClosure = true
cfg.RejectFatalRepeatedCER = true
cfg.CloseCEROnInvalidHeaderBitsOnly = onlyBits
})
in := receive(cer(t))
Expand All @@ -142,10 +127,10 @@ func TestCERMessageErrorClosurePolicy(t *testing.T) {
}
}

func TestCompletedCEAValidationPolicy(t *testing.T) {
for _, validate := range []bool{false, true} {
t.Run(fmt.Sprintf("validate=%t", validate), func(t *testing.T) {
c := machine(t, func(cfg *Config) { cfg.Role, cfg.ValidateCompletedCEA = Initiator, validate })
func TestCompletedCEAUsesCorrelationDiagnostic(t *testing.T) {
for _, duplicate := range []bool{false, true} {
t.Run(fmt.Sprintf("duplicate=%t", duplicate), func(t *testing.T) {
c := machine(t, func(cfg *Config) { cfg.Role = Initiator })
sent := grant(t, c, c.Handle(event(Start)))
c.Handle(complete(sent))
answer, err := base.BuildCEA(sent.Message, remote(t).Local, diam.Success)
Expand All @@ -157,80 +142,55 @@ func TestCompletedCEAValidationPolicy(t *testing.T) {
release.Token = ready.Token
c.Handle(release)
answer.AVP = nil
n := policyProtocolNotice(t, c.Handle(receive(answer)))
kind, code := diam.ProtocolUnexpectedMessage, uint32(0)
if validate {
kind, code = diam.ProtocolCEARejected, diam.MissingAVP
diagnostic := "duplicate-cea"
if !duplicate {
answer.Header.HopByHopID++
diagnostic = "cea-correlation"
}
if n.Protocol != kind || n.ResultCode != code || n.Closing || c.Phase() != Open {
n := protocolNotice(t, c.Handle(receive(answer)))
if n.Protocol != diam.ProtocolUnexpectedMessage || n.ResultCode != 0 || n.Diagnostic != diagnostic || n.Closing || c.Phase() != Open {
t.Fatalf("completed CEA diagnostic=%+v phase=%v", n, c.Phase())
}
})
}
}

func TestDWAOwnershipPrecedesSemanticDiagnosticsPolicy(t *testing.T) {
for _, ownership := range []bool{false, true} {
t.Run(fmt.Sprintf("ownership-first=%t", ownership), func(t *testing.T) {
c := opened(t, func(cfg *Config) {
cfg.Watchdog, cfg.DWAOwnershipBeforeSemantics = true, ownership
cfg.WatchdogInterval = 6 * time.Second
})
sent := probe(t, c)
answer := dwa(t, sent, diam.UnableToComply)
answer.Header.HopByHopID++
n := policyProtocolNotice(t, c.Handle(receive(answer)))
kind, code := diam.ProtocolRejectedDWA, uint32(diam.UnableToComply)
if ownership {
kind, code = diam.ProtocolUnexpectedMessage, 0
}
if n.Protocol != kind || n.ResultCode != code || !c.Snapshot().Watchdog().Pending {
t.Fatalf("unowned DWA changed diagnosis/ownership: %+v", n)
}
})
func TestDWAOwnershipPrecedesSemanticDiagnostics(t *testing.T) {
c := opened(t, func(cfg *Config) { cfg.Watchdog = true; cfg.WatchdogInterval = 6 * time.Second })
sent := probe(t, c)
answer := dwa(t, sent, diam.UnableToComply)
answer.Header.HopByHopID++
n := protocolNotice(t, c.Handle(receive(answer)))
if n.Protocol != diam.ProtocolUnexpectedMessage || n.ResultCode != 0 || n.Diagnostic != "dwa-correlation" || !c.Snapshot().Watchdog().Pending {
t.Fatalf("unowned DWA changed diagnosis/ownership: %+v", n)
}
}

func TestDWAIdentityPrecedesUnsuccessfulResultPolicy(t *testing.T) {
for _, identity := range []bool{false, true} {
t.Run(fmt.Sprintf("identity-first=%t", identity), func(t *testing.T) {
c := opened(t, func(cfg *Config) {
cfg.Watchdog, cfg.DWAIdentityBeforeResult = true, identity
cfg.WatchdogInterval = 6 * time.Second
})
sent := probe(t, c)
answer := dwa(t, sent, diam.UnableToComply)
for _, field := range answer.AVP {
if field.Code == avp.OriginHost {
field.Data = datatype.DiameterIdentity("other.example")
}
}
n := policyProtocolNotice(t, c.Handle(receive(answer)))
kind, code := diam.ProtocolRejectedDWA, uint32(diam.UnableToComply)
if identity {
kind, code = diam.ProtocolInvalidDWA, 0
}
if n.Protocol != kind || n.ResultCode != code || !c.Snapshot().Watchdog().Pending {
t.Fatalf("unbound DWA changed diagnosis/ownership: %+v", n)
}
})
func TestDWAIdentityPrecedesUnsuccessfulResult(t *testing.T) {
c := opened(t, func(cfg *Config) { cfg.Watchdog = true; cfg.WatchdogInterval = 6 * time.Second })
sent := probe(t, c)
answer := dwa(t, sent, diam.UnableToComply)
for _, field := range answer.AVP {
if field.Code == avp.OriginHost {
field.Data = datatype.DiameterIdentity("other.example")
}
}
n := protocolNotice(t, c.Handle(receive(answer)))
if n.Protocol != diam.ProtocolInvalidDWA || n.ResultCode != 0 || !c.Snapshot().Watchdog().Pending {
t.Fatalf("unbound DWA changed diagnosis/ownership: %+v", n)
}
}

func TestReplyBuilderErrorWrapperPolicy(t *testing.T) {
for _, preserve := range []bool{false, true} {
t.Run(fmt.Sprintf("preserve=%t", preserve), func(t *testing.T) {
c := opened(t, func(cfg *Config) { cfg.PreserveReplyErrorFormatWrapper = preserve })
empty, err := dict.NewParser()
if err != nil {
t.Fatal(err)
}
in := receive(diam.NewMessage(diam.DeviceWatchdog, diam.RequestFlag, 0, 1, 2, empty))
es := c.reject(nil, in, diam.ProtocolInvalidRequest, &diam.MessageError{ResultCode: diam.MissingAVP})
n := policyProtocolNotice(t, es)
if got := fmt.Sprintf("%T", n.ReplyError); (got == "*fmt.wrapError") != preserve {
t.Fatalf("builder wrapper=%s, preserve=%t", got, preserve)
}
})
func TestReplyBuilderErrorRetainsFormatWrapper(t *testing.T) {
c := opened(t)
empty, err := dict.NewParser()
if err != nil {
t.Fatal(err)
}
in := receive(diam.NewMessage(diam.DeviceWatchdog, diam.RequestFlag, 0, 1, 2, empty))
es := c.reject(nil, in, diam.ProtocolInvalidRequest, &diam.MessageError{ResultCode: diam.MissingAVP})
n := protocolNotice(t, es)
if got := fmt.Sprintf("%T", n.ReplyError); got != "*fmt.wrapError" {
t.Fatalf("builder wrapper=%s, want *fmt.wrapError", got)
}
}
Loading
Loading