File: networks_test.go

package info (click to toggle)
packer 1.6.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,156 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (54 lines) | stat: -rw-r--r-- 1,525 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package openstack

import (
	"net"
	"testing"
)

func testYes(t *testing.T, a, b string) {
	var m, n *net.IPNet
	_, m, _ = net.ParseCIDR(a)
	_, n, _ = net.ParseCIDR(b)
	if !containsNet(m, n) {
		t.Errorf("%s expected to contain %s", m, n)
	}
}

func testNot(t *testing.T, a, b string) {
	var m, n *net.IPNet
	_, m, _ = net.ParseCIDR(a)
	_, n, _ = net.ParseCIDR(b)
	if containsNet(m, n) {
		t.Errorf("%s expected to not contain %s", m, n)
	}
}

func TestNetworkDiscovery_SubnetContainsGood_IPv4(t *testing.T) {
	testYes(t, "192.168.0.0/23", "192.168.0.0/24")
	testYes(t, "192.168.0.0/24", "192.168.0.0/24")
	testNot(t, "192.168.0.0/25", "192.168.0.0/24")

	testYes(t, "192.168.101.202/16", "192.168.202.101/16")
	testNot(t, "192.168.101.202/24", "192.168.202.101/24")
	testNot(t, "192.168.202.101/24", "192.168.101.202/24")

	testYes(t, "0.0.0.0/0", "192.168.0.0/24")
	testYes(t, "0.0.0.0/0", "0.0.0.0/1")
	testNot(t, "192.168.0.0/24", "0.0.0.0/0")
	testNot(t, "0.0.0.0/1", "0.0.0.0/0")
}

func TestNetworkDiscovery_SubnetContainsGood_IPv6(t *testing.T) {
	testYes(t, "2001:db8::/63", "2001:db8::/64")
	testYes(t, "2001:db8::/64", "2001:db8::/64")
	testNot(t, "2001:db8::/65", "2001:db8::/64")

	testYes(t, "2001:db8:fefe:b00b::/32", "2001:db8:b00b:fefe::/32")
	testNot(t, "2001:db8:fefe:b00b::/64", "2001:db8:b00b:fefe::/64")
	testNot(t, "2001:db8:b00b:fefe::/64", "2001:db8:fefe:b00b::/64")

	testYes(t, "::/0", "2001:db8::/64")
	testYes(t, "::/0", "::/1")
	testNot(t, "2001:db8::/64", "::/0")
	testNot(t, "::/1", "::/0")
}