From 3fc35d7a3231861d603567c8bd904fe95dae9e75 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Wed, 14 Dec 2022 06:12:31 -0800 Subject: [PATCH] net.Addr --- default.go | 6 ++++ t_netaddr.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 t_netaddr.go diff --git a/default.go b/default.go index be51ad1..514ced5 100644 --- a/default.go +++ b/default.go @@ -2,6 +2,7 @@ package arbitrary import ( "math/rand" + "net" "time" ) @@ -14,6 +15,11 @@ func Bool() bool { return Default.Bool() } +// NetAddr returns an arbitrary net.Addr. +func NetAddr() net.Addr { + return Default.NetAddr() +} + // Password returns an arbitrary password. func Password() string { return Default.Password() diff --git a/t_netaddr.go b/t_netaddr.go new file mode 100644 index 0000000..6d3028d --- /dev/null +++ b/t_netaddr.go @@ -0,0 +1,85 @@ +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" + } + } + + var port uint16 + { + port = uint16(arb.randomness.Intn(65536)) + + if 0 == arb.randomness.Intn(5) { + port = uint16(arb.randomness.Intn(1024)) + } + + if 0 == arb.randomness.Intn(4) { + port = uint16(arb.randomness.Intn(100)) + } + + if 0 == arb.randomness.Intn(30) { + port = 80 + } + if 0 == arb.randomness.Intn(40) { + port = 79 + } + } + + 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 +}