github.com/noisysockets/noisysockets@v0.21.2-0.20240515114641-7f467e651c90/networkutil/ip_version_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 networkutil_test
    11  
    12  import (
    13  	"net/netip"
    14  	"testing"
    15  
    16  	"github.com/noisysockets/noisysockets/networkutil"
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  func TestIPVersion(t *testing.T) {
    21  	t.Run("IPv4", func(t *testing.T) {
    22  		addrs := []netip.Addr{
    23  			netip.MustParseAddr("127.0.0.1"),
    24  		}
    25  
    26  		hasV4 := networkutil.HasIPv4(addrs)
    27  		require.True(t, hasV4)
    28  
    29  		hasV6 := networkutil.HasIPv6(addrs)
    30  		require.False(t, hasV6)
    31  	})
    32  
    33  	t.Run("IPv6", func(t *testing.T) {
    34  		addrs := []netip.Addr{
    35  			netip.MustParseAddr("::1"),
    36  		}
    37  
    38  		hasV4 := networkutil.HasIPv4(addrs)
    39  		require.False(t, hasV4)
    40  
    41  		hasV6 := networkutil.HasIPv6(addrs)
    42  		require.True(t, hasV6)
    43  	})
    44  
    45  	t.Run("Both", func(t *testing.T) {
    46  		addrs := []netip.Addr{
    47  			netip.MustParseAddr("127.0.0.1"),
    48  			netip.MustParseAddr("::1"),
    49  		}
    50  
    51  		hasV4 := networkutil.HasIPv4(addrs)
    52  		require.True(t, hasV4)
    53  
    54  		hasV6 := networkutil.HasIPv6(addrs)
    55  		require.True(t, hasV6)
    56  	})
    57  }