2021-09-09 21:24:27 +02:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2022-05-25 17:03:58 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-09-09 21:24:27 +02:00
|
|
|
)
|
|
|
|
|
2021-09-28 10:47:59 +02:00
|
|
|
func TestDNSEncoder(t *testing.T) {
|
2021-09-09 21:24:27 +02:00
|
|
|
t.Run("Encode", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
2022-05-25 17:03:58 +02:00
|
|
|
queryID := dns.Id()
|
2021-09-28 10:47:59 +02:00
|
|
|
e := &DNSEncoder{
|
2022-05-25 17:03:58 +02:00
|
|
|
MockEncode: func(domain string, qtype uint16, padding bool) model.DNSQuery {
|
|
|
|
return &DNSQuery{
|
|
|
|
MockDomain: func() string {
|
|
|
|
return dns.Fqdn(domain) // do what an implementation MUST do
|
|
|
|
},
|
|
|
|
MockType: func() uint16 {
|
|
|
|
return qtype
|
|
|
|
},
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
MockID: func() uint16 {
|
|
|
|
return queryID
|
|
|
|
},
|
|
|
|
}
|
2021-09-09 21:24:27 +02:00
|
|
|
},
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
query := e.Encode("dns.google", dns.TypeA, true)
|
|
|
|
if query.Domain() != "dns.google." {
|
|
|
|
t.Fatal("invalid domain")
|
|
|
|
}
|
|
|
|
if query.Type() != dns.TypeA {
|
|
|
|
t.Fatal("invalid type")
|
|
|
|
}
|
|
|
|
out, err := query.Bytes()
|
2021-09-09 21:24:27 +02:00
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("unexpected out")
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if query.ID() != queryID {
|
2022-05-14 19:38:46 +02:00
|
|
|
t.Fatal("unexpected queryID")
|
|
|
|
}
|
2021-09-09 21:24:27 +02:00
|
|
|
})
|
|
|
|
}
|