plugin_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 mtu
  5. import (
  6. "net"
  7. "testing"
  8. "github.com/insomniacslk/dhcp/dhcpv4"
  9. )
  10. func TestAddServer4(t *testing.T) {
  11. req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, dhcpv4.WithRequestedOptions(dhcpv4.OptionInterfaceMTU))
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. stub, err := dhcpv4.NewReplyFromRequest(req)
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. mtu = 1500
  20. resp, stop := Handler4(req, stub)
  21. if resp == nil {
  22. t.Fatal("plugin did not return a message")
  23. }
  24. if stop {
  25. t.Error("plugin interrupted processing")
  26. }
  27. rMTU, err := dhcpv4.GetUint16(dhcpv4.OptionInterfaceMTU, resp.Options)
  28. if err != nil {
  29. t.Errorf("Failed to retrieve mtu from response")
  30. }
  31. if mtu != int(rMTU) {
  32. t.Errorf("Found %d mtu, expected %d", rMTU, mtu)
  33. }
  34. }
  35. func TestNotRequested4(t *testing.T) {
  36. req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. stub, err := dhcpv4.NewReplyFromRequest(req)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. mtu = 1500
  45. req.UpdateOption(dhcpv4.OptParameterRequestList(dhcpv4.OptionBroadcastAddress))
  46. resp, stop := Handler4(req, stub)
  47. if resp == nil {
  48. t.Fatal("plugin did not return a message")
  49. }
  50. if stop {
  51. t.Error("plugin interrupted processing")
  52. }
  53. if mtu, err := dhcpv4.GetUint16(dhcpv4.OptionInterfaceMTU, resp.Options); err == nil {
  54. t.Errorf("Retrieve mtu %d in response, expected none", mtu)
  55. }
  56. }