test(dnsserver): improve core/dnsserver test coverage (#7317)

Add comprehensive tests for multiple components including server blocks
inspection, configuration handling, DoH/DoQ writers, and server startup
functions. Increases overall test coverage from 27% to 38.4% with
particular focus on register.go, https.go, quic.go, and config.go.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
This commit is contained in:
Ville Vesilehto
2025-05-22 16:40:13 +03:00
committed by GitHub
parent 32ea433a29
commit a070d22bc3
5 changed files with 701 additions and 0 deletions

View File

@@ -88,3 +88,72 @@ func TestDoHWriter_Request(t *testing.T) {
})
}
}
func TestDoHWriter_Write(t *testing.T) {
tests := []struct {
name string
input []byte
wantErr bool
}{
{
name: "valid DNS message",
// A minimal valid DNS query message
input: []byte{
0x00, 0x01, /* ID */
0x01, 0x00, /* Flags: query, recursion desired */
0x00, 0x01, /* Questions: 1 */
0x00, 0x00, /* Answer RRs: 0 */
0x00, 0x00, /* Authority RRs: 0 */
0x00, 0x00, /* Additional RRs: 0 */
0x03, 'w', 'w', 'w',
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00, /* Null terminator for domain name */
0x00, 0x01, /* Type: A */
0x00, 0x01, /* Class: IN */
},
wantErr: false,
},
{
name: "empty message",
input: []byte{},
wantErr: true, // Expect an error because unpacking an empty message will fail
},
{
name: "invalid DNS message",
input: []byte{0x00, 0x01, 0x02}, // Truncated message
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &DoHWriter{}
n, err := d.Write(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("Write() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && n != len(tt.input) {
t.Errorf("Write() bytes written = %v, want %v", n, len(tt.input))
}
if !tt.wantErr && d.Msg == nil {
t.Errorf("Write() d.Msg is nil, expected a parsed message")
}
})
}
}
func TestDoHWriter_Close(t *testing.T) {
d := &DoHWriter{}
if err := d.Close(); err != nil {
t.Errorf("Close() error = %v, want nil", err)
}
}
func TestDoHWriter_TsigStatus(t *testing.T) {
d := &DoHWriter{}
if err := d.TsigStatus(); err != nil {
t.Errorf("TsigStatus() error = %v, want nil", err)
}
}