geodb_test.go 2.8 KB

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