Skip to content

Commit

Permalink
server: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IrineSistiana committed Apr 18, 2021
1 parent 8b0e366 commit 4c4846b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 13 deletions.
77 changes: 68 additions & 9 deletions dispatcher/pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,50 @@ func TestTCPServer(t *testing.T) {
server *Server
}{
{
name: "tcp with listener",
server: NewServer("tcp", "", WithHandler(dnsHandler), WithListener(getListener(t))),
name: "tcp with listener",
server: NewServer(
"tcp",
"",
WithHandler(dnsHandler),
WithListener(getListener(t)),
),
},
{
name: "tcp with addr",
server: NewServer("tcp", "127.0.0.1:0", WithHandler(dnsHandler)),
name: "tcp with addr",
server: NewServer(
"tcp",
"127.0.0.1:0",
WithHandler(dnsHandler),
),
},
{
name: "dot with listener",
server: NewServer("dot", "", WithHandler(dnsHandler), WithListener(getListener(t)), WithTLSConfig(getTLSConfig(t))),
name: "dot with listener",
server: NewServer(
"dot",
"",
WithHandler(dnsHandler),
WithListener(getListener(t)),
WithTLSConfig(getTLSConfig(t)),
),
},
{
name: "dot with addr",
server: NewServer("dot", "127.0.0.1:0", WithHandler(dnsHandler), WithTLSConfig(getTLSConfig(t))),
name: "dot with cert and key",
server: NewServer(
"dot",
"",
WithHandler(dnsHandler),
WithListener(getListener(t)),
WithCertificate("./testdata/test.test.cert", "./testdata/test.test.key"),
),
},
{
name: "dot with addr",
server: NewServer(
"dot",
"127.0.0.1:0",
WithHandler(dnsHandler),
WithTLSConfig(getTLSConfig(t)),
),
},
}

Expand Down Expand Up @@ -201,7 +231,7 @@ func TestDoHServer(t *testing.T) {
server *Server
}{
{
name: "doh",
name: "doh with listener",
server: NewServer(
"doh",
"",
Expand All @@ -210,6 +240,35 @@ func TestDoHServer(t *testing.T) {
WithTLSConfig(getTLSConfig(t)),
),
},
{
name: "doh with address",
server: NewServer(
"doh",
"127.0.0.1:0",
WithHttpHandler(http_handler.NewHandler(dnsHandler, http_handler.WithPath("/dns-query"))),
WithTLSConfig(getTLSConfig(t)),
),
},
{
name: "doh with cert and key",
server: NewServer(
"doh",
"",
WithHttpHandler(http_handler.NewHandler(dnsHandler, http_handler.WithPath("/dns-query"))),
WithListener(getListener(t)),
WithCertificate("./testdata/test.test.cert", "./testdata/test.test.key"),
),
},
{
name: "doh without url path",
server: NewServer(
"doh",
"",
WithHttpHandler(http_handler.NewHandler(dnsHandler)),
WithListener(getListener(t)),
WithCertificate("./testdata/test.test.cert", "./testdata/test.test.key"),
),
},
// TODO: Add http test.
}

Expand Down
10 changes: 10 additions & 0 deletions dispatcher/pkg/server/testdata/test.test.cert
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-----BEGIN CERTIFICATE-----
MIIBcTCCARegAwIBAgIQDmAzIsMq/6e3slB6nTEK0TAKBggqhkjOPQQDAjAUMRIw
EAYDVQQDEwl0ZXN0LnRlc3QwHhcNMjEwNDE4MDQzNzMxWhcNMzEwNDE4MDQzNzMx
WjAUMRIwEAYDVQQDEwl0ZXN0LnRlc3QwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNC
AARhxRbCpNr2B6qk6gxnPpvScH/NAhpYD5PZYZHu15ZUjf1LWrEpfoyjsot0ltRk
jYhBu3NgyxKm62UmI0UC+moio0swSTAOBgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAww
CgYIKwYBBQUHAwEwDAYDVR0TAQH/BAIwADAUBgNVHREEDTALggl0ZXN0LnRlc3Qw
CgYIKoZIzj0EAwIDSAAwRQIhAL47WhwilIcDLy7BiQguiPXQeY7qSaBhKJ1KmQs1
FZZdAiBO+A2E60qm8snZJDuSyI0tz1CYiHEmAzpijNi/pCsZ9Q==
-----END CERTIFICATE-----
5 changes: 5 additions & 0 deletions dispatcher/pkg/server/testdata/test.test.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN EC PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgcr8+ki8M2LEnG+P8
bC9kbHg4zvNxoxw8w8+FNkmWC1KhRANCAARhxRbCpNr2B6qk6gxnPpvScH/NAhpY
D5PZYZHu15ZUjf1LWrEpfoyjsot0ltRkjYhBu3NgyxKm62UmI0UC+moi
-----END EC PRIVATE KEY-----
4 changes: 0 additions & 4 deletions dispatcher/pkg/server/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package server

import (
"context"
"errors"
"fmt"
"github.com/IrineSistiana/mosdns/dispatcher/handler"
"github.com/IrineSistiana/mosdns/dispatcher/pkg/dnsutils"
Expand Down Expand Up @@ -50,9 +49,6 @@ func (u *udpResponseWriter) Write(m *dns.Msg) (n int, err error) {

// startUDP always returns a non-nil error.
func (s *Server) startUDP() error {
if s.packetConn == nil {
return errors.New("udp server has a nil packet conn")
}
c := s.packetConn

listenerCtx, cancel := context.WithCancel(context.Background())
Expand Down

0 comments on commit 4c4846b

Please sign in to comment.