summaryrefslogtreecommitdiff
path: root/network/addr_test.go
blob: 377fe72a910e8b48e9931f39d4c0f1b0b5f4b82e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package network

import (
	"net/url"
	"testing"

	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/stretchr/testify/require"
)

type urlCase struct {
	text string
	out  url.URL
}

func TestParseHostOrURL(t *testing.T) {
	partitiontest.PartitionTest(t)
	urlTestCases := []urlCase{
		{"localhost:123", url.URL{Scheme: "http", Host: "localhost:123"}},
		{"http://localhost:123", url.URL{Scheme: "http", Host: "localhost:123"}},
		{"ws://localhost:9999", url.URL{Scheme: "ws", Host: "localhost:9999"}},
		{"wss://localhost:443", url.URL{Scheme: "wss", Host: "localhost:443"}},
		{"https://localhost:123", url.URL{Scheme: "https", Host: "localhost:123"}},
		{"https://somewhere.tld", url.URL{Scheme: "https", Host: "somewhere.tld"}},
		{"http://127.0.0.1:123", url.URL{Scheme: "http", Host: "127.0.0.1:123"}},
		{"//somewhere.tld", url.URL{Scheme: "", Host: "somewhere.tld"}},
		{"//somewhere.tld:4601", url.URL{Scheme: "", Host: "somewhere.tld:4601"}},
		{"http://[::]:123", url.URL{Scheme: "http", Host: "[::]:123"}},
		{"1.2.3.4:123", url.URL{Scheme: "http", Host: "1.2.3.4:123"}},
		{"[::]:123", url.URL{Scheme: "http", Host: "[::]:123"}},
		{"r2-devnet.devnet.algodev.network:4560", url.URL{Scheme: "http", Host: "r2-devnet.devnet.algodev.network:4560"}},
		{"::11.22.33.44:123", url.URL{Scheme: "http", Host: "::11.22.33.44:123"}},
	}
	badUrls := []string{
		"justahost",
		"localhost:WAT",
		"http://localhost:WAT",
		"https://localhost:WAT",
		"ws://localhost:WAT",
		"wss://localhost:WAT",
		"//localhost:WAT",
		"://badaddress", // See rpcs/blockService_test.go TestRedirectFallbackEndpoints
		"://localhost:1234",
		":xxx",
		":xxx:1234",
		"::11.22.33.44",
		":a:1",
		":a:",
		":1",
		":a",
		":",
		"",
	}
	for _, tc := range urlTestCases {
		t.Run(tc.text, func(t *testing.T) {
			v, err := ParseHostOrURL(tc.text)
			require.NoError(t, err)
			if tc.out != *v {
				t.Errorf("url wanted %#v, got %#v", tc.out, v)
				return
			}
		})
		t.Run(tc.text+"-multiaddr", func(t *testing.T) {
			v, err := ParseHostOrURLOrMultiaddr(tc.text)
			require.NoError(t, err)
			if tc.out.Host != v {
				t.Errorf("url wanted %#v, got %#v", tc.text, v)
				return
			}
		})
	}
	for _, addr := range badUrls {
		t.Run(addr, func(t *testing.T) {
			_, err := ParseHostOrURL(addr)
			require.Error(t, err, "url should fail", addr)
		})
		t.Run(addr+"-multiaddr", func(t *testing.T) {
			_, err := ParseHostOrURLOrMultiaddr(addr)
			require.Error(t, err, "url should fail", addr)
		})
	}

}

func TestParseHostURLOrMultiaddr(t *testing.T) {
	partitiontest.PartitionTest(t)

	validMultiAddrs := []string{
		"/ip4/127.0.0.1/tcp/8080",
		"/ip6/::1/tcp/8080",
		"/ip4/192.168.1.1/udp/9999/quic",
		"/ip4/192.168.1.1/tcp/8180/p2p/Qmewz5ZHN1AAGTarRbMupNPbZRfg3p5jUGoJ3JYEatJVVk",
		"/ip4/192.255.2.8/tcp/8180/ws",
	}

	badMultiAddrs := []string{
		"/ip4/256.256.256.256/tcp/8080", // Invalid IPv4 address.
		"/ip4/127.0.0.1/abc/8080",       // abc is not a valid protocol.
		"/ip4/127.0.0.1/tcp/abc",        // Port is not a valid number.
		"/unix",                         // Unix protocol without a path is invalid.
		"/ip4/127.0.0.1/tcp",            // Missing a port after tcp
		"/p2p/invalidPeerID",            // Invalid peer ID after p2p.
		"ip4/127.0.0.1/tcp/8080",        // Missing starting /.
	}

	for _, addr := range validMultiAddrs {
		t.Run(addr, func(t *testing.T) {
			v, err := ParseHostOrURLOrMultiaddr(addr)
			require.NoError(t, err)
			require.Equal(t, addr, v)
		})
	}

	for _, addr := range badMultiAddrs {
		t.Run(addr, func(t *testing.T) {
			_, err := ParseHostOrURLOrMultiaddr(addr)
			require.Error(t, err)
		})
	}

}