package bird import ( "strings" "testing" ) func TestRender_Host001(t *testing.T) { out, err := Render(NodeBGP{ NodeName: "host001", RouterID: "172.25.25.101", LocalASN: 65101, Peers: []Peer{ {Family: "v6", Address: "2602:817:3000:a25::1", ASN: 65000}, {Family: "v4", Address: "172.25.25.1", ASN: 65000}, }, CIDR6: []string{"2602:817:3000:f001::/64"}, CIDR4: []string{"172.25.210.0/24"}, }) if err != nil { t.Fatal(err) } for _, want := range []string{ "router id 172.25.25.101", "local as 65101;", "neighbor 2602:817:3000:a25::1 as 65000;", "neighbor 172.25.25.1 as 65000;", "if net = 2602:817:3000:f001::/64 then accept;", "if net = 172.25.210.0/24 then accept;", "graceful restart;", } { if !strings.Contains(out, want) { t.Errorf("missing %q in output:\n%s", want, out) } } } func TestRender_AnycastInjection(t *testing.T) { out, err := Render(NodeBGP{ RouterID: "10.0.0.1", LocalASN: 65101, Peers: []Peer{{Family: "v6", Address: "2001:db8::1", ASN: 65000}}, CIDR6: []string{"2001:db8:f001::/64"}, Anycast6: []string{"2001:db8:a::1"}, }) if err != nil { t.Fatal(err) } if !strings.Contains(out, "if net = 2001:db8:a::1/128 then accept;") { t.Fatalf("anycast /128 not advertised:\n%s", out) } } func TestRender_StableOutput(t *testing.T) { in := NodeBGP{ RouterID: "10.0.0.1", LocalASN: 65101, Peers: []Peer{ {Family: "v4", Address: "10.0.0.2", ASN: 65000}, {Family: "v6", Address: "2001:db8::1", ASN: 65000}, }, CIDR6: []string{"2001:db8:f002::/64", "2001:db8:f001::/64"}, CIDR4: []string{"10.1.1.0/24", "10.0.1.0/24"}, } a, _ := Render(in) b, _ := Render(in) if a != b { t.Fatalf("render not deterministic") } // Sorted ordering of CIDR6. i1 := strings.Index(a, "2001:db8:f001::/64") i2 := strings.Index(a, "2001:db8:f002::/64") if !(i1 < i2) { t.Fatalf("CIDR6 not sorted") } } func TestRender_LocalSubnetImportFilter(t *testing.T) { out, err := Render(NodeBGP{ RouterID: "172.25.25.104", LocalASN: 65104, Peers: []Peer{{Family: "v6", Address: "2602:817:3000:a25::1", ASN: 65000}, {Family: "v4", Address: "172.25.25.1", ASN: 65000}}, CIDR6: []string{"2602:817:3000:f004::/64"}, CIDR4: []string{"172.25.214.0/24"}, LocalSubnetV6: "2602:817:3000:a25::/64", LocalSubnetV4: "172.25.25.0/24", }) if err != nil { t.Fatal(err) } for _, want := range []string{ "import where net != 2602:817:3000:a25::/64;", "import where net != 172.25.25.0/24;", } { if !strings.Contains(out, want) { t.Errorf("missing %q in output:\n%s", want, out) } } // Each BGP peer block should use the import filter, not import all. // Slice out just the `protocol bgp ...` stanzas to avoid catching the // kernel proto's legitimate `import all;`. for _, marker := range []string{"protocol bgp upstream6_", "protocol bgp upstream4_"} { idx := strings.Index(out, marker) if idx < 0 { continue } end := strings.Index(out[idx:], "\n}") if end < 0 { continue } stanza := out[idx : idx+end] if strings.Contains(stanza, "import all;") { t.Errorf("BGP stanza still has `import all;`:\n%s", stanza) } } } func TestRender_LocalSubnetEmpty_FallsBackToImportAll(t *testing.T) { out, err := Render(NodeBGP{ RouterID: "10.0.0.1", LocalASN: 65101, Peers: []Peer{{Family: "v6", Address: "2001:db8::1", ASN: 65000}}, CIDR6: []string{"2001:db8:f001::/64"}, }) if err != nil { t.Fatal(err) } if !strings.Contains(out, "import all;") { t.Errorf("expected `import all;` when LocalSubnetV6 unset:\n%s", out) } } func TestRender_LocalSubnetValidation(t *testing.T) { cases := []struct { name string v6, v4 string wantErr string }{ {name: "non-canonical v6", v6: "2602:817:3000:a25::1/64", wantErr: "non-zero host bits"}, {name: "non-canonical v4", v4: "172.25.25.1/24", wantErr: "non-zero host bits"}, {name: "v6 family mismatch", v6: "172.25.25.0/24", wantErr: "is IPv4"}, {name: "v4 family mismatch", v4: "2602:817:3000:a25::/64", wantErr: "is IPv6"}, {name: "garbage", v6: "not-a-cidr", wantErr: "not a valid CIDR"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { _, err := Render(NodeBGP{ RouterID: "10.0.0.1", LocalASN: 65101, Peers: []Peer{{Family: "v6", Address: "2001:db8::1", ASN: 65000}}, LocalSubnetV6: tc.v6, LocalSubnetV4: tc.v4, }) if err == nil || !strings.Contains(err.Error(), tc.wantErr) { t.Fatalf("want error containing %q, got %v", tc.wantErr, err) } }) } } func TestFamilyOf(t *testing.T) { if FamilyOf("2001:db8::1") != "v6" { t.Fatal("v6 detection broken") } if FamilyOf("10.0.0.1") != "v4" { t.Fatal("v4 detection broken") } if FamilyOf("not-an-ip") != "" { t.Fatal("garbage should return empty") } }