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.Duid{
  18. Type: dhcpv6.DUID_LL,
  19. HwType: dhcpIana.HWTypeEthernet,
  20. LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
  21. }))
  22. req.AddOption(&dhcpv6.OptIAPD{
  23. IaId: reqIAID,
  24. T1: 0,
  25. T2: 0,
  26. })
  27. resp, err := dhcpv6.NewAdvertiseFromSolicit(req)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. handler, err := setupPrefix("2001:db8::/48", "64")
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. result, final := handler(req, resp)
  36. if final {
  37. t.Log("Handler declared final")
  38. }
  39. t.Logf("%#v", result)
  40. // Sanity checks on the response
  41. success := result.GetOption(dhcpv6.OptionStatusCode)
  42. if len(success) > 1 {
  43. t.Fatal("Got multiple StatusCode options")
  44. } else if len(success) == 0 { // Everything OK
  45. } else if status, err := dhcpv6.ParseOptStatusCode(success[0].ToBytes()); err != nil || 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. }