Skip to content

Commit 0a22d63

Browse files
committed
Add IPv4 and IPv6 generators
These generators return `net.IP`, and are also used in generating URLs.
1 parent 5bbc9c0 commit 0a22d63

File tree

4 files changed

+125
-8
lines changed

4 files changed

+125
-8
lines changed

ip_addr.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2020 Walter Scheper <[email protected]>
2+
//
3+
// This Source Code Form is subject to the terms of the Mozilla Public
4+
// License, v. 2.0. If a copy of the MPL was not distributed with this
5+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6+
7+
package rapid
8+
9+
import (
10+
"fmt"
11+
"net"
12+
"reflect"
13+
)
14+
15+
const (
16+
ipv4Len = 4
17+
ipv6Len = 16
18+
)
19+
20+
var (
21+
ipType = reflect.TypeOf(net.IP{})
22+
23+
ipv4Gen = SliceOfN(Byte(), ipv4Len, ipv4Len)
24+
ipv6Gen = SliceOfN(Byte(), ipv6Len, ipv6Len)
25+
)
26+
27+
type ipGen struct {
28+
ipv6 bool
29+
}
30+
31+
func (g *ipGen) String() string {
32+
return fmt.Sprintf("IP(ipv6=%v)", g.ipv6)
33+
}
34+
35+
func (*ipGen) type_() reflect.Type {
36+
return ipType
37+
}
38+
39+
func (g *ipGen) value(t *T) value {
40+
var gen *Generator
41+
if g.ipv6 {
42+
gen = ipv6Gen
43+
} else {
44+
gen = ipv4Gen
45+
}
46+
47+
b := gen.Draw(t, g.String()).([]byte)
48+
return net.IP(b)
49+
}
50+
51+
func IPv4() *Generator {
52+
return newGenerator(&ipGen{
53+
ipv6: false,
54+
})
55+
}
56+
57+
func IPv6() *Generator {
58+
return newGenerator(&ipGen{
59+
ipv6: true,
60+
})
61+
}

ip_addr_example_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2020 Walter Scheper <[email protected]>
2+
//
3+
// This Source Code Form is subject to the terms of the Mozilla Public
4+
// License, v. 2.0. If a copy of the MPL was not distributed with this
5+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6+
7+
package rapid_test
8+
9+
import (
10+
"fmt"
11+
"net"
12+
13+
"pgregory.net/rapid"
14+
)
15+
16+
func ExampleIPv4() {
17+
gen := rapid.IPv4()
18+
19+
for i := 0; i < 5; i++ {
20+
addr := gen.Example(i).(net.IP)
21+
fmt.Println(addr.String())
22+
}
23+
24+
// Output:
25+
// 0.23.24.7
26+
// 100.146.0.0
27+
// 0.222.65.1
28+
// 1.49.104.14
29+
// 11.56.0.83
30+
}
31+
32+
func ExampleIPv6() {
33+
gen := rapid.IPv6()
34+
35+
for i := 0; i < 5; i++ {
36+
addr := gen.Example(i).(net.IP)
37+
fmt.Println(addr.String())
38+
}
39+
40+
// Output:
41+
// 17:1807:e2c4:8210:7202:f4b2:a0e2:8dc
42+
// 6492:0:fa37:b00:b5c3:4e6:6a01:c802
43+
// de:4101:9f5:3:104:5dc:b600:905
44+
// 131:680e:97ff:d200:ae1:4d00:2300:103
45+
// b38:53:ff07:200:8c28:ee:ad00:1b
46+
}

url.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package rapid
88

99
import (
1010
"fmt"
11+
"net"
1112
"net/url"
1213
"reflect"
1314
"strings"
@@ -83,7 +84,16 @@ var printableGen = StringOf(RuneFrom(nil, unicode.PrintRanges...))
8384

8485
func (g *urlGenerator) value(t *T) value {
8586
scheme := SampledFrom(g.schemes).Draw(t, "scheme").(string)
86-
domain := Domain().Draw(t, "domain").(string)
87+
var domain string
88+
switch SampledFrom([]int{0, 1, 2}).Draw(t, "g").(int) {
89+
case 2:
90+
domain = Domain().Draw(t, "domain").(string)
91+
case 1:
92+
domain = IPv6().Draw(t, "domain").(net.IP).String()
93+
domain = "[" + domain + "]"
94+
case 0:
95+
domain = IPv4().Draw(t, "domain").(net.IP).String()
96+
}
8797
port := IntRange(0, 2^16-1).
8898
Map(func(i int) string {
8999
if i == 0 {
@@ -97,9 +107,9 @@ func (g *urlGenerator) value(t *T) value {
97107
fragment := printableGen.Draw(t, "fragment").(string)
98108

99109
return url.URL{
100-
Host: domain + port,
110+
Host: domain + port,
101111
Path: strings.Join(path_, "/"),
102-
Scheme: scheme,
112+
Scheme: scheme,
103113
RawQuery: strings.Join(query, "&"),
104114
Fragment: fragment,
105115
}

url_example_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func ExampleURL() {
3737
}
3838

3939
// Output:
40-
// https://r125pz05Rz-0j1d-11.AArP:17#0%E2%81%9B%F3%A0%84%9A
41-
// http://L2.aBArTh:7/%F0%9F%AA%95%22%D6%93%E0%A9%AD%E1%B3%930%D0%8A/%C2%BC%E0%B4%BE3%F0%9E%8B%B0%F0%91%86%BD%C2%B2%E0%B3%A9%CC%80D/%7C+%F0%9F%81%9D+%5D%CC%81%CB%85/%CC%80/%E1%B0%BF/%CD%82K%E0%A5%A9%CC%81#%CC%82
42-
// https://pH20DR11.aaA?%DB%97%F0%90%AC%BC%24%F0%91%99%83%E2%82%A5%F0%9D%A8%A8%E0%BC%95%E0%B5%A8%3C%E0%BE%B0%F0%97%8D%91%E2%9E%8E%E0%B9%91%24v%CC%80&%CC%94Z%E4%87%A4#%CC%A0%E1%81%AD
43-
// http://h.AcCounTaNtS:4/%F0%9E%A5%9F/:%21J%E2%9D%87#L%CC%82%E9%98%A6%22
44-
// http://A.xN--s9bRJ9C:2
40+
// https://[e506:816b:407:316:fb4c:ffa0:e208:dc0e]/%F0%97%B3%80%F0%92%91%ADX/1=%22?%C4%90%F0%90%A9%87%26#%F0%91%B2%9E1%CC%88%CC%81D
41+
// http://G.BLoG/%E0%AD%8C~%F0%9F%AA%95%22%D6%93%E0%A9%AD%E1%B3%930%D0%8A/%C2%BC%E0%B4%BE3%F0%9E%8B%B0%F0%91%86%BD%C2%B2%E0%B3%A9%CC%80D/%7C+%F0%9F%81%9D+%5D%CC%81%CB%85/%CC%80/%E1%B0%BF/%CD%82K%E0%A5%A9%CC%81#%CC%82
42+
// https://1.47.4.5:11/+%3E%E2%9F%BCK//A%DB%97%F0%90%AC%BC$%F0%91%99%83%E2%82%A5%F0%9D%A8%A8%E0%BC%95%E0%B5%A8%3C%E0%BE%B0%F0%97%8D%91%E2%9E%8E%E0%B9%91$v%CC%80/%CC%94Z%E4%87%A4?%F0%91%91%9BC%C2%B9%E2%8A%A5%F0%91%91%8F1%E0%A0%BE%CB%BE%C3%9D%E1%B3%A8%E0%AB%A6%CC%81%CC%86&%E2%A4%88%F0%91%8A%A9%24B%F0%9D%8B%AA%CC%9A&&%CC%80%C2%A7%E4%92%9B&#%E0%AB%AE%F0%92%91%A2
43+
// http://G.hM/%CC%80%E0%A0%B1%CC%82%CC%80%F0%9E%A5%9F/:%21J%E2%9D%87#L%CC%82%E9%98%A6%22
44+
// http://1.1.4.6:2/%E3%B5%B8%C3%B2%DE%A6%E1%9E%B9/%C2%A7/?%E2%80%A60%CC%80%C3%B7&%CC%81%CC%A2%21%E0%AF%AB%CC%81%C3%A4&%F0%9F%AA%95%EA%99%B4%CC%80%E0%A5%AD&%F0%AD%B9%A9%F0%91%87%AE&&%E1%B7%93%CC%8B%E2%87%94%E2%90%8E%EA%A3%A5%E0%B5%9A%3D%E5%8E%A4%D9%AAB%F0%A5%8F%9A%3D%C2%A4%C3%AE%F0%91%84%AD%DC%8A%21%E2%82%8D3&%E1%81%8F%23#%CC%80%E0%BF%8B+$
4545
}

0 commit comments

Comments
 (0)