File: fuzz.go

package info (click to toggle)
golang-blitiri-go-spf 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,524 kB
  • sloc: makefile: 3
file content (58 lines) | stat: -rw-r--r-- 1,514 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Fuzz testing for package spf.
//
// Run it with:
//
//   go-fuzz-build blitiri.com.ar/go/spf
//   go-fuzz -bin=./spf-fuzz.zip -workdir=testdata/fuzz
//

//go:build gofuzz
// +build gofuzz

package spf

import (
	"net"

	"blitiri.com.ar/go/spf/internal/dnstest"
)

// Parsed IP addresses, for convenience.
var (
	ip1110 = net.ParseIP("1.1.1.0")
	ip1111 = net.ParseIP("1.1.1.1")
	ip6666 = net.ParseIP("2001:db8::68")
	ip6660 = net.ParseIP("2001:db8::0")
)

// DNS resolver to use. Will be initialized once with the expected fixtures,
// and then reused on each fuzz run.
var dns = dnstest.NewResolver()

func init() {
	dns.Ip["d1111"] = []net.IP{ip1111}
	dns.Ip["d1110"] = []net.IP{ip1110}
	dns.Mx["d1110"] = []*net.MX{{"d1110", 5}, {"nothing", 10}}
	dns.Ip["d6666"] = []net.IP{ip6666}
	dns.Ip["d6660"] = []net.IP{ip6660}
	dns.Mx["d6660"] = []*net.MX{{"d6660", 5}, {"nothing", 10}}
	dns.Addr["2001:db8::68"] = []string{"sonlas6.", "domain.", "d6666."}
	dns.Addr["1.1.1.1"] = []string{"lalala.", "domain.", "d1111."}
}

func Fuzz(data []byte) int {
	// The domain's TXT record comes from the fuzzer.
	dns.Txt["domain"] = []string{string(data)}

	v4result, _ := CheckHostWithSender(
		ip1111, "helo", "domain", WithResolver(dns))
	v6result, _ := CheckHostWithSender(
		ip6666, "helo", "domain", WithResolver(dns))

	// Raise priority if any of the results was something other than
	// PermError, as it means the data was better formed.
	if v4result != PermError || v6result != PermError {
		return 1
	}
	return 0
}