github.com/theQRL/go-zond@v0.1.1/p2p/server_nat_test.go (about) 1 // Copyright 2023 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 p2p 18 19 import ( 20 "net" 21 "sync/atomic" 22 "testing" 23 "time" 24 25 "github.com/theQRL/go-zond/common/mclock" 26 "github.com/theQRL/go-zond/internal/testlog" 27 "github.com/theQRL/go-zond/log" 28 ) 29 30 func TestServerPortMapping(t *testing.T) { 31 clock := new(mclock.Simulated) 32 mockNAT := &mockNAT{mappedPort: 30000} 33 srv := Server{ 34 Config: Config{ 35 PrivateKey: newkey(), 36 NoDial: true, 37 ListenAddr: ":0", 38 NAT: mockNAT, 39 Logger: testlog.Logger(t, log.LvlTrace), 40 clock: clock, 41 }, 42 } 43 err := srv.Start() 44 if err != nil { 45 t.Fatal(err) 46 } 47 defer srv.Stop() 48 49 // Wait for the port mapping to be registered. Synchronization with the port mapping 50 // goroutine works like this: For each iteration, we allow other goroutines to run and 51 // also advance the virtual clock by 1 second. Waiting stops when the NAT interface 52 // has received some requests, or when the clock reaches a timeout. 53 deadline := clock.Now().Add(portMapRefreshInterval) 54 for clock.Now() < deadline && mockNAT.mapRequests.Load() < 2 { 55 time.Sleep(10 * time.Millisecond) 56 clock.Run(1 * time.Second) 57 } 58 59 if mockNAT.ipRequests.Load() == 0 { 60 t.Fatal("external IP was never requested") 61 } 62 reqCount := mockNAT.mapRequests.Load() 63 if reqCount != 2 { 64 t.Error("wrong request count:", reqCount) 65 } 66 enr := srv.LocalNode().Node() 67 if enr.IP().String() != "192.0.2.0" { 68 t.Error("wrong IP in ENR:", enr.IP()) 69 } 70 if enr.TCP() != 30000 { 71 t.Error("wrong TCP port in ENR:", enr.TCP()) 72 } 73 if enr.UDP() != 30000 { 74 t.Error("wrong UDP port in ENR:", enr.UDP()) 75 } 76 } 77 78 type mockNAT struct { 79 mappedPort uint16 80 mapRequests atomic.Int32 81 unmapRequests atomic.Int32 82 ipRequests atomic.Int32 83 } 84 85 func (m *mockNAT) AddMapping(protocol string, extport, intport int, name string, lifetime time.Duration) (uint16, error) { 86 m.mapRequests.Add(1) 87 return m.mappedPort, nil 88 } 89 90 func (m *mockNAT) DeleteMapping(protocol string, extport, intport int) error { 91 m.unmapRequests.Add(1) 92 return nil 93 } 94 95 func (m *mockNAT) ExternalIP() (net.IP, error) { 96 m.ipRequests.Add(1) 97 return net.ParseIP("192.0.2.0"), nil 98 } 99 100 func (m *mockNAT) String() string { 101 return "mockNAT" 102 }