github.com/core-coin/go-core/v2@v2.1.9/p2p/enode/localnode_test.go (about)

     1  // Copyright 2018 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package enode
    18  
    19  import (
    20  	crand "crypto/rand"
    21  	"math/rand"
    22  	"net"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  
    27  	"github.com/core-coin/go-core/v2/crypto"
    28  	"github.com/core-coin/go-core/v2/p2p/enr"
    29  )
    30  
    31  func newLocalNodeForTesting() (*LocalNode, *DB) {
    32  	db, _ := OpenDB("")
    33  	key, _ := crypto.GenerateKey(crand.Reader)
    34  	return NewLocalNode(db, key), db
    35  }
    36  
    37  func TestLocalNode(t *testing.T) {
    38  	ln, db := newLocalNodeForTesting()
    39  	defer db.Close()
    40  
    41  	if ln.Node().ID() != ln.ID() {
    42  		t.Fatal("inconsistent ID")
    43  	}
    44  
    45  	ln.Set(enr.WithEntry("x", uint(3)))
    46  	var x uint
    47  	if err := ln.Node().Load(enr.WithEntry("x", &x)); err != nil {
    48  		t.Fatal("can't load entry 'x':", err)
    49  	} else if x != 3 {
    50  		t.Fatal("wrong value for entry 'x':", x)
    51  	}
    52  }
    53  
    54  func TestLocalNodeSeqPersist(t *testing.T) {
    55  	ln, db := newLocalNodeForTesting()
    56  	defer db.Close()
    57  
    58  	if s := ln.Node().Seq(); s != 1 {
    59  		t.Fatalf("wrong initial seq %d, want 1", s)
    60  	}
    61  	ln.Set(enr.WithEntry("x", uint(1)))
    62  	if s := ln.Node().Seq(); s != 2 {
    63  		t.Fatalf("wrong seq %d after set, want 2", s)
    64  	}
    65  
    66  	// Create a new instance, it should reload the sequence number.
    67  	// The number increases just after that because a new record is
    68  	// created without the "x" entry.
    69  	ln2 := NewLocalNode(db, ln.key)
    70  	if s := ln2.Node().Seq(); s != 3 {
    71  		t.Fatalf("wrong seq %d on new instance, want 3", s)
    72  	}
    73  
    74  	// Create a new instance with a different node key on the same database.
    75  	// This should reset the sequence number.
    76  	key, _ := crypto.GenerateKey(crand.Reader)
    77  	ln3 := NewLocalNode(db, key)
    78  	if s := ln3.Node().Seq(); s != 1 {
    79  		t.Fatalf("wrong seq %d on instance with changed key, want 1", s)
    80  	}
    81  }
    82  
    83  // This test checks behavior of the endpoint predictor.
    84  func TestLocalNodeEndpoint(t *testing.T) {
    85  	var (
    86  		fallback  = &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: 80}
    87  		predicted = &net.UDPAddr{IP: net.IP{127, 0, 1, 2}, Port: 81}
    88  		staticIP  = net.IP{127, 0, 1, 2}
    89  	)
    90  	ln, db := newLocalNodeForTesting()
    91  	defer db.Close()
    92  
    93  	// Nothing is set initially.
    94  	assert.Equal(t, net.IP(nil), ln.Node().IP())
    95  	assert.Equal(t, 0, ln.Node().UDP())
    96  	assert.Equal(t, uint64(1), ln.Node().Seq())
    97  
    98  	// Set up fallback address.
    99  	ln.SetFallbackIP(fallback.IP)
   100  	ln.SetFallbackUDP(fallback.Port)
   101  	assert.Equal(t, fallback.IP, ln.Node().IP())
   102  	assert.Equal(t, fallback.Port, ln.Node().UDP())
   103  	assert.Equal(t, uint64(2), ln.Node().Seq())
   104  
   105  	// Add endpoint statements from random hosts.
   106  	for i := 0; i < iptrackMinStatements; i++ {
   107  		assert.Equal(t, fallback.IP, ln.Node().IP())
   108  		assert.Equal(t, fallback.Port, ln.Node().UDP())
   109  		assert.Equal(t, uint64(2), ln.Node().Seq())
   110  
   111  		from := &net.UDPAddr{IP: make(net.IP, 4), Port: 90}
   112  		rand.Read(from.IP)
   113  		ln.UDPEndpointStatement(from, predicted)
   114  	}
   115  	assert.Equal(t, predicted.IP, ln.Node().IP())
   116  	assert.Equal(t, predicted.Port, ln.Node().UDP())
   117  	assert.Equal(t, uint64(3), ln.Node().Seq())
   118  
   119  	// Static IP overrides prediction.
   120  	ln.SetStaticIP(staticIP)
   121  	assert.Equal(t, staticIP, ln.Node().IP())
   122  	assert.Equal(t, fallback.Port, ln.Node().UDP())
   123  	assert.Equal(t, uint64(4), ln.Node().Seq())
   124  }