opendnsmyip_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (c) 2020 Jeffrey H. Johnson.
  2. // Copyright (c) 2020 Gridfinity, LLC.
  3. // Copyright (c) 2020 Dmitriy Kuznetsov.
  4. // Copyright (c) 2016 James Polera.
  5. // Use of this source code is governed by the MIT
  6. // license that can be found in the LICENSE file.
  7. package opendnsmyip_test
  8. import (
  9. "testing"
  10. "github.com/miekg/dns"
  11. u "go.gridfinity.dev/leaktestfe"
  12. myip "go.gridfinity.dev/opendnsmyip"
  13. )
  14. func TestGetMyIPAddress(t *testing.T) {
  15. defer u.Leakplug(
  16. t,
  17. )
  18. _, err := myip.GetMyIP()
  19. if err != nil {
  20. t.Errorf("Error: %s\n", err)
  21. }
  22. }
  23. func TestGenerateClientError(t *testing.T) {
  24. defer u.Leakplug(
  25. t,
  26. )
  27. config := dns.ClientConfig{Servers: []string{"0.0.0.0"}, Port: "53"}
  28. dnsClient := new(dns.Client)
  29. message := new(dns.Msg)
  30. message.SetQuestion("myip.opendns.com.", dns.TypeA)
  31. message.RecursionDesired = false
  32. _, err := myip.MyIPDNSLookup(config, dnsClient, message)
  33. if err == nil {
  34. t.Errorf("Expected error, got nil")
  35. }
  36. }
  37. func TestGenerateLookupError(t *testing.T) {
  38. defer u.Leakplug(
  39. t,
  40. )
  41. config := dns.ClientConfig{
  42. Servers: []string{"208.67.220.220", "208.67.222.222"},
  43. Port: "53",
  44. }
  45. dnsClient := new(dns.Client)
  46. message := new(dns.Msg)
  47. message.SetQuestion("notmyip.opendns.com.", dns.TypeA)
  48. message.RecursionDesired = false
  49. _, err := myip.MyIPDNSLookup(config, dnsClient, message)
  50. if err == nil {
  51. t.Errorf("Expected error, got nil")
  52. }
  53. }