-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregator_fraud_test.go
More file actions
490 lines (448 loc) · 11.5 KB
/
aggregator_fraud_test.go
File metadata and controls
490 lines (448 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package gofeat_test
import (
"math"
"testing"
"time"
"github.com/w0rng/gofeat"
)
func TestVelocity(t *testing.T) {
now := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
tests := []struct {
name string
events []gofeat.Event
window time.Duration
want float64
}{
{
name: "5 events over 5 minutes",
events: []gofeat.Event{
{Timestamp: now},
{Timestamp: now.Add(1 * time.Minute)},
{Timestamp: now.Add(2 * time.Minute)},
{Timestamp: now.Add(3 * time.Minute)},
{Timestamp: now.Add(5 * time.Minute)},
},
window: time.Hour,
want: 1.0, // 5 events / 5 minutes = 1 event/min
},
{
name: "10 events in same second",
events: []gofeat.Event{
{Timestamp: now},
{Timestamp: now},
{Timestamp: now},
{Timestamp: now},
{Timestamp: now},
{Timestamp: now},
{Timestamp: now},
{Timestamp: now},
{Timestamp: now},
{Timestamp: now},
},
window: time.Hour,
want: 10.0 / 60.0, // 10 events per hour window = 10/60 per minute
},
{
name: "2 events 1 minute apart",
events: []gofeat.Event{
{Timestamp: now},
{Timestamp: now.Add(1 * time.Minute)},
},
window: time.Hour,
want: 2.0, // 2 events / 1 minute = 2 events/min
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
agg := gofeat.Velocity(tt.window)()
for _, e := range tt.events {
agg.Add(e)
}
result := agg.Result().(float64)
if math.Abs(result-tt.want) > 0.01 {
t.Errorf("velocity: got %.2f, want %.2f", result, tt.want)
}
})
}
}
func TestEntropy(t *testing.T) {
tests := []struct {
name string
events []gofeat.Event
want float64
field string
}{
{
name: "all same value",
events: []gofeat.Event{
{Data: map[string]any{"device": "dev1"}},
{Data: map[string]any{"device": "dev1"}},
{Data: map[string]any{"device": "dev1"}},
},
field: "device",
want: 0.0, // H = 0 for single value
},
{
name: "two values equally distributed",
events: []gofeat.Event{
{Data: map[string]any{"device": "dev1"}},
{Data: map[string]any{"device": "dev2"}},
},
field: "device",
want: 1.0, // H = 1 for two equally probable values
},
{
name: "four values equally distributed",
events: []gofeat.Event{
{Data: map[string]any{"country": "US"}},
{Data: map[string]any{"country": "CA"}},
{Data: map[string]any{"country": "MX"}},
{Data: map[string]any{"country": "BR"}},
},
field: "country",
want: 2.0, // H = 2 for four equally probable values
},
{
name: "skewed distribution",
events: []gofeat.Event{
{Data: map[string]any{"device": "dev1"}},
{Data: map[string]any{"device": "dev1"}},
{Data: map[string]any{"device": "dev1"}},
{Data: map[string]any{"device": "dev2"}},
},
field: "device",
want: 0.81, // H ≈ 0.81 for 75%/25% distribution
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
agg := gofeat.Entropy(tt.field)()
for _, e := range tt.events {
agg.Add(e)
}
result := agg.Result().(float64)
if math.Abs(result-tt.want) > 0.01 {
t.Errorf("entropy: got %.2f, want %.2f", result, tt.want)
}
})
}
}
func TestUniqueRatio(t *testing.T) {
tests := []struct {
name string
events []gofeat.Event
want float64
}{
{
name: "all unique values",
events: []gofeat.Event{
{Data: map[string]any{"card": "1111"}},
{Data: map[string]any{"card": "2222"}},
{Data: map[string]any{"card": "3333"}},
},
want: 1.0, // 3 unique / 3 total = 1.0
},
{
name: "all same value",
events: []gofeat.Event{
{Data: map[string]any{"card": "1111"}},
{Data: map[string]any{"card": "1111"}},
{Data: map[string]any{"card": "1111"}},
},
want: 0.33, // 1 unique / 3 total ≈ 0.33
},
{
name: "half unique",
events: []gofeat.Event{
{Data: map[string]any{"email": "a@x.com"}},
{Data: map[string]any{"email": "b@x.com"}},
{Data: map[string]any{"email": "a@x.com"}},
{Data: map[string]any{"email": "b@x.com"}},
},
want: 0.5, // 2 unique / 4 total = 0.5
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
field := "card"
if tt.name == "half unique" {
field = "email"
}
agg := gofeat.UniqueRatio(field)()
for _, e := range tt.events {
agg.Add(e)
}
result := agg.Result().(float64)
if math.Abs(result-tt.want) > 0.01 {
t.Errorf("unique ratio: got %.2f, want %.2f", result, tt.want)
}
})
}
}
func TestTimeSinceFirst(t *testing.T) {
now := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
tests := []struct {
name string
events []gofeat.Event
want time.Duration
}{
{
name: "single event",
events: []gofeat.Event{
{Timestamp: now},
},
want: 0,
},
{
name: "events 1 hour apart",
events: []gofeat.Event{
{Timestamp: now},
{Timestamp: now.Add(1 * time.Hour)},
},
want: 1 * time.Hour,
},
{
name: "events out of order",
events: []gofeat.Event{
{Timestamp: now.Add(1 * time.Hour)},
{Timestamp: now}, // first event
{Timestamp: now.Add(2 * time.Hour)},
},
want: 2 * time.Hour, // from earliest to latest
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
agg := gofeat.TimeSinceFirst()()
for _, e := range tt.events {
agg.Add(e)
}
result := agg.Result().(time.Duration)
if result != tt.want {
t.Errorf("time since first: got %v, want %v", result, tt.want)
}
})
}
}
func TestPercentile(t *testing.T) {
events := []gofeat.Event{
{Data: map[string]any{"amount": 10.0}},
{Data: map[string]any{"amount": 20.0}},
{Data: map[string]any{"amount": 30.0}},
{Data: map[string]any{"amount": 40.0}},
{Data: map[string]any{"amount": 50.0}},
{Data: map[string]any{"amount": 60.0}},
{Data: map[string]any{"amount": 70.0}},
{Data: map[string]any{"amount": 80.0}},
{Data: map[string]any{"amount": 90.0}},
{Data: map[string]any{"amount": 100.0}},
}
tests := []struct {
name string
p float64
want float64
}{
{"p25", 0.25, 30.0},
{"p50", 0.5, 50.0},
{"p75", 0.75, 70.0},
{"p95", 0.95, 90.0},
{"p99", 0.99, 90.0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
agg := gofeat.Percentile("amount", tt.p)()
for _, e := range events {
agg.Add(e)
}
result := agg.Result().(float64)
if math.Abs(result-tt.want) > 0.01 {
t.Errorf("percentile %.2f: got %.2f, want %.2f", tt.p, result, tt.want)
}
})
}
}
func TestStandardDeviation(t *testing.T) {
tests := []struct {
name string
events []gofeat.Event
want float64
}{
{
name: "all same values",
events: []gofeat.Event{
{Data: map[string]any{"amount": 100.0}},
{Data: map[string]any{"amount": 100.0}},
{Data: map[string]any{"amount": 100.0}},
},
want: 0.0,
},
{
name: "simple sequence",
events: []gofeat.Event{
{Data: map[string]any{"amount": 10.0}},
{Data: map[string]any{"amount": 20.0}},
{Data: map[string]any{"amount": 30.0}},
},
want: 8.16, // std dev ≈ 8.16
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
agg := gofeat.StandardDeviation("amount")()
for _, e := range tt.events {
agg.Add(e)
}
result := agg.Result().(float64)
if math.Abs(result-tt.want) > 0.5 {
t.Errorf("std dev: got %.2f, want %.2f", result, tt.want)
}
})
}
}
func TestMean(t *testing.T) {
tests := []struct {
name string
events []gofeat.Event
want float64
}{
{
name: "simple average",
events: []gofeat.Event{
{Data: map[string]any{"amount": 100.0}},
{Data: map[string]any{"amount": 200.0}},
{Data: map[string]any{"amount": 300.0}},
},
want: 200.0,
},
{
name: "single value",
events: []gofeat.Event{
{Data: map[string]any{"amount": 42.0}},
},
want: 42.0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
agg := gofeat.Mean("amount")()
for _, e := range tt.events {
agg.Add(e)
}
result := agg.Result().(float64)
if math.Abs(result-tt.want) > 0.01 {
t.Errorf("mean: got %.2f, want %.2f", result, tt.want)
}
})
}
}
// Real fraud detection scenarios
func TestFraudScenario_CardTesting(t *testing.T) {
// Card testing attack: 15 small transactions in 5 minutes
now := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
events := make([]gofeat.Event, 0, 15)
for i := range 15 {
events = append(events, gofeat.Event{
Timestamp: now.Add(time.Duration(i) * 20 * time.Second), // Every 20 seconds
Data: map[string]any{
"amount": 2.0 + float64(i)*0.5, // Small amounts $2-9
"card": "card_" + string(rune('0'+i)), // Different cards
},
})
}
// Test velocity
velocityAgg := gofeat.Velocity(time.Hour)()
for _, e := range events {
velocityAgg.Add(e)
}
velocity := velocityAgg.Result().(float64)
// 15 transactions over ~5 minutes = 3 tx/min
if velocity < 2.5 || velocity > 3.5 {
t.Errorf("velocity: got %.2f, want ~3.0 tx/min", velocity)
}
// Test unique ratio for cards
ratioAgg := gofeat.UniqueRatio("card")()
for _, e := range events {
ratioAgg.Add(e)
}
ratio := ratioAgg.Result().(float64)
// All different cards = 1.0 ratio (very suspicious)
if ratio < 0.95 {
t.Errorf("unique ratio: got %.2f, want ~1.0 (all unique cards)", ratio)
}
// Test mean amount
meanAgg := gofeat.Mean("amount")()
for _, e := range events {
meanAgg.Add(e)
}
mean := meanAgg.Result().(float64)
// Small average amount
if mean > 10.0 {
t.Errorf("mean amount: got %.2f, want <$10 (card testing amounts)", mean)
}
// VERDICT: velocity > 3 AND unique_ratio > 0.9 AND mean < 10 = CARD TESTING
isCardTesting := velocity > 3.0 && ratio > 0.9 && mean < 10.0
if !isCardTesting {
t.Error("failed to detect card testing pattern")
}
}
func TestFraudScenario_LocationDiversity(t *testing.T) {
// Impossible travel: transactions from 5 different countries in 1 hour
now := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
countries := []string{"US", "UK", "CN", "BR", "AU"}
events := make([]gofeat.Event, 0, len(countries))
for i, country := range countries {
events = append(events, gofeat.Event{
Timestamp: now.Add(time.Duration(i) * 10 * time.Minute),
Data: map[string]any{
"country": country,
"amount": 100.0,
},
})
}
// Test entropy
entropyAgg := gofeat.Entropy("country")()
for _, e := range events {
entropyAgg.Add(e)
}
entropy := entropyAgg.Result().(float64)
// 5 equally distributed countries = high entropy
if entropy < 2.0 {
t.Errorf("entropy: got %.2f, want >2.0 (high diversity)", entropy)
}
// Test distinct count
distinctAgg := gofeat.DistinctCount("country")()
for _, e := range events {
distinctAgg.Add(e)
}
distinct := distinctAgg.Result().(int)
if distinct != 5 {
t.Errorf("distinct countries: got %d, want 5", distinct)
}
// VERDICT: distinct > 3 AND entropy > 2.0 = IMPOSSIBLE TRAVEL
isImpossibleTravel := distinct > 3 && entropy > 2.0
if !isImpossibleTravel {
t.Error("failed to detect impossible travel pattern")
}
}
func TestFraudScenario_NewAccountAbuse(t *testing.T) {
// New account immediately uses promo code
now := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
events := []gofeat.Event{
{Timestamp: now, Data: map[string]any{"amount": 50.0}}, // Account created + first tx
}
// Test account age
ageAgg := gofeat.TimeSinceFirst()()
for _, e := range events {
ageAgg.Add(e)
}
age := ageAgg.Result().(time.Duration)
// Brand new account
if age != 0 {
t.Errorf("account age: got %v, want 0 (new account)", age)
}
// VERDICT: age < 1 hour AND promo_used = true = PROMO ABUSE RISK
isPromoAbuseRisk := age < time.Hour
if !isPromoAbuseRisk {
t.Error("failed to detect new account pattern")
}
}