github.com/noisysockets/noisysockets@v0.21.2-0.20240515114641-7f467e651c90/network_test.go (about) 1 // SPDX-License-Identifier: MPL-2.0 2 /* 3 * Copyright (C) 2024 The Noisy Sockets Authors. 4 * 5 * This Source Code Form is subject to the terms of the Mozilla Public 6 * License, v. 2.0. If a copy of the MPL was not distributed with this 7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 */ 9 10 package noisysockets 11 12 import ( 13 stdnet "net" 14 "net/netip" 15 "testing" 16 17 "github.com/miekg/dns" 18 "github.com/neilotoole/slogt" 19 latestconfig "github.com/noisysockets/noisysockets/config/v1alpha2" 20 "github.com/noisysockets/noisysockets/types" 21 "github.com/stretchr/testify/require" 22 ) 23 24 func TestNetwork(t *testing.T) { 25 logger := slogt.New(t) 26 27 sk, err := types.NewPrivateKey() 28 require.NoError(t, err) 29 30 net, err := OpenNetwork(logger, &latestconfig.Config{ 31 Peers: []latestconfig.PeerConfig{ 32 { 33 Name: "peer1", 34 IPs: []string{ 35 "10.0.0.1", 36 "fc00::1", 37 }, 38 PublicKey: sk.Public().String(), 39 }, 40 }, 41 }) 42 require.NoError(t, err) 43 t.Cleanup(func() { 44 require.NoError(t, net.Close()) 45 }) 46 47 net.(*NoisySocketsNetwork).resolver = &dummyDNSResolver{} 48 49 t.Run("LookupHost", func(t *testing.T) { 50 t.Run("IPv4 Address", func(t *testing.T) { 51 addrs, err := net.LookupHost("127.0.0.1") 52 require.NoError(t, err) 53 54 require.Len(t, addrs, 1) 55 require.Equal(t, "127.0.0.1", addrs[0]) 56 }) 57 58 t.Run("IPv6 Address", func(t *testing.T) { 59 addrs, err := net.LookupHost("::1") 60 require.NoError(t, err) 61 62 require.Len(t, addrs, 1) 63 require.Equal(t, "::1", addrs[0]) 64 }) 65 66 t.Run("Peer Name", func(t *testing.T) { 67 addrs, err := net.LookupHost("peer1") 68 require.NoError(t, err) 69 70 require.Len(t, addrs, 2) 71 require.Contains(t, addrs, "10.0.0.1") 72 require.Contains(t, addrs, "fc00::1") 73 74 // Fully qualified domain name. 75 addrs, err = net.LookupHost("peer1.") 76 require.NoError(t, err) 77 78 require.Len(t, addrs, 2) 79 }) 80 81 t.Run("Peer Name With Domain", func(t *testing.T) { 82 addrs, err := net.LookupHost("peer1.my.nzzy.net") 83 require.NoError(t, err) 84 85 require.Len(t, addrs, 2) 86 require.Contains(t, addrs, "10.0.0.1") 87 require.Contains(t, addrs, "fc00::1") 88 89 // Fully qualified domain name. 90 addrs, err = net.LookupHost("peer1.my.nzzy.net.") 91 require.NoError(t, err) 92 93 require.Len(t, addrs, 2) 94 }) 95 96 t.Run("Domain Name", func(t *testing.T) { 97 addrs, err := net.LookupHost("host.example.com") 98 require.NoError(t, err) 99 100 require.Len(t, addrs, 2) 101 102 require.Contains(t, addrs, "10.0.0.2") 103 require.Contains(t, addrs, "fc00::2") 104 }) 105 }) 106 } 107 108 type dummyDNSResolver struct{} 109 110 func (r *dummyDNSResolver) LookupHost(host string) ([]netip.Addr, error) { 111 if dns.Fqdn(host) == "host.example.com." { 112 return []netip.Addr{ 113 netip.MustParseAddr("10.0.0.2"), 114 netip.MustParseAddr("fc00::2"), 115 }, nil 116 } 117 118 return nil, &stdnet.DNSError{Err: "no such host", Name: host} 119 }