plugin_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 prefix
  5. import (
  6. "net"
  7. "testing"
  8. "github.com/insomniacslk/dhcp/dhcpv6"
  9. dhcpIana "github.com/insomniacslk/dhcp/iana"
  10. )
  11. func TestRoundTrip(t *testing.T) {
  12. reqIAID := [4]uint8{0x12, 0x34, 0x56, 0x78}
  13. req, err := dhcpv6.NewMessage()
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. req.AddOption(dhcpv6.OptClientID(&dhcpv6.DUIDLL{
  18. HWType: dhcpIana.HWTypeEthernet,
  19. LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
  20. }))
  21. req.AddOption(&dhcpv6.OptIAPD{
  22. IaId: reqIAID,
  23. T1: 0,
  24. T2: 0,
  25. })
  26. resp, err := dhcpv6.NewAdvertiseFromSolicit(req)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. handler, err := setupPrefix("2001:db8::/48", "64")
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. result, final := handler(req, resp)
  35. if final {
  36. t.Log("Handler declared final")
  37. }
  38. t.Logf("%#v", result)
  39. // Sanity checks on the response
  40. success := result.GetOption(dhcpv6.OptionStatusCode)
  41. var mo dhcpv6.MessageOptions
  42. if len(success) > 1 {
  43. t.Fatal("Got multiple StatusCode options")
  44. } else if len(success) == 0 { // Everything OK
  45. } else if err := mo.FromBytes(success[0].ToBytes()); err != nil || mo.Status().StatusCode != dhcpIana.StatusSuccess {
  46. t.Fatalf("Did not get a (implicit or explicit) success status code: %v", success)
  47. }
  48. var iapd *dhcpv6.OptIAPD
  49. {
  50. // Check for IA_PD
  51. iapds := result.(*dhcpv6.Message).Options.IAPD()
  52. if len(iapds) != 1 {
  53. t.Fatal("Malformed response, expected exactly 1 IAPD")
  54. }
  55. iapd = iapds[0]
  56. }
  57. if iapd.IaId != reqIAID {
  58. t.Fatalf("IAID doesn't match: request %x, response: %x", iapd.IaId, reqIAID)
  59. }
  60. // Check the status code
  61. if status := result.(*dhcpv6.Message).Options.Status(); status != nil && status.StatusCode != dhcpIana.StatusSuccess {
  62. t.Fatalf("Did not get a (implicit or explicit) success status code: %v", success)
  63. }
  64. t.Logf("%#v", iapd)
  65. // Check IAPrefix within IAPD
  66. if len(iapd.Options.Prefixes()) != 1 {
  67. t.Fatalf("Response did not contain exactly one prefix in the IA_PD option (found %s)",
  68. iapd.Options.Prefixes())
  69. }
  70. }
  71. func TestDup(t *testing.T) {
  72. _, prefix, err := net.ParseCIDR("2001:db8::/48")
  73. if err != nil {
  74. panic("bad cidr")
  75. }
  76. dupPrefix := dup(prefix)
  77. if !samePrefix(dupPrefix, prefix) {
  78. t.Fatalf("dup doesn't work: got %v expected %v", dupPrefix, prefix)
  79. }
  80. }