Files
NTrace-core/dn42/ptr_test.go
T
tsosunchia aa3df62875 Add tests for DN42 and PTR record functionalities
- Added focused unit tests for listener parsing helpers so buildListenInfo and isDigitsOnly are now validated without invoking the full CLI stack (cmd/listen_info_test.go:10-32).
  - Replaced placeholder DN42 tests with deterministic coverage for geofeed parsing and PTR matching, using temp CSV fixtures to check both positive and negative paths (dn42/geofeed_test.go:13-57, dn42/ptr_test.go:13-56).
  - Rebuilt the ipgeo tests to assert provider lookup mappings, DN42 integration (including PTR fallback and “unknown” defaults), and the disableGeoIP shim (ipgeo/ipgeo_test.go:12-53, ipgeo/dn42_test.go:14-78).
  - Introduced environment helper tests that exercise trimming, boolean, default, and integer reads without touching external DNS (util/env_test.go:9-46).
  - Removed legacy network-dependent tests that only printed output, ensuring the suite is deterministic.
2025-10-29 13:27:37 +08:00

57 lines
1.4 KiB
Go

package dn42
import (
"os"
"path/filepath"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFindPtrRecordMatchesCity(t *testing.T) {
dir := t.TempDir()
ptrPath := filepath.Join(dir, "ptr.csv")
content := "HKG,hk,Hong Kong,Hong Kong\nLAX,us,California,Los Angeles\n"
require.NoError(t, os.WriteFile(ptrPath, []byte(content), 0o644))
viper.Set("ptrPath", ptrPath)
t.Cleanup(viper.Reset)
row, err := FindPtrRecord("core.hongkong-1.example")
require.NoError(t, err)
assert.Equal(t, "hk", row.LtdCode)
assert.Equal(t, "Hong Kong", row.Region)
assert.Equal(t, "Hong Kong", row.City)
}
func TestFindPtrRecordMatchesIATACode(t *testing.T) {
dir := t.TempDir()
ptrPath := filepath.Join(dir, "ptr.csv")
require.NoError(t, os.WriteFile(ptrPath, []byte("LAX,us,California,Los Angeles\n"), 0o644))
viper.Set("ptrPath", ptrPath)
t.Cleanup(viper.Reset)
row, err := FindPtrRecord("edge.lax01.provider.test")
require.NoError(t, err)
assert.Equal(t, "lax", row.IATACode)
assert.Equal(t, "us", row.LtdCode)
assert.Equal(t, "California", row.Region)
}
func TestFindPtrRecordNotFound(t *testing.T) {
dir := t.TempDir()
ptrPath := filepath.Join(dir, "ptr.csv")
require.NoError(t, os.WriteFile(ptrPath, []byte(""), 0o644))
viper.Set("ptrPath", ptrPath)
t.Cleanup(viper.Reset)
_, err := FindPtrRecord("unmatched.example")
require.Error(t, err)
}