From 6881d8405471c31c4f3f512d1e4e920061b24c59 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 6 Jan 2015 10:32:29 +0200 Subject: [PATCH 1/2] protection against malformed destination keys --- engine/storage_redis.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/engine/storage_redis.go b/engine/storage_redis.go index ced19f517..49b567e4b 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -86,6 +86,10 @@ func (rs *RedisStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []s CleanStalePrefixes(dKeys) } for _, key := range dKeys { + if len(key) <= len(DESTINATION_PREFIX) { + Logger.Warning(fmt.Sprintf("Got malformed destination id: ", key)) + continue + } if _, err = rs.GetDestination(key[len(DESTINATION_PREFIX):]); err != nil { cache2go.RollbackTransaction() return err From ef101ce3b0fe26aa80ed420a2306ac103ad9a8a9 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 6 Jan 2015 14:21:40 +0200 Subject: [PATCH 2/2] nil balance map fix fix for adding default balance on uninitialized account --- engine/account.go | 3 +++ engine/account_test.go | 19 +++++++++++++++++++ engine/balances.go | 3 ++- engine/balances_test.go | 7 +++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/engine/account.go b/engine/account.go index 5e16aa84d..2f0f0da74 100644 --- a/engine/account.go +++ b/engine/account.go @@ -302,6 +302,9 @@ func (ub *Account) GetDefaultMoneyBalance(direction string) *Balance { } // create default balance defaultBalance := &Balance{Weight: 0} // minimum weight + if ub.BalanceMap == nil { + ub.BalanceMap = make(map[string]BalanceChain) + } ub.BalanceMap[CREDIT+direction] = append(ub.BalanceMap[CREDIT+direction], defaultBalance) return defaultBalance } diff --git a/engine/account_test.go b/engine/account_test.go index f80f94841..6a1e139fa 100644 --- a/engine/account_test.go +++ b/engine/account_test.go @@ -1138,6 +1138,25 @@ func TestDebitDataMoney(t *testing.T) { } } +func TestAccountGetDefaultMoneyBalanceEmpty(t *testing.T) { + acc := &Account{} + defBal := acc.GetDefaultMoneyBalance(OUTBOUND) + if defBal == nil || len(acc.BalanceMap) != 1 || !defBal.IsDefault() { + t.Errorf("Bad default money balance: %+v", defBal) + } +} + +func TestAccountGetDefaultMoneyBalance(t *testing.T) { + acc := &Account{} + acc.BalanceMap = make(map[string]BalanceChain) + tag := CREDIT + OUTBOUND + acc.BalanceMap[tag] = append(acc.BalanceMap[tag], &Balance{Weight: 10}) + defBal := acc.GetDefaultMoneyBalance(OUTBOUND) + if defBal == nil || len(acc.BalanceMap[tag]) != 2 || !defBal.IsDefault() { + t.Errorf("Bad default money balance: %+v", defBal) + } +} + /*********************************** Benchmarks *******************************/ func BenchmarkGetSecondForPrefix(b *testing.B) { diff --git a/engine/balances.go b/engine/balances.go index aabb73e2a..8c4cfc7db 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -84,7 +84,8 @@ func (b *Balance) IsDefault() bool { b.RatingSubject == "" && b.Category == "" && b.ExpirationDate.IsZero() && - b.SharedGroup == "" + b.SharedGroup == "" && + b.Weight == 0 } func (b *Balance) IsExpired() bool { diff --git a/engine/balances_test.go b/engine/balances_test.go index 57cb7b020..9f3551b7c 100644 --- a/engine/balances_test.go +++ b/engine/balances_test.go @@ -231,3 +231,10 @@ func TestBalanceMatchActionTriggerSharedGroup(t *testing.T) { t.Errorf("Error matching action trigger: %+v %+v", b, at) } } + +func TestBalanceIsDefault(t *testing.T) { + b := &Balance{Weight: 0} + if !b.IsDefault() { + t.Errorf("Balance should be default: +v", b) + } +}