geodb_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package geodb_test
  2. import (
  3. "testing"
  4. "imuslab.com/zoraxy/mod/geodb"
  5. )
  6. /*
  7. func TestTrieConstruct(t *testing.T) {
  8. tt := geodb.NewTrie()
  9. data := [][]string{
  10. {"1.0.16.0", "1.0.31.255", "JP"},
  11. {"1.0.32.0", "1.0.63.255", "CN"},
  12. {"1.0.64.0", "1.0.127.255", "JP"},
  13. {"1.0.128.0", "1.0.255.255", "TH"},
  14. {"1.1.0.0", "1.1.0.255", "CN"},
  15. {"1.1.1.0", "1.1.1.255", "AU"},
  16. {"1.1.2.0", "1.1.63.255", "CN"},
  17. {"1.1.64.0", "1.1.127.255", "JP"},
  18. {"1.1.128.0", "1.1.255.255", "TH"},
  19. {"1.2.0.0", "1.2.2.255", "CN"},
  20. {"1.2.3.0", "1.2.3.255", "AU"},
  21. }
  22. for _, entry := range data {
  23. startIp := entry[0]
  24. endIp := entry[1]
  25. cc := entry[2]
  26. tt.Insert(startIp, cc)
  27. tt.Insert(endIp, cc)
  28. }
  29. t.Log(tt.Search("1.0.16.20"), "== JP") //JP
  30. t.Log(tt.Search("1.2.0.122"), "== CN") //CN
  31. t.Log(tt.Search("1.2.1.0"), "== CN") //CN
  32. t.Log(tt.Search("1.0.65.243"), "== JP") //JP
  33. t.Log(tt.Search("1.0.62.243"), "== CN") //CN
  34. }
  35. */
  36. func TestResolveCountryCodeFromIP(t *testing.T) {
  37. // Create a new store
  38. store, err := geodb.NewGeoDb(nil, &geodb.StoreOptions{
  39. true,
  40. true,
  41. 0,
  42. })
  43. if err != nil {
  44. t.Errorf("error creating store: %v", err)
  45. return
  46. }
  47. // Test an IP address that should return a valid country code
  48. knownIpCountryMap := [][]string{
  49. {"3.224.220.101", "US"},
  50. {"176.113.115.113", "RU"},
  51. {"65.21.233.213", "FI"},
  52. {"94.23.207.193", "FR"},
  53. {"77.131.21.232", "FR"},
  54. }
  55. for _, testcase := range knownIpCountryMap {
  56. ip := testcase[0]
  57. expected := testcase[1]
  58. info, err := store.ResolveCountryCodeFromIP(ip)
  59. if err != nil {
  60. t.Errorf("error resolving country code for IP %s: %v", ip, err)
  61. return
  62. }
  63. if info.CountryIsoCode != expected {
  64. t.Errorf("expected country code %s, but got %s for IP %s", expected, info.CountryIsoCode, ip)
  65. }
  66. }
  67. // Test an IP address that should return an empty country code
  68. ip := "127.0.0.1"
  69. expected := ""
  70. info, err := store.ResolveCountryCodeFromIP(ip)
  71. if err != nil {
  72. t.Errorf("error resolving country code for IP %s: %v", ip, err)
  73. return
  74. }
  75. if info.CountryIsoCode != expected {
  76. t.Errorf("expected country code %s, but got %s for IP %s", expected, info.CountryIsoCode, ip)
  77. }
  78. // Test for issue #401
  79. // Create 100 concurrent goroutines to resolve country code for random IP addresses in the test cases above
  80. for i := 0; i < 100; i++ {
  81. go func() {
  82. for _, testcase := range knownIpCountryMap {
  83. ip := testcase[0]
  84. expected := testcase[1]
  85. info, err := store.ResolveCountryCodeFromIP(ip)
  86. if err != nil {
  87. t.Errorf("error resolving country code for IP %s: %v", ip, err)
  88. return
  89. }
  90. if info.CountryIsoCode != expected {
  91. t.Errorf("expected country code %s, but got %s for IP %s", expected, info.CountryIsoCode, ip)
  92. }
  93. }
  94. }()
  95. }
  96. }