github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/config/application_config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestApplicationConfigurationEquals(t *testing.T) {
    11  	a := &ApplicationConfiguration{}
    12  	o := &ApplicationConfiguration{}
    13  	require.True(t, a.EqualsButServices(o))
    14  	require.True(t, o.EqualsButServices(a))
    15  	require.True(t, a.EqualsButServices(a))
    16  
    17  	cfg1, err := LoadFile(filepath.Join("..", "..", "config", "protocol.mainnet.yml"))
    18  	require.NoError(t, err)
    19  	cfg2, err := LoadFile(filepath.Join("..", "..", "config", "protocol.testnet.yml"))
    20  	require.NoError(t, err)
    21  	require.False(t, cfg1.ApplicationConfiguration.EqualsButServices(&cfg2.ApplicationConfiguration))
    22  }
    23  
    24  func TestGetAddresses(t *testing.T) {
    25  	type testcase struct {
    26  		cfg        *ApplicationConfiguration
    27  		expected   []AnnounceableAddress
    28  		shouldFail bool
    29  	}
    30  	addr1 := "1.2.3.4"
    31  	addr2 := "5.6.7.8"
    32  	v6Plain0 := "3731:54:65fe:2::a7"
    33  	v6Plain1 := "3731:54:65fe:2::1"
    34  	v6Plain2 := "3731:54:65fe::a:1"
    35  	v6Plain3 := "3731:54:65fe::1:1"
    36  	v6Plain4 := "3731:54:65fe:2::"
    37  	port1 := uint16(1)
    38  	port2 := uint16(2)
    39  	cases := []testcase{
    40  		// Multi-addresses checks.
    41  		{
    42  			cfg: &ApplicationConfiguration{
    43  				P2P: P2P{Addresses: []string{addr1}},
    44  			},
    45  			expected: []AnnounceableAddress{
    46  				{Address: addr1},
    47  			},
    48  		},
    49  		{
    50  			cfg: &ApplicationConfiguration{
    51  				P2P: P2P{Addresses: []string{":1"}},
    52  			},
    53  			expected: []AnnounceableAddress{
    54  				{Address: ":1"},
    55  			},
    56  		},
    57  		{
    58  			cfg: &ApplicationConfiguration{
    59  				P2P: P2P{Addresses: []string{"::1"}},
    60  			},
    61  			expected: []AnnounceableAddress{
    62  				{Address: ":", AnnouncedPort: port1},
    63  			},
    64  		},
    65  		{
    66  			cfg: &ApplicationConfiguration{
    67  				P2P: P2P{Addresses: []string{addr1 + ":1"}},
    68  			},
    69  			expected: []AnnounceableAddress{
    70  				{Address: addr1 + ":1"},
    71  			},
    72  		},
    73  		{
    74  			cfg: &ApplicationConfiguration{
    75  				P2P: P2P{Addresses: []string{addr1 + "::1"}},
    76  			},
    77  			expected: []AnnounceableAddress{
    78  				{Address: addr1 + ":", AnnouncedPort: port1},
    79  			},
    80  		},
    81  		{
    82  			cfg: &ApplicationConfiguration{
    83  				P2P: P2P{Addresses: []string{addr1 + ":1:2"}},
    84  			},
    85  			expected: []AnnounceableAddress{
    86  				{Address: addr1 + ":1", AnnouncedPort: port2},
    87  			},
    88  		},
    89  		{
    90  			cfg: &ApplicationConfiguration{
    91  				P2P: P2P{Addresses: []string{addr1 + ":1", addr2 + "::2"}},
    92  			},
    93  			expected: []AnnounceableAddress{
    94  				{Address: addr1 + ":1"},
    95  				{Address: addr2 + ":", AnnouncedPort: port2},
    96  			},
    97  		},
    98  		{
    99  			cfg: &ApplicationConfiguration{
   100  				P2P: P2P{Addresses: []string{v6Plain0, v6Plain1, v6Plain2, v6Plain3, v6Plain4}},
   101  			},
   102  			expected: []AnnounceableAddress{
   103  				{Address: v6Plain0},
   104  				{Address: v6Plain1},
   105  				{Address: v6Plain2},
   106  				{Address: v6Plain3},
   107  				{Address: v6Plain4},
   108  			},
   109  		},
   110  		{
   111  			cfg: &ApplicationConfiguration{
   112  				P2P: P2P{Addresses: []string{"[3731:54:65fe:2::]:123", "[3731:54:65fe:2::]:123:124"}},
   113  			},
   114  			expected: []AnnounceableAddress{
   115  				{Address: "[3731:54:65fe:2::]:123"},
   116  				{Address: "[3731:54:65fe:2::]:123", AnnouncedPort: 124},
   117  			},
   118  		},
   119  		{
   120  			cfg: &ApplicationConfiguration{
   121  				P2P: P2P{Addresses: []string{"127.0.0.1:QWER:123"}},
   122  			},
   123  			shouldFail: true,
   124  		},
   125  	}
   126  	for i, c := range cases {
   127  		actual, err := c.cfg.GetAddresses()
   128  		if c.shouldFail {
   129  			require.Error(t, err, i)
   130  		} else {
   131  			require.NoError(t, err)
   132  			require.Equal(t, c.expected, actual, i)
   133  		}
   134  	}
   135  }