testing | dont be recursive for external domains | finalize oauth #5

Merged
simponic merged 24 commits from dont-be-authoritative into main 2024-04-06 15:43:19 -04:00
2 changed files with 36 additions and 28 deletions
Showing only changes of commit 35a5e9a263 - Show all commits

View File

@ -2,3 +2,4 @@
hatecomputers.club hatecomputers.club
Dockerfile Dockerfile
*.db *.db
.drone.yml

View File

@ -2,6 +2,8 @@ package hcdns
import ( import (
"database/sql" "database/sql"
"fmt"
"math/rand"
"os" "os"
"sync" "sync"
"testing" "testing"
@ -9,17 +11,24 @@ import (
"git.hatecomputers.club/hatecomputers/hatecomputers.club/args" "git.hatecomputers.club/hatecomputers/hatecomputers.club/args"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/database" "git.hatecomputers.club/hatecomputers/hatecomputers.club/database"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/hcdns" "git.hatecomputers.club/hatecomputers/hatecomputers.club/hcdns"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/utils"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
const ( func destroy(conn *sql.DB, path string) {
testDBPath = "test.db" conn.Close()
address = "127.0.0.1:8353" os.Remove(path)
dnsPort = 8353 }
)
func setup(dbPath string) (*sql.DB, *dns.Server, *sync.WaitGroup) { func randomPort() int {
testDb := database.MakeConn(&dbPath) return rand.Intn(3000) + 10000
}
func setup() (*sql.DB, *dns.Server, int, *string, func()) {
randomDb := utils.RandomId()
dnsPort := randomPort()
testDb := database.MakeConn(&randomDb)
database.Migrate(testDb) database.Migrate(testDb)
testUser := &database.User{ testUser := &database.User{
ID: "test", ID: "test",
@ -37,20 +46,21 @@ func setup(dbPath string) (*sql.DB, *dns.Server, *sync.WaitGroup) {
waitGroup.Done() waitGroup.Done()
}() }()
return testDb, server, &waitGroup address := fmt.Sprintf("127.0.0.1:%d", dnsPort)
} return testDb, server, dnsPort, &address, func() {
testDb.Close()
os.Remove(randomDb)
func destroy(conn *sql.DB, path string) { server.Shutdown()
conn.Close() waitGroup.Wait()
os.Remove(path) }
} }
func TestWhenCNAMEIsResolved(t *testing.T) { func TestWhenCNAMEIsResolved(t *testing.T) {
t.Log("TestWhenCNAMEIsResolved") t.Log("TestWhenCNAMEIsResolved")
testDb, server, _ := setup(testDBPath) testDb, _, _, addr, cleanup := setup()
defer destroy(testDb, testDBPath) defer cleanup()
defer server.Shutdown()
cname := &database.DNSRecord{ cname := &database.DNSRecord{
ID: "1", ID: "1",
@ -79,7 +89,7 @@ func TestWhenCNAMEIsResolved(t *testing.T) {
message := new(dns.Msg) message := new(dns.Msg)
message.SetQuestion(domain, qtype) message.SetQuestion(domain, qtype)
in, _, err := client.Exchange(message, address) in, _, err := client.Exchange(message, *addr)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -125,9 +135,8 @@ func TestWhenCNAMEIsResolved(t *testing.T) {
func TestWhenNoRecordNxDomain(t *testing.T) { func TestWhenNoRecordNxDomain(t *testing.T) {
t.Log("TestWhenNoRecordNxDomain") t.Log("TestWhenNoRecordNxDomain")
testDb, server, _ := setup(testDBPath) _, _, _, addr, cleanup := setup()
defer destroy(testDb, testDBPath) defer cleanup()
defer server.Shutdown()
qtype := dns.TypeA qtype := dns.TypeA
domain := dns.Fqdn("nonexistant.example.com.") domain := dns.Fqdn("nonexistant.example.com.")
@ -135,7 +144,7 @@ func TestWhenNoRecordNxDomain(t *testing.T) {
message := new(dns.Msg) message := new(dns.Msg)
message.SetQuestion(domain, qtype) message.SetQuestion(domain, qtype)
in, _, err := client.Exchange(message, address) in, _, err := client.Exchange(message, *addr)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -153,9 +162,8 @@ func TestWhenNoRecordNxDomain(t *testing.T) {
func TestWhenUnresolvingCNAME(t *testing.T) { func TestWhenUnresolvingCNAME(t *testing.T) {
t.Log("TestWhenUnresolvingCNAME") t.Log("TestWhenUnresolvingCNAME")
testDb, server, _ := setup(testDBPath) testDb, _, _, addr, cleanup := setup()
defer destroy(testDb, testDBPath) defer cleanup()
defer server.Shutdown()
cname := &database.DNSRecord{ cname := &database.DNSRecord{
ID: "1", ID: "1",
@ -174,7 +182,7 @@ func TestWhenUnresolvingCNAME(t *testing.T) {
message := new(dns.Msg) message := new(dns.Msg)
message.SetQuestion(domain, qtype) message.SetQuestion(domain, qtype)
in, _, err := client.Exchange(message, address) in, _, err := client.Exchange(message, *addr)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -208,9 +216,8 @@ func TestWhenUnresolvingCNAME(t *testing.T) {
func TestWhenUnresolvingCNAMEWithMaxDepth(t *testing.T) { func TestWhenUnresolvingCNAMEWithMaxDepth(t *testing.T) {
t.Log("TestWhenUnresolvingCNAMEWithMaxDepth") t.Log("TestWhenUnresolvingCNAMEWithMaxDepth")
testDb, server, _ := setup(testDBPath) testDb, _, _, addr, cleanup := setup()
defer destroy(testDb, testDBPath) defer cleanup()
defer server.Shutdown()
cname := &database.DNSRecord{ cname := &database.DNSRecord{
ID: "1", ID: "1",
@ -229,7 +236,7 @@ func TestWhenUnresolvingCNAMEWithMaxDepth(t *testing.T) {
message := new(dns.Msg) message := new(dns.Msg)
message.SetQuestion(domain, qtype) message.SetQuestion(domain, qtype)
in, _, err := client.Exchange(message, address) in, _, err := client.Exchange(message, *addr)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)