plugin_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 autoconfigure
  5. import (
  6. "bytes"
  7. "net"
  8. "testing"
  9. "github.com/insomniacslk/dhcp/dhcpv4"
  10. )
  11. func TestOptionRequested0(t *testing.T) {
  12. req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. req.UpdateOption(dhcpv4.OptGeneric(dhcpv4.OptionAutoConfigure, []byte{1}))
  17. stub, err := dhcpv4.NewReplyFromRequest(req,
  18. dhcpv4.WithMessageType(dhcpv4.MessageTypeOffer),
  19. )
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  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 interrupted processing")
  29. }
  30. opt := resp.Options.Get(dhcpv4.OptionAutoConfigure)
  31. if opt == nil {
  32. t.Fatal("plugin did not return the Auto-Configure option")
  33. }
  34. if !bytes.Equal(opt, []byte{0}) {
  35. t.Errorf("plugin gave wrong option response: %v", opt)
  36. }
  37. }
  38. func TestOptionRequested1(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. req.UpdateOption(dhcpv4.OptGeneric(dhcpv4.OptionAutoConfigure, []byte{1}))
  44. stub, err := dhcpv4.NewReplyFromRequest(req,
  45. dhcpv4.WithMessageType(dhcpv4.MessageTypeOffer),
  46. )
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. autoconfigure = 1
  51. resp, stop := Handler4(req, stub)
  52. if resp == nil {
  53. t.Fatal("plugin did not return a message")
  54. }
  55. if stop {
  56. t.Error("plugin interrupted processing")
  57. }
  58. opt := resp.Options.Get(dhcpv4.OptionAutoConfigure)
  59. if opt == nil {
  60. t.Fatal("plugin did not return the Auto-Configure option")
  61. }
  62. if !bytes.Equal(opt, []byte{1}) {
  63. t.Errorf("plugin gave wrong option response: %v", opt)
  64. }
  65. }
  66. func TestNotRequestedAssignedIP(t *testing.T) {
  67. req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. stub, err := dhcpv4.NewReplyFromRequest(req,
  72. dhcpv4.WithMessageType(dhcpv4.MessageTypeOffer),
  73. )
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. stub.YourIPAddr = net.ParseIP("192.0.2.100")
  78. resp, stop := Handler4(req, stub)
  79. if resp == nil {
  80. t.Fatal("plugin did not return a message")
  81. }
  82. if stop {
  83. t.Error("plugin interrupted processing")
  84. }
  85. if resp.Options.Get(dhcpv4.OptionAutoConfigure) != nil {
  86. t.Error("plugin responsed with AutoConfigure option")
  87. }
  88. }
  89. func TestNotRequestedNoIP(t *testing.T) {
  90. req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. stub, err := dhcpv4.NewReplyFromRequest(req,
  95. dhcpv4.WithMessageType(dhcpv4.MessageTypeOffer),
  96. )
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. resp, stop := Handler4(req, stub)
  101. if resp != nil {
  102. t.Error("plugin returned a message")
  103. }
  104. if !stop {
  105. t.Error("plugin did not interrupt processing")
  106. }
  107. }