mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-05 07:26:36 +08:00
2f60329886
代码质量全量清理,零行为变化:golangci 扩展集 13 类 linter(gosec/modernize/perfsprint/canonicalheader/usestdlibvars/wastedassign/intrange/errorlint/forcetypeassert/recvcheck/exhaustive/unparam)全量修复,测试代码质量(testifylint/thelper/usetesting)25→0,frpc 进程生命周期真 bug(进程组击杀)、全仓 go test -race 6 类数据竞争(含 1 个生产竞争)、SPDX license 头补齐 131 文件、前端测试套件 next-intl 迁移后 44 失败→全绿、过期 swagger 文档重新生成、pnpm-workspace 构建审批。 Experiments: #2-#17, #18, #20, #21, #23 Metric: total_issues 108 → 8 (-92.6%)
103 lines
2.1 KiB
Go
103 lines
2.1 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package geoip
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
)
|
|
|
|
type fakeProvider struct {
|
|
calls int
|
|
}
|
|
|
|
func (f *fakeProvider) Name() string {
|
|
return "fake"
|
|
}
|
|
|
|
func (f *fakeProvider) GetGeoInfo(ip net.IP) (*GeoInfo, error) {
|
|
f.calls++
|
|
return &GeoInfo{
|
|
ISOCode: "CN",
|
|
Name: "China",
|
|
}, nil
|
|
}
|
|
|
|
func (f *fakeProvider) UpdateDatabase() error {
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeProvider) Close() error {
|
|
return nil
|
|
}
|
|
|
|
func TestGetGeoInfoCachesByProviderAndIP(t *testing.T) {
|
|
originalProvider := CurrentProvider
|
|
geoCache.Flush()
|
|
fake := &fakeProvider{}
|
|
CurrentProvider = fake
|
|
defer func() {
|
|
CurrentProvider = originalProvider
|
|
}()
|
|
|
|
ip := net.ParseIP("8.8.8.8")
|
|
record, err := GetGeoInfo(ip)
|
|
if err != nil {
|
|
t.Fatalf("expected nil error, got %v", err)
|
|
}
|
|
if record == nil || record.ISOCode != "CN" {
|
|
t.Fatalf("expected cached record, got %#v", record)
|
|
}
|
|
|
|
_, err = GetGeoInfo(ip)
|
|
if err != nil {
|
|
t.Fatalf("expected nil error on second call, got %v", err)
|
|
}
|
|
if fake.calls != 1 {
|
|
t.Fatalf("expected provider to be called once, got %d", fake.calls)
|
|
}
|
|
}
|
|
|
|
func TestUnicodeEmoji(t *testing.T) {
|
|
emoji := GetRegionUnicodeEmoji("CN")
|
|
if emoji != "🇨🇳" {
|
|
t.Errorf("expected emoji for CN, got %s", emoji)
|
|
}
|
|
}
|
|
|
|
func TestIsValidProvider(t *testing.T) {
|
|
cases := map[string]bool{
|
|
"disabled": true,
|
|
"mmdb": true,
|
|
"ip-api": true,
|
|
"geojs": true,
|
|
"ipinfo": true,
|
|
"unknown": false,
|
|
}
|
|
|
|
for provider, want := range cases {
|
|
if got := IsValidProvider(provider); got != want {
|
|
t.Fatalf("provider %s validity mismatch: want %v, got %v", provider, want, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLookupGeoInfoWithProviderUsesTemporaryProvider(t *testing.T) {
|
|
previousFactory := providerFactory
|
|
providerFactory = func(provider string) (Service, error) {
|
|
return &fakeProvider{}, nil
|
|
}
|
|
defer func() {
|
|
providerFactory = previousFactory
|
|
}()
|
|
|
|
info, err := LookupGeoInfoWithProvider("ipinfo", net.ParseIP("8.8.8.8"))
|
|
if err != nil {
|
|
t.Fatalf("expected lookup to succeed, got %v", err)
|
|
}
|
|
if info == nil || info.ISOCode != "CN" || info.Name != "China" {
|
|
t.Fatalf("unexpected geo info: %#v", info)
|
|
}
|
|
}
|