carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package edgesfile
2
3import "testing"
4
5// Census ruling region-ha-2a: a private address passes only when it is a
6// well-formed private IPv4 address inside the region network's CIDR, and
7// never loopback or link-local (the metadata range included).
8func TestPrivateAddress(t *testing.T) {
9 vpc := []string{"10.0.0.0/16"}
10 cases := []struct {
11 name, addr string
12 cidrs []string
13 want, cidr string
14 }{
15 {"inside the region network", "10.0.1.5", vpc, "10.0.1.5", "10.0.0.0/16"},
16 {"inside the second block", "172.31.4.9", []string{"10.0.0.0/16", " 172.31.0.0/16 "}, "172.31.4.9", "172.31.0.0/16"},
17 {"malformed", "10.0.1", vpc, "", ""},
18 {"a hostname", "box.internal", vpc, "", ""},
19 {"the metadata range", "169.254.169.254", []string{"169.254.0.0/16"}, "", ""},
20 {"loopback", "127.0.0.1", []string{"127.0.0.0/8"}, "", ""},
21 {"outside the region network", "10.1.1.5", vpc, "", ""},
22 {"a public address", "203.0.113.4", []string{"203.0.113.0/24"}, "", ""},
23 {"IPv6", "fd00::5", []string{"fd00::/8"}, "", ""},
24 {"no network to check it against", "10.0.1.5", nil, "", ""},
25 {"a malformed network", "10.0.1.5", []string{"10.0.0.0/33"}, "", ""},
26 }
27 for _, c := range cases {
28 t.Run(c.name, func(t *testing.T) {
29 got, cidr := PrivateAddress(c.addr, c.cidrs)
30 if got != c.want || cidr != c.cidr {
31 t.Fatalf("PrivateAddress(%q, %v) = %q, %q; want %q, %q", c.addr, c.cidrs, got, cidr, c.want, c.cidr)
32 }
33 })
34 }
35}
36