-
Notifications
You must be signed in to change notification settings - Fork 0
test: Add unit tests for DNS dispatcher logic #4
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
6 commits
Select commit
Hold shift + click to select a range
c3cd9aa
test: Add unit tests for DNS dispatcher logic
rm-hull 7cdd03b
feat: Make NewDNSDispatcher return an error
rm-hull 3d27e94
chore: Add .editorconfig
rm-hull 4b078e0
chore: Configure CI/CD and dependency updates
rm-hull 2a666ac
test: Expect NXDOMAIN for multi-question block
rm-hull 94c2da3
Merge branch 'main' into testing/integration
rm-hull 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
Some comments aren't visible on the classic Files Changed page.
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
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,161 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "net" | ||
| "testing" | ||
|
|
||
| "github.com/miekg/dns" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
| ) | ||
|
|
||
| // MockResponseWriter is a mock implementation of dns.ResponseWriter. | ||
| type MockResponseWriter struct { | ||
| mock.Mock | ||
| WrittenMsg *dns.Msg | ||
| } | ||
|
|
||
| func (m *MockResponseWriter) LocalAddr() net.Addr { | ||
| return &net.TCPAddr{ | ||
| IP: net.ParseIP("192.0.2.10"), | ||
| Port: 8080, | ||
| } | ||
| } | ||
|
|
||
| func (m *MockResponseWriter) RemoteAddr() net.Addr { | ||
| return &net.TCPAddr{ | ||
| IP: net.ParseIP("192.0.2.10"), | ||
| Port: 8080, | ||
| } | ||
| } | ||
|
|
||
| func (m *MockResponseWriter) WriteMsg(msg *dns.Msg) error { | ||
| m.WrittenMsg = msg | ||
| args := m.Called(msg) | ||
| return args.Error(0) | ||
| } | ||
|
|
||
| func (m *MockResponseWriter) Write(b []byte) (int, error) { | ||
| return len(b), nil | ||
| } | ||
|
|
||
| func (m *MockResponseWriter) Close() error { | ||
| return nil | ||
| } | ||
|
|
||
| func (m *MockResponseWriter) TsigStatus() error { | ||
| return nil | ||
| } | ||
|
|
||
| func (m *MockResponseWriter) TsigTimersOnly(b bool) { | ||
| } | ||
|
|
||
| func (m *MockResponseWriter) Hijack() { | ||
| } | ||
|
|
||
| var blockList = NewBlockList([]string{"ads.0xbt.net"}, 0.0001) | ||
| var upstream = "8.8.8.8:53" | ||
|
|
||
| func TestDNSDispatcher_HandleDNSRequest_Allowed(t *testing.T) { | ||
| dispatcher, err := NewDNSDispatcher(upstream, blockList, 100) | ||
| assert.NoError(t, err) | ||
|
|
||
| req := new(dns.Msg) | ||
| req.SetQuestion("google.com.", dns.TypeA) | ||
|
|
||
| writer := new(MockResponseWriter) | ||
| writer.On("WriteMsg", mock.Anything).Return(nil) | ||
|
|
||
| // Call the method under test | ||
| dispatcher.HandleDNSRequest(writer, req) | ||
|
|
||
| // Assert that the response writer was called with a non-nil message | ||
| assert.NotNil(t, writer.WrittenMsg) | ||
| assert.Equal(t, dns.RcodeSuccess, writer.WrittenMsg.Rcode) | ||
| } | ||
|
|
||
| func TestDNSDispatcher_HandleDNSRequest_Blocked(t *testing.T) { | ||
| dispatcher, err := NewDNSDispatcher(upstream, blockList, 100) | ||
| assert.NoError(t, err) | ||
|
|
||
| req := new(dns.Msg) | ||
| req.SetQuestion("ads.0xbt.net.", dns.TypeA) | ||
|
|
||
| writer := new(MockResponseWriter) | ||
| writer.On("WriteMsg", mock.Anything).Return(nil) | ||
|
|
||
| // Call the method under test | ||
| dispatcher.HandleDNSRequest(writer, req) | ||
|
|
||
| // Assert that the response has an NXDOMAIN Rcode | ||
| assert.NotNil(t, writer.WrittenMsg) | ||
| assert.Equal(t, dns.RcodeNameError, writer.WrittenMsg.Rcode) | ||
| } | ||
|
|
||
| func TestDNSDispatcher_HandleDNSRequest_MultipleQuestions(t *testing.T) { | ||
| dispatcher, err := NewDNSDispatcher(upstream, blockList, 100) | ||
| assert.NoError(t, err) | ||
|
|
||
| req := new(dns.Msg) | ||
| req.Question = []dns.Question{ | ||
| {Name: "google.com.", Qtype: dns.TypeA, Qclass: dns.ClassINET}, | ||
| {Name: "ads.0xbt.net.", Qtype: dns.TypeA, Qclass: dns.ClassINET}, | ||
| } | ||
|
|
||
| writer := new(MockResponseWriter) | ||
| writer.On("WriteMsg", mock.Anything).Return(nil) | ||
|
|
||
| // Call the method under test | ||
| dispatcher.HandleDNSRequest(writer, req) | ||
|
|
||
| // FIXME: this is the correct/expected behaviour .. to be fixed in #3 | ||
| // ================================================================== | ||
| // Assert that the response writer was called with a non-nil message | ||
| // assert.NotNil(t, writer.WrittenMsg) | ||
| // assert.Equal(t, dns.RcodeSuccess, writer.WrittenMsg.Rcode) | ||
| // assert.Len(t, writer.WrittenMsg.Answer, 1) | ||
| // assert.Len(t, writer.WrittenMsg.Question, 2) | ||
|
|
||
| // FIXME: current behaviour (to be removed) | ||
| // ================================================ | ||
| // Assert that the response has an NXDOMAIN Rcode because one of the questions is blocked | ||
| assert.NotNil(t, writer.WrittenMsg) | ||
| assert.Equal(t, dns.RcodeNameError, writer.WrittenMsg.Rcode) | ||
| assert.Len(t, writer.WrittenMsg.Answer, 0) | ||
| assert.Len(t, writer.WrittenMsg.Question, 1) | ||
| } | ||
|
|
||
| func TestDNSDispatcher_HandleDNSRequest_CacheHit(t *testing.T) { | ||
| dispatcher, err := NewDNSDispatcher(upstream, blockList, 100) | ||
| assert.NoError(t, err) | ||
|
|
||
| req := new(dns.Msg) | ||
| req.SetQuestion("example.com.", dns.TypeA) | ||
|
|
||
| writer := new(MockResponseWriter) | ||
| writer.On("WriteMsg", mock.Anything).Return(nil) | ||
|
|
||
| // First request: should be a cache miss and populate the cache | ||
| dispatcher.HandleDNSRequest(writer, req) | ||
| assert.NotNil(t, writer.WrittenMsg) | ||
| assert.Equal(t, dns.RcodeSuccess, writer.WrittenMsg.Rcode) | ||
|
|
||
| // Assert cache stats | ||
| stats := dispatcher.cache.Stat() | ||
| assert.Equal(t, 0, stats.Hits, "Expected 0 cache hit") | ||
| assert.Equal(t, 1, stats.Misses, "Expected 1 cache miss") | ||
|
|
||
| // Reset mock for the second request | ||
| writer = new(MockResponseWriter) | ||
| writer.On("WriteMsg", mock.Anything).Return(nil) | ||
|
|
||
| // Second request: should be a cache hit | ||
| dispatcher.HandleDNSRequest(writer, req) | ||
| assert.NotNil(t, writer.WrittenMsg) | ||
| assert.Equal(t, dns.RcodeSuccess, writer.WrittenMsg.Rcode) | ||
|
|
||
| // Assert cache stats | ||
| stats = dispatcher.cache.Stat() | ||
| assert.Equal(t, 1, stats.Hits, "Expected 1 cache hit") | ||
| assert.Equal(t, 1, stats.Misses, "Expected 1 cache miss") | ||
| } | ||
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.
Using global variables for test setup can introduce side effects between tests, especially if tests are run in parallel in the future (
t.Parallel()). It's a best practice to define these variables within each test function or a test-specific setup function to ensure complete test isolation.