-
Notifications
You must be signed in to change notification settings - Fork 4
fix: refactor relay URLs validation #1405
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
Open
sameh-farouk
wants to merge
4
commits into
development
Choose a base branch
from
development-refactor-relay-urls-validation
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0f869b3
fix: refactor relay URLs validation
sameh-farouk 6cfd642
normalize relay URL handling and add tests
sameh-farouk 1383de7
fix: non-adjacent duplicate hosts won’t be removed
sameh-farouk c59f919
test: Add non-adjacent duplicates test
sameh-farouk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package peer | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestValidateRelayURLs_DedupSort(t *testing.T) { | ||
inputs := []string{ | ||
"ws://Relay.Grid.tf", // same host different case, ws | ||
"wss://relay.grid.tf", // same host wss | ||
"wss://relay.grid.tf:443", // same host with default port -> ignored for dedup | ||
"ws://relay.grid.tf/some/path", // same host path -> ignored for dedup | ||
"wss://beta.grid.tf", // distinct host | ||
"http://not-allowed.example", // invalid scheme | ||
"://bad://url", // unparsable | ||
"wss://relay.dev.grid.tf:443", // another host | ||
"ws://relay.dev.grid.tf", // duplicate host, ws should be ignored in favor of wss entry | ||
} | ||
|
||
urls, err := validateRelayURLs(inputs) | ||
if err != nil { | ||
// should not error; we have valid entries | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
// Expect unique hostnames (lowercased) sorted: [beta.grid.tf relay.dev.grid.tf relay.grid.tf] | ||
if len(urls) != 3 { | ||
t.Fatalf("expected 3 unique hosts, got %d", len(urls)) | ||
} | ||
|
||
expectedHosts := []string{"beta.grid.tf", "relay.dev.grid.tf", "relay.grid.tf"} | ||
gotHosts := []string{urls[0].Hostname(), urls[1].Hostname(), urls[2].Hostname()} | ||
if !reflect.DeepEqual(expectedHosts, gotHosts) { | ||
t.Fatalf("hosts mismatch\nexpected: %v\n got: %v", expectedHosts, gotHosts) | ||
} | ||
} | ||
|
||
func TestValidateRelayURLs_DedupSort_NonAdjacent(t *testing.T) { | ||
inputs := []string{ | ||
"ws://Relay.Grid.tf", // same host different case, ws | ||
"wss://beta.grid.tf", // distinct host | ||
"http://not-allowed.example", // invalid scheme | ||
"://bad://url", // unparsable | ||
"wss://relay.dev.grid.tf:443", // another host | ||
"wss://relay.grid.tf", // same host wss | ||
"ws://relay.dev.grid.tf", // duplicate host, ws should be ignored in favor of wss entry | ||
"wss://relay.grid.tf:443", // same host with default port -> ignored for dedup | ||
"ws://relay.grid.tf/some/path", // same host path -> ignored for dedup | ||
} | ||
|
||
urls, err := validateRelayURLs(inputs) | ||
if err != nil { | ||
// should not error; we have valid entries | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
// Expect unique hostnames (lowercased) sorted: [beta.grid.tf relay.dev.grid.tf relay.grid.tf] | ||
if len(urls) != 3 { | ||
t.Fatalf("expected 3 unique hosts, got %d", len(urls)) | ||
} | ||
|
||
expectedHosts := []string{"beta.grid.tf", "relay.dev.grid.tf", "relay.grid.tf"} | ||
gotHosts := []string{urls[0].Hostname(), urls[1].Hostname(), urls[2].Hostname()} | ||
if !reflect.DeepEqual(expectedHosts, gotHosts) { | ||
t.Fatalf("hosts mismatch\nexpected: %v\n got: %v", expectedHosts, gotHosts) | ||
} | ||
} | ||
|
||
func TestValidateRelayURLs_AllInvalid(t *testing.T) { | ||
inputs := []string{"http://a", "ftp://b", "::::"} | ||
_, err := validateRelayURLs(inputs) | ||
if err == nil { | ||
t.Fatalf("expected error for no valid relay URLs") | ||
} | ||
if err != ErrNoValidRelayURLs { | ||
t.Fatalf("expected ErrNoValidRelayURLs, got %v", err) | ||
} | ||
} | ||
|
||
func TestGetRelayConnections_HostsNormalizedAndAligned(t *testing.T) { | ||
inputs := []string{ | ||
"ws://relay.grid.tf:443/path", // same host as below, different scheme/port/path | ||
"wss://relay.grid.tf", | ||
"wss://Relay.Dev.Grid.TF", // case-insensitive host | ||
} | ||
|
||
hosts, conns, err := getRelayConnections(inputs, nil, "sess", 42) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if len(hosts) != len(conns) { | ||
t.Fatalf("hosts and connections length mismatch: %d vs %d", len(hosts), len(conns)) | ||
} | ||
|
||
expectedHosts := []string{"relay.dev.grid.tf", "relay.grid.tf"} | ||
if !reflect.DeepEqual(expectedHosts, hosts) { | ||
t.Fatalf("normalized hosts mismatch\nexpected: %v\n got: %v", expectedHosts, hosts) | ||
} | ||
} | ||
|
||
func TestValidateRelayURLs_DeterministicSort(t *testing.T) { | ||
inputs := []string{ | ||
"wss://relay.grid.tf", | ||
"wss://relay.dev.grid.tf", | ||
"wss://beta.grid.tf", | ||
} | ||
|
||
urls, err := validateRelayURLs(inputs) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
expectedHosts := []string{"beta.grid.tf", "relay.dev.grid.tf", "relay.grid.tf"} | ||
gotHosts := []string{urls[0].Hostname(), urls[1].Hostname(), urls[2].Hostname()} | ||
if !reflect.DeepEqual(expectedHosts, gotHosts) { | ||
t.Fatalf("hosts mismatch\nexpected: %v\n got: %v", expectedHosts, gotHosts) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we only lowercase
Hostname
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is better imo since I convert to lower case once, hostname() will be inherently lowercase when I do sort or compact later