github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/p2p/security/banscore_test.go (about) 1 package security 2 3 import ( 4 "math" 5 "testing" 6 "time" 7 ) 8 9 func TestInt(t *testing.T) { 10 var banScoreIntTests = []struct { 11 bs DynamicBanScore 12 timeLapse int64 13 wantValue uint32 14 }{ 15 {bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, timeLapse: 1, wantValue: 99}, 16 {bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, timeLapse: Lifetime, wantValue: 50}, 17 {bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, timeLapse: Lifetime + 1, wantValue: 50}, 18 {bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, timeLapse: -1, wantValue: 50}, 19 {bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: 0}, timeLapse: Lifetime + 1, wantValue: 0}, 20 {bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: math.MaxUint32}, timeLapse: 0, wantValue: math.MaxUint32}, 21 {bs: DynamicBanScore{lastUnix: 0, transient: math.MaxUint32, persistent: 0}, timeLapse: Lifetime + 1, wantValue: 0}, 22 {bs: DynamicBanScore{lastUnix: 0, transient: math.MaxUint32, persistent: 0}, timeLapse: 60, wantValue: math.MaxUint32 / 2}, 23 {bs: DynamicBanScore{lastUnix: 0, transient: math.MaxUint32, persistent: math.MaxUint32}, timeLapse: 0, wantValue: math.MaxUint32 - 1}, 24 } 25 26 for i, intTest := range banScoreIntTests { 27 rst := intTest.bs.int(time.Unix(intTest.timeLapse, 0)) 28 if rst != intTest.wantValue { 29 t.Fatal("test ban score int err.", "num:", i, "want:", intTest.wantValue, "got:", rst) 30 } 31 } 32 } 33 34 func TestIncrease(t *testing.T) { 35 var banScoreIncreaseTests = []struct { 36 bs DynamicBanScore 37 transientAdd uint32 38 persistentAdd uint32 39 timeLapse int64 40 wantValue uint32 41 }{ 42 {bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, transientAdd: 50, persistentAdd: 50, timeLapse: 1, wantValue: 199}, 43 {bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, transientAdd: 50, persistentAdd: 50, timeLapse: Lifetime, wantValue: 150}, 44 {bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, transientAdd: 50, persistentAdd: 50, timeLapse: Lifetime + 1, wantValue: 150}, 45 {bs: DynamicBanScore{lastUnix: 0, transient: 50, persistent: 50}, transientAdd: 50, persistentAdd: 50, timeLapse: -1, wantValue: 200}, 46 {bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: 0}, transientAdd: math.MaxUint32, persistentAdd: 0, timeLapse: 60, wantValue: math.MaxUint32}, 47 {bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: 0}, transientAdd: 0, persistentAdd: math.MaxUint32, timeLapse: 60, wantValue: math.MaxUint32}, 48 {bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: 0}, transientAdd: 0, persistentAdd: math.MaxUint32, timeLapse: Lifetime + 1, wantValue: math.MaxUint32}, 49 {bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: 0}, transientAdd: math.MaxUint32, persistentAdd: 0, timeLapse: Lifetime + 1, wantValue: math.MaxUint32}, 50 {bs: DynamicBanScore{lastUnix: 0, transient: math.MaxUint32, persistent: 0}, transientAdd: math.MaxUint32, persistentAdd: 0, timeLapse: Lifetime + 1, wantValue: math.MaxUint32}, 51 {bs: DynamicBanScore{lastUnix: 0, transient: math.MaxUint32, persistent: 0}, transientAdd: math.MaxUint32, persistentAdd: 0, timeLapse: 0, wantValue: math.MaxUint32 - 1}, 52 {bs: DynamicBanScore{lastUnix: 0, transient: 0, persistent: math.MaxUint32}, transientAdd: math.MaxUint32, persistentAdd: 0, timeLapse: Lifetime + 1, wantValue: math.MaxUint32 - 1}, 53 } 54 55 for i, incTest := range banScoreIncreaseTests { 56 rst := incTest.bs.increase(incTest.persistentAdd, incTest.transientAdd, time.Unix(incTest.timeLapse, 0)) 57 if rst != incTest.wantValue { 58 t.Fatal("test ban score int err.", "num:", i, "want:", incTest.wantValue, "got:", rst) 59 } 60 } 61 } 62 63 func TestReset(t *testing.T) { 64 var bs DynamicBanScore 65 if bs.Int() != 0 { 66 t.Errorf("Initial state is not zero.") 67 } 68 bs.Increase(100, 0) 69 r := bs.Int() 70 if r != 100 { 71 t.Errorf("Unexpected result %d after ban score increase.", r) 72 } 73 bs.Reset() 74 if bs.Int() != 0 { 75 t.Errorf("Failed to reset ban score.") 76 } 77 } 78 79 func TestString(t *testing.T) { 80 want := "persistent 100 + transient 0 at 0 = 100 as of now" 81 var bs DynamicBanScore 82 if bs.Int() != 0 { 83 t.Errorf("Initial state is not zero.") 84 } 85 86 bs.Increase(100, 0) 87 if bs.String() != want { 88 t.Fatal("DynamicBanScore String test error.") 89 } 90 }