Updated tests for path sanitizations

This commit is contained in:
porosnicuadrian
2021-05-28 16:14:31 +03:00
committed by Dan Christian Bogos
parent 5c3eb53bac
commit faaf19d806
15 changed files with 122 additions and 40 deletions

View File

@@ -160,7 +160,7 @@ func TestActionsSetActionProfileCheckErrors(t *testing.T) {
actPrf.ID = "TestActionsSetActionProfileCheckErrors"
actPrf.Actions = make([]*engine.APAction, 1)
actPrf.FilterIDs = []string{"invalid_filter_format"}
experr = "SERVER_ERROR: broken reference to filter: invalid_filter_format for item with ID: cgrates.org:TestActionsSetActionProfileCheckErrors"
experr = "SERVER_ERROR: broken reference to filter: <invalid_filter_format> for item with ID: cgrates.org:TestActionsSetActionProfileCheckErrors"
if err := adms.SetActionProfile(context.Background(), actPrf, &reply); err == nil ||
err.Error() != experr {

View File

@@ -376,7 +376,7 @@ func testAttributeSetAttributeProfileBrokenReference(t *testing.T) {
},
}
var reply string
expectedErr := "SERVER_ERROR: broken reference to filter: invalid_filter_format for item with ID: cgrates.org:TEST_ATTRIBUTES_IT_TEST_SECOND"
expectedErr := "SERVER_ERROR: broken reference to filter: <invalid_filter_format> for item with ID: cgrates.org:TEST_ATTRIBUTES_IT_TEST_SECOND"
if err := attrSRPC.Call(context.Background(), utils.AdminSv1SetAttributeProfile,
attrPrf, &reply); err == nil || err.Error() != expectedErr {
t.Errorf("Expected %+v \n, received %+v", expectedErr, err)

View File

@@ -130,7 +130,7 @@ func TestAttributesSetAttributeProfileCheckErrors(t *testing.T) {
attrPrf.ID = "TestSetAttributeProfileCheckErrors"
attrPrf.FilterIDs = []string{"invalid_fltier_format"}
expected = "SERVER_ERROR: broken reference to filter: invalid_fltier_format for item with ID: cgrates.org:TestSetAttributeProfileCheckErrors"
expected = "SERVER_ERROR: broken reference to filter: <invalid_fltier_format> for item with ID: cgrates.org:TestSetAttributeProfileCheckErrors"
if err := admS.SetAttributeProfile(context.Background(), attrPrf, &reply); err == nil || err.Error() != expected {
t.Errorf("Expected %+v, received %+v", expected, err)
}

View File

@@ -402,7 +402,7 @@ func testRateSetAttributeProfileBrokenReference(t *testing.T) {
},
}
var reply string
expectedErr := "SERVER_ERROR: broken reference to filter: invalid_filter_format for item with ID: cgrates.org:TEST_RATE_IT_TEST_SECOND"
expectedErr := "SERVER_ERROR: broken reference to filter: <invalid_filter_format> for item with ID: cgrates.org:TEST_RATE_IT_TEST_SECOND"
if err := rateSRPC.Call(context.Background(), utils.AdminSv1SetRateProfile,
ratePrf, &reply); err == nil || err.Error() != expectedErr {
t.Errorf("Expected %+v \n, received %+v", expectedErr, err)

View File

@@ -154,7 +154,7 @@ func TestResourcesSetResourceProfileCheckErrors(t *testing.T) {
resPrf.ID = "TestResourcesSetResourceProfileCheckErrors"
resPrf.FilterIDs = []string{"invalid_filter_format"}
experr = "SERVER_ERROR: broken reference to filter: invalid_filter_format for item with ID: cgrates.org:TestResourcesSetResourceProfileCheckErrors"
experr = "SERVER_ERROR: broken reference to filter: <invalid_filter_format> for item with ID: cgrates.org:TestResourcesSetResourceProfileCheckErrors"
if err := adms.SetResourceProfile(context.Background(), resPrf, &reply); err == nil ||
err.Error() != experr {

View File

@@ -153,7 +153,7 @@ func TestStatsSetStatQueueProfileCheckErrors(t *testing.T) {
sqPrf.ID = "TestStatsSetStatQueueProfileCheckErrors"
sqPrf.FilterIDs = []string{"invalid_filter_format"}
experr = "SERVER_ERROR: broken reference to filter: invalid_filter_format for item with ID: cgrates.org:TestStatsSetStatQueueProfileCheckErrors"
experr = "SERVER_ERROR: broken reference to filter: <invalid_filter_format> for item with ID: cgrates.org:TestStatsSetStatQueueProfileCheckErrors"
if err := adms.SetStatQueueProfile(context.Background(), sqPrf, &reply); err == nil ||
err.Error() != experr {

View File

@@ -154,7 +154,7 @@ func TestThresholdsSetThresholdProfileCheckErrors(t *testing.T) {
thPrf.ID = "TestThresholdsSetThresholdProfileCheckErrors"
thPrf.FilterIDs = []string{"invalid_filter_format"}
experr = "SERVER_ERROR: broken reference to filter: invalid_filter_format for item with ID: cgrates.org:TestThresholdsSetThresholdProfileCheckErrors"
experr = "SERVER_ERROR: broken reference to filter: <invalid_filter_format> for item with ID: cgrates.org:TestThresholdsSetThresholdProfileCheckErrors"
if err := adms.SetThresholdProfile(context.Background(), thPrf, &reply); err == nil ||
err.Error() != experr {

View File

@@ -267,6 +267,7 @@ func testCGRConfigReloadThresholdS(t *testing.T) {
SuffixIndexedFields: &[]string{},
ActionSConns: []string{},
IndexedSelects: true,
ActionSConns: []string{},
}
if !reflect.DeepEqual(expAttr, cfg.ThresholdSCfg()) {
t.Errorf("Expected %s , received: %s ", utils.ToJSON(expAttr), utils.ToJSON(cfg.ThresholdSCfg()))

View File

@@ -98,11 +98,11 @@ func TestServerIT(t *testing.T) {
type mockRegister string
func (x *mockRegister) ForTest(method *context.Context, args, reply interface{}) error {
func (x *mockRegister) ForTest(ctx *context.Context, args, reply interface{}) error {
return nil
}
func (robj *mockRegister) Ping(in string, out *string) error {
func (robj *mockRegister) Ping(ctx *context.Context, in string, out *string) error {
*out = utils.Pong
return nil
}
@@ -759,7 +759,7 @@ func testAcceptBiRPC(t *testing.T) {
go server.acceptBiRPC(server.birpcSrv, l, utils.JSONCaps, jsonrpc.NewJSONBirpcCodec)
rpc := jsonrpc.NewClient(p2)
var reply string
expected := "rpc: can't find method AttributeSv1.Ping"
expected := "birpc: can't find method AttributeSv1.Ping"
if err := rpc.Call(context.TODO(), utils.AttributeSv1Ping, utils.CGREvent{}, &reply); err == nil || err.Error() != expected {
t.Errorf("Expected %+v, received %+v", expected, err)
}

View File

@@ -1137,7 +1137,7 @@ func testITResourceProfileIndexes(t *testing.T) {
Stored: true,
}
expected := "broken reference to filter: FIRST for item with ID: cgrates.org:RES_PRF1"
expected := "broken reference to filter: <FIRST> for item with ID: cgrates.org:RES_PRF1"
if err := dataManager.SetResourceProfile(context.TODO(), resPref1, true); err == nil || err.Error() != expected {
t.Errorf("Expected %+v, received %+v", expected, err)
}

View File

@@ -23,12 +23,12 @@ import (
"errors"
"flag"
"io"
"net/rpc"
"net/rpc/jsonrpc"
"strings"
"testing"
"github.com/cgrates/birpc"
"github.com/cgrates/birpc/jsonrpc"
"github.com/cgrates/birpc/context"
"github.com/cgrates/rpcclient"
@@ -48,12 +48,12 @@ var loaderPaths = []string{"/tmp/In", "/tmp/Out", "/tmp/LoaderIn", "/tmp/Subpath
"/tmp/SubpathLoaderWithMove", "/tmp/SubpathOut", "/tmp/templateLoaderIn", "/tmp/templateLoaderOut",
"/tmp/customSepLoaderIn", "/tmp/customSepLoaderOut"}
func newRPCClient(cfg *config.ListenCfg) (c *rpc.Client, err error) {
func newRPCClient(cfg *config.ListenCfg) (c *birpc.Client, err error) {
switch *encoding {
case utils.MetaJSON:
return jsonrpc.Dial(utils.TCP, cfg.RPCJSONListen)
case utils.MetaGOB:
return rpc.Dial(utils.TCP, cfg.RPCGOBListen)
return birpc.Dial(utils.TCP, cfg.RPCGOBListen)
default:
return nil, errors.New("UNSUPPORTED_RPC")
}

View File

@@ -22,8 +22,9 @@ package loaders
import (
"encoding/csv"
"fmt"
"sort"
"io"
"net/rpc"
"os"
"path"
"reflect"
@@ -31,6 +32,7 @@ import (
"testing"
"time"
"github.com/cgrates/birpc"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
@@ -41,7 +43,7 @@ var (
loaderCfgPath string
loaderCfgDIR string //run tests for specific configuration
loaderCfg *config.CGRConfig
loaderRPC *rpc.Client
loaderRPC *birpc.Client
customAttributes = "12012000001\t12018209998\n12012000002\t15512580598\n12012000007\t19085199998\n12012000008\t18622784999\n12012000010\t17329440866\n12012000011\t18623689800\n12012000012\t19082050951\n12012000014\t17329440866\n12012000015\t12018209999\n12012000031\t12018209999\n12012000032\t19082050951\n12012000033\t12018209998\n12012000034\t12018209998\n"
sTestsLoader = []func(t *testing.T){
@@ -174,7 +176,7 @@ func testLoaderPopulateData(t *testing.T) {
func testLoaderLoadAttributes(t *testing.T) {
var reply string
if err := loaderRPC.Call(utils.LoaderSv1Load,
if err := loaderRPC.Call(context.Background(), utils.LoaderSv1Load,
&ArgsProcessFolder{LoaderID: "CustomLoader"}, &reply); err != nil {
t.Error(err)
}
@@ -190,22 +192,22 @@ func testLoaderVerifyOutDir(t *testing.T) {
}
func testLoaderCheckAttributes(t *testing.T) {
eAttrPrf := &engine.AttributeProfile{
eAttrPrf := &engine.APIAttributeProfile{
Tenant: "cgrates.org",
ID: "ALS1",
FilterIDs: []string{"*string:~*req.Account:1001", "*ai:~*req.AnswerTime:2014-07-29T15:00:00Z", "*string:~*opts.*context:con1|con2|con3"},
Attributes: []*engine.Attribute{
FilterIDs: []string{"*string:~*req.Account:1001", "*string:~*opts.*context:con1", "*string:~*opts.*context:con2|con3"},
Attributes: []*engine.ExternalAttribute{
{
FilterIDs: []string{"*string:~*req.Field1:Initial"},
Path: utils.MetaReq + utils.NestingSep + "Field1",
Type: utils.MetaVariable,
Value: config.NewRSRParsersMustCompile("Sub1", utils.InfieldSep),
Value: "Sub1",
},
{
FilterIDs: []string{},
Path: utils.MetaReq + utils.NestingSep + "Field2",
Type: utils.MetaVariable,
Value: config.NewRSRParsersMustCompile("Sub2", utils.InfieldSep),
Value: "Sub2",
}},
Blocker: true,
Weight: 20,
@@ -213,15 +215,15 @@ func testLoaderCheckAttributes(t *testing.T) {
if *encoding == utils.MetaGOB { // gob threats empty slices as nil values
eAttrPrf.Attributes[1].FilterIDs = nil
}
var reply *engine.AttributeProfile
if err := loaderRPC.Call(utils.AdminSv1GetAttributeProfile,
var reply *engine.APIAttributeProfile
if err := loaderRPC.Call(context.Background(), utils.AdminSv1GetAttributeProfile,
&utils.TenantIDWithAPIOpts{
TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "ALS1"},
}, &reply); err != nil {
t.Fatal(err)
}
eAttrPrf.Compile()
reply.Compile()
sort.Strings(reply.FilterIDs)
sort.Strings(eAttrPrf.FilterIDs)
if !reflect.DeepEqual(eAttrPrf, reply) {
t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(eAttrPrf), utils.ToJSON(reply))
}
@@ -240,7 +242,7 @@ func testLoaderPopulateDataWithoutMoving(t *testing.T) {
func testLoaderLoadAttributesWithoutMoving(t *testing.T) {
var reply string
if err := loaderRPC.Call(utils.LoaderSv1Load,
if err := loaderRPC.Call(context.Background(), utils.LoaderSv1Load,
&ArgsProcessFolder{LoaderID: "WithoutMoveToOut"}, &reply); err != nil {
t.Error(err)
}
@@ -272,7 +274,7 @@ func testLoaderPopulateDataWithSubpath(t *testing.T) {
func testLoaderLoadAttributesWithSubpath(t *testing.T) {
var reply string
if err := loaderRPC.Call(utils.LoaderSv1Load,
if err := loaderRPC.Call(context.Background(), utils.LoaderSv1Load,
&ArgsProcessFolder{LoaderID: "SubpathLoaderWithoutMove"}, &reply); err != nil {
t.Error(err)
}
@@ -304,7 +306,7 @@ func testLoaderPopulateDataWithSubpathWithMove(t *testing.T) {
func testLoaderLoadAttributesWithoutSubpathWithMove(t *testing.T) {
var reply string
if err := loaderRPC.Call(utils.LoaderSv1Load,
if err := loaderRPC.Call(context.Background(), utils.LoaderSv1Load,
&ArgsProcessFolder{LoaderID: "SubpathLoaderWithMove"}, &reply); err != nil {
t.Error(err)
}
@@ -335,7 +337,7 @@ func testLoaderPopulateDataForTemplateLoader(t *testing.T) {
func testLoaderLoadAttributesForTemplateLoader(t *testing.T) {
var reply string
if err := loaderRPC.Call(utils.LoaderSv1Load,
if err := loaderRPC.Call(context.Background(), utils.LoaderSv1Load,
&ArgsProcessFolder{LoaderID: "LoaderWithTemplate"}, &reply); err != nil {
t.Error(err)
}
@@ -372,31 +374,29 @@ func testLoaderPopulateDataForCustomSep(t *testing.T) {
}
func testLoaderCheckForCustomSep(t *testing.T) {
eAttrPrf := &engine.AttributeProfile{
eAttrPrf := &engine.APIAttributeProfile{
Tenant: "cgrates.org",
ID: "ATTR_12012000001",
FilterIDs: []string{"*string:~*req.Destination:12012000001"},
Attributes: []*engine.Attribute{
Attributes: []*engine.ExternalAttribute{
{
FilterIDs: []string{},
Path: "*req.Destination",
Type: utils.MetaConstant,
Value: config.NewRSRParsersMustCompile("12018209998", utils.InfieldSep),
Value: "12018209998",
},
},
}
if *encoding == utils.MetaGOB { // gob threats empty slices as nil values
eAttrPrf.Attributes[0].FilterIDs = nil
}
var reply *engine.AttributeProfile
if err := loaderRPC.Call(utils.AdminSv1GetAttributeProfile,
var reply *engine.APIAttributeProfile
if err := loaderRPC.Call(context.Background(), utils.AdminSv1GetAttributeProfile,
&utils.TenantIDWithAPIOpts{
TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "ATTR_12012000001"},
}, &reply); err != nil {
t.Fatal(err)
}
eAttrPrf.Compile()
reply.Compile()
if !reflect.DeepEqual(eAttrPrf, reply) {
t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(eAttrPrf), utils.ToJSON(reply))
}

View File

@@ -182,7 +182,7 @@ func testResITMigrateAndMove(t *testing.T) {
resPrfl := &engine.ResourceProfile{
Tenant: "cgrates.org",
ID: "RES1",
FilterIDs: []string{"*string:~Account:1001"},
FilterIDs: []string{"*string:~*req.Account:1001"},
UsageTTL: time.Second,
Limit: 1,
Weight: 10,

View File

@@ -181,7 +181,7 @@ func testSupITMigrateAndMove(t *testing.T) {
supPrfl := &engine.RouteProfile{
Tenant: "cgrates.org",
ID: "SUP1",
FilterIDs: []string{"*string:~Account:1001"},
FilterIDs: []string{"*string:~*req.Account:1001"},
Weight: 10,
Sorting: utils.MetaQOS,
SortingParameters: []string{},

View File

@@ -134,3 +134,84 @@ func TestComposeNavMapVal(t *testing.T) {
t.Errorf("Expected error: %s received: %v", ErrWrongPath, err)
}
}
func TestIsPathValid(t *testing.T) {
path := "Field1.Field2[0]"
if err := IsPathValid(path); err != nil {
t.Error(err)
}
///
path = "~Field1"
errExpect := "Path is missing "
if err := IsPathValid(path); err == nil || err.Error() != errExpect {
t.Errorf("Expected %v but received %v", errExpect, err)
}
///
path = "~Field1.\n\t.Field2[0]"
errExpect = "Empty field path "
if err := IsPathValid(path); err == nil || err.Error() != errExpect {
t.Errorf("Expected %v but received %v", errExpect, err)
}
///
path = "~Field1.Field2[0]"
if err := IsPathValid(path); err != nil {
t.Error(err)
}
}
func TestIsPathValidForExporters(t *testing.T) {
path := "Field1.Field2[0]"
if err := IsPathValidForExporters(path); err != nil {
t.Error(err)
}
///
path = "~Field1.\n\t.Field2[0]"
errExpect := "Empty field path "
if err := IsPathValidForExporters(path); err == nil || err.Error() != errExpect {
t.Errorf("Expected %v but received %v", errExpect, err)
}
///
path = "~Field1.Field2[0]"
if err := IsPathValidForExporters(path); err != nil {
t.Error(err)
}
}
func TestCheckInLineFilter(t *testing.T) {
fltrs := []string{"Test1", "Test2"}
if err := CheckInLineFilter(fltrs); err != nil {
t.Error(err)
}
///
fltrs = []string{"*Test1", "*Test2"}
errExpect := "inline parse error for string: <*Test1>"
if err := CheckInLineFilter(fltrs); err == nil || err.Error() != errExpect {
t.Errorf("Expected %v but received %v", errExpect, err)
}
///
fltrs = []string{"*Test1:*Test2:*Test3:*Test4"}
if err := CheckInLineFilter(fltrs); err != nil {
t.Error(err)
}
///
fltrs = []string{"*empty:~Field1..Field2[0]:*Test3:*Test4"}
errExpect = "Empty field path for <*empty:~Field1..Field2[0]:*Test3:*Test4>"
if err := CheckInLineFilter(fltrs); err == nil || err.Error() != errExpect {
t.Errorf("Expected %v but received %v", errExpect, err)
}
///
fltrs = []string{"*empty:~Field1.Field2[0]:~Field1..Field2[0]|Test4"}
errExpect = "Empty field path for <*empty:~Field1.Field2[0]:~Field1..Field2[0]|Test4>"
if err := CheckInLineFilter(fltrs); err == nil || err.Error() != errExpect {
t.Errorf("Expected %v but received %v", errExpect, err)
}
}