github.com/btcsuite/btcd@v0.24.0/addrmgr/knownaddress_test.go (about) 1 // Copyright (c) 2013-2015 The btcsuite developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package addrmgr_test 6 7 import ( 8 "math" 9 "testing" 10 "time" 11 12 "github.com/btcsuite/btcd/addrmgr" 13 "github.com/btcsuite/btcd/wire" 14 ) 15 16 func TestChance(t *testing.T) { 17 now := time.Unix(time.Now().Unix(), 0) 18 var tests = []struct { 19 addr *addrmgr.KnownAddress 20 expected float64 21 }{ 22 { 23 //Test normal case 24 addrmgr.TstNewKnownAddress(&wire.NetAddressV2{Timestamp: now.Add(-35 * time.Second)}, 25 0, time.Now().Add(-30*time.Minute), time.Now(), false, 0), 26 1.0, 27 }, { 28 //Test case in which lastseen < 0 29 addrmgr.TstNewKnownAddress(&wire.NetAddressV2{Timestamp: now.Add(20 * time.Second)}, 30 0, time.Now().Add(-30*time.Minute), time.Now(), false, 0), 31 1.0, 32 }, { 33 //Test case in which lastattempt < 0 34 addrmgr.TstNewKnownAddress(&wire.NetAddressV2{Timestamp: now.Add(-35 * time.Second)}, 35 0, time.Now().Add(30*time.Minute), time.Now(), false, 0), 36 1.0 * .01, 37 }, { 38 //Test case in which lastattempt < ten minutes 39 addrmgr.TstNewKnownAddress(&wire.NetAddressV2{Timestamp: now.Add(-35 * time.Second)}, 40 0, time.Now().Add(-5*time.Minute), time.Now(), false, 0), 41 1.0 * .01, 42 }, { 43 //Test case with several failed attempts. 44 addrmgr.TstNewKnownAddress(&wire.NetAddressV2{Timestamp: now.Add(-35 * time.Second)}, 45 2, time.Now().Add(-30*time.Minute), time.Now(), false, 0), 46 1 / 1.5 / 1.5, 47 }, 48 } 49 50 err := .0001 51 for i, test := range tests { 52 chance := addrmgr.TstKnownAddressChance(test.addr) 53 if math.Abs(test.expected-chance) >= err { 54 t.Errorf("case %d: got %f, expected %f", i, chance, test.expected) 55 } 56 } 57 } 58 59 func TestIsBad(t *testing.T) { 60 now := time.Unix(time.Now().Unix(), 0) 61 future := now.Add(35 * time.Minute) 62 monthOld := now.Add(-43 * time.Hour * 24) 63 secondsOld := now.Add(-2 * time.Second) 64 minutesOld := now.Add(-27 * time.Minute) 65 hoursOld := now.Add(-5 * time.Hour) 66 zeroTime := time.Time{} 67 68 futureNa := &wire.NetAddressV2{Timestamp: future} 69 minutesOldNa := &wire.NetAddressV2{Timestamp: minutesOld} 70 monthOldNa := &wire.NetAddressV2{Timestamp: monthOld} 71 currentNa := &wire.NetAddressV2{Timestamp: secondsOld} 72 73 //Test addresses that have been tried in the last minute. 74 if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(futureNa, 3, secondsOld, zeroTime, false, 0)) { 75 t.Errorf("test case 1: addresses that have been tried in the last minute are not bad.") 76 } 77 if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(monthOldNa, 3, secondsOld, zeroTime, false, 0)) { 78 t.Errorf("test case 2: addresses that have been tried in the last minute are not bad.") 79 } 80 if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(currentNa, 3, secondsOld, zeroTime, false, 0)) { 81 t.Errorf("test case 3: addresses that have been tried in the last minute are not bad.") 82 } 83 if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(currentNa, 3, secondsOld, monthOld, true, 0)) { 84 t.Errorf("test case 4: addresses that have been tried in the last minute are not bad.") 85 } 86 if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(currentNa, 2, secondsOld, secondsOld, true, 0)) { 87 t.Errorf("test case 5: addresses that have been tried in the last minute are not bad.") 88 } 89 90 //Test address that claims to be from the future. 91 if !addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(futureNa, 0, minutesOld, hoursOld, true, 0)) { 92 t.Errorf("test case 6: addresses that claim to be from the future are bad.") 93 } 94 95 //Test address that has not been seen in over a month. 96 if !addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(monthOldNa, 0, minutesOld, hoursOld, true, 0)) { 97 t.Errorf("test case 7: addresses more than a month old are bad.") 98 } 99 100 //It has failed at least three times and never succeeded. 101 if !addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(minutesOldNa, 3, minutesOld, zeroTime, true, 0)) { 102 t.Errorf("test case 8: addresses that have never succeeded are bad.") 103 } 104 105 //It has failed ten times in the last week 106 if !addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(minutesOldNa, 10, minutesOld, monthOld, true, 0)) { 107 t.Errorf("test case 9: addresses that have not succeeded in too long are bad.") 108 } 109 110 //Test an address that should work. 111 if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(minutesOldNa, 2, minutesOld, hoursOld, true, 0)) { 112 t.Errorf("test case 10: This should be a valid address.") 113 } 114 }