Skip to content

Commit ac78eef

Browse files
committed
search domain discovery: Increase test coverage
Randomly pick IP from address space Update Go version to current scionproto master Replace usage of deprecated package with math/rand/v2
1 parent bc5f2a8 commit ac78eef

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ require (
2727

2828
replace github.com/insomniacslk/dhcp => github.com/stapelberg/dhcp v0.0.0-20190429172946-5244c0daddf0
2929

30-
go 1.21
30+
go 1.22

hinting/dns.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package hinting
1717

1818
import (
19-
"math/rand"
19+
"math/rand/v2"
2020
"net"
2121
"net/netip"
2222
"sort"
@@ -214,10 +214,10 @@ func (s byPriority) Less(i, j int) bool {
214214
return false
215215
} else {
216216
if s[i].Weight == 0 && s[j].Weight == 0 {
217-
return rand.Intn(2) == 0
217+
return rand.IntN(2) == 0
218218
}
219219
max := int(s[i].Weight) + int(s[j].Weight)
220-
return rand.Intn(max) < int(s[i].Weight)
220+
return rand.IntN(max) < int(s[i].Weight)
221221
}
222222
}
223223

hinting/dns_search_domain_fallbacks_reverse.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package hinting
33
import (
44
"context"
55
"errors"
6-
"math/rand"
6+
"math/rand/v2"
77
"net"
88
"net/netip"
99
"time"
@@ -37,13 +37,13 @@ func getAkaNS() (nameserver string, err error) {
3737
defer cancel()
3838
nameservers, err := resolver.LookupNS(ctx, akamaiDomain)
3939
if err == nil {
40-
return nameservers[rand.Intn(len(nameservers))].Host, err
40+
return nameservers[rand.IntN(len(nameservers))].Host, err
4141
}
4242
if err, ok := err.(*net.DNSError); ok && err.IsNotFound {
4343
// Do not attempt further fallback to a public resolver.
4444
// We got a NXDOMAIN response or no NS type response. Since we know the NS record exists,
4545
// it must have been intentionally shadowed by the system default resolver.
46-
return nil, err
46+
return "", err
4747
}
4848

4949
m := new(dns.Msg)
@@ -57,7 +57,7 @@ func getAkaNS() (nameserver string, err error) {
5757
err = errors.New("getAkaNS: No DNS RR answer")
5858
return "", err
5959
}
60-
if ns, ok := in.Answer[rand.Intn(len(in.Answer))].(*dns.NS); ok {
60+
if ns, ok := in.Answer[rand.IntN(len(in.Answer))].(*dns.NS); ok {
6161
return ns.Ns, nil
6262
}
6363
return "", errors.New("getAkaNS: Invalid NS record")

hinting/dns_search_domain_fallbacks_test.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package hinting
22

33
import (
4-
"github.com/stretchr/testify/assert"
4+
"crypto/rand"
5+
"net"
56
"net/netip"
67
"testing"
8+
9+
"github.com/stretchr/testify/assert"
710
)
811

912
func TestReverseLookupDomains(t *testing.T) {
@@ -32,6 +35,8 @@ func TestReverseLookupDomains(t *testing.T) {
3235
domain string
3336
}{
3437
{ip: netip.MustParseAddr("66.180.178.131"), domain: "princeton.edu"},
38+
//{ip: randIPFromCIDR("128.112.0.0/16"), domain: "princeton.edu"},
39+
{ip: randIPFromCIDR("128.112.66.0/23"), domain: "princeton.edu"},
3540
},
3641
},
3742
{
@@ -42,6 +47,7 @@ func TestReverseLookupDomains(t *testing.T) {
4247
}{
4348
{ip: netip.MustParseAddr("128.143.33.137"), domain: "virginia.edu"},
4449
{ip: netip.MustParseAddr("128.143.33.144"), domain: "virginia.edu"},
50+
{ip: randIPFromCIDR("128.143.0.128/25"), domain: "virginia.edu"},
4551
},
4652
},
4753
{
@@ -51,6 +57,7 @@ func TestReverseLookupDomains(t *testing.T) {
5157
domain string
5258
}{
5359
{ip: netip.MustParseAddr("130.59.31.80"), domain: "switch.ch"},
60+
{ip: randIPFromCIDR("130.59.2.128/25"), domain: "switch.ch"},
5461
},
5562
},
5663
{
@@ -59,6 +66,7 @@ func TestReverseLookupDomains(t *testing.T) {
5966
ip netip.Addr
6067
domain string
6168
}{
69+
//{ip: netip.MustParseAddr("203.250.215.48"), domain: "kreonet.net"},
6270
{ip: netip.MustParseAddr("134.75.254.11"), domain: "kreonet.net"},
6371
{ip: netip.MustParseAddr("134.75.254.12"), domain: "kreonet.net"},
6472
},
@@ -70,6 +78,7 @@ func TestReverseLookupDomains(t *testing.T) {
7078
domain string
7179
}{
7280
{ip: netip.MustParseAddr("163.152.6.10"), domain: "korea.ac.kr"},
81+
{ip: randIPFromCIDR("163.152.6.16/29"), domain: "korea.ac.kr"},
7382
},
7483
},
7584
}
@@ -124,3 +133,41 @@ func TestDomainsFromHostnamesDerivation(t *testing.T) {
124133
}
125134
}
126135
}
136+
137+
// randIPFromCIDR returns a random host IP in the subnet specified by the CIDR
138+
func randIPFromCIDR(cidr string) (ip netip.Addr) {
139+
_, ipNet, err := net.ParseCIDR(cidr)
140+
if err != nil {
141+
return
142+
}
143+
ip, ok := netip.AddrFromSlice(ipNet.IP)
144+
if !ok {
145+
return
146+
}
147+
var randBytes []byte
148+
randBytes = make([]byte, net.IPv6len)
149+
_, err = rand.Read(randBytes)
150+
if err != nil {
151+
return
152+
}
153+
if ip.Is4() {
154+
randBytes = randBytes[:net.IPv4len]
155+
}
156+
randomIP := net.IP(randBytes)
157+
maskedIP := randomIP.Mask(invertMask(ipNet.Mask))
158+
if len(ipNet.IP) != len(maskedIP) {
159+
return
160+
}
161+
for i := range ipNet.IP {
162+
randomIP[i] = ipNet.IP[i] | maskedIP[i]
163+
}
164+
return netip.MustParseAddr(randomIP.String())
165+
}
166+
167+
func invertMask(mask net.IPMask) (invertedMask net.IPMask) {
168+
invertedMask = make(net.IPMask, len(mask))
169+
for i, b := range mask {
170+
invertedMask[i] = 0xff ^ b
171+
}
172+
return
173+
}

0 commit comments

Comments
 (0)