forked from xddxdd/bird-lg-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'xddxdd:master' into master
- Loading branch information
Showing
34 changed files
with
3,610 additions
and
403 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v1.2.0 | ||
v1.3.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
"github.com/magiconair/properties/assert" | ||
) | ||
|
||
func TestApiServerListHandler(t *testing.T) { | ||
setting.servers = []string{"alpha", "beta", "gamma"} | ||
response := apiServerListHandler(apiRequest{}) | ||
|
||
assert.Equal(t, len(response.Result), 3) | ||
assert.Equal(t, response.Result[0].(apiGenericResultPair).Server, "alpha") | ||
assert.Equal(t, response.Result[1].(apiGenericResultPair).Server, "beta") | ||
assert.Equal(t, response.Result[2].(apiGenericResultPair).Server, "gamma") | ||
} | ||
|
||
func TestApiGenericHandlerFactory(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpResponse := httpmock.NewStringResponder(200, BirdSummaryData) | ||
httpmock.RegisterResponder("GET", "http://alpha:8000/bird?q="+url.QueryEscape("show protocols"), httpResponse) | ||
|
||
setting.servers = []string{"alpha"} | ||
setting.domain = "" | ||
setting.proxyPort = 8000 | ||
|
||
request := apiRequest{ | ||
Servers: setting.servers, | ||
Type: "bird", | ||
Args: "show protocols", | ||
} | ||
|
||
handler := apiGenericHandlerFactory("bird") | ||
response := handler(request) | ||
|
||
assert.Equal(t, response.Error, "") | ||
|
||
result := response.Result[0].(*apiGenericResultPair) | ||
assert.Equal(t, result.Server, "alpha") | ||
assert.Equal(t, result.Data, BirdSummaryData) | ||
} | ||
|
||
func TestApiSummaryHandler(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpResponse := httpmock.NewStringResponder(200, BirdSummaryData) | ||
httpmock.RegisterResponder("GET", "http://alpha:8000/bird?q="+url.QueryEscape("show protocols"), httpResponse) | ||
|
||
setting.servers = []string{"alpha"} | ||
setting.domain = "" | ||
setting.proxyPort = 8000 | ||
|
||
request := apiRequest{ | ||
Servers: setting.servers, | ||
Type: "summary", | ||
Args: "", | ||
} | ||
response := apiSummaryHandler(request) | ||
|
||
assert.Equal(t, response.Error, "") | ||
|
||
summary := response.Result[0].(*apiSummaryResultPair) | ||
assert.Equal(t, summary.Server, "alpha") | ||
// Protocol list will be sorted | ||
assert.Equal(t, summary.Data[1].Name, "device1") | ||
assert.Equal(t, summary.Data[1].Proto, "Device") | ||
assert.Equal(t, summary.Data[1].Table, "---") | ||
assert.Equal(t, summary.Data[1].State, "up") | ||
assert.Equal(t, summary.Data[1].Since, "2021-08-27") | ||
assert.Equal(t, summary.Data[1].Info, "") | ||
} | ||
|
||
func TestApiSummaryHandlerError(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpResponse := httpmock.NewStringResponder(200, "Mock backend error") | ||
httpmock.RegisterResponder("GET", "http://alpha:8000/bird?q="+url.QueryEscape("show protocols"), httpResponse) | ||
|
||
setting.servers = []string{"alpha"} | ||
setting.domain = "" | ||
setting.proxyPort = 8000 | ||
|
||
request := apiRequest{ | ||
Servers: setting.servers, | ||
Type: "summary", | ||
Args: "", | ||
} | ||
response := apiSummaryHandler(request) | ||
|
||
assert.Equal(t, response.Error, "Mock backend error") | ||
} | ||
|
||
func TestApiWhoisHandler(t *testing.T) { | ||
expectedData := "Mock Data" | ||
server := WhoisServer{ | ||
t: t, | ||
expectedQuery: "AS6939", | ||
response: expectedData, | ||
} | ||
|
||
server.Listen() | ||
go server.Run() | ||
defer server.Close() | ||
|
||
setting.whoisServer = server.server.Addr().String() | ||
|
||
request := apiRequest{ | ||
Servers: []string{}, | ||
Type: "", | ||
Args: "AS6939", | ||
} | ||
response := apiWhoisHandler(request) | ||
|
||
assert.Equal(t, response.Error, "") | ||
|
||
whoisResult := response.Result[0].(apiGenericResultPair) | ||
assert.Equal(t, whoisResult.Server, "") | ||
assert.Equal(t, whoisResult.Data, expectedData) | ||
} | ||
|
||
func TestApiErrorHandler(t *testing.T) { | ||
err := errors.New("Mock Error") | ||
response := apiErrorHandler(err) | ||
assert.Equal(t, response.Error, "Mock Error") | ||
} | ||
|
||
func TestApiHandler(t *testing.T) { | ||
setting.servers = []string{"alpha", "beta", "gamma"} | ||
|
||
request := apiRequest{ | ||
Servers: []string{}, | ||
Type: "server_list", | ||
Args: "", | ||
} | ||
requestJson, err := json.Marshal(request) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
r := httptest.NewRequest(http.MethodGet, "/api", bytes.NewReader(requestJson)) | ||
w := httptest.NewRecorder() | ||
apiHandler(w, r) | ||
|
||
var response apiResponse | ||
err = json.Unmarshal(w.Body.Bytes(), &response) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
assert.Equal(t, len(response.Result), 3) | ||
// Hard to unmarshal JSON into apiGenericResultPair objects, won't check here | ||
} | ||
|
||
func TestApiHandlerBadJSON(t *testing.T) { | ||
setting.servers = []string{"alpha", "beta", "gamma"} | ||
|
||
r := httptest.NewRequest(http.MethodGet, "/api", strings.NewReader("{bad json}")) | ||
w := httptest.NewRecorder() | ||
apiHandler(w, r) | ||
|
||
var response apiResponse | ||
err := json.Unmarshal(w.Body.Bytes(), &response) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
assert.Equal(t, len(response.Result), 0) | ||
} | ||
|
||
func TestApiHandlerInvalidType(t *testing.T) { | ||
setting.servers = []string{"alpha", "beta", "gamma"} | ||
|
||
request := apiRequest{ | ||
Servers: setting.servers, | ||
Type: "invalid_type", | ||
Args: "", | ||
} | ||
requestJson, err := json.Marshal(request) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
r := httptest.NewRequest(http.MethodGet, "/api", bytes.NewReader(requestJson)) | ||
w := httptest.NewRecorder() | ||
apiHandler(w, r) | ||
|
||
var response apiResponse | ||
err = json.Unmarshal(w.Body.Bytes(), &response) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
assert.Equal(t, len(response.Result), 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strings" | ||
) | ||
|
||
type ASNCache map[string]string | ||
|
||
func (cache ASNCache) _lookup(asn string) string { | ||
// Try to get ASN representation using DNS | ||
if setting.dnsInterface != "" { | ||
records, err := net.LookupTXT(fmt.Sprintf("AS%s.%s", asn, setting.dnsInterface)) | ||
if err == nil { | ||
result := strings.Join(records, " ") | ||
if resultSplit := strings.Split(result, " | "); len(resultSplit) > 1 { | ||
result = strings.Join(resultSplit[1:], "\n") | ||
} | ||
return fmt.Sprintf("AS%s\n%s", asn, result) | ||
} | ||
} | ||
|
||
// Try to get ASN representation using WHOIS | ||
if setting.whoisServer != "" { | ||
if setting.bgpmapInfo == "" { | ||
setting.bgpmapInfo = "asn,as-name,ASName,descr" | ||
} | ||
records := whois(fmt.Sprintf("AS%s", asn)) | ||
if records != "" { | ||
recordsSplit := strings.Split(records, "\n") | ||
var result []string | ||
for _, title := range strings.Split(setting.bgpmapInfo, ",") { | ||
if title == "asn" { | ||
result = append(result, "AS"+asn) | ||
} | ||
} | ||
for _, title := range strings.Split(setting.bgpmapInfo, ",") { | ||
allow_multiline := false | ||
if title[0] == ':' && len(title) >= 2 { | ||
title = title[1:] | ||
allow_multiline = true | ||
} | ||
for _, line := range recordsSplit { | ||
if len(line) == 0 || line[0] == '%' || !strings.Contains(line, ":") { | ||
continue | ||
} | ||
linearr := strings.SplitN(line, ":", 2) | ||
line_title := linearr[0] | ||
content := strings.TrimSpace(linearr[1]) | ||
if line_title != title { | ||
continue | ||
} | ||
result = append(result, content) | ||
if !allow_multiline { | ||
break | ||
} | ||
|
||
} | ||
} | ||
if len(result) > 0 { | ||
return strings.Join(result, "\n") | ||
} | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func (cache ASNCache) Lookup(asn string) string { | ||
cachedValue, cacheOk := cache[asn] | ||
if cacheOk { | ||
return cachedValue | ||
} | ||
|
||
result := cache._lookup(asn) | ||
if len(result) == 0 { | ||
result = fmt.Sprintf("AS%s", asn) | ||
} | ||
|
||
cache[asn] = result | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/magiconair/properties/assert" | ||
) | ||
|
||
func TestGetASNRepresentationDNS(t *testing.T) { | ||
checkNetwork(t) | ||
|
||
setting.dnsInterface = "asn.cymru.com" | ||
setting.whoisServer = "" | ||
cache := make(ASNCache) | ||
result := cache.Lookup("6939") | ||
if !strings.Contains(result, "HURRICANE") { | ||
t.Errorf("Lookup AS6939 failed, got %s", result) | ||
} | ||
} | ||
|
||
func TestGetASNRepresentationDNSFallback(t *testing.T) { | ||
checkNetwork(t) | ||
|
||
setting.dnsInterface = "invalid.example.com" | ||
setting.whoisServer = "whois.arin.net" | ||
cache := make(ASNCache) | ||
result := cache.Lookup("6939") | ||
if !strings.Contains(result, "HURRICANE") { | ||
t.Errorf("Lookup AS6939 failed, got %s", result) | ||
} | ||
} | ||
|
||
func TestGetASNRepresentationWhois(t *testing.T) { | ||
checkNetwork(t) | ||
|
||
setting.dnsInterface = "" | ||
setting.whoisServer = "whois.arin.net" | ||
cache := make(ASNCache) | ||
result := cache.Lookup("6939") | ||
if !strings.Contains(result, "HURRICANE") { | ||
t.Errorf("Lookup AS6939 failed, got %s", result) | ||
} | ||
} | ||
|
||
func TestGetASNRepresentationFallback(t *testing.T) { | ||
setting.dnsInterface = "" | ||
setting.whoisServer = "" | ||
cache := make(ASNCache) | ||
result := cache.Lookup("6939") | ||
assert.Equal(t, result, "AS6939") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.