plugin_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2018-present the CoreDHCP Authors. All rights reserved
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. package searchdomains
  5. import (
  6. "net"
  7. "testing"
  8. "github.com/insomniacslk/dhcp/dhcpv4"
  9. "github.com/insomniacslk/dhcp/dhcpv6"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestAddDomains6(t *testing.T) {
  13. assert := assert.New(t)
  14. // Search domains we will expect the DHCP server to assign
  15. searchDomains := []string{"domain.a", "domain.b"}
  16. // Init plugin
  17. handler6, err := Plugin.Setup6(searchDomains...)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. // Fake request
  22. req, err := dhcpv6.NewMessage()
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. req.MessageType = dhcpv6.MessageTypeRequest
  27. req.AddOption(dhcpv6.OptRequestedOption(dhcpv6.OptionDNSRecursiveNameServer))
  28. // Fake response input
  29. stub, err := dhcpv6.NewMessage()
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. stub.MessageType = dhcpv6.MessageTypeReply
  34. // Call plugin
  35. resp, stop := handler6(req, stub)
  36. if resp == nil {
  37. t.Fatal("plugin did not return a message")
  38. }
  39. if stop {
  40. t.Error("plugin interrupted processing")
  41. }
  42. searchLabels := resp.(*dhcpv6.Message).Options.DomainSearchList().Labels
  43. assert.Equal(searchDomains, searchLabels)
  44. }
  45. func TestAddDomains4(t *testing.T) {
  46. assert := assert.New(t)
  47. // Search domains we will expect the DHCP server to assign
  48. // NOTE: these domains should be different from the v6 test domains;
  49. // this tests that we haven't accidentally set the v6 domains in the
  50. // v4 plugin handler or vice versa.
  51. searchDomains := []string{"domain.b", "domain.c"}
  52. // Init plugin
  53. handler4, err := Plugin.Setup4(searchDomains...)
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. // Fake request
  58. req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. // Fake response input
  63. stub, err := dhcpv4.NewReplyFromRequest(req)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. // Call plugin
  68. resp, stop := handler4(req, stub)
  69. if resp == nil {
  70. t.Fatal("plugin did not return a message")
  71. }
  72. if stop {
  73. t.Error("plugin interrupted processing")
  74. }
  75. searchLabels := resp.DomainSearch().Labels
  76. assert.Equal(searchDomains, searchLabels)
  77. }