astagent: prevent panic in handleChannelDestroyed

This commit is contained in:
armirveliaj
2025-02-21 09:11:51 -05:00
committed by Dan Christian Bogos
parent 322afc585f
commit e64d02e187
4 changed files with 147 additions and 5 deletions

View File

@@ -18,6 +18,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package agents
import (
"bytes"
"log"
"os"
"strings"
"testing"
"github.com/cgrates/birpc"
@@ -27,6 +31,7 @@ import (
"github.com/cgrates/cgrates/sessions"
"github.com/cgrates/cgrates/utils"
"github.com/cgrates/rpcclient"
"github.com/google/go-cmp/cmp"
)
func TestAAsSessionSClientIface(t *testing.T) {
@@ -83,10 +88,11 @@ func TestHandleChannelDestroyedFail(t *testing.T) {
"type": "ChannelDestroyed",
}
ev := NewSMAsteriskEvent(ariEv, "127.0.0.1", utils.EmptyString)
evCopy := ev
evCopy := ev.Clone()
evCopy.cachedFields = map[string]string{"channelID": "1714719185.3"}
sma.handleChannelDestroyed(ev)
if ev != evCopy {
t.Errorf("Expected ev to not change, received <%v>", utils.ToJSON(ev))
if diff := cmp.Diff(evCopy, ev, cmp.AllowUnexported(SMAsteriskEvent{})); diff != "" {
t.Errorf("handleChannelDestroyed modified SMAsteriskEvent unexpectedly (-want +got): \n%s", diff)
}
}
@@ -119,3 +125,68 @@ func TestAsteriskAgentV1AlterSession(t *testing.T) {
t.Errorf("Expected error: %v, got: %v", utils.ErrNotImplemented, err)
}
}
func TestHandleChannelDestroyedCases(t *testing.T) {
cfg := config.NewDefaultCGRConfig()
internalSessionSChan := make(chan birpc.ClientConnector, 1)
cM := engine.NewConnManager(cfg, map[string]chan context.ClientConnector{
utils.ConcatenatedKey(rpcclient.BiRPCInternal, utils.MetaSessionS): internalSessionSChan,
})
sma, err := NewAsteriskAgent(cfg, 1, cM)
if err != nil {
t.Error(err)
}
utils.Logger.SetLogLevel(4)
utils.Logger.SetSyslog(nil)
t.Cleanup(func() {
utils.Logger.SetLogLevel(0)
log.SetOutput(os.Stderr)
})
testCases := []struct {
name string
ariEv map[string]any
expLog string
}{
{
name: "Missing Channel",
ariEv: map[string]any{},
expLog: "<AsteriskAgent> missing or invalid 'channel' field in event: {}",
},
{
name: "Invalid Channel",
ariEv: map[string]any{"channel": "invalid"},
expLog: `<AsteriskAgent> missing or invalid 'channel' field in event: {"channel":"invalid"}`,
},
{
name: "Missing ChannelVars",
ariEv: map[string]any{"channel": map[string]any{"channel": "1"}},
expLog: `<AsteriskAgent> missing or invalid 'channelvars' field in 'channel': {"channel":"1"}`,
},
{
name: "Invalid ChannelVars",
ariEv: map[string]any{"channel": map[string]any{"channelvars": "invalid"}},
expLog: `<AsteriskAgent> missing or invalid 'channelvars' field in 'channel': {"channelvars":"invalid"}`,
},
{
name: "Valid ChannelVars",
ariEv: map[string]any{"channel": map[string]any{"channelvars": map[string]any{utils.CGRReqType: "test type"}}},
expLog: "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
buf := &bytes.Buffer{}
log.SetOutput(buf)
ev := NewSMAsteriskEvent(tc.ariEv, "127.0.0.1", utils.EmptyString)
sma.handleChannelDestroyed(ev)
if !strings.Contains(buf.String(), tc.expLog) {
t.Errorf("expected log warning %s", buf)
}
})
}
}