plugin_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 ipv6only
  5. import (
  6. "bytes"
  7. "net"
  8. "testing"
  9. "time"
  10. "github.com/insomniacslk/dhcp/dhcpv4"
  11. )
  12. func TestOptionRequested(t *testing.T) {
  13. req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. req.UpdateOption(dhcpv4.OptParameterRequestList(dhcpv4.OptionBroadcastAddress, dhcpv4.OptionIPv6OnlyPreferred))
  18. stub, err := dhcpv4.NewReplyFromRequest(req)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. v6only_wait = 0x1234 * time.Second
  23. resp, stop := Handler4(req, stub)
  24. if resp == nil {
  25. t.Fatal("plugin did not return a message")
  26. }
  27. if !stop {
  28. t.Error("plugin did not interrupt processing")
  29. }
  30. opt := resp.Options.Get(dhcpv4.OptionIPv6OnlyPreferred)
  31. if opt == nil {
  32. t.Fatal("plugin did not return the IPv6-Only Preferred option")
  33. }
  34. if !bytes.Equal(opt, []byte{0x00, 0x00, 0x12, 0x34}) {
  35. t.Errorf("plugin gave wrong option response: %v", opt)
  36. }
  37. }
  38. func TestNotRequested(t *testing.T) {
  39. req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. stub, err := dhcpv4.NewReplyFromRequest(req)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. resp, stop := Handler4(req, stub)
  48. if resp == nil {
  49. t.Fatal("plugin did not return a message")
  50. }
  51. if stop {
  52. t.Error("plugin interrupted processing")
  53. }
  54. if resp.Options.Get(dhcpv4.OptionIPv6OnlyPreferred) != nil {
  55. t.Error("Found IPv6-Only Preferred option when not requested")
  56. }
  57. }