66 lines
3.1 KiB
Go
66 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
"github.com/novox/mesh-controller/internal/inventory"
|
|
)
|
|
|
|
// **node shows what the local network resolved to** (novox/hq ADR 0340 §8): every attached network,
|
|
// whether it counts, and why the others do not — the public subnet named as never counted.
|
|
func TestNodeShowsWhichAttachedNetworksCountAndWhy(t *testing.T) {
|
|
account := inventory.MachineAccount{Architecture: "amd64", Kernel: "linux", Machine: &inventory.MadeOf{
|
|
Networks: []inventory.AttachedNetwork{
|
|
{Link: "eth0", Network: "192.168.1.10/24"},
|
|
{Link: "eth1", Network: "203.0.113.7/24"},
|
|
{Link: "wg-mesh", Network: "10.10.0.4/24"},
|
|
}}}
|
|
judged := judgedNetworks(account, true, []string{"eth0", "eth1"}, "wg-mesh")
|
|
text := strings.Join(localNetworkLines(account, true, judged), "\n")
|
|
for _, want := range []string{
|
|
"192.168.1.0/24", "counts: in a private range (RFC 1918, 192.168.0.0/16)",
|
|
"203.0.113.0/24", "not counted: public, never counted",
|
|
"10.10.0.0/24", "the private network's own link",
|
|
} {
|
|
if !strings.Contains(text, want) {
|
|
t.Errorf("node does not say %q:\n%s", want, text)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A machine that said none, or whose node-engine is older, is said as that — never as an empty list
|
|
// that reads like a machine on no network.
|
|
func TestNodeSaysWhenNoNetworkWasSaid(t *testing.T) {
|
|
older := inventory.MachineAccount{Machine: &inventory.MadeOf{}}
|
|
if text := strings.Join(localNetworkLines(older, true, nil), "\n"); !strings.Contains(text, "older than the one that says it") {
|
|
t.Errorf("an older node-engine is not said:\n%s", text)
|
|
}
|
|
public := inventory.MachineAccount{Machine: &inventory.MadeOf{
|
|
Networks: []inventory.AttachedNetwork{{Link: "eth0", Network: "198.51.100.9/24"}}}}
|
|
judged := judgedNetworks(public, true, []string{"eth0"}, "wg-mesh")
|
|
if text := strings.Join(localNetworkLines(public, true, judged), "\n"); !strings.Contains(text, "none counts") {
|
|
t.Errorf("a machine on a public network only is not said to have no local network:\n%s", text)
|
|
}
|
|
if text := strings.Join(localNetworkLines(inventory.MachineAccount{}, false, nil), "\n"); !strings.Contains(text, "not said") {
|
|
t.Errorf("a machine that never reported is not said:\n%s", text)
|
|
}
|
|
}
|
|
|
|
// The preview of a flip says what a port from the local network keeps: the local networks it stays
|
|
// open to, or that the machine reported none (ADR 0340 §6).
|
|
func TestTheFlipPreviewSaysWhatALocalNetworkPortKeeps(t *testing.T) {
|
|
rules := []catalogue.Rule{{Port: 1883, Protocol: "tcp", From: catalogue.FromLocalNetwork, Because: []string{"mosquitto"}}}
|
|
at := inventory.Reach{Protocol: "tcp", Address: "0.0.0.0", Port: 1883}
|
|
with := derivedFilter{rules: rules, mesh: []string{"10.10.0.1"}, outwardLinks: []string{"eth0"},
|
|
local: []string{"192.168.1.0/24"}}
|
|
if fate := with.fate(at); !strings.Contains(fate, "192.168.1.0/24") {
|
|
t.Errorf("the preview says %q, without the local network it stays open to", fate)
|
|
}
|
|
with.local = nil
|
|
if fate := with.fate(at); !strings.Contains(fate, "reported none") {
|
|
t.Errorf("the preview says %q, without saying no local network was reported", fate)
|
|
}
|
|
}
|