forked from RedTeamPentesting/adauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver_test.go
98 lines (77 loc) · 1.67 KB
/
resolver_test.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package adauth_test
import (
"context"
"fmt"
"net"
"github.com/RedTeamPentesting/adauth"
)
type testResolver struct {
HostToAddr map[string][]net.IP
AddrToHost map[string][]string
SRV map[string]map[string]map[string]struct {
Name string
SRV []*net.SRV
}
Error error
}
func (r *testResolver) LookupAddr(ctx context.Context, addr string) ([]string, error) {
if r.Error != nil {
return nil, r.Error
}
if r.AddrToHost == nil {
return nil, nil
}
return r.AddrToHost[addr], nil
}
func (r *testResolver) LookupIP(ctx context.Context, network string, host string) ([]net.IP, error) {
if r.Error != nil {
return nil, r.Error
}
if r.HostToAddr == nil {
return nil, nil
}
addrs := r.HostToAddr[host]
switch network {
case "ip":
return addrs, nil
case "ip4":
var ipv4s []net.IP
for _, addr := range addrs {
if addr.To4() != nil {
ipv4s = append(ipv4s, addr)
}
}
return ipv4s, nil
case "ip6":
var ipv6s []net.IP
for _, addr := range addrs {
if addr.To4() == nil {
ipv6s = append(ipv6s, addr)
}
}
return ipv6s, nil
default:
return nil, fmt.Errorf("invalid network: %q", network)
}
}
func (r *testResolver) LookupSRV(
ctx context.Context, service string, proto string, name string,
) (string, []*net.SRV, error) {
if r.Error != nil {
return "", nil, r.Error
}
if r.SRV == nil {
return "", nil, nil
}
srvForService := r.SRV[service]
if srvForService == nil {
return "", nil, nil
}
srvForServiceAndProto := srvForService[proto]
if srvForServiceAndProto == nil {
return "", nil, nil
}
record := srvForServiceAndProto[name]
return record.Name, record.SRV, nil
}
var _ adauth.Resolver = &testResolver{}