github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/addrmgr/knownaddress_test.go (about)

     1  // Copyright (c) 2013-2015 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package addrmgr_test
     7  
     8  import (
     9  	"math"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/dashpay/godash/addrmgr"
    14  	"github.com/dashpay/godash/wire"
    15  )
    16  
    17  func TestChance(t *testing.T) {
    18  	var tests = []struct {
    19  		addr     *addrmgr.KnownAddress
    20  		expected float64
    21  	}{
    22  		{
    23  			//Test normal case
    24  			addrmgr.TstNewKnownAddress(&wire.NetAddress{Timestamp: time.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.NetAddress{Timestamp: time.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.NetAddress{Timestamp: time.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.NetAddress{Timestamp: time.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.NetAddress{Timestamp: time.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  	future := time.Now().Add(35 * time.Minute)
    61  	monthOld := time.Now().Add(-43 * time.Hour * 24)
    62  	secondsOld := time.Now().Add(-2 * time.Second)
    63  	minutesOld := time.Now().Add(-27 * time.Minute)
    64  	hoursOld := time.Now().Add(-5 * time.Hour)
    65  	zeroTime, _ := time.Parse("Jan 1, 1970 at 0:00am (GMT)", "Jan 1, 1970 at 0:00am (GMT)")
    66  
    67  	futureNa := &wire.NetAddress{Timestamp: future}
    68  	minutesOldNa := &wire.NetAddress{Timestamp: minutesOld}
    69  	monthOldNa := &wire.NetAddress{Timestamp: monthOld}
    70  	currentNa := &wire.NetAddress{Timestamp: secondsOld}
    71  
    72  	//Test addresses that have been tried in the last minute.
    73  	if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(futureNa, 3, secondsOld, zeroTime, false, 0)) {
    74  		t.Errorf("test case 1: addresses that have been tried in the last minute are not bad.")
    75  	}
    76  	if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(monthOldNa, 3, secondsOld, zeroTime, false, 0)) {
    77  		t.Errorf("test case 2: addresses that have been tried in the last minute are not bad.")
    78  	}
    79  	if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(currentNa, 3, secondsOld, zeroTime, false, 0)) {
    80  		t.Errorf("test case 3: addresses that have been tried in the last minute are not bad.")
    81  	}
    82  	if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(currentNa, 3, secondsOld, monthOld, true, 0)) {
    83  		t.Errorf("test case 4: addresses that have been tried in the last minute are not bad.")
    84  	}
    85  	if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(currentNa, 2, secondsOld, secondsOld, true, 0)) {
    86  		t.Errorf("test case 5: addresses that have been tried in the last minute are not bad.")
    87  	}
    88  
    89  	//Test address that claims to be from the future.
    90  	if !addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(futureNa, 0, minutesOld, hoursOld, true, 0)) {
    91  		t.Errorf("test case 6: addresses that claim to be from the future are bad.")
    92  	}
    93  
    94  	//Test address that has not been seen in over a month.
    95  	if !addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(monthOldNa, 0, minutesOld, hoursOld, true, 0)) {
    96  		t.Errorf("test case 7: addresses more than a month old are bad.")
    97  	}
    98  
    99  	//It has failed at least three times and never succeeded.
   100  	if !addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(minutesOldNa, 3, minutesOld, zeroTime, true, 0)) {
   101  		t.Errorf("test case 8: addresses that have never succeeded are bad.")
   102  	}
   103  
   104  	//It has failed ten times in the last week
   105  	if !addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(minutesOldNa, 10, minutesOld, monthOld, true, 0)) {
   106  		t.Errorf("test case 9: addresses that have not succeeded in too long are bad.")
   107  	}
   108  
   109  	//Test an address that should work.
   110  	if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(minutesOldNa, 2, minutesOld, hoursOld, true, 0)) {
   111  		t.Errorf("test case 10: This should be a valid address.")
   112  	}
   113  }