dns_server_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package nebula
  2. import (
  3. "net/netip"
  4. "testing"
  5. "github.com/miekg/dns"
  6. "github.com/sirupsen/logrus"
  7. "github.com/slackhq/nebula/config"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestParsequery(t *testing.T) {
  11. l := logrus.New()
  12. hostMap := &HostMap{}
  13. ds := newDnsRecords(l, &CertState{}, hostMap)
  14. addrs := []netip.Addr{
  15. netip.MustParseAddr("1.2.3.4"),
  16. netip.MustParseAddr("1.2.3.5"),
  17. netip.MustParseAddr("fd01::24"),
  18. netip.MustParseAddr("fd01::25"),
  19. }
  20. ds.Add("test.com.com", addrs)
  21. m := &dns.Msg{}
  22. m.SetQuestion("test.com.com", dns.TypeA)
  23. ds.parseQuery(m, nil)
  24. assert.NotNil(t, m.Answer)
  25. assert.Equal(t, "1.2.3.4", m.Answer[0].(*dns.A).A.String())
  26. m = &dns.Msg{}
  27. m.SetQuestion("test.com.com", dns.TypeAAAA)
  28. ds.parseQuery(m, nil)
  29. assert.NotNil(t, m.Answer)
  30. assert.Equal(t, "fd01::24", m.Answer[0].(*dns.AAAA).AAAA.String())
  31. }
  32. func Test_getDnsServerAddr(t *testing.T) {
  33. c := config.NewC(nil)
  34. c.Settings["lighthouse"] = map[string]any{
  35. "dns": map[string]any{
  36. "host": "0.0.0.0",
  37. "port": "1",
  38. },
  39. }
  40. assert.Equal(t, "0.0.0.0:1", getDnsServerAddr(c))
  41. c.Settings["lighthouse"] = map[string]any{
  42. "dns": map[string]any{
  43. "host": "::",
  44. "port": "1",
  45. },
  46. }
  47. assert.Equal(t, "[::]:1", getDnsServerAddr(c))
  48. c.Settings["lighthouse"] = map[string]any{
  49. "dns": map[string]any{
  50. "host": "[::]",
  51. "port": "1",
  52. },
  53. }
  54. assert.Equal(t, "[::]:1", getDnsServerAddr(c))
  55. // Make sure whitespace doesn't mess us up
  56. c.Settings["lighthouse"] = map[string]any{
  57. "dns": map[string]any{
  58. "host": "[::] ",
  59. "port": "1",
  60. },
  61. }
  62. assert.Equal(t, "[::]:1", getDnsServerAddr(c))
  63. }