go-arbitrary/t_netaddr.go

68 lines
1.2 KiB
Go
Raw Permalink Normal View History

2022-12-14 14:12:31 +00:00
package arbitrary
import (
"fmt"
"net"
)
// NetAddr returns an arbitrary net.Addr.
func (arb T) NetAddr() net.Addr {
var network string
{
if arb.Bool() {
network = "tcp"
} else {
network = "udp"
}
}
2022-12-14 14:38:54 +00:00
var port uint16 = arb.NetPort()
2022-12-14 14:12:31 +00:00
var value string
func(){
value = fmt.Sprintf("%d.%d.%d.%d:%d", arb.randomness.Intn(256), arb.randomness.Intn(256), arb.randomness.Intn(256), arb.randomness.Intn(256), port)
if 0 == arb.randomness.Intn(16) {
value = fmt.Sprintf("127.0.0.1:%d", port)
return
}
if 0 == arb.randomness.Intn(16) {
value = fmt.Sprintf("192.168.0.1:%d", port)
return
}
if 0 == arb.randomness.Intn(7) {
value = fmt.Sprintf("%d.%d:%d", arb.randomness.Intn(256), arb.randomness.Intn(16777216), port)
return
}
if 0 == arb.randomness.Intn(7) {
value = fmt.Sprintf("%d.%d.%d:%d", arb.randomness.Intn(256), arb.randomness.Intn(256), arb.randomness.Intn(65536), port)
return
}
}()
return internalNetAddr{
network: network,
value: value,
}
}
type internalNetAddr struct {
network string
value string
}
var _ net.Addr = internalNetAddr{}
func (receiver internalNetAddr) Network() string {
return receiver.network
}
func (receiver internalNetAddr) String() string {
return receiver.value
}