1// Copyright (C) 2019 Storj Labs, Inc.
2// See LICENSE for copying information.
3
4package trust_test
5
6import (
7	"testing"
8
9	"github.com/stretchr/testify/require"
10
11	"storj.io/common/storj"
12	"storj.io/storj/storagenode/trust"
13)
14
15func TestSatelliteURLAddress(t *testing.T) {
16	satelliteURL, err := trust.ParseSatelliteURL("121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6@127.0.0.1:7777")
17	require.NoError(t, err)
18
19	require.Equal(t, "127.0.0.1:7777", satelliteURL.Address())
20}
21
22func TestSatelliteURLString(t *testing.T) {
23	satelliteURL, err := trust.ParseSatelliteURL("121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6@127.0.0.1:7777")
24	require.NoError(t, err)
25
26	require.Equal(t, "121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6@127.0.0.1:7777", satelliteURL.String())
27}
28
29func TestSatelliteURLNodeURLConversion(t *testing.T) {
30	nodeURL, err := storj.ParseNodeURL("121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6@127.0.0.1:7777")
31	require.NoError(t, err)
32
33	satelliteURL, err := trust.ParseSatelliteURL("121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6@127.0.0.1:7777")
34	require.NoError(t, err)
35
36	require.Equal(t, nodeURL, satelliteURL.NodeURL())
37}
38
39func TestParseSatelliteURL_Valid(t *testing.T) {
40	id, err := storj.NodeIDFromString("121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6")
41	require.NoError(t, err)
42
43	for _, tt := range []struct {
44		name   string
45		url    string
46		parsed trust.SatelliteURL
47	}{
48		{
49			name: "success",
50			url:  "121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6@127.0.0.1:7777",
51			parsed: trust.SatelliteURL{
52				ID:   id,
53				Host: "127.0.0.1",
54				Port: 7777,
55			},
56		},
57		{
58			name: "success with storj schema",
59			url:  "storj://121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6@127.0.0.1:7777/",
60			parsed: trust.SatelliteURL{
61				ID:   id,
62				Host: "127.0.0.1",
63				Port: 7777,
64			},
65		},
66	} {
67		tt := tt // quiet linting
68		t.Run(tt.name, func(t *testing.T) {
69			u, err := trust.ParseSatelliteURL(tt.url)
70			require.NoError(t, err)
71			require.Equal(t, tt.parsed, u)
72		})
73	}
74
75}
76func TestParseSatelliteURL_Invalid(t *testing.T) {
77	for _, tt := range []struct {
78		name   string
79		url    string
80		parsed trust.SatelliteURL
81		errs   []string
82	}{
83		{
84			name: "not a valid URL",
85			url:  "://",
86			errs: []string{
87				`invalid satellite URL: node URL error: parse ://: missing protocol scheme`,
88				`invalid satellite URL: node URL error: parse "://": missing protocol scheme`,
89				`invalid satellite URL: node URL: parse ://: missing protocol scheme`,
90				`invalid satellite URL: node URL: parse "://": missing protocol scheme`,
91			},
92		},
93		{
94			name: "missing ID",
95			url:  "127.0.0.1:7777",
96			errs: []string{"invalid satellite URL: must contain an ID"},
97		},
98		{
99			name: "short ID",
100			url:  "121RTSDpy@127.0.0.1:7777",
101			errs: []string{
102				"invalid satellite URL: node URL error: node ID error: checksum error",
103				"invalid satellite URL: node URL: node ID: checksum error",
104			},
105		},
106		{
107			name: "missing host:port",
108			url:  "121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6@",
109			errs: []string{
110				"invalid satellite URL: must specify the host:port",
111				"invalid satellite URL: must specify the host:port",
112			},
113		},
114		{
115			name: "missing port",
116			url:  "121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6@127.0.0.1",
117			errs: []string{"invalid satellite URL: must specify the port"},
118		},
119	} {
120		tt := tt // quiet linting
121		t.Run(tt.name, func(t *testing.T) {
122			_, err := trust.ParseSatelliteURL(tt.url)
123			require.Error(t, err)
124			require.Contains(t, tt.errs, err.Error())
125		})
126	}
127
128}
129