opendnsmyip_test.go 1.4 KB

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