2015-09-17 20:57:27 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2015-09-18 17:13:14 +02:00
|
|
|
"log"
|
2016-07-06 23:44:33 +02:00
|
|
|
"math/big"
|
2015-09-17 20:57:27 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-04-17 11:09:56 +02:00
|
|
|
type mockOracle struct{}
|
|
|
|
|
2016-04-17 21:34:00 +02:00
|
|
|
func (r *mockOracle) LookupAddr(net.IP) ([]string, error) { return []string{"localhost"}, nil }
|
2016-04-17 11:09:56 +02:00
|
|
|
func (r *mockOracle) LookupCountry(net.IP) (string, error) { return "Elbonia", nil }
|
2016-04-17 11:28:47 +02:00
|
|
|
func (r *mockOracle) LookupCity(net.IP) (string, error) { return "Bornyasherk", nil }
|
2016-04-17 11:09:56 +02:00
|
|
|
func (r *mockOracle) LookupPort(net.IP, uint64) error { return nil }
|
|
|
|
func (r *mockOracle) IsLookupAddrEnabled() bool { return true }
|
|
|
|
func (r *mockOracle) IsLookupCountryEnabled() bool { return true }
|
2016-04-17 11:28:47 +02:00
|
|
|
func (r *mockOracle) IsLookupCityEnabled() bool { return true }
|
2016-04-17 11:09:56 +02:00
|
|
|
func (r *mockOracle) IsLookupPortEnabled() bool { return true }
|
|
|
|
|
2015-09-29 20:39:21 +02:00
|
|
|
func newTestAPI() *API {
|
2016-04-17 16:03:00 +02:00
|
|
|
return &API{oracle: &mockOracle{}}
|
2015-09-29 20:39:21 +02:00
|
|
|
}
|
|
|
|
|
2015-09-18 17:13:14 +02:00
|
|
|
func httpGet(url string, json bool, userAgent string) (string, int, error) {
|
2015-09-17 20:57:27 +02:00
|
|
|
r, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
2015-09-18 17:13:14 +02:00
|
|
|
return "", 0, err
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
|
|
|
if json {
|
|
|
|
r.Header.Set("Accept", "application/json")
|
|
|
|
}
|
|
|
|
r.Header.Set("User-Agent", userAgent)
|
|
|
|
res, err := http.DefaultClient.Do(r)
|
|
|
|
if err != nil {
|
2015-09-18 17:13:14 +02:00
|
|
|
return "", 0, err
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
data, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
2015-09-18 17:13:14 +02:00
|
|
|
return "", 0, err
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
2015-09-18 17:13:14 +02:00
|
|
|
return string(data), res.StatusCode, nil
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 19:35:23 +02:00
|
|
|
func TestCLIHandlers(t *testing.T) {
|
2016-04-16 09:18:21 +02:00
|
|
|
log.SetOutput(ioutil.Discard)
|
2015-09-29 20:39:21 +02:00
|
|
|
s := httptest.NewServer(newTestAPI().Handlers())
|
2016-04-16 09:52:43 +02:00
|
|
|
|
2015-09-17 20:57:27 +02:00
|
|
|
var tests = []struct {
|
2016-09-06 19:35:23 +02:00
|
|
|
url string
|
|
|
|
out string
|
|
|
|
status int
|
|
|
|
userAgent string
|
2015-09-17 20:57:27 +02:00
|
|
|
}{
|
2016-09-06 19:35:23 +02:00
|
|
|
{s.URL, "127.0.0.1\n", 200, "curl/7.43.0"},
|
|
|
|
{s.URL + "/ip", "127.0.0.1\n", 200, ""},
|
|
|
|
{s.URL + "/country", "Elbonia\n", 200, ""},
|
|
|
|
{s.URL + "/city", "Bornyasherk\n", 200, ""},
|
|
|
|
{s.URL + "/foo", "404 page not found", 404, ""},
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
2015-09-18 17:42:43 +02:00
|
|
|
|
2015-09-17 20:57:27 +02:00
|
|
|
for _, tt := range tests {
|
2016-09-06 19:35:23 +02:00
|
|
|
out, status, err := httpGet(tt.url /* json = */, false, tt.userAgent)
|
2015-09-17 20:57:27 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-18 17:13:14 +02:00
|
|
|
if status != tt.status {
|
|
|
|
t.Errorf("Expected %d, got %d", tt.status, status)
|
|
|
|
}
|
2015-09-17 20:57:27 +02:00
|
|
|
if out != tt.out {
|
|
|
|
t.Errorf("Expected %q, got %q", tt.out, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-16 09:52:43 +02:00
|
|
|
func TestJSONHandlers(t *testing.T) {
|
2015-09-29 20:39:21 +02:00
|
|
|
log.SetOutput(ioutil.Discard)
|
2016-04-16 09:52:43 +02:00
|
|
|
s := httptest.NewServer(newTestAPI().Handlers())
|
2015-09-29 20:39:21 +02:00
|
|
|
|
2016-04-16 09:52:43 +02:00
|
|
|
var tests = []struct {
|
|
|
|
url string
|
|
|
|
out string
|
|
|
|
status int
|
|
|
|
}{
|
2016-07-06 23:44:33 +02:00
|
|
|
{s.URL, `{"ip":"127.0.0.1","ip_decimal":2130706433,"country":"Elbonia","city":"Bornyasherk","hostname":"localhost"}`, 200},
|
2016-04-27 17:07:53 +02:00
|
|
|
{s.URL + "/port/foo", `{"error":"404 page not found"}`, 404},
|
|
|
|
{s.URL + "/port/0", `{"error":"Invalid port: 0"}`, 400},
|
|
|
|
{s.URL + "/port/65356", `{"error":"Invalid port: 65356"}`, 400},
|
2016-04-16 10:53:08 +02:00
|
|
|
{s.URL + "/port/31337", `{"ip":"127.0.0.1","port":31337,"reachable":true}`, 200},
|
2016-04-16 09:52:43 +02:00
|
|
|
{s.URL + "/foo", `{"error":"404 page not found"}`, 404},
|
2015-09-29 20:39:21 +02:00
|
|
|
}
|
2016-04-16 09:52:43 +02:00
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
out, status, err := httpGet(tt.url /* json = */, true, "curl/7.2.6.0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if status != tt.status {
|
|
|
|
t.Errorf("Expected %d, got %d", tt.status, status)
|
|
|
|
}
|
|
|
|
if out != tt.out {
|
|
|
|
t.Errorf("Expected %q, got %q", tt.out, out)
|
|
|
|
}
|
2015-09-29 20:39:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 20:57:27 +02:00
|
|
|
func TestIPFromRequest(t *testing.T) {
|
|
|
|
var tests = []struct {
|
2016-04-17 15:52:06 +02:00
|
|
|
remoteAddr string
|
|
|
|
headerKey string
|
|
|
|
headerValue string
|
|
|
|
trustedHeader string
|
|
|
|
out string
|
2015-09-17 20:57:27 +02:00
|
|
|
}{
|
2016-04-17 15:52:06 +02:00
|
|
|
{"127.0.0.1:9999", "", "", "", "127.0.0.1"}, // No header given
|
|
|
|
{"127.0.0.1:9999", "X-Real-IP", "1.3.3.7", "", "127.0.0.1"}, // Trusted header is empty
|
|
|
|
{"127.0.0.1:9999", "X-Real-IP", "1.3.3.7", "X-Foo-Bar", "127.0.0.1"}, // Trusted header does not match
|
|
|
|
{"127.0.0.1:9999", "X-Real-IP", "1.3.3.7", "X-Real-IP", "1.3.3.7"}, // Trusted header matches
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2016-04-17 15:52:06 +02:00
|
|
|
r := &http.Request{
|
|
|
|
RemoteAddr: tt.remoteAddr,
|
|
|
|
Header: http.Header{},
|
|
|
|
}
|
|
|
|
r.Header.Add(tt.headerKey, tt.headerValue)
|
|
|
|
ip, err := ipFromRequest(tt.trustedHeader, r)
|
2015-09-17 20:57:27 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-04-17 15:52:06 +02:00
|
|
|
out := net.ParseIP(tt.out)
|
|
|
|
if !ip.Equal(out) {
|
|
|
|
t.Errorf("Expected %s, got %s", out, ip)
|
2015-09-17 20:57:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCLIMatcher(t *testing.T) {
|
|
|
|
browserUserAgent := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
|
|
|
|
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.28 " +
|
|
|
|
"Safari/537.36"
|
|
|
|
var tests = []struct {
|
|
|
|
in string
|
|
|
|
out bool
|
|
|
|
}{
|
|
|
|
{"curl/7.26.0", true},
|
|
|
|
{"Wget/1.13.4 (linux-gnu)", true},
|
|
|
|
{"fetch libfetch/2.0", true},
|
2016-04-15 20:19:14 +02:00
|
|
|
{"HTTPie/0.9.3", true},
|
2016-04-16 09:52:43 +02:00
|
|
|
{"Go 1.1 package http", true},
|
|
|
|
{"Go-http-client/1.1", true},
|
|
|
|
{"Go-http-client/2.0", true},
|
2016-05-26 21:36:23 +02:00
|
|
|
{"ddclient/3.8.3", true},
|
2015-09-17 20:57:27 +02:00
|
|
|
{browserUserAgent, false},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
r := &http.Request{Header: http.Header{"User-Agent": []string{tt.in}}}
|
|
|
|
if got := cliMatcher(r, nil); got != tt.out {
|
|
|
|
t.Errorf("Expected %t, got %t for %q", tt.out, got, tt.in)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-06 23:44:33 +02:00
|
|
|
|
|
|
|
func TestIPToDecimal(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
in string
|
|
|
|
out *big.Int
|
|
|
|
}{
|
|
|
|
{"127.0.0.1", big.NewInt(2130706433)},
|
|
|
|
{"::1", big.NewInt(1)},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
i := ipToDecimal(net.ParseIP(tt.in))
|
|
|
|
if i.Cmp(tt.out) != 0 {
|
|
|
|
t.Errorf("Expected %d, got %d for IP %s", tt.out, i, tt.in)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|