-
Notifications
You must be signed in to change notification settings - Fork 33
[P2P] test: URL conversion with mockdns #586
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
addc9b3
test: mock DNS resolution in url_conversion
bryanchriswhite 3405757
test: add getPeerIP success tests w/ mocked DNS resolution
bryanchriswhite 097148c
chore: add mockdns test dependency
bryanchriswhite 5440e33
chore: update changelog
bryanchriswhite 5fc7407
fix: defer closeDNSMock()
bryanchriswhite 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
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 |
|---|---|---|
|
|
@@ -2,24 +2,32 @@ package network | |
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "net" | ||
| "testing" | ||
|
|
||
| "github.com/foxcpp/go-mockdns" | ||
|
|
||
| "github.com/multiformats/go-multiaddr" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestPeerMultiAddrFromServiceURL_Success(t *testing.T) { | ||
| zones := map[string]mockdns.Zone{ | ||
| "www.google.com.": { | ||
| A: []string{"142.250.181.196"}, | ||
| }, | ||
| } | ||
| closeDNSMock := prepareDNSResolverMock(t, zones) | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| serviceURL string | ||
| expetedMultiaddrRegex string | ||
| name string | ||
| serviceURL string | ||
| expectedMultiaddr string | ||
| }{ | ||
| { | ||
| "fqdn", | ||
| "www.google.com:8080", | ||
| // These regexes match a superset of valid IPv4/6 address space; simplified for convenience. | ||
| `(/ip4/(\d{1,3}\.){3}\d{1,3}/tcp/8080)|(/ip6/([0-9a-f]{1,4}::?){1,7}[0-9a-f]{1,4}/tcp/8080)`, | ||
| "/ip4/142.250.181.196/tcp/8080", | ||
| }, | ||
| { | ||
| "IPv4", | ||
|
|
@@ -38,9 +46,10 @@ func TestPeerMultiAddrFromServiceURL_Success(t *testing.T) { | |
| actualMultiaddr, err := Libp2pMultiaddrFromServiceURL(testCase.serviceURL) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, actualMultiaddr) | ||
| require.Regexp(t, regexp.MustCompile(testCase.expetedMultiaddrRegex), actualMultiaddr.String()) | ||
| require.Equal(t, testCase.expectedMultiaddr, actualMultiaddr.String()) | ||
| }) | ||
| } | ||
| closeDNSMock() | ||
| } | ||
|
|
||
| const ( | ||
|
|
@@ -173,33 +182,134 @@ func TestServiceURLFromLibp2pMultiaddr_Error(t *testing.T) { | |
|
|
||
| // TECHDEBT: add helpers for crating and/or using a "test resolver" which can | ||
| // be switched with `net.DefaultResolver` and can return mocked responses. | ||
| func TestGetPeerIP_Success(t *testing.T) { | ||
| t.Skip("TODO: replace `net.DefaultResolver` with one which has a `Dial` function that returns a mocked `net.Conn` (see: https://pkg.go.dev/net#Resolver)") | ||
|
|
||
| //nolint:gocritic // commentedOutCode - Outlines the minimum requirements for disproving regression. | ||
| // testCases := []struct { | ||
| // name string | ||
| // hostname string | ||
| // // TECHDEBT: seed math/rand for predictable selection within mocked response. | ||
| // expectedIP net.IP | ||
| // }{ | ||
| // { | ||
| // "single A record", | ||
| // "single.A.example", | ||
| // }, | ||
| // { | ||
| // "single AAAA record", | ||
| // "single.AAAA.example", | ||
| // }, | ||
| // { | ||
| // "multiple A records", | ||
| // "multi.A.example", | ||
| // }, | ||
| // { | ||
| // "multiple AAAA records", | ||
| // "multi.AAAA.example", | ||
| // }, | ||
| // } | ||
| func TestGetPeerIP_SingleRecord_Success(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| hostname string | ||
| recordType dnsRecordType | ||
| expectedIP string | ||
| }{ | ||
| { | ||
| "A record", | ||
| "single.a.example", | ||
| aRecord, | ||
| "10.0.0.1", | ||
| }, | ||
| { | ||
| "AAAA record", | ||
| "single.aaaa.example", | ||
| quadARecord, | ||
| "fc00::1", | ||
| }, | ||
| } | ||
|
|
||
| // Setup mock DNS | ||
| zones := make(map[string]mockdns.Zone) | ||
| for _, testCase := range testCases { | ||
| // Fully qualified domain name | ||
| // (see: https://en.wikipedia.org/wiki/Fully_qualified_domain_name) | ||
| fqdn := fmt.Sprintf("%s.", testCase.hostname) | ||
|
|
||
| switch testCase.recordType { | ||
| case aRecord: | ||
| zones[fqdn] = mockdns.Zone{ | ||
| A: []string{testCase.expectedIP}, | ||
| } | ||
| case quadARecord: | ||
| zones[fqdn] = mockdns.Zone{ | ||
| AAAA: []string{testCase.expectedIP}, | ||
| } | ||
| } | ||
| } | ||
| closeDNSMock := prepareDNSResolverMock(t, zones) | ||
| defer closeDNSMock() | ||
|
|
||
| // Run tests | ||
| for _, testCase := range testCases { | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
|
|
||
| actualIP, err := getPeerIP(testCase.hostname) | ||
| require.NoError(t, err) | ||
| require.Equal(t, testCase.expectedIP, actualIP.String()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetPeerIP_MultipleRecord_Success(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| hostname string | ||
| recordType dnsRecordType | ||
| expectedIPs []string | ||
| }{ | ||
| { | ||
| "A records", | ||
| "multi.a.example", | ||
| aRecord, | ||
| []string{ | ||
| "10.0.0.2", | ||
| "10.0.0.3", | ||
| "10.0.0.4", | ||
| "10.0.0.5", | ||
| }, | ||
| }, | ||
| { | ||
| "AAAA records", | ||
| "multi.aaaa.example", | ||
| quadARecord, | ||
| []string{ | ||
| "fc00::1", | ||
| "fc00::2", | ||
| "fc00::3", | ||
| "fc00::4", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // Setup mock DNS | ||
| zones := make(map[string]mockdns.Zone) | ||
| for _, testCase := range testCases { | ||
| // Fully qualified domain name | ||
| // (see: https://en.wikipedia.org/wiki/Fully_qualified_domain_name) | ||
| fqdn := fmt.Sprintf("%s.", testCase.hostname) | ||
|
|
||
| switch testCase.recordType { | ||
| case aRecord: | ||
| zones[fqdn] = mockdns.Zone{ | ||
| A: testCase.expectedIPs, | ||
| } | ||
| case quadARecord: | ||
| zones[fqdn] = mockdns.Zone{ | ||
| AAAA: testCase.expectedIPs, | ||
| } | ||
| } | ||
| } | ||
| closeDNSMock := prepareDNSResolverMock(t, zones) | ||
| defer closeDNSMock() | ||
|
|
||
| // Run tests | ||
| for _, testCase := range testCases { | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| seenIPCounts := make(map[string]uint) | ||
| maxAttempts := len(testCase.expectedIPs) * 100 // arbitrary scalar | ||
| for i := 0; i < maxAttempts; i++ { | ||
| // Break if all IPs already seen | ||
| if len(seenIPCounts) == len(testCase.expectedIPs) { | ||
| break | ||
| } | ||
|
|
||
| actualIP, err := getPeerIP(testCase.hostname) | ||
| require.NoError(t, err) | ||
|
|
||
| seenIPCounts[actualIP.String()]++ | ||
| } | ||
| var seenIPs []string | ||
| for ip := range seenIPCounts { | ||
| seenIPs = append(seenIPs, ip) | ||
| } | ||
| require.ElementsMatchf(t, seenIPs, testCase.expectedIPs, "expected and seen IPs don't match") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetPeerIP_Error(t *testing.T) { | ||
|
|
@@ -212,3 +322,29 @@ func TestGetPeerIP_Error(t *testing.T) { | |
| require.ErrorContains(t, err, errResolvePeerIPMsg) | ||
| require.ErrorContains(t, err, hostname) | ||
| } | ||
|
|
||
| type dnsRecordType = string | ||
|
|
||
| const ( | ||
| aRecord dnsRecordType = "A" | ||
| quadARecord dnsRecordType = "AAAA" | ||
| ) | ||
|
|
||
| func prepareDNSResolverMock(t *testing.T, zones map[string]mockdns.Zone) (done func()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! 😍
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||
| srv, err := mockdns.NewServerWithLogger(zones, noopLogger{}, false) | ||
| require.NoError(t, err) | ||
|
|
||
| srv.PatchNet(net.DefaultResolver) | ||
| return func() { | ||
| _ = srv.Close() | ||
| mockdns.UnpatchNet(net.DefaultResolver) | ||
| } | ||
| } | ||
|
|
||
| // noopLogger implements go-mockdns's `mockdns.Logger` interface. | ||
| // The default logging behavior in mockdns is too noisy. | ||
| type noopLogger struct{} | ||
|
|
||
| func (nl noopLogger) Printf(format string, args ...interface{}) { | ||
| // noop | ||
| } | ||
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.
Should we remove this comment?