maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

  1/*
  2Maddy Mail Server - Composable all-in-one email server.
  3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
  4
  5This program is free software: you can redistribute it and/or modify
  6it under the terms of the GNU General Public License as published by
  7the Free Software Foundation, either version 3 of the License, or
  8(at your option) any later version.
  9
 10This program is distributed in the hope that it will be useful,
 11but WITHOUT ANY WARRANTY; without even the implied warranty of
 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13GNU General Public License for more details.
 14
 15You should have received a copy of the GNU General Public License
 16along with this program.  If not, see <https://www.gnu.org/licenses/>.
 17*/
 18
 19package dnsbl
 20
 21import (
 22	"context"
 23	"net"
 24	"reflect"
 25	"testing"
 26
 27	"github.com/foxcpp/go-mockdns"
 28)
 29
 30func TestQueryString(t *testing.T) {
 31	test := func(ip, queryStr string) {
 32		t.Helper()
 33
 34		parsed := net.ParseIP(ip)
 35		if parsed == nil {
 36			panic("Malformed IP in test")
 37		}
 38
 39		actual := queryString(parsed)
 40		if actual != queryStr {
 41			t.Errorf("want queryString(%s) to be %s, got %s", ip, queryStr, actual)
 42		}
 43	}
 44
 45	test("2001:db8:1:2:3:4:567:89ab", "b.a.9.8.7.6.5.0.4.0.0.0.3.0.0.0.2.0.0.0.1.0.0.0.8.b.d.0.1.0.0.2")
 46	test("2001::1:2:3:4:567:89ab", "b.a.9.8.7.6.5.0.4.0.0.0.3.0.0.0.2.0.0.0.1.0.0.0.0.0.0.0.1.0.0.2")
 47	test("192.0.2.99", "99.2.0.192")
 48}
 49
 50func TestCheckDomain(t *testing.T) {
 51	test := func(zones map[string]mockdns.Zone, cfg List, domain string, expectedErr error) {
 52		t.Helper()
 53		resolver := mockdns.Resolver{Zones: zones}
 54		err := checkDomain(context.Background(), &resolver, cfg, domain)
 55		if !reflect.DeepEqual(err, expectedErr) {
 56			t.Errorf("expected err to be '%#v', got '%#v'", expectedErr, err)
 57		}
 58	}
 59
 60	test(nil, List{Zone: "example.org"}, "example.com", nil)
 61	test(map[string]mockdns.Zone{
 62		"example.com.example.org.": {
 63			Err: &net.DNSError{
 64				Err:         "i/o timeout",
 65				IsTimeout:   true,
 66				IsTemporary: true,
 67			},
 68		},
 69	}, List{Zone: "example.org"}, "example.com", &net.DNSError{
 70		Err:         "i/o timeout",
 71		IsTimeout:   true,
 72		IsTemporary: true,
 73	})
 74	test(map[string]mockdns.Zone{
 75		"example.com.example.org.": {
 76			A: []string{"127.0.0.1"},
 77		},
 78	}, List{Zone: "example.org"}, "example.com", ListedErr{
 79		Identity: "example.com",
 80		List:     "example.org",
 81		Reason:   "127.0.0.1",
 82	})
 83	test(map[string]mockdns.Zone{
 84		"example.org.example.com.": {
 85			A: []string{"127.0.0.1"},
 86		},
 87	}, List{Zone: "example.org"}, "example.com", nil)
 88	test(map[string]mockdns.Zone{
 89		"example.com.example.org.": {
 90			A:   []string{"127.0.0.1"},
 91			TXT: []string{"Reason"},
 92		},
 93	}, List{Zone: "example.org"}, "example.com", ListedErr{
 94		Identity: "example.com",
 95		List:     "example.org",
 96		Reason:   "Reason",
 97	})
 98	test(map[string]mockdns.Zone{
 99		"example.com.example.org.": {
100			A:   []string{"127.0.0.1"},
101			TXT: []string{"Reason 1", "Reason 2"},
102		},
103	}, List{Zone: "example.org"}, "example.com", ListedErr{
104		Identity: "example.com",
105		List:     "example.org",
106		Reason:   "Reason 1; Reason 2",
107	})
108	test(map[string]mockdns.Zone{
109		"example.com.example.org.": {
110			A: []string{"127.0.0.1", "127.0.0.2"},
111		},
112	}, List{Zone: "example.org"}, "example.com", ListedErr{
113		Identity: "example.com",
114		List:     "example.org",
115		Reason:   "127.0.0.1; 127.0.0.2",
116	})
117}
118
119func TestCheckIP(t *testing.T) {
120	test := func(zones map[string]mockdns.Zone, cfg List, ip net.IP, expectedErr error) {
121		t.Helper()
122		resolver := mockdns.Resolver{Zones: zones}
123		err := checkIP(context.Background(), &resolver, cfg, ip)
124		if !reflect.DeepEqual(err, expectedErr) {
125			t.Errorf("expected err to be '%#v', got '%#v'", expectedErr, err)
126		}
127	}
128
129	test(nil, List{Zone: "example.org"}, net.IPv4(1, 2, 3, 4), nil)
130	test(nil, List{Zone: "example.org", ClientIPv4: true}, net.IPv4(1, 2, 3, 4), nil)
131	test(map[string]mockdns.Zone{
132		"4.3.2.1.example.org.": {
133			A: []string{"127.0.0.1"},
134		},
135	}, List{Zone: "example.org", ClientIPv4: true}, net.IPv4(1, 2, 3, 4), ListedErr{
136		Identity: "1.2.3.4",
137		List:     "example.org",
138		Reason:   "127.0.0.1",
139	})
140	test(map[string]mockdns.Zone{
141		"4.3.2.1.example.org.": {
142			A: []string{"128.0.0.1"},
143		},
144	}, List{
145		Zone:       "example.org",
146		ClientIPv4: true,
147		Responses: []net.IPNet{
148			{
149				IP:   net.IPv4(127, 0, 0, 1),
150				Mask: net.IPv4Mask(255, 255, 255, 0),
151			},
152		},
153	}, net.IPv4(1, 2, 3, 4), nil)
154	test(map[string]mockdns.Zone{
155		"4.3.2.1.example.org.": {
156			A: []string{"128.0.0.1"},
157		},
158	}, List{
159		Zone:       "example.org",
160		ClientIPv4: true,
161		Responses: []net.IPNet{
162			{
163				IP:   net.IPv4(127, 0, 0, 0),
164				Mask: net.IPv4Mask(255, 255, 255, 0),
165			},
166			{
167				IP:   net.IPv4(128, 0, 0, 0),
168				Mask: net.IPv4Mask(255, 255, 255, 0),
169			},
170		},
171	}, net.IPv4(1, 2, 3, 4), ListedErr{
172		Identity: "1.2.3.4",
173		List:     "example.org",
174		Reason:   "128.0.0.1",
175	})
176	test(map[string]mockdns.Zone{
177		"4.3.2.1.example.org.": {
178			A: []string{"127.0.0.1"},
179		},
180	}, List{Zone: "example.org"}, net.IPv4(1, 2, 3, 4), nil)
181	test(map[string]mockdns.Zone{
182		"4.3.2.1.example.org.": {
183			Err: &net.DNSError{
184				Err:         "i/o timeout",
185				IsTimeout:   true,
186				IsTemporary: true,
187			},
188		},
189	}, List{Zone: "example.org", ClientIPv4: true}, net.IPv4(1, 2, 3, 4), &net.DNSError{
190		Err:         "i/o timeout",
191		IsTimeout:   true,
192		IsTemporary: true,
193	})
194
195	test(map[string]mockdns.Zone{
196		"4.3.2.1.example.org.": {
197			A:   []string{"127.0.0.1"},
198			TXT: []string{"Reason"},
199		},
200	}, List{Zone: "example.org", ClientIPv4: true}, net.IPv4(1, 2, 3, 4), ListedErr{
201		Identity: "1.2.3.4",
202		List:     "example.org",
203		Reason:   "Reason",
204	})
205	test(map[string]mockdns.Zone{
206		"4.3.2.1.example.org.": {
207			A: []string{"127.0.0.1", "127.0.0.2"},
208		},
209	}, List{Zone: "example.org", ClientIPv4: true}, net.IPv4(1, 2, 3, 4), ListedErr{
210		Identity: "1.2.3.4",
211		List:     "example.org",
212		Reason:   "127.0.0.1; 127.0.0.2",
213	})
214	test(map[string]mockdns.Zone{
215		"4.3.2.1.example.org.": {
216			A:   []string{"127.0.0.1", "127.0.0.2"},
217			TXT: []string{"Reason", "Reason 2"},
218		},
219	}, List{Zone: "example.org", ClientIPv4: true}, net.IPv4(1, 2, 3, 4), ListedErr{
220		Identity: "1.2.3.4",
221		List:     "example.org",
222		Reason:   "Reason; Reason 2",
223	})
224	test(map[string]mockdns.Zone{
225		"b.a.9.8.7.6.5.0.4.0.0.0.3.0.0.0.2.0.0.0.1.0.0.0.8.b.d.0.1.0.0.2.example.org.": {
226			A: []string{"127.0.0.1"},
227		},
228	}, List{Zone: "example.org", ClientIPv4: true}, net.ParseIP("2001:db8:1:2:3:4:567:89ab"), nil)
229	test(map[string]mockdns.Zone{
230		"b.a.9.8.7.6.5.0.4.0.0.0.3.0.0.0.2.0.0.0.1.0.0.0.8.b.d.0.1.0.0.2.example.org.": {
231			A: []string{"127.0.0.1"},
232		},
233	}, List{Zone: "example.org", ClientIPv6: true}, net.ParseIP("2001:db8:1:2:3:4:567:89ab"), ListedErr{
234		Identity: "2001:db8:1:2:3:4:567:89ab",
235		List:     "example.org",
236		Reason:   "127.0.0.1",
237	})
238}