From 9dab60faf7cda288624a11f545e7655c130d29a3 Mon Sep 17 00:00:00 2001 From: tianyeyouyou Date: Mon, 16 Sep 2024 10:34:53 +0800 Subject: [PATCH 1/3] test(p2p/addrutil): add unit tests --- p2p/netutil/addrutil_test.go | 191 +++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 p2p/netutil/addrutil_test.go diff --git a/p2p/netutil/addrutil_test.go b/p2p/netutil/addrutil_test.go new file mode 100644 index 000000000000..ceb663944209 --- /dev/null +++ b/p2p/netutil/addrutil_test.go @@ -0,0 +1,191 @@ +package netutil + +import ( + "math/rand" + "net" + "net/netip" + "path/filepath" + "testing" +) + +// customNetAddr is a custom implementation of net.Addr for testing purposes. +type customNetAddr struct{} + +func (c *customNetAddr) Network() string { return "custom" } +func (c *customNetAddr) String() string { return "custom" } + +func TestAddrAddr(t *testing.T) { + tempDir := t.TempDir() + tests := []struct { + name string + addr net.Addr + want netip.Addr + }{ + { + name: "IPAddr IPv4", + addr: &net.IPAddr{IP: net.ParseIP("192.0.2.1")}, + want: netip.MustParseAddr("192.0.2.1"), + }, + { + name: "IPAddr IPv6", + addr: &net.IPAddr{IP: net.ParseIP("2001:db8::1")}, + want: netip.MustParseAddr("2001:db8::1"), + }, + { + name: "TCPAddr IPv4", + addr: &net.TCPAddr{IP: net.ParseIP("192.0.2.1"), Port: 8080}, + want: netip.MustParseAddr("192.0.2.1"), + }, + { + name: "TCPAddr IPv6", + addr: &net.TCPAddr{IP: net.ParseIP("2001:db8::1"), Port: 8080}, + want: netip.MustParseAddr("2001:db8::1"), + }, + { + name: "UDPAddr IPv4", + addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.1"), Port: 8080}, + want: netip.MustParseAddr("192.0.2.1"), + }, + { + name: "UDPAddr IPv6", + addr: &net.UDPAddr{IP: net.ParseIP("2001:db8::1"), Port: 8080}, + want: netip.MustParseAddr("2001:db8::1"), + }, + { + name: "Unsupported Addr type", + addr: &net.UnixAddr{Name: filepath.Join(tempDir, "test.sock"), Net: "unix"}, + want: netip.Addr{}, + }, + { + name: "Nil input", + addr: nil, + want: netip.Addr{}, + }, + { + name: "Custom net.Addr implementation", + addr: &customNetAddr{}, + want: netip.Addr{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := AddrAddr(tt.addr); got != tt.want { + t.Errorf("AddrAddr() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestIPToAddr(t *testing.T) { + tests := []struct { + name string + ip net.IP + want netip.Addr + }{ + { + name: "IPv4", + ip: net.ParseIP("192.0.2.1"), + want: netip.MustParseAddr("192.0.2.1"), + }, + { + name: "IPv6", + ip: net.ParseIP("2001:db8::1"), + want: netip.MustParseAddr("2001:db8::1"), + }, + { + name: "Invalid IP", + ip: net.IP{1, 2, 3}, + want: netip.Addr{}, + }, + { + name: "Invalid IP (5 octets)", + ip: net.IP{192, 0, 2, 1, 1}, + want: netip.Addr{}, + }, + { + name: "IPv4-mapped IPv6", + ip: net.ParseIP("::ffff:192.0.2.1"), + want: netip.MustParseAddr("192.0.2.1"), + }, + { + name: "Nil input", + ip: nil, + want: netip.Addr{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := IPToAddr(tt.ip); got != tt.want { + t.Errorf("IPToAddr() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestRandomAddr(t *testing.T) { + // Use a fixed seed for reproducibility + rng := rand.New(rand.NewSource(42)) + + // Test IPv4 generation + t.Run("IPv4", func(t *testing.T) { + addr := RandomAddr(rng, true) + if !addr.Is4() { + t.Errorf("Expected IPv4 address, got %v", addr) + } + }) + + // Test IPv6 generation + t.Run("IPv6", func(t *testing.T) { + addr := RandomAddr(rng, false) + if !addr.Is6() { + t.Errorf("Expected IPv6 address, got %v", addr) + } + }) + + // Test random choice between IPv4 and IPv6 + t.Run("Random choice", func(t *testing.T) { + ipv4Count := 0 + ipv6Count := 0 + iterations := 1000 + + for i := 0; i < iterations; i++ { + addr := RandomAddr(rng, false) + if addr.Is4() { + ipv4Count++ + } else if addr.Is6() { + ipv6Count++ + } else { + t.Errorf("Invalid address generated: %v", addr) + } + } + + if ipv4Count == 0 || ipv6Count == 0 { + t.Errorf("Expected mix of IPv4 and IPv6 addresses, got %d IPv4 and %d IPv6", ipv4Count, ipv6Count) + } + }) + + // Test randomness of generated addresses + t.Run("Randomness check", func(t *testing.T) { + addresses := make(map[string]bool) + for i := 0; i < 1000; i++ { + addr := RandomAddr(rng, false) + addresses[addr.String()] = true + } + if len(addresses) < 990 { + t.Errorf("Expected close to 1000 unique addresses, got %d", len(addresses)) + } + }) + + // Test different seeds produce different results + t.Run("Different seeds", func(t *testing.T) { + rng1 := rand.New(rand.NewSource(42)) + rng2 := rand.New(rand.NewSource(43)) + addr1 := RandomAddr(rng1, false) + addr2 := RandomAddr(rng2, false) + if addr1 == addr2 { + t.Errorf("Expected different addresses for different seeds, got %v and %v", addr1, addr2) + } + }) +} From 93cf6964c85e2a48a43dc2d103547ac018e4f0bc Mon Sep 17 00:00:00 2001 From: tianyeyouyou Date: Sat, 9 Nov 2024 23:45:51 +0800 Subject: [PATCH 2/3] add license info and remove `TestRandomAddr` --- p2p/netutil/addrutil_test.go | 82 +++++++----------------------------- 1 file changed, 16 insertions(+), 66 deletions(-) diff --git a/p2p/netutil/addrutil_test.go b/p2p/netutil/addrutil_test.go index ceb663944209..b320a02bfc98 100644 --- a/p2p/netutil/addrutil_test.go +++ b/p2p/netutil/addrutil_test.go @@ -1,3 +1,19 @@ +// Copyright 2024 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + package netutil import ( @@ -123,69 +139,3 @@ func TestIPToAddr(t *testing.T) { }) } } - -func TestRandomAddr(t *testing.T) { - // Use a fixed seed for reproducibility - rng := rand.New(rand.NewSource(42)) - - // Test IPv4 generation - t.Run("IPv4", func(t *testing.T) { - addr := RandomAddr(rng, true) - if !addr.Is4() { - t.Errorf("Expected IPv4 address, got %v", addr) - } - }) - - // Test IPv6 generation - t.Run("IPv6", func(t *testing.T) { - addr := RandomAddr(rng, false) - if !addr.Is6() { - t.Errorf("Expected IPv6 address, got %v", addr) - } - }) - - // Test random choice between IPv4 and IPv6 - t.Run("Random choice", func(t *testing.T) { - ipv4Count := 0 - ipv6Count := 0 - iterations := 1000 - - for i := 0; i < iterations; i++ { - addr := RandomAddr(rng, false) - if addr.Is4() { - ipv4Count++ - } else if addr.Is6() { - ipv6Count++ - } else { - t.Errorf("Invalid address generated: %v", addr) - } - } - - if ipv4Count == 0 || ipv6Count == 0 { - t.Errorf("Expected mix of IPv4 and IPv6 addresses, got %d IPv4 and %d IPv6", ipv4Count, ipv6Count) - } - }) - - // Test randomness of generated addresses - t.Run("Randomness check", func(t *testing.T) { - addresses := make(map[string]bool) - for i := 0; i < 1000; i++ { - addr := RandomAddr(rng, false) - addresses[addr.String()] = true - } - if len(addresses) < 990 { - t.Errorf("Expected close to 1000 unique addresses, got %d", len(addresses)) - } - }) - - // Test different seeds produce different results - t.Run("Different seeds", func(t *testing.T) { - rng1 := rand.New(rand.NewSource(42)) - rng2 := rand.New(rand.NewSource(43)) - addr1 := RandomAddr(rng1, false) - addr2 := RandomAddr(rng2, false) - if addr1 == addr2 { - t.Errorf("Expected different addresses for different seeds, got %v and %v", addr1, addr2) - } - }) -} From 9c99c1f79e1068e8623b1819bc9a55111390457f Mon Sep 17 00:00:00 2001 From: Martin HS Date: Sat, 9 Nov 2024 20:31:56 +0100 Subject: [PATCH 3/3] Update p2p/netutil/addrutil_test.go --- p2p/netutil/addrutil_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/p2p/netutil/addrutil_test.go b/p2p/netutil/addrutil_test.go index b320a02bfc98..0abbabb54b79 100644 --- a/p2p/netutil/addrutil_test.go +++ b/p2p/netutil/addrutil_test.go @@ -17,7 +17,6 @@ package netutil import ( - "math/rand" "net" "net/netip" "path/filepath"