Skip to content
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

Add more target tests, add invalid keyID error code #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions target.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package main

import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -99,6 +100,10 @@ func (s *targetServer) resolveQueryWithResolver(q *dns.Msg, r resolver) ([]byte,

start := time.Now()
response, err := r.resolve(q)
if err != nil {
log.Println("Resolution failed: ", err)
return nil, err
}
elapsed := time.Since(start)

packedResponse, err := response.Pack()
Expand Down Expand Up @@ -176,6 +181,9 @@ func (s *targetServer) parseObliviousQueryFromRequest(r *http.Request) (odoh.Obl
func (s *targetServer) createObliviousResponseForQuery(context odoh.ResponseContext, dnsResponse []byte) (odoh.ObliviousDNSMessage, error) {
response := odoh.CreateObliviousDNSResponse(dnsResponse, 0)
odohResponse, err := context.EncryptResponse(response)
if err != nil {
return odoh.ObliviousDNSMessage{}, err
}

if s.verbose {
log.Printf("Encrypted response: %x", odohResponse)
Expand All @@ -200,6 +208,13 @@ func (s *targetServer) odohQueryHandler(w http.ResponseWriter, r *http.Request)
return
}

keyID := s.odohKeyPair.Config.Contents.KeyID()
receivedKeyID := odohMessage.KeyID
if !bytes.Equal(keyID, receivedKeyID) {
log.Println("received keyID is different from expected key ID")
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}

obliviousQuery, responseContext, err := s.odohKeyPair.DecryptQuery(odohMessage)
if err != nil {
log.Println("DecryptQuery failed:", err)
Expand Down
88 changes: 86 additions & 2 deletions target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

odoh "github.com/cloudflare/odoh-go"
"github.com/miekg/dns"
Expand Down Expand Up @@ -359,8 +360,8 @@ func TestQueryHandlerODoHWithInvalidKey(t *testing.T) {
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, request)

if status := rr.Result().StatusCode; status != http.StatusBadRequest {
t.Fatal(fmt.Errorf("Result did not yield %d, got %d instead", http.StatusBadRequest, status))
if status := rr.Result().StatusCode; status != http.StatusUnauthorized {
t.Fatal(fmt.Errorf("Result did not yield %d, got %d instead", http.StatusUnauthorized, status))
}
}

Expand Down Expand Up @@ -392,3 +393,86 @@ func TestQueryHandlerODoHWithCorruptCiphertext(t *testing.T) {
t.Fatal(fmt.Errorf("Result did not yield %d, got %d instead", http.StatusBadRequest, status))
}
}

func TestQueryHandlerODoHWithMalformedQuery(t *testing.T) {
r := createLocalResolver(t)
target := createTarget(t, r)

handler := http.HandlerFunc(target.targetQueryHandler)

// malformed odoh query
queryBytes := []byte{1, 2, 3}
request, err := http.NewRequest(http.MethodPost, queryEndpoint, bytes.NewReader(queryBytes))
if err != nil {
t.Fatal(err)
}
request.Header.Add("Content-Type", odohMessageContentType)

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, request)

if status := rr.Result().StatusCode; status != http.StatusBadRequest {
t.Fatal(fmt.Errorf("Result did not yield %d, got %d instead", http.StatusBadRequest, status))
}
}

func TestODoHResolutionWithRealResolver(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type of dependency shouldn't be in a unit test. Why did we go with 1.1.1.1 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likelihood of 1.1.1.1 being unavailable seems extremely low, and I wanted to exercise the resolve(): https://github.com/cloudflare/odoh-server-go/blob/master/resolver.go#L47 codepath. I figured that the benefits of having atleast one test for that codepath far outweighed the risk of creating a 1.1.1.1 dependency.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get that, but I think we can exercise that path by mocking things (or wrapping resolve() appropriately). Adding a network dependency doesn't seem to be the right answer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re: mocking it, personally I think exercising the networking codepath in resolve() is pretty valuable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I can remove it if you think it's fine without.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing the implementation of resolve() isn't something this project should do. We should only be testing that the code that uses resolve is correct, which is why I suggested mocking it.

r := &targetResolver{
timeout: 2500 * time.Millisecond,
nameserver: "1.1.1.1:53",
}
target := createTarget(t, r)

handler := http.HandlerFunc(target.targetQueryHandler)

// malformed DNS query
obliviousQuery := odoh.CreateObliviousDNSQuery([]byte{1, 2, 3}, 0)
encryptedQuery, _, err := target.odohKeyPair.Config.Contents.EncryptQuery(obliviousQuery)
if err != nil {
t.Fatal(err)
}

request, err := http.NewRequest(http.MethodPost, queryEndpoint, bytes.NewReader(encryptedQuery.Marshal()))
if err != nil {
t.Fatal(err)
}
request.Header.Add("Content-Type", odohMessageContentType)

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, request)

if status := rr.Result().StatusCode; status != http.StatusBadRequest {
t.Fatal(fmt.Errorf("Result did not yield %d, got %d instead", http.StatusBadRequest, status))
}

handler = http.HandlerFunc(target.targetQueryHandler)

// valid dns query
q := new(dns.Msg)
q.SetQuestion("example.com.", dns.TypeA)
packedQuery, err := q.Pack()
if err != nil {
t.Fatal(err)
}
obliviousQuery = odoh.CreateObliviousDNSQuery([]byte(packedQuery), 0)
encryptedQuery, _, err = target.odohKeyPair.Config.Contents.EncryptQuery(obliviousQuery)
if err != nil {
t.Fatal(err)
}

request, err = http.NewRequest(http.MethodPost, queryEndpoint, bytes.NewReader(encryptedQuery.Marshal()))
if err != nil {
t.Fatal(err)
}
request.Header.Add("Content-Type", odohMessageContentType)

rr = httptest.NewRecorder()
handler.ServeHTTP(rr, request)

if status := rr.Result().StatusCode; status != http.StatusOK {
t.Fatal(fmt.Errorf("Result did not yield %d, got %d instead", http.StatusOK, status))
}
if rr.Result().Header.Get("Content-Type") != odohMessageContentType {
t.Fatal("Invalid content type response")
}
}