github.com/titbtcqash/go-ethereum@v1.9.7/p2p/enode/localnode_test.go (about)

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