diff --git a/frontend/bgpmap_test.go b/frontend/bgpmap_test.go new file mode 100644 index 0000000..bf638ec --- /dev/null +++ b/frontend/bgpmap_test.go @@ -0,0 +1,74 @@ +package main + +import ( + "strings" + "testing" +) + +func TestGetASNRepresentation(t *testing.T) { + setting.dnsInterface = "asn.cymru.com" + result := getASNRepresentation("6939") + if !strings.Contains(result, "HURRICANE") { + t.Errorf("Lookup AS6939 failed, got %s", result) + } +} + +func TestGetASNRepresentationFallback(t *testing.T) { + setting.dnsInterface = "" + result := getASNRepresentation("6939") + if result != "AS6939" { + t.Errorf("Lookup AS6939 failed, got %s", result) + } +} + +func TestBirdRouteToGraphviz(t *testing.T) { + setting.dnsInterface = "" + + // Don't change formatting of the following strings! + + fakeResult := `192.168.0.1/32 unicast [alpha 2021-01-14 from 192.168.0.2] * (100) [AS12345i] + via 192.168.0.2 on eth0 + Type: BGP univ + BGP.origin: IGP + BGP.as_path: 4242422601 + BGP.next_hop: 172.18.0.2` + + expectedResult := `digraph { +"Nexthop:\n172.18.0.2" -> "AS4242422601" [color=red]; +"Nexthop:\n172.18.0.2" [shape=diamond]; +"AS4242422601" -> "Target: 192.168.0.1" [color=red]; +"Target: 192.168.0.1" [color=red,shape=diamond]; +"alpha" [color=blue,shape=box]; +"alpha" -> "Nexthop:\n172.18.0.2" [color=red]; +}` + + result := birdRouteToGraphviz([]string{ + "alpha", + }, []string{ + fakeResult, + }, "192.168.0.1") + + for _, line := range strings.Split(result, "\n") { + if !strings.Contains(expectedResult, line) { + t.Errorf("Unexpected line in result: %s", line) + } + } +} + +func TestBirdRouteToGraphvizXSS(t *testing.T) { + setting.dnsInterface = "" + + // Don't change formatting of the following strings! + + fakeResult := `` + + result := birdRouteToGraphviz([]string{ + "alpha", + }, []string{ + fakeResult, + }, fakeResult) + + if strings.Contains(result, "" + + r := httptest.NewRequest("GET", "/whois/"+url.PathEscape(evil), nil) + w := httptest.NewRecorder() + + // renderPageTemplate doesn't escape content, filter is done beforehand + renderPageTemplate(w, r, evil, "Test Content") + + resultBytes, _ := ioutil.ReadAll(w.Result().Body) + result := string(resultBytes) + + if strings.Contains(result, evil) { + t.Errorf("XSS injection succeeded: %s", result) + } +} + +func TestSmartFormatterXSS(t *testing.T) { + evil := "" + result := smartFormatter(evil) + + if strings.Contains(result, evil) { + t.Errorf("XSS injection succeeded: %s", result) + } +} + +func TestSummaryTableXSS(t *testing.T) { + evil := "" + evilData := `Name Proto Table State Since Info +` + evil + ` ` + evil + ` --- up 2021-01-04 17:21:44 ` + evil + + result := summaryTable(evilData, evil) + + if strings.Contains(result, evil) { + t.Errorf("XSS injection succeeded: %s", result) + } +}