<commit_before>package dns

import (
	"testing"
)

type spfTestpair struct {
	query    []string
	expected bool
}

func TestSPFValidator(t *testing.T) {
	queries := []spfTestpair{
		{[]string{"v=spf1"}, true},
		{[]string{"v=spf1 "}, true},
		{[]string{"v=spf10"}, false},
		{[]string{"v=spf11 "}, false},
		{[]string{"v=spf1 mx -all"}, true},
		{[]string{"v=spf1", "mx", "-all"}, true},
		{[]string{"random string"}, false},
	}

	for _, testcase := range queries {
		result := checkSPFVersion(testcase.query)
		if result != testcase.expected {
			t.Error(
				"Query: ", testcase.query, "got",
				result, "instead of", testcase.expected)
		}
	}
}<commit_msg>Add tests for dns package.<commit_after>package dns

import (
	"net"
	"reflect"
	"sort"
	"strings"
	"testing"
)

type spfTestpair struct {
	query    []string
	expected bool
}

func TestSPFValidator(t *testing.T) {
	queries := []spfTestpair{
		{[]string{"v=spf1"}, true},
		{[]string{"v=spf1 "}, true},
		{[]string{"v=spf10"}, false},
		{[]string{"v=spf11 "}, false},
		{[]string{"v=spf1 mx -all"}, true},
		{[]string{"v=spf1", "mx", "-all"}, true},
		{[]string{"random string"}, false},
	}

	for _, testcase := range queries {
		result := checkSPFVersion(testcase.query)
		if result != testcase.expected {
			t.Error(
				"Query: ", testcase.query, "got",
				result, "instead of", testcase.expected)
		}
	}
}

type SPFTestCase struct {
	Host string
	Txt  []string
}

//TestSPFLookup ensures a TXT records are properly queried and reurned to the called. Function should also work with
// multiple TXT records for a given host.
func TestSPFLookup(t *testing.T) {
	testcases := []SPFTestCase{
		SPFTestCase{"multi.spf.matching.com", []string{"v=spf1 ip6:2001:db8:a0b:12f0::1 -all", "v=spf1 mx -all"}},
		SPFTestCase{"1.spf.matching.com", []string{"v=spf1 a mx -all"}},
		SPFTestCase{"2.spf.matching.com", []string{"v=spf1 ip4:192.168.3.11 -all"}},
		SPFTestCase{"3.spf.matching.com", []string{"v=spf1 ip4:192.168.127.12/24 ?all"}},
	}

	for _, testcase := range testcases {
		lookup, err := LookupSPF(testcase.Host)
		// There is no guarantee in which order TXT records will be returned for a given host, so we need to sort here
		// in order to ensure the expected ordering will be provided (expected is sorted here)
		sort.Strings(lookup)
		if err != nil {
			t.Error("Caught error: ", err)
		} else if reflect.DeepEqual(testcase.Txt, lookup) == false {
			t.Error("Host: ", testcase.Host, " expected: ", testcase.Txt, " got: ", lookup)
		}
	}
}

func TestSPFLookupNegative(t *testing.T) {
	testcase := SPFTestCase{"incorrect.spf.matching.com", nil}

	spfPrefix := "Invalid SPF record:"
	_, err := LookupSPF(testcase.Host)
	if strings.HasPrefix(err.Error(), spfPrefix) == false {
		t.Error("Expected error to start with: ", spfPrefix, " got: ", err.Error(), " instead.")
	}
}

func TestHandleNoSuchHostDNSError(t *testing.T) {
	host := "idontexist.matching.com"
	_, err := LookupSPF(host)
	switch err.(type) {
	case *net.DNSError:
		break
	default:
		t.Errorf("Expected 'net.DNSError' error type, instead got:  %T\n", err)
	}
}
{'fraction_non_alphanumeric': 0.17919637070641609, 'fraction_numerical': 0.01911860012961763, 'mean_word_length': 4.241086587436333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7270774', 'n_tokens_mistral': 1253, 'n_tokens_neox': 1164, 'n_words': 276}