55 lines
1.6 KiB
Go
55 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestCityInstallRejectsBadDownload(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("DASHBOARD_DB_PATH", filepath.Join(dir, "dashboard.db"))
|
|
existing := []byte("existing database")
|
|
if err := os.WriteFile(trafficCityPath(), existing, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
g := gzip.NewWriter(w)
|
|
g.Write([]byte("corrupt database"))
|
|
g.Close()
|
|
}))
|
|
defer server.Close()
|
|
if err := installTrafficCityFrom(server.URL, cityDatabaseSHA1); err == nil {
|
|
t.Fatal("bad checksum accepted")
|
|
}
|
|
got, err := os.ReadFile(trafficCityPath())
|
|
if err != nil || string(got) != string(existing) {
|
|
t.Fatal("existing database replaced on failure")
|
|
}
|
|
files, _ := filepath.Glob(filepath.Join(dir, ".city-*"))
|
|
if len(files) != 0 {
|
|
t.Fatal("partial file left behind")
|
|
}
|
|
}
|
|
func TestCityDatabaseDownloaded(t *testing.T) {
|
|
source := os.Getenv("DASHBOARD_CITY_TEST_GZIP")
|
|
if source == "" {
|
|
t.Skip("optional official database integration test")
|
|
}
|
|
t.Setenv("DASHBOARD_DB_PATH", filepath.Join(t.TempDir(), "dashboard.db"))
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, source) }))
|
|
defer server.Close()
|
|
trafficCityReady()
|
|
if err := installTrafficCityFrom(server.URL, cityDatabaseSHA1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
city, region := lookupTrafficCity("20.194.96.114")
|
|
t.Logf("city=%q region=%q", city, region)
|
|
if city == "" {
|
|
t.Fatal("public IP city not decoded")
|
|
}
|
|
}
|