github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/p2p/enode/localnode_test.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package enode
    19  
    20  import (
    21  	"math/rand"
    22  	"net"
    23  	"testing"
    24  
    25  	"github.com/AigarNetwork/aigar/crypto"
    26  	"github.com/AigarNetwork/aigar/p2p/enr"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func newLocalNodeForTesting() (*LocalNode, *DB) {
    31  	db, _ := OpenDB("")
    32  	key, _ := crypto.GenerateKey()
    33  	return NewLocalNode(db, key), db
    34  }
    35  
    36  func TestLocalNode(t *testing.T) {
    37  	ln, db := newLocalNodeForTesting()
    38  	defer db.Close()
    39  
    40  	if ln.Node().ID() != ln.ID() {
    41  		t.Fatal("inconsistent ID")
    42  	}
    43  
    44  	ln.Set(enr.WithEntry("x", uint(3)))
    45  	var x uint
    46  	if err := ln.Node().Load(enr.WithEntry("x", &x)); err != nil {
    47  		t.Fatal("can't load entry 'x':", err)
    48  	} else if x != 3 {
    49  		t.Fatal("wrong value for entry 'x':", x)
    50  	}
    51  }
    52  
    53  func TestLocalNodeSeqPersist(t *testing.T) {
    54  	ln, db := newLocalNodeForTesting()
    55  	defer db.Close()
    56  
    57  	if s := ln.Node().Seq(); s != 1 {
    58  		t.Fatalf("wrong initial seq %d, want 1", s)
    59  	}
    60  	ln.Set(enr.WithEntry("x", uint(1)))
    61  	if s := ln.Node().Seq(); s != 2 {
    62  		t.Fatalf("wrong seq %d after set, want 2", s)
    63  	}
    64  
    65  	// Create a new instance, it should reload the sequence number.
    66  	// The number increases just after that because a new record is
    67  	// created without the "x" entry.
    68  	ln2 := NewLocalNode(db, ln.key)
    69  	if s := ln2.Node().Seq(); s != 3 {
    70  		t.Fatalf("wrong seq %d on new instance, want 3", s)
    71  	}
    72  
    73  	// Create a new instance with a different node key on the same database.
    74  	// This should reset the sequence number.
    75  	key, _ := crypto.GenerateKey()
    76  	ln3 := NewLocalNode(db, key)
    77  	if s := ln3.Node().Seq(); s != 1 {
    78  		t.Fatalf("wrong seq %d on instance with changed key, want 1", s)
    79  	}
    80  }
    81  
    82  // This test checks behavior of the endpoint predictor.
    83  func TestLocalNodeEndpoint(t *testing.T) {
    84  	var (
    85  		fallback  = &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: 80}
    86  		predicted = &net.UDPAddr{IP: net.IP{127, 0, 1, 2}, Port: 81}
    87  		staticIP  = net.IP{127, 0, 1, 2}
    88  	)
    89  	ln, db := newLocalNodeForTesting()
    90  	defer db.Close()
    91  
    92  	// Nothing is set initially.
    93  	assert.Equal(t, net.IP(nil), ln.Node().IP())
    94  	assert.Equal(t, 0, ln.Node().UDP())
    95  	assert.Equal(t, uint64(1), ln.Node().Seq())
    96  
    97  	// Set up fallback address.
    98  	ln.SetFallbackIP(fallback.IP)
    99  	ln.SetFallbackUDP(fallback.Port)
   100  	assert.Equal(t, fallback.IP, ln.Node().IP())
   101  	assert.Equal(t, fallback.Port, ln.Node().UDP())
   102  	assert.Equal(t, uint64(2), ln.Node().Seq())
   103  
   104  	// Add endpoint statements from random hosts.
   105  	for i := 0; i < iptrackMinStatements; i++ {
   106  		assert.Equal(t, fallback.IP, ln.Node().IP())
   107  		assert.Equal(t, fallback.Port, ln.Node().UDP())
   108  		assert.Equal(t, uint64(2), ln.Node().Seq())
   109  
   110  		from := &net.UDPAddr{IP: make(net.IP, 4), Port: 90}
   111  		rand.Read(from.IP)
   112  		ln.UDPEndpointStatement(from, predicted)
   113  	}
   114  	assert.Equal(t, predicted.IP, ln.Node().IP())
   115  	assert.Equal(t, predicted.Port, ln.Node().UDP())
   116  	assert.Equal(t, uint64(3), ln.Node().Seq())
   117  
   118  	// Static IP overrides prediction.
   119  	ln.SetStaticIP(staticIP)
   120  	assert.Equal(t, staticIP, ln.Node().IP())
   121  	assert.Equal(t, fallback.Port, ln.Node().UDP())
   122  	assert.Equal(t, uint64(4), ln.Node().Seq())
   123  }