Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: cleanup of PR 2998 to simplify #3051

Merged
merged 7 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libs/utils/address.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package utils

import (
"errors"
"fmt"
"net"
"strings"
)

var ErrInvalidIP = errors.New("invalid IP address or hostname given")

// SanitizeAddr trims leading protocol scheme and port from the given
// IP address or hostname if present.
func SanitizeAddr(addr string) (string, error) {
Expand All @@ -16,7 +19,7 @@ func SanitizeAddr(addr string) (string, error) {
addr = strings.TrimSuffix(addr, "/")
addr = strings.Split(addr, ":")[0]
if addr == "" {
return "", fmt.Errorf("invalid IP address or hostname given: %s", original)
return "", fmt.Errorf("%w: %s", ErrInvalidIP, original)
}
return addr, nil
}
Expand Down
5 changes: 4 additions & 1 deletion libs/utils/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func TestSanitizeAddr(t *testing.T) {
var tests = []struct {
addr string
want string
err error
}{
// Testcase: trims protocol prefix
{addr: "http://celestia.org", want: "celestia.org"},
Expand All @@ -20,13 +21,15 @@ func TestSanitizeAddr(t *testing.T) {
{addr: "tcp://192.168.42.42:5050/", want: "192.168.42.42"},
// Testcase: invariant ip
{addr: "192.168.42.42", want: "192.168.42.42"},
// Testcase: empty addr
{addr: "", want: "", err: ErrInvalidIP},
}

for _, tt := range tests {
t.Run(tt.addr, func(t *testing.T) {
got, err := SanitizeAddr(tt.addr)
require.NoError(t, err)
require.Equal(t, tt.want, got)
require.ErrorIs(t, err, tt.err)
})
}
}
Expand Down
Loading