195 lines
5.1 KiB
Go
195 lines
5.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"image/png"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestCountedClient(t *testing.T) {
|
|
ok := []string{
|
|
"Mozilla/5.0 QGIS/40202/Windows 10 Version 22H2",
|
|
"Mozilla/5.0 QGIS/33801/Ubuntu 22.04.4 LTS",
|
|
"QField/3.4.0 QGIS/33400/Android 14",
|
|
"Mozilla/5.0 qfield/4.2.0",
|
|
}
|
|
for _, ua := range ok {
|
|
if !countedClient(ua) {
|
|
t.Fatalf("should count %q", ua)
|
|
}
|
|
}
|
|
bad := []string{
|
|
"",
|
|
"curl/8.13.0",
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/99.0.4844.51 Safari/537.36",
|
|
"QGIS Desktop",
|
|
}
|
|
for _, ua := range bad {
|
|
if countedClient(ua) {
|
|
t.Fatalf("should ignore %q", ua)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseTilePath(t *testing.T) {
|
|
p, ok := parseTilePath("/tiles/%E4%B8%9C%E5%8C%BA/12/3/4.png")
|
|
if !ok || p.table != "东区" || p.z != "12" || p.x != "3" || p.y != "4" {
|
|
t.Fatalf("chinese path: %+v ok=%v", p, ok)
|
|
}
|
|
if _, ok := parseTilePath("/tiles/" + strings.Repeat("a", 64) + "/1/2/3.png"); !ok {
|
|
t.Fatal("64 chars should pass")
|
|
}
|
|
if _, ok := parseTilePath("/tiles/grid/30/0/0.png"); !ok {
|
|
t.Fatal("z=30 should pass")
|
|
}
|
|
if _, ok := parseTilePath("/tiles/grid/0/0/0.png"); !ok {
|
|
t.Fatal("zeros should pass")
|
|
}
|
|
bad := []string{
|
|
"/tiles//1/2/3.png",
|
|
"/tiles/" + strings.Repeat("b", 65) + "/1/2/3.png",
|
|
"/tiles/a%20b/1/2/3.png",
|
|
"/tiles/%2F/1/2/3.png",
|
|
"/tiles/../1/2/3.png",
|
|
"/tiles/%2E%2E/1/2/3.png",
|
|
"/tiles/grid/31/1/2.png",
|
|
"/tiles/grid/1/2/3.PNG",
|
|
"/tiles/grid/1/2/3.jpg",
|
|
"/tiles/grid/1/2/.png",
|
|
"/tiles/grid/1/2/3.png/extra",
|
|
"/tiles/grid/1/2.png",
|
|
"/tiles/grid/x/1/2.png",
|
|
"/tiles/has space/1/2/3.png",
|
|
"/nope/grid/1/2/3.png",
|
|
}
|
|
for _, path := range bad {
|
|
if _, ok := parseTilePath(path); ok {
|
|
t.Fatalf("accepted %s", path)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidSID(t *testing.T) {
|
|
if !validSID(strings.Repeat("A", 22)) {
|
|
t.Fatal("22 letters")
|
|
}
|
|
if !validSID("abcdefghijklmnopqrstuv") {
|
|
t.Fatal("22 mixed")
|
|
}
|
|
if !validSID("0123456789-_ABCDEFGHIJ") {
|
|
t.Fatal("charset")
|
|
}
|
|
if validSID(strings.Repeat("A", 21)) {
|
|
t.Fatal("21 should fail")
|
|
}
|
|
if validSID(strings.Repeat("A", 21) + "+") {
|
|
t.Fatal("plus should fail")
|
|
}
|
|
if validSID(strings.Repeat("A", 21) + "/") {
|
|
t.Fatal("slash should fail")
|
|
}
|
|
id, err := newSID()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(id) != 22 || !validSID(id) {
|
|
t.Fatalf("new sid %q", id)
|
|
}
|
|
}
|
|
|
|
func TestFingerprintAndClientIP(t *testing.T) {
|
|
salt := bytes.Repeat([]byte{7}, 32)
|
|
ua := "Mozilla/5.0 QGIS/33801/Windows"
|
|
fp := fingerprint(salt, "203.0.113.5", ua)
|
|
if len(fp) != 16 {
|
|
t.Fatalf("fingerprint length %d", len(fp))
|
|
}
|
|
if fp != fingerprint(salt, "203.0.113.5", ua) {
|
|
t.Fatal("same input differs")
|
|
}
|
|
if fingerprint(salt, "203.0.113.5", ua) == fingerprint(salt, "203.0.113.6", ua) {
|
|
t.Fatal("ip should change fingerprint")
|
|
}
|
|
long := strings.Repeat("a", 256)
|
|
if fingerprint(salt, "1.1.1.1", long) != fingerprint(salt, "1.1.1.1", long+"MORE") {
|
|
t.Fatal("ua over 256 bytes was not truncated")
|
|
}
|
|
alt := strings.Repeat("a", 255) + "b"
|
|
if fingerprint(salt, "1.1.1.1", long) == fingerprint(salt, "1.1.1.1", alt) {
|
|
t.Fatal("byte 256 should matter")
|
|
}
|
|
|
|
r1 := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
r1.RemoteAddr = "[2001:db8:1:2::1]:100"
|
|
r2 := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
r2.RemoteAddr = "[2001:db8:1:2::abcd]:200"
|
|
ip1 := clientIP(r1, false)
|
|
ip2 := clientIP(r2, false)
|
|
if ip1 != ip2 {
|
|
t.Fatalf("same /64: %s vs %s", ip1, ip2)
|
|
}
|
|
if fingerprint(salt, ip1, ua) != fingerprint(salt, ip2, ua) {
|
|
t.Fatal("same /64 fingerprint differs")
|
|
}
|
|
r3 := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
r3.RemoteAddr = "[2001:db8:1:3::1]:100"
|
|
if clientIP(r3, false) == ip1 {
|
|
t.Fatal("different /64 should differ")
|
|
}
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
r.RemoteAddr = "192.0.2.10:1234"
|
|
r.Header.Set("X-Real-IP", "198.51.100.7")
|
|
if got := clientIP(r, false); got != "192.0.2.10" {
|
|
t.Fatalf("ignore proxy: %s", got)
|
|
}
|
|
if got := clientIP(r, true); got != "198.51.100.7" {
|
|
t.Fatalf("trust proxy: %s", got)
|
|
}
|
|
|
|
mapped := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
mapped.RemoteAddr = "[::ffff:192.0.2.8]:9"
|
|
if got := clientIP(mapped, false); got != "192.0.2.8" {
|
|
t.Fatalf("v4-mapped: %s", got)
|
|
}
|
|
|
|
v6a := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
v6a.RemoteAddr = "192.0.2.1:1"
|
|
v6a.Header.Set("X-Real-IP", "2001:db8:9:9::1")
|
|
v6b := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
v6b.RemoteAddr = "192.0.2.1:1"
|
|
v6b.Header.Set("X-Real-IP", "2001:db8:9:9::ffff")
|
|
if clientIP(v6a, true) != clientIP(v6b, true) {
|
|
t.Fatalf("trusted ipv6 /64: %s %s", clientIP(v6a, true), clientIP(v6b, true))
|
|
}
|
|
}
|
|
|
|
func TestTransparentPNG(t *testing.T) {
|
|
raw := transparentPNG()
|
|
if len(raw) == 0 || len(raw) > 200 {
|
|
t.Fatalf("size %d", len(raw))
|
|
}
|
|
img, err := png.Decode(bytes.NewReader(raw))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b := img.Bounds()
|
|
if b.Dx() != 256 || b.Dy() != 256 {
|
|
t.Fatalf("bounds %v", b)
|
|
}
|
|
for y := b.Min.Y; y < b.Max.Y; y++ {
|
|
for x := b.Min.X; x < b.Max.X; x++ {
|
|
_, _, _, a := img.At(x, y).RGBA()
|
|
if a != 0 {
|
|
t.Fatalf("pixel %d,%d alpha %d", x, y, a)
|
|
}
|
|
}
|
|
}
|
|
if !bytes.Equal(raw, transparentPNG()) {
|
|
t.Fatal("png not stable")
|
|
}
|
|
}
|