github.com/crowdsecurity/crowdsec@v1.6.1/pkg/leakybucket/manager_load_test.go (about) 1 package leakybucket 2 3 import ( 4 "fmt" 5 "testing" 6 7 "gopkg.in/tomb.v2" 8 ) 9 10 type cfgTest struct { 11 cfg BucketFactory 12 loadable bool 13 valid bool 14 } 15 16 func runTest(tests []cfgTest) error { 17 var tomb = &tomb.Tomb{} 18 for idx, cfg := range tests { 19 err := LoadBucket(&cfg.cfg, tomb) 20 if cfg.loadable && err != nil { 21 return fmt.Errorf("expected loadable result (%d/%d), got: %s", idx+1, len(tests), err) 22 } 23 if !cfg.loadable && err == nil { 24 return fmt.Errorf("expected unloadable result (%d/%d)", idx+1, len(tests)) 25 } 26 err = ValidateFactory(&cfg.cfg) 27 if cfg.valid && err != nil { 28 return fmt.Errorf("expected valid result (%d/%d), got: %s", idx+1, len(tests), err) 29 } 30 if !cfg.valid && err == nil { 31 return fmt.Errorf("expected invalid result (%d/%d)", idx+1, len(tests)) 32 } 33 } 34 return nil 35 } 36 37 func TestBadBucketsConfig(t *testing.T) { 38 var CfgTests = []cfgTest{ 39 //empty 40 {BucketFactory{}, false, false}, 41 //missing description 42 {BucketFactory{Name: "test"}, false, false}, 43 //missing type 44 {BucketFactory{Name: "test", Description: "test1"}, false, false}, 45 //bad type 46 {BucketFactory{Name: "test", Description: "test1", Type: "ratata"}, false, false}, 47 } 48 if err := runTest(CfgTests); err != nil { 49 t.Fatalf("%s", err) 50 } 51 } 52 53 func TestLeakyBucketsConfig(t *testing.T) { 54 var CfgTests = []cfgTest{ 55 //leaky with bad capacity 56 {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 0}, false, false}, 57 //leaky with empty leakspeed 58 {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1}, false, false}, 59 //leaky with missing filter 60 {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1, LeakSpeed: "1s"}, false, true}, 61 //leaky with invalid leakspeed 62 {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1, LeakSpeed: "abs", Filter: "true"}, false, false}, 63 //leaky with valid filter 64 {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1, LeakSpeed: "1s", Filter: "true"}, true, true}, 65 //leaky with invalid filter 66 {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1, LeakSpeed: "1s", Filter: "xu"}, false, true}, 67 //leaky with valid filter 68 {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1, LeakSpeed: "1s", Filter: "true"}, true, true}, 69 //leaky with bad overflow filter 70 {BucketFactory{Name: "test", Description: "test1", Type: "leaky", Capacity: 1, LeakSpeed: "1s", Filter: "true", OverflowFilter: "xu"}, false, true}, 71 } 72 73 if err := runTest(CfgTests); err != nil { 74 t.Fatalf("%s", err) 75 } 76 77 } 78 79 func TestBlackholeConfig(t *testing.T) { 80 var CfgTests = []cfgTest{ 81 //basic bh 82 {BucketFactory{Name: "test", Description: "test1", Type: "trigger", Filter: "true", Blackhole: "15s"}, true, true}, 83 //bad bh 84 {BucketFactory{Name: "test", Description: "test1", Type: "trigger", Filter: "true", Blackhole: "abc"}, false, true}, 85 } 86 87 if err := runTest(CfgTests); err != nil { 88 t.Fatalf("%s", err) 89 } 90 91 } 92 93 func TestTriggerBucketsConfig(t *testing.T) { 94 var CfgTests = []cfgTest{ 95 //basic valid counter 96 {BucketFactory{Name: "test", Description: "test1", Type: "trigger", Filter: "true"}, true, true}, 97 } 98 99 if err := runTest(CfgTests); err != nil { 100 t.Fatalf("%s", err) 101 } 102 103 } 104 105 func TestCounterBucketsConfig(t *testing.T) { 106 var CfgTests = []cfgTest{ 107 108 //basic valid counter 109 {BucketFactory{Name: "test", Description: "test1", Type: "counter", Capacity: -1, Duration: "5s", Filter: "true"}, true, true}, 110 //missing duration 111 {BucketFactory{Name: "test", Description: "test1", Type: "counter", Capacity: -1, Filter: "true"}, false, false}, 112 //bad duration 113 {BucketFactory{Name: "test", Description: "test1", Type: "counter", Capacity: -1, Duration: "abc", Filter: "true"}, false, false}, 114 //capacity must be -1 115 {BucketFactory{Name: "test", Description: "test1", Type: "counter", Capacity: 0, Duration: "5s", Filter: "true"}, false, false}, 116 } 117 if err := runTest(CfgTests); err != nil { 118 t.Fatalf("%s", err) 119 } 120 121 } 122 123 func TestBayesianBucketsConfig(t *testing.T) { 124 var CfgTests = []cfgTest{ 125 126 //basic valid counter 127 {BucketFactory{Name: "test", Description: "test1", Type: "bayesian", Capacity: -1, Filter: "true", BayesianPrior: 0.5, BayesianThreshold: 0.5, BayesianConditions: []RawBayesianCondition{{ConditionalFilterName: "true", ProbGivenEvil: 0.5, ProbGivenBenign: 0.5}}}, true, true}, 128 //bad capacity 129 {BucketFactory{Name: "test", Description: "test1", Type: "bayesian", Capacity: 1, Filter: "true", BayesianPrior: 0.5, BayesianThreshold: 0.5, BayesianConditions: []RawBayesianCondition{{ConditionalFilterName: "true", ProbGivenEvil: 0.5, ProbGivenBenign: 0.5}}}, false, false}, 130 //missing prior 131 {BucketFactory{Name: "test", Description: "test1", Type: "bayesian", Capacity: -1, Filter: "true", BayesianThreshold: 0.5, BayesianConditions: []RawBayesianCondition{{ConditionalFilterName: "true", ProbGivenEvil: 0.5, ProbGivenBenign: 0.5}}}, false, false}, 132 //missing threshold 133 {BucketFactory{Name: "test", Description: "test1", Type: "bayesian", Capacity: -1, Filter: "true", BayesianPrior: 0.5, BayesianConditions: []RawBayesianCondition{{ConditionalFilterName: "true", ProbGivenEvil: 0.5, ProbGivenBenign: 0.5}}}, false, false}, 134 //bad prior 135 {BucketFactory{Name: "test", Description: "test1", Type: "bayesian", Capacity: -1, Filter: "true", BayesianPrior: 1.5, BayesianThreshold: 0.5, BayesianConditions: []RawBayesianCondition{{ConditionalFilterName: "true", ProbGivenEvil: 0.5, ProbGivenBenign: 0.5}}}, false, false}, 136 //bad threshold 137 {BucketFactory{Name: "test", Description: "test1", Type: "bayesian", Capacity: -1, Filter: "true", BayesianPrior: 0.5, BayesianThreshold: 1.5, BayesianConditions: []RawBayesianCondition{{ConditionalFilterName: "true", ProbGivenEvil: 0.5, ProbGivenBenign: 0.5}}}, false, false}, 138 } 139 if err := runTest(CfgTests); err != nil { 140 t.Fatalf("%s", err) 141 } 142 143 }