|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "reflect" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/crowdsecurity/crowdsec/pkg/models" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + binaryPath = "./tests/custombinary" |
| 16 | + binaryOutputFile = "./data.txt" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + durationWithUnit = "1200s" |
| 21 | + durationInSeconds = durationWithUnit[:len(durationWithUnit)-1] |
| 22 | + sceanario = "crowdsec/bruteforce" |
| 23 | + ip1 = "1.2.3.4" |
| 24 | + ip2 = "1.2.3.5" |
| 25 | + decisionType = "IP" |
| 26 | +) |
| 27 | + |
| 28 | +type parsedLine struct { |
| 29 | + action string |
| 30 | + value string |
| 31 | + duration string |
| 32 | + sceanario string |
| 33 | +} |
| 34 | + |
| 35 | +func parseFile(path string) []parsedLine { |
| 36 | + dat, err := ioutil.ReadFile(binaryOutputFile) |
| 37 | + parsedLines := make([]parsedLine, 0) |
| 38 | + if err != nil { |
| 39 | + panic(err) |
| 40 | + } |
| 41 | + for _, line := range strings.Split(string(dat), "\n") { |
| 42 | + if len(line) == 0 { |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + parsedLines = append(parsedLines, parseLine(line)) |
| 47 | + } |
| 48 | + return parsedLines |
| 49 | +} |
| 50 | + |
| 51 | +func parseLine(line string) parsedLine { |
| 52 | + words := strings.Split(line, " ") |
| 53 | + return parsedLine{ |
| 54 | + action: words[0], |
| 55 | + value: words[1], |
| 56 | + duration: words[2], |
| 57 | + sceanario: words[3], |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func cleanup() { |
| 62 | + if _, err := os.Stat(binaryOutputFile); err != nil { |
| 63 | + fmt.Println("didnt found the file") |
| 64 | + return |
| 65 | + } |
| 66 | + os.Remove(binaryOutputFile) |
| 67 | +} |
| 68 | + |
| 69 | +func Test_customBouncer_Add(t *testing.T) { |
| 70 | + type args struct { |
| 71 | + Decisions []*models.Decision |
| 72 | + } |
| 73 | + tests := []struct { |
| 74 | + name string |
| 75 | + args args |
| 76 | + expectedLines []parsedLine |
| 77 | + }{ |
| 78 | + { |
| 79 | + name: "simple, single decision", |
| 80 | + args: args{ |
| 81 | + Decisions: []*models.Decision{ |
| 82 | + { |
| 83 | + Duration: &durationWithUnit, |
| 84 | + Value: &ip1, |
| 85 | + Scenario: &sceanario, |
| 86 | + Type: &decisionType, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + expectedLines: []parsedLine{ |
| 91 | + { |
| 92 | + action: "add", |
| 93 | + value: ip1, |
| 94 | + duration: durationInSeconds, |
| 95 | + sceanario: sceanario, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "simple, two decisions", |
| 101 | + args: args{ |
| 102 | + Decisions: []*models.Decision{ |
| 103 | + { |
| 104 | + Duration: &durationWithUnit, |
| 105 | + Value: &ip1, |
| 106 | + Scenario: &sceanario, |
| 107 | + Type: &decisionType, |
| 108 | + }, |
| 109 | + { |
| 110 | + Duration: &durationWithUnit, |
| 111 | + Value: &ip2, |
| 112 | + Scenario: &sceanario, |
| 113 | + Type: &decisionType, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + expectedLines: []parsedLine{ |
| 118 | + { |
| 119 | + action: "add", |
| 120 | + value: ip1, |
| 121 | + duration: durationInSeconds, |
| 122 | + sceanario: sceanario, |
| 123 | + }, |
| 124 | + { |
| 125 | + action: "add", |
| 126 | + value: ip2, |
| 127 | + duration: durationInSeconds, |
| 128 | + sceanario: sceanario, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "duplicates", |
| 134 | + args: args{ |
| 135 | + Decisions: []*models.Decision{ |
| 136 | + { |
| 137 | + Duration: &durationWithUnit, |
| 138 | + Value: &ip1, |
| 139 | + Scenario: &sceanario, |
| 140 | + Type: &decisionType, |
| 141 | + }, |
| 142 | + { |
| 143 | + Duration: &durationWithUnit, |
| 144 | + Value: &ip1, |
| 145 | + Scenario: &sceanario, |
| 146 | + Type: &decisionType, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + expectedLines: []parsedLine{ |
| 151 | + { |
| 152 | + action: "add", |
| 153 | + value: ip1, |
| 154 | + duration: durationInSeconds, |
| 155 | + sceanario: sceanario, |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + } |
| 160 | + for _, tt := range tests { |
| 161 | + t.Run(tt.name, func(t *testing.T) { |
| 162 | + defer cleanup() |
| 163 | + c := &customBouncer{ |
| 164 | + path: binaryPath, |
| 165 | + } |
| 166 | + c.ResetCache() |
| 167 | + for _, decision := range tt.args.Decisions { |
| 168 | + err := c.Add(decision) |
| 169 | + if err != nil { |
| 170 | + t.Error(err) |
| 171 | + } |
| 172 | + } |
| 173 | + foundData := parseFile(binaryOutputFile) |
| 174 | + if !reflect.DeepEqual(foundData, tt.expectedLines) { |
| 175 | + t.Errorf("expected=%v, found=%v", tt.expectedLines, foundData) |
| 176 | + } |
| 177 | + |
| 178 | + }) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +func Test_customBouncer_Delete(t *testing.T) { |
| 183 | + type args struct { |
| 184 | + Decisions []*models.Decision |
| 185 | + } |
| 186 | + tests := []struct { |
| 187 | + name string |
| 188 | + args args |
| 189 | + expectedLines []parsedLine |
| 190 | + }{ |
| 191 | + { |
| 192 | + name: "simple, single decision", |
| 193 | + args: args{ |
| 194 | + Decisions: []*models.Decision{ |
| 195 | + { |
| 196 | + Duration: &durationWithUnit, |
| 197 | + Value: &ip1, |
| 198 | + Scenario: &sceanario, |
| 199 | + Type: &decisionType, |
| 200 | + }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + expectedLines: []parsedLine{ |
| 204 | + { |
| 205 | + action: "del", |
| 206 | + value: ip1, |
| 207 | + duration: durationInSeconds, |
| 208 | + sceanario: sceanario, |
| 209 | + }, |
| 210 | + }, |
| 211 | + }, |
| 212 | + { |
| 213 | + name: "simple, two decisions", |
| 214 | + args: args{ |
| 215 | + Decisions: []*models.Decision{ |
| 216 | + { |
| 217 | + Duration: &durationWithUnit, |
| 218 | + Value: &ip1, |
| 219 | + Scenario: &sceanario, |
| 220 | + Type: &decisionType, |
| 221 | + }, |
| 222 | + { |
| 223 | + Duration: &durationWithUnit, |
| 224 | + Value: &ip2, |
| 225 | + Scenario: &sceanario, |
| 226 | + Type: &decisionType, |
| 227 | + }, |
| 228 | + }, |
| 229 | + }, |
| 230 | + expectedLines: []parsedLine{ |
| 231 | + { |
| 232 | + action: "del", |
| 233 | + value: ip1, |
| 234 | + duration: durationInSeconds, |
| 235 | + sceanario: sceanario, |
| 236 | + }, |
| 237 | + { |
| 238 | + action: "del", |
| 239 | + value: ip2, |
| 240 | + duration: durationInSeconds, |
| 241 | + sceanario: sceanario, |
| 242 | + }, |
| 243 | + }, |
| 244 | + }, |
| 245 | + { |
| 246 | + name: "duplicates", |
| 247 | + args: args{ |
| 248 | + Decisions: []*models.Decision{ |
| 249 | + { |
| 250 | + Duration: &durationWithUnit, |
| 251 | + Value: &ip1, |
| 252 | + Scenario: &sceanario, |
| 253 | + Type: &decisionType, |
| 254 | + }, |
| 255 | + { |
| 256 | + Duration: &durationWithUnit, |
| 257 | + Value: &ip1, |
| 258 | + Scenario: &sceanario, |
| 259 | + Type: &decisionType, |
| 260 | + }, |
| 261 | + }, |
| 262 | + }, |
| 263 | + expectedLines: []parsedLine{ |
| 264 | + { |
| 265 | + action: "del", |
| 266 | + value: ip1, |
| 267 | + duration: durationInSeconds, |
| 268 | + sceanario: sceanario, |
| 269 | + }, |
| 270 | + }, |
| 271 | + }, |
| 272 | + } |
| 273 | + for _, tt := range tests { |
| 274 | + t.Run(tt.name, func(t *testing.T) { |
| 275 | + defer cleanup() |
| 276 | + c := &customBouncer{ |
| 277 | + path: binaryPath, |
| 278 | + } |
| 279 | + c.ResetCache() |
| 280 | + for _, decision := range tt.args.Decisions { |
| 281 | + err := c.Delete(decision) |
| 282 | + if err != nil { |
| 283 | + t.Error(err) |
| 284 | + } |
| 285 | + } |
| 286 | + foundData := parseFile(binaryOutputFile) |
| 287 | + if !reflect.DeepEqual(foundData, tt.expectedLines) { |
| 288 | + t.Errorf("expected=%v, found=%v", tt.expectedLines, foundData) |
| 289 | + } |
| 290 | + |
| 291 | + }) |
| 292 | + } |
| 293 | +} |
0 commit comments